Search in sources :

Example 71 with MediaType

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);
    }
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 72 with MediaType

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();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 73 with MediaType

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);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget)

Example 74 with MediaType

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);
    }
}
Also used : Arrays(java.util.Arrays) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Trace(com.evolveum.midpoint.util.logging.Trace) AbstractConfigurableProvider(org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) MediaType(javax.ws.rs.core.MediaType) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) OutputStream(java.io.OutputStream) IOException(java.io.IOException) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) Type(java.lang.reflect.Type) SystemException(com.evolveum.midpoint.util.exception.SystemException) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) QName(javax.xml.namespace.QName) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) WebApplicationException(javax.ws.rs.WebApplicationException) Annotation(java.lang.annotation.Annotation) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 75 with MediaType

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);
    }
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Individual(org.apache.jena.ontology.Individual) OntModel(org.apache.jena.ontology.OntModel) MediaType(javax.ws.rs.core.MediaType)

Aggregations

MediaType (javax.ws.rs.core.MediaType)477 Test (org.junit.Test)184 Path (javax.ws.rs.Path)44 Produces (javax.ws.rs.Produces)44 ByteArrayInputStream (java.io.ByteArrayInputStream)42 WebApplicationException (javax.ws.rs.WebApplicationException)41 IOException (java.io.IOException)40 Response (javax.ws.rs.core.Response)40 InputStream (java.io.InputStream)38 ArrayList (java.util.ArrayList)31 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)31 Type (java.lang.reflect.Type)30 Consumes (javax.ws.rs.Consumes)27 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)27 GET (javax.ws.rs.GET)25 OutputStream (java.io.OutputStream)23 Annotation (java.lang.annotation.Annotation)23 HashSet (java.util.HashSet)22 Locale (java.util.Locale)22 HashMap (java.util.HashMap)21