Package javax.ws.rs.core

Examples of javax.ws.rs.core.MediaType


        Set<MediaType> consumedMimes = record.getMetadata().getConsumes();
        // if not specified, then treat as if consumes */*
        if (consumedMimes.size() == 0) {
            return false;
        }
        MediaType inputMediaType = context.getHttpHeaders().getMediaType();
        if (inputMediaType == null) {
            inputMediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
        }
        for (MediaType mediaType : consumedMimes) {
            if (mediaType.isCompatible(inputMediaType)) {
View Full Code Here


     * @return
     */
    private MethodRecord selectBestMatchingMethod(List<? extends MethodRecord> methodRecords,
                                                  RuntimeContext context) {
        HttpHeaders httpHeaders = context.getHttpHeaders();
        MediaType inputMediaType = httpHeaders.getMediaType();
        List<MediaType> acceptableMediaTypes = httpHeaders.getAcceptableMediaTypes();

        MethodRecord bestMatch = null;
        for (MethodRecord record : methodRecords) {
            if (compareMethods(record, bestMatch, inputMediaType, acceptableMediaTypes) > 0) {
View Full Code Here

     */
    private int compareMethodsConsumes(MethodRecord record1,
                                       MethodRecord record2,
                                       MediaType inputMediaType) {
        // get media type of metadata 1 best matching the input media type
        MediaType bestMatch1 =
            selectBestMatchingMediaType(inputMediaType, record1.getMetadata().getConsumes());
        // get media type of metadata 2 best matching the input media type
        MediaType bestMatch2 =
            selectBestMatchingMediaType(inputMediaType, record2.getMetadata().getConsumes());

        if (bestMatch1 == null && bestMatch2 == null) {
            return 0;
        }
View Full Code Here

     */
    private int compareMethodsProduces(MethodRecord record1,
                                       MethodRecord record2,
                                       List<MediaType> acceptableMediaTypes) {

        MediaType bestMatch1 = null;
        MediaType bestMatch2 = null;

        // the acceptMediaTypes list is already sorted according to preference
        // of media types,
        // so we need to stop with the first media type that has a match
        for (MediaType acceptableMediaType : acceptableMediaTypes) {
View Full Code Here

     * @param mediaType the media type to match against
     * @param mediaTypes the list of media types to select the best match from
     * @return the best matching media type from the list
     */
    private MediaType selectBestMatchingMediaType(MediaType mediaType, Set<MediaType> mediaTypes) {
        MediaType bestMatch = null;
        for (MediaType mt : mediaTypes) {
            if (!mt.isCompatible(mediaType)) {
                continue;
            }
            if (bestMatch == null || MediaTypeUtils.compareTo(mt, bestMatch) > 0) {
View Full Code Here

            String errMsg = String.format("Wrong MediaType format for MediaType:\"%s\"", value);
            logger.error(errMsg);
            throw new IllegalArgumentException(errMsg, e);
        }

        return new MediaType(type, subType, paramsMap);
    }
View Full Code Here

    public static OpenSearchImage createOpenSearchImage(String mediaTypeString, String url) {
        if (mediaTypeString == null) {
            throw new NullPointerException("mediaType parameter is null");
        }
        OpenSearchImage image = new OpenSearchImage();
        MediaType mediaType = MediaType.valueOf(mediaTypeString);
        if (MediaTypeUtils.equalsIgnoreParameters(MediaTypeUtils.IMAGE_X_ICON, mediaType) || MediaTypeUtils
            .equalsIgnoreParameters(MediaTypeUtils.IMAGE_VND, mediaType)) {
            image.setHeight(ICON_SIZE);
            image.setWidth(ICON_SIZE);
            image.setType(mediaTypeString);
View Full Code Here

    public MediaType fromString(String value) throws IllegalArgumentException {
        if (value == null) {
            throw new IllegalArgumentException("MediaType header is null");
        }

        MediaType cached = cache.get(value);
        if (cached != null) {
            return cached;
        }

        String type = "*";
        String subType = "*";
        Map<String, String> paramsMap = null;
        String[] all = SEMICOLON.split(value);

        try {
            // type and subType
            String main = all[0];
            String[] mainArray = SLASH.split(main);
            type = mainArray[0];
            if ("".equals(type)) {
                String errMsg = String.format(Messages.getMessage("mediaTypeWrongFormat"), value);
                logger.error(errMsg);
                throw new IllegalArgumentException(errMsg);
            }
            if (mainArray.length == 1 && MediaType.MEDIA_TYPE_WILDCARD.equals(type)) {
                subType = MediaType.MEDIA_TYPE_WILDCARD;
            } else {
                subType = mainArray[1];
            }

            // parameters
            if (all.length > 1) {
                paramsMap = new LinkedHashMap<String, String>();
                for (int i = 1; i < all.length; i++) {
                    String[] param = EQUALS.split(all[i]);
                    paramsMap.put(param[0].trim(), param[1].trim());
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            String errMsg = String.format(Messages.getMessage("mediaTypeWrongFormat"), value);
            logger.error(errMsg, e);
            throw new IllegalArgumentException(errMsg, e);
        }

        return cache.put(value, new MediaType(type, subType, paramsMap));
    }
View Full Code Here

     * @param providers
     * @return
     * @throws IOException
     */
    public <T> T getBody(Class<T> type, Type genericType, Providers providers) throws IOException {
        MediaType mt = MediaType.valueOf(getContentType());
        MessageBodyReader<T> reader = providers.getMessageBodyReader(type, genericType, null, mt);
        if (reader == null)
            throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
        return reader.readFrom(type, genericType, null, mt, getHeaders(), getInputStream());
    }
View Full Code Here

        return new XmlWrapper(((ByteArrayOutputStream)rt.getOutputStream()).toByteArray(), null);
    }

    @SuppressWarnings("unchecked")
    public Source marshal(XmlWrapper xmlWrapper, ValidationEventHandler errorHandler) {
        MediaType type = null;

        String contentType = xmlWrapper.getType();
        if (contentType == null) {
            // should never happen
            type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
        } else if (contentType.equals("xhtml")) {
            type = MediaType.APPLICATION_XML_TYPE;
        } else {
            type = MediaType.valueOf(contentType);
        }

        RuntimeContext runtimeContext = RuntimeContextTLS.getRuntimeContext();
        Providers providers = runtimeContext.getProviders();
        Class<? extends Object> cls = xmlWrapper.getValue().getClass();

        // this code ignores possible generic types
        // if in the future we would like to support the generic types
        // should check here if cls is GenericEntity and handle it

        MessageBodyWriter<Object> writer =
            (MessageBodyWriter<Object>)providers.getMessageBodyWriter(cls, cls, null, type);

        if (writer == null) {
            logger.error(Messages.getMessage("noWriterFound"), cls.getName(), type.toString());
            throw new WebApplicationException(500);
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
View Full Code Here

TOP

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

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.