Package javax.ws.rs.core

Examples of javax.ws.rs.core.Variant


    public static Entity<Form> form(final MultivaluedMap<String, String> formData) {
        return new Entity<Form>(new Form(formData), MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    }

    private Entity(final T entity, final MediaType mediaType) {
        this(entity, new Variant(mediaType, (Locale) null, null), EMPTY_ANNOTATIONS);
    }
View Full Code Here


    private Entity(final T entity, final Variant variant) {
        this(entity, variant, EMPTY_ANNOTATIONS);
    }

    private Entity(final T entity, final MediaType mediaType, Annotation[] annotations) {
        this(entity, new Variant(mediaType, (Locale) null, null), annotations);
    }
View Full Code Here

        if (encodings.isEmpty()) addVariant(mediaType, language, null);
        else for (String encoding : encodings) addVariant(mediaType, language, encoding);       
    }

    private void addVariant(MediaType mediaType, Locale language, String encoding) {
        variants.add(new Variant(mediaType, language, encoding));
    }
View Full Code Here

                        context.getUriInfo(),
                        resource, path);

                List<Variant> vl = Variant.mediaTypes(MediaTypes.WADL, MediaTypes.WADL_JSON, MediaType.APPLICATION_XML_TYPE)
                        .add().build();
                Variant v = context.getRequest().selectVariant(vl);
                if (v == null) {
                    context.getResponse().setResponse(
                            Response.noContent().header("Allow", allow).header("Last-modified", lastModified).build());
                }
                else {
View Full Code Here

        }

        // Select the right variant based on the request type
        List<Variant> vl = Variant.mediaTypes(MediaTypes.WADL, MediaTypes.WADL_JSON, MediaType.APPLICATION_XML_TYPE)
            .add().build();
        Variant v = request.selectVariant(vl);
        if (v==null) {
            return Response.notAcceptable(vl).build();
        }
       
        // Update the last modified stamp
        if (applicationDescription == null || ((lastBaseUri != null) && !lastBaseUri.equals(uriInfo.getBaseUri()) && !lastVariant.equals(v))) {
            this.lastBaseUri = uriInfo.getBaseUri();
            this.lastModified = new SimpleDateFormat(HTTPDATEFORMAT).format(new Date());
            this.lastVariant = v;

            applicationDescription = wadlContext.getApplication(uriInfo);
            final Application application = applicationDescription.getApplication();

            final ByteArrayOutputStream os = new ByteArrayOutputStream();

            if(v.getMediaType().equals(MediaTypes.WADL)) {
                try {
                    final Marshaller marshaller = wadlContext.getJAXBContext().createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.marshal(application, os);
                    cachedWadl = os.toByteArray();
                    os.close();
                } catch (Exception e) {
                    LOGGER.log(Level.WARNING, "Could not marshal wadl Application.", e);
                    return Response.serverError().build();
                }
            } else {
                final MessageBodyWriter<Application> messageBodyWriter = providers.getMessageBodyWriter(Application.class, null, new Annotation[0], v.getMediaType());

                if(messageBodyWriter == null) {
                    return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
                }

                try {
                    messageBodyWriter.writeTo(application, Application.class, null, new Annotation[0], v.getMediaType(), null  /* headers */, os);
                    cachedWadl = os.toByteArray();
                    os.close();
                } catch (Exception e) {
                    LOGGER.log(Level.WARNING, "Could not serialize wadl Application.", e);
                    return Response.serverError().build();
View Full Code Here

    public Variant selectVariant(List<Variant> variants) throws IllegalArgumentException {
        if (variants == null || variants.isEmpty()) {
            throw new IllegalArgumentException(METHOD_PARAMETER_CANNOT_BE_NULL_OR_EMPTY);
        }
        Ref<String> varyValueRef = Refs.emptyRef();
        final Variant variant = VariantSelector.selectVariant(this, variants, varyValueRef);
        this.varyValue = varyValueRef.get();
        return variant;
    }
View Full Code Here

            }
        }
    }

    private void addVariant(MediaType mediaType, Locale language, String encoding) {
        variants.add(new Variant(mediaType, language, encoding));
    }
View Full Code Here

    public String getResourceWithVaryAccept(@Context Request request) {
        List<Variant> variants =
            VariantListBuilder.newInstance().mediaTypes(MediaType.TEXT_PLAIN_TYPE,
                                                        MediaType.APPLICATION_OCTET_STREAM_TYPE)
                .add().build();
        Variant bestVariant = request.selectVariant(variants);
        return bestVariant.toString();
    }
View Full Code Here

    public String getResourceWithVaryAcceptEncoding(@Context Request request) {
        List<Variant> variants =
            VariantListBuilder.newInstance().mediaTypes(MediaType.TEXT_PLAIN_TYPE,
                                                        MediaType.APPLICATION_OCTET_STREAM_TYPE)
                .encodings("gzip", "identity", "deflate").add().build();
        Variant bestVariant = request.selectVariant(variants);
        return bestVariant.toString();
    }
View Full Code Here

        if (acceptHeader == null || acceptHeader.isEmpty()
            || MediaType.WILDCARD_TYPE.equals(requestHeaders.getAcceptableMediaTypes().get(0))) {
            return Response.ok(sb.toString()).type(MediaType.TEXT_PLAIN_TYPE).build();
        }

        Variant variant =
            request.selectVariant(VariantListBuilder.newInstance()
                .mediaTypes(MediaType.TEXT_PLAIN_TYPE,
                            MediaType.TEXT_XML_TYPE,
                            MediaType.APPLICATION_JSON_TYPE).add().build());
        if (variant != null) {
            if (MediaType.APPLICATION_JSON_TYPE.isCompatible(variant.getMediaType())) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", sb.toString());
                return Response.ok(jsonObject).type(MediaType.APPLICATION_JSON).build();
            } else if (MediaType.TEXT_XML_TYPE.isCompatible(variant.getMediaType())) {
                Echo e = new Echo();
                e.setValue(sb.toString());
                return Response.ok(e).type(MediaType.TEXT_XML).build();
            }
        }
View Full Code Here

TOP

Related Classes of javax.ws.rs.core.Variant

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.