Skip to content

Commit

Permalink
Add support for jsonp requests in IndexResource#list
Browse files Browse the repository at this point in the history
  • Loading branch information
chalkos committed Oct 11, 2018
1 parent 2f9e9f2 commit 4c17130
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public enum DateGranularity {
public static final String API_QUERY_KEY_ACCEPT_FORMAT = "acceptFormat";
public static final String API_QUERY_KEY_INLINE = "inline";
public static final String API_QUERY_KEY_JSONP_CALLBACK = "callback";
public static final String API_QUERY_DEFAULT_JSONP_CALLBACK = "rodaCallback";
public static final String API_QUERY_DEFAULT_JSONP_CALLBACK = "";
public static final String API_QUERY_VALUE_ACCEPT_FORMAT_BIN = "bin";
public static final String API_QUERY_VALUE_ACCEPT_FORMAT_XML = "xml";
public static final String API_QUERY_VALUE_ACCEPT_FORMAT_HTML = "html";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.configuration.Configuration;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.lang3.StringUtils;
import org.glassfish.jersey.server.JSONP;
import org.roda.core.RodaCoreFactory;
import org.roda.core.common.UserUtility;
import org.roda.core.data.common.RodaConstants;
Expand Down Expand Up @@ -140,7 +141,8 @@ public class IndexResource {
* if some error occurs.
*/
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, ExtraMediaType.TEXT_CSV})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, ExtraMediaType.TEXT_CSV, ExtraMediaType.APPLICATION_JAVASCRIPT})
@JSONP(callback = RodaConstants.API_QUERY_DEFAULT_JSONP_CALLBACK, queryParam = RodaConstants.API_QUERY_KEY_JSONP_CALLBACK)
@ApiOperation(value = "Find indexed resources", notes = "Find indexed resources.", response = IndexResult.class, responseContainer = "List")
public <T extends IsIndexed> Response list(
@ApiParam(value = "Class of resources to return", required = true, example = "org.roda.core.data.v2.ip.IndexedFile") @QueryParam(RodaConstants.API_QUERY_KEY_RETURN_CLASS) final String returnClass,
Expand All @@ -154,7 +156,8 @@ public <T extends IsIndexed> Response list(
@ApiParam(value = "Return only active resources?", defaultValue = "true") @QueryParam(RodaConstants.API_QUERY_KEY_ONLY_ACTIVE) final Boolean onlyActive,
@ApiParam(value = "Export facet data", defaultValue = "false") @QueryParam(RodaConstants.API_QUERY_KEY_EXPORT_FACETS) final boolean exportFacets,
@ApiParam(value = "Filename", defaultValue = DEFAULT_CSV_FILENAME) @QueryParam(RodaConstants.API_QUERY_KEY_FILENAME) final String filename,
@ApiParam(value = "Choose format in which to get the response") @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat)
@ApiParam(value = "Choose format in which to get the response") @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat,
@ApiParam(value = "JSONP callback name", required = false, allowMultiple = false, defaultValue = RodaConstants.API_QUERY_DEFAULT_JSONP_CALLBACK) @QueryParam(RodaConstants.API_QUERY_KEY_JSONP_CALLBACK) String jsonpCallbackName)
throws RODAException {

final String mediaType = ApiUtils.getMediaType(acceptFormat, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,19 @@ private ApiUtils() {

/**
* Get media type
*
*
* @param acceptFormat
* String with required format
* @param request
* http request
* @return media type
*/
public static String getMediaType(String acceptFormat, HttpServletRequest request) {
return getMediaType(acceptFormat, request.getHeader(RodaConstants.API_HTTP_HEADER_ACCEPT));
if(StringUtils.isBlank(acceptFormat) && StringUtils.isNotBlank(request.getParameter("callback"))){
return ExtraMediaType.APPLICATION_JAVASCRIPT + "; charset=UTF-8";
}else{
return getMediaType(acceptFormat, request.getHeader(RodaConstants.API_HTTP_HEADER_ACCEPT));
}
}

/**
Expand Down

0 comments on commit 4c17130

Please sign in to comment.