-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow header to control report format
- Loading branch information
1 parent
8ee9972
commit bdb3d60
Showing
12 changed files
with
179 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
fili-core/src/main/java/com/yahoo/bard/webservice/web/DefaultResponseFormatResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2017 Yahoo Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.web; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
|
||
/** | ||
* A Fili default implementation of ResponseFormatResolver. This implementation works with three formats: json, jsonapi | ||
* and csv. | ||
*/ | ||
public class DefaultResponseFormatResolver implements ResponseFormatResolver { | ||
public static final String ACCEPT_HEADER_JSON = "application/json"; | ||
public static final String ACCEPT_HEADER_JSONAPI = "application/vnd.api+json"; | ||
public static final String ACCEPT_HEADER_CSV = "text/csv"; | ||
public static final String URI_JSON = "json"; | ||
public static final String URI_JSONAPI = "jsonapi"; | ||
public static final String URI_CSV = "csv"; | ||
|
||
private final Map<String, String> formatsMap; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public DefaultResponseFormatResolver() { | ||
formatsMap = new LinkedHashMap<>(); | ||
formatsMap.put(ACCEPT_HEADER_JSON, URI_JSON); | ||
formatsMap.put(ACCEPT_HEADER_JSONAPI, URI_JSONAPI); | ||
formatsMap.put(ACCEPT_HEADER_CSV, URI_CSV); | ||
} | ||
|
||
@Override | ||
public String apply(final String format, final ContainerRequestContext containerRequestContext) { | ||
String headerFormat = containerRequestContext.getHeaderString("Accept"); | ||
if (format != null || headerFormat == null) { | ||
return format; | ||
} | ||
return formatsMap.entrySet().stream() | ||
.filter(entry -> headerFormat.contains(entry.getKey())) | ||
.map(Map.Entry::getValue) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
fili-core/src/main/java/com/yahoo/bard/webservice/web/ResponseFormatResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2017 Yahoo Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.web; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
|
||
/** | ||
* A functional interface which allows the logic of resolving response format customizable. It has one method that | ||
* takes the format name string from URI and a ContainerRequestContext object from which the header Accept field is | ||
* accessible. | ||
*/ | ||
public interface ResponseFormatResolver extends BiFunction<String, ContainerRequestContext, String> { | ||
/** | ||
* Resolve desirable format from URI and ContainerRequestContext. | ||
* | ||
* @param format The format String from URI | ||
* @param containerRequestContext ContainerRequestContext object that contains request related information | ||
* | ||
* @return A resolved format decided by the function, null if none match | ||
*/ | ||
String apply(String format, ContainerRequestContext containerRequestContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.