use of javax.ws.rs.core.Variant in project cxf by apache.
the class ResponseBuilderImplTest method testVariant.
@Test
public void testVariant() throws Exception {
MetadataMap<String, Object> m = new MetadataMap<>();
m.putSingle("Content-Type", MediaType.TEXT_XML_TYPE);
m.putSingle("Content-Language", new Locale("en"));
m.putSingle("Content-Encoding", "gzip");
Variant v = new Variant(MediaType.TEXT_XML_TYPE, new Locale("en"), "gzip");
checkBuild(Response.ok().variant(v).build(), 200, null, m);
}
use of javax.ws.rs.core.Variant in project cxf by apache.
the class ResponseBuilderImplTest method testVariantsArray.
@Test
public void testVariantsArray() throws Exception {
MetadataMap<String, Object> m = new MetadataMap<>();
m.add("Content-Type", MediaType.APPLICATION_JSON_TYPE);
m.add("Content-Language", new Locale("en_uk"));
m.add("Content-Language", new Locale("en_gb"));
m.add("Vary", "Accept");
m.add("Vary", "Accept-Language");
Variant json = new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en_uk"), null);
Variant xml = new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en_gb"), null);
checkBuild(Response.ok().variants(json, xml).build(), 200, null, m);
}
use of javax.ws.rs.core.Variant in project dataverse by IQSS.
the class Users method getTraces.
@GET
@Path("{identifier}/traces/{element}")
@Produces("text/csv, application/json")
public Response getTraces(@Context Request req, @PathParam("identifier") String identifier, @PathParam("element") String element) {
try {
AuthenticatedUser userToQuery = authSvc.getAuthenticatedUser(identifier);
if (!elements.contains(element)) {
throw new BadRequestException("Not a valid element");
}
JsonObjectBuilder jsonObj = execCommand(new GetUserTracesCommand(createDataverseRequest(findUserOrDie()), userToQuery, element));
List<Variant> vars = Variant.mediaTypes(MediaType.valueOf(FileUtil.MIME_TYPE_CSV), MediaType.APPLICATION_JSON_TYPE).add().build();
MediaType requestedType = req.selectVariant(vars).getMediaType();
if ((requestedType != null) && (requestedType.equals(MediaType.APPLICATION_JSON_TYPE))) {
return ok(jsonObj);
}
JsonArray items = null;
try {
items = jsonObj.build().getJsonObject("traces").getJsonObject(element).getJsonArray("items");
} catch (Exception e) {
return ok(jsonObj);
}
return ok(FileUtil.jsonArrayOfObjectsToCSV(items, items.getJsonObject(0).keySet().toArray(new String[0])), MediaType.valueOf(FileUtil.MIME_TYPE_CSV), element + ".csv");
} catch (WrappedResponse ex) {
return ex.getResponse();
}
}
use of javax.ws.rs.core.Variant in project OpenOLAT by OpenOLAT.
the class MediaTypeVariants method isPaged.
public static boolean isPaged(HttpServletRequest httpRequest, Request request) {
String accept = httpRequest.getHeader("Accept");
if (StringHelper.containsNonWhitespace(accept)) {
try {
MediaType requestMediaType = MediaType.valueOf(accept);
if (APPLICATION_JSON_PAGED.equals(requestMediaType) || APPLICATION_XML_PAGED.equals(requestMediaType)) {
return true;
}
} catch (IllegalArgumentException e) {
// can fail
}
}
Variant variant = request.selectVariant(variants);
return (variant != null && (variant.getMediaType().equals(APPLICATION_JSON_PAGED) || variant.getMediaType().equals(APPLICATION_XML_PAGED)));
}
use of javax.ws.rs.core.Variant in project fcrepo by fcrepo.
the class FedoraLdpIT method testGetObjectGraphVariants.
@Test
public void testGetObjectGraphVariants() throws IOException {
final String location = getLocation(postObjMethod());
for (final Variant variant : POSSIBLE_RDF_VARIANTS) {
final HttpGet getObjMethod = new HttpGet(location);
final String type = variant.getMediaType().getType();
getObjMethod.addHeader(ACCEPT, type);
assertEquals("Got bad response for type " + type + " !", OK.getStatusCode(), getStatus(getObjMethod));
}
}
Aggregations