use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MultiPartReaderWriterTest method testTwo.
@Test
public void testTwo() {
final WebTarget target = target().path("multipart/two");
try {
final MultiPart result = target.request("multipart/mixed").get(MultiPart.class);
checkMediaType(new MediaType("multipart", "mixed"), result.getMediaType());
assertEquals(2, result.getBodyParts().size());
final BodyPart part1 = result.getBodyParts().get(0);
checkMediaType(new MediaType("text", "plain"), part1.getMediaType());
checkEntity("This is the first segment", (BodyPartEntity) part1.getEntity());
final BodyPart part2 = result.getBodyParts().get(1);
checkMediaType(new MediaType("text", "xml"), part2.getMediaType());
checkEntity("<outer><inner>value</inner></outer>", (BodyPartEntity) part2.getEntity());
result.getParameterizedHeaders();
result.cleanup();
} catch (final IOException | ParseException e) {
e.printStackTrace(System.out);
fail("Caught exception: " + e);
}
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MultiPartResource method etag.
@GET
@Path("etag")
@Produces("multipart/mixed")
public Response etag() {
MultiPart entity = new MultiPart();
// Exercise manually adding part(s) to the bodyParts property
BodyPart part = new BodyPart("This is the only segment", new MediaType("text", "plain"));
part.getHeaders().add("ETag", "\"value\"");
entity.getBodyParts().add(part);
return Response.ok(entity).type("multipart/mixed").build();
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class CharsetTest method _test.
@Override
public <T> void _test(T in, Class resource, MediaType m) {
WebTarget t = target(resource.getSimpleName());
for (String charset : CHARSETS) {
Map<String, String> p = new HashMap<>();
p.put("charset", charset);
MediaType _m = new MediaType(m.getType(), m.getSubtype(), p);
Response rib = t.request().post(Entity.entity(in, _m));
byte[] inBytes = getRequestEntity();
byte[] outBytes = getEntityAsByteArray(rib);
_verify(inBytes, outBytes);
}
}
use of javax.ws.rs.core.MediaType in project midpoint by Evolveum.
the class MidpointAbstractProvider method readFrom.
@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
if (entityStream == null) {
return null;
}
PrismParser parser = getParser(entityStream);
T object;
try {
LOGGER.info("type of request: {}", type);
if (PrismObject.class.isAssignableFrom(type)) {
object = (T) parser.parse();
} else {
// TODO consider prescribing type here (if no convertor is specified)
object = parser.parseRealValue();
}
if (object != null && !type.isAssignableFrom(object.getClass())) {
// TODO treat multivalues here
Optional<Annotation> convertorAnnotation = Arrays.stream(annotations).filter(a -> a instanceof Convertor).findFirst();
if (convertorAnnotation.isPresent()) {
Class<? extends ConvertorInterface> convertorClass = ((Convertor) convertorAnnotation.get()).value();
ConvertorInterface convertor;
try {
convertor = convertorClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new SystemException("Couldn't instantiate convertor class " + convertorClass, e);
}
object = (T) convertor.convert(object);
}
}
return object;
} catch (SchemaException ex) {
throw new WebApplicationException(ex);
}
}
use of javax.ws.rs.core.MediaType in project ORCID-Source by ORCID.
the class RDFMessageBodyWriter method writeTo.
/**
* Write a type to an HTTP response. The response header map is mutable but
* any changes must be made before writing to the output stream since the
* headers will be flushed prior to writing the response body.
*
* @param message
* the instance to write.
* @param type
* the class of object that is to be written.
* @param genericType
* the type of object to be written, obtained either by
* reflection of a resource method return type or by inspection
* of the returned instance.
* {@link javax.ws.rs.core.GenericEntity} provides a way to
* specify this information at runtime.
* @param annotations
* an array of the annotations on the resource method that
* returns the object.
* @param mediaType
* the media type of the HTTP entity.
* @param httpHeaders
* a mutable map of the HTTP response headers.
* @param entityStream
* the {@link java.io.OutputStream} for the HTTP entity. The
* implementation should not close the output stream.
* @throws java.io.IOException
* if an IO error arises
* @throws javax.ws.rs.WebApplicationException
* if a specific HTTP error response needs to be produced. Only
* effective if thrown prior to the response being committed.
*/
@Override
public void writeTo(OrcidMessage xml, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
OntModel m = getOntModel();
if (xml.getErrorDesc() != null) {
describeError(xml.getErrorDesc(), m);
}
OrcidProfile orcidProfile = xml.getOrcidProfile();
// System.out.println(httpHeaders);
Individual profileDoc = null;
if (orcidProfile != null) {
Individual person = describePerson(orcidProfile, m);
if (person != null) {
profileDoc = describeAccount(orcidProfile, m, person);
}
}
MediaType jsonLd = new MediaType("application", "ld+json");
MediaType nTriples = new MediaType("application", "n-triples");
MediaType rdfXml = new MediaType("application", "rdf+xml");
String base = null;
if (getUriInfo() != null) {
getUriInfo().getAbsolutePath().toASCIIString();
}
if (mediaType.isCompatible(nTriples)) {
// NOTE: N-Triples requires absolute URIs
m.write(entityStream, "N-TRIPLES");
} else if (mediaType.isCompatible(jsonLd)) {
m.write(entityStream, "JSON-LD", base);
} else if (mediaType.isCompatible(rdfXml)) {
m.write(entityStream, "RDF/XML", base);
} else {
// Turtle is the safest default
m.write(entityStream, "TURTLE", base);
}
}
Aggregations