Search in sources :

Example 11 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class MessageBodyFactory method addWriters.

private static void addWriters(final List<WriterModel> models, final Set<MessageBodyWriter> writers, final boolean custom) {
    for (final MessageBodyWriter provider : writers) {
        final List<MediaType> values = MediaTypes.createFrom(provider.getClass().getAnnotation(Produces.class));
        models.add(new WriterModel(provider, values, custom));
    }
}
Also used : WriterModel(org.glassfish.jersey.message.WriterModel) Produces(javax.ws.rs.Produces) MediaType(javax.ws.rs.core.MediaType) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 12 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class TestRuntimeDelegate method testMediaType.

public void testMediaType() {
    MediaType m = new MediaType("text", "plain");
    Assert.assertNotNull(m);
}
Also used : MediaType(javax.ws.rs.core.MediaType)

Example 13 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class UriConnegFilter method filter.

@Override
public void filter(final ContainerRequestContext rc) throws IOException {
    final UriInfo uriInfo = rc.getUriInfo();
    // Quick check for a '.' character
    String path = uriInfo.getRequestUri().getRawPath();
    if (path.indexOf('.') == -1) {
        return;
    }
    final List<PathSegment> l = uriInfo.getPathSegments(false);
    if (l.isEmpty()) {
        return;
    }
    // Get the last non-empty path segment
    PathSegment segment = null;
    for (int i = l.size() - 1; i >= 0; i--) {
        segment = l.get(i);
        if (segment.getPath().length() > 0) {
            break;
        }
    }
    if (segment == null) {
        return;
    }
    final int length = path.length();
    // Get the suffixes
    final String[] suffixes = segment.getPath().split("\\.");
    for (int i = suffixes.length - 1; i >= 1; i--) {
        final String suffix = suffixes[i];
        if (suffix.length() == 0) {
            continue;
        }
        final MediaType accept = mediaTypeMappings.get(suffix);
        if (accept != null) {
            rc.getHeaders().putSingle(HttpHeaders.ACCEPT, accept.toString());
            final int index = path.lastIndexOf('.' + suffix);
            path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
            suffixes[i] = "";
            break;
        }
    }
    for (int i = suffixes.length - 1; i >= 1; i--) {
        final String suffix = suffixes[i];
        if (suffix.length() == 0) {
            continue;
        }
        final String acceptLanguage = languageMappings.get(suffix);
        if (acceptLanguage != null) {
            rc.getHeaders().putSingle(HttpHeaders.ACCEPT_LANGUAGE, acceptLanguage);
            final int index = path.lastIndexOf('.' + suffix);
            path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
            suffixes[i] = "";
            break;
        }
    }
    if (length != path.length()) {
        rc.setRequestUri(uriInfo.getRequestUriBuilder().replacePath(path).build());
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) PathSegment(javax.ws.rs.core.PathSegment) UriInfo(javax.ws.rs.core.UriInfo)

Example 14 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class MethodSelectingRouter method addAllConsumesProducesCombinations.

private void addAllConsumesProducesCombinations(final List<ConsumesProducesAcceptor> acceptors, final MethodRouting methodRouting) {
    final ResourceMethod resourceMethod = methodRouting.method;
    final Set<MediaType> effectiveInputTypes = new LinkedHashSet<>();
    boolean consumesFromWorkers = fillMediaTypes(effectiveInputTypes, resourceMethod, resourceMethod.getConsumedTypes(), true);
    final Set<MediaType> effectiveOutputTypes = new LinkedHashSet<>();
    boolean producesFromWorkers = fillMediaTypes(effectiveOutputTypes, resourceMethod, resourceMethod.getProducedTypes(), false);
    final Set<ConsumesProducesAcceptor> acceptorSet = new HashSet<>();
    for (MediaType consumes : effectiveInputTypes) {
        for (MediaType produces : effectiveOutputTypes) {
            acceptorSet.add(new ConsumesProducesAcceptor(new CombinedMediaType.EffectiveMediaType(consumes, consumesFromWorkers), new CombinedMediaType.EffectiveMediaType(produces, producesFromWorkers), methodRouting));
        }
    }
    acceptors.addAll(acceptorSet);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 15 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class MethodSelectingRouter method selectMethod.

/**
     * Select method to be invoked. Method is chosen among the given set of acceptors (if they are compatible with acceptable
     * media types).
     *
     * @param acceptableMediaTypes  media types acceptable by the client.
     * @param satisfyingAcceptors   pre-computed acceptors.
     * @param effectiveContentType  media type of incoming entity.
     * @param singleInvokableMethod flag determining whether only one method to be invoked has been found among satisfying
     *                              acceptors.
     * @return method to be invoked.
     */
private MethodSelector selectMethod(final List<AcceptableMediaType> acceptableMediaTypes, final List<ConsumesProducesAcceptor> satisfyingAcceptors, final MediaType effectiveContentType, final boolean singleInvokableMethod) {
    // Selected method we have a reader and writer for.
    final MethodSelector method = new MethodSelector(null);
    // If we cannot find a writer at this point use the best alternative.
    final MethodSelector alternative = new MethodSelector(null);
    for (final MediaType acceptableMediaType : acceptableMediaTypes) {
        for (final ConsumesProducesAcceptor satisfiable : satisfyingAcceptors) {
            final CombinedMediaType produces = CombinedMediaType.create(acceptableMediaType, satisfiable.produces);
            if (produces != CombinedMediaType.NO_MATCH) {
                final CombinedMediaType consumes = CombinedMediaType.create(effectiveContentType, satisfiable.consumes);
                final RequestSpecificConsumesProducesAcceptor candidate = new RequestSpecificConsumesProducesAcceptor(consumes, produces, satisfiable.produces.isDerived(), satisfiable.methodRouting);
                if (singleInvokableMethod) {
                    // Only one possible method and it's compatible.
                    return new MethodSelector(candidate);
                } else if (candidate.compareTo(method.selected) < 0) {
                    // Candidate is better than the previous one.
                    if (method.selected == null || candidate.methodRouting.method != method.selected.methodRouting.method) {
                        // No candidate so far or better candidate.
                        if (isReadable(candidate) && isWriteable(candidate)) {
                            method.consider(candidate);
                        } else {
                            alternative.consider(candidate);
                        }
                    } else {
                        // Same resource method - better candidate, no need to compare anything else.
                        method.consider(candidate);
                    }
                }
            }
        }
    }
    return method.selected != null ? method : alternative;
}
Also used : MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType)

Aggregations

MediaType (javax.ws.rs.core.MediaType)228 Test (org.junit.Test)112 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)29 Path (javax.ws.rs.Path)25 Produces (javax.ws.rs.Produces)24 WebApplicationException (javax.ws.rs.WebApplicationException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 HashSet (java.util.HashSet)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)20 IOException (java.io.IOException)18 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)18 InputStream (java.io.InputStream)16 GET (javax.ws.rs.GET)16 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)16 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)15 ArrayList (java.util.ArrayList)14 Consumes (javax.ws.rs.Consumes)14 HashMap (java.util.HashMap)13 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)12 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)12