Search in sources :

Example 46 with MediaType

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

the class OutboundMessageContext method getAcceptableMediaTypes.

/**
     * Get a list of media types that are acceptable for the message.
     *
     * @return a read-only list of requested message media types sorted according
     * to their q-value, with highest preference first.
     */
@SuppressWarnings("unchecked")
public List<MediaType> getAcceptableMediaTypes() {
    final List<Object> values = headers.get(HttpHeaders.ACCEPT);
    if (values == null || values.isEmpty()) {
        return WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST;
    }
    final List<MediaType> result = new ArrayList<>(values.size());
    final RuntimeDelegate rd = RuntimeDelegate.getInstance();
    boolean conversionApplied = false;
    for (final Object value : values) {
        try {
            if (value instanceof MediaType) {
                final AcceptableMediaType _value = AcceptableMediaType.valueOf((MediaType) value);
                // true if value was not an instance of AcceptableMediaType already
                conversionApplied = _value != value;
                result.add(_value);
            } else {
                conversionApplied = true;
                result.addAll(HttpHeaderReader.readAcceptMediaType(HeaderUtils.asString(value, rd)));
            }
        } catch (java.text.ParseException e) {
            throw exception(HttpHeaders.ACCEPT, value, e);
        }
    }
    if (conversionApplied) {
        // cache converted
        headers.put(HttpHeaders.ACCEPT, result.stream().map((Function<MediaType, Object>) mediaType -> mediaType).collect(Collectors.toList()));
    }
    return Collections.unmodifiableList(result);
}
Also used : CommonProperties(org.glassfish.jersey.CommonProperties) LocalizationMessages(org.glassfish.jersey.internal.LocalizationMessages) Date(java.util.Date) Configuration(javax.ws.rs.core.Configuration) HashMap(java.util.HashMap) NewCookie(javax.ws.rs.core.NewCookie) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) ParseException(java.text.ParseException) OutputStream(java.io.OutputStream) GenericEntity(javax.ws.rs.core.GenericEntity) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) EntityTag(javax.ws.rs.core.EntityTag) Collectors(java.util.stream.Collectors) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) Cookie(javax.ws.rs.core.Cookie) GenericType(javax.ws.rs.core.GenericType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ProcessingException(javax.ws.rs.ProcessingException) Collections(java.util.Collections) Link(javax.ws.rs.core.Link) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) ParseException(java.text.ParseException) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate)

Example 47 with MediaType

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

the class MessageBodyFactory method getMessageBodyWriterMediaTypes.

@Override
@SuppressWarnings("unchecked")
public List<MediaType> getMessageBodyWriterMediaTypes(final Class<?> c, final Type t, final Annotation[] as) {
    final Set<MediaType> writeableMediaTypes = new LinkedHashSet<>();
    for (final WriterModel model : writers) {
        boolean writeableWorker = false;
        for (final MediaType mt : model.declaredTypes()) {
            if (model.isWriteable(c, t, as, mt)) {
                writeableMediaTypes.add(mt);
                writeableWorker = true;
            }
            if (!writeableMediaTypes.contains(MediaType.WILDCARD_TYPE) && writeableWorker && model.declaredTypes().contains(MediaType.WILDCARD_TYPE)) {
                writeableMediaTypes.add(MediaType.WILDCARD_TYPE);
            }
        }
    }
    final List<MediaType> mtl = new ArrayList<>(writeableMediaTypes);
    Collections.sort(mtl, MediaTypes.PARTIAL_ORDER_COMPARATOR);
    return mtl;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WriterModel(org.glassfish.jersey.message.WriterModel) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType)

Example 48 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 49 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 50 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)

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