Package javax.ws.rs.core

Examples of javax.ws.rs.core.MediaType


       
        SortedMap<OperationResourceInfo, MultivaluedMap<String, String>> candidateList =
            new TreeMap<OperationResourceInfo, MultivaluedMap<String, String>>(
                new OperationResourceInfoComparator(message, httpMethod));

        MediaType requestType = requestContentType == null
                                ? ALL_TYPES : MediaType.valueOf(requestContentType);
       
        int pathMatched = 0;
        int methodMatched = 0;
        int consumeMatched = 0;
        int produceMatched = 0;
       
        boolean subresourcesOnly = true;
        for (MediaType acceptType : acceptContentTypes) {
            for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) {
                URITemplate uriTemplate = ori.getURITemplate();
                MultivaluedMap<String, String> map = new MetadataMap<String, String>(values);
                if (uriTemplate != null && uriTemplate.match(path, map)) {
                    boolean added = false;
                    if (ori.isSubResourceLocator()) {
                        candidateList.put(ori, map);
                        added = true;
                    } else {
                        String finalGroup = map.getFirst(URITemplate.FINAL_MATCH_GROUP);
                        if (finalGroup == null || StringUtils.isEmpty(finalGroup)
                            || finalGroup.equals("/")) {
                            pathMatched++;
                            boolean mMatched = matchHttpMethod(ori.getHttpMethod(), httpMethod);
                            boolean cMatched = matchConsumeTypes(requestType, ori);
                            boolean pMatched = matchProduceTypes(acceptType, ori);
                            if (mMatched && cMatched && pMatched) {
                                subresourcesOnly = false;
                                candidateList.put(ori, map);
                                added = true;
                            } else {
                                methodMatched = mMatched ? methodMatched + 1 : methodMatched;
                                produceMatched = pMatched ? produceMatched + 1 : produceMatched;
                                consumeMatched = cMatched ? consumeMatched + 1 : consumeMatched;
                                logNoMatchMessage(ori, path, httpMethod, requestType, acceptContentTypes);
                            }
                        } else {
                            logNoMatchMessage(ori, path, httpMethod, requestType, acceptContentTypes);
                        }
                    }
                    if (added && LOG.isLoggable(Level.FINE)) {
                        LOG.fine(new org.apache.cxf.common.i18n.Message("OPER_SELECTED_POSSIBLY",
                                  BUNDLE,
                                  ori.getMethodToInvoke().getName()).toString());
                    }
                } else {
                    logNoMatchMessage(ori, path, httpMethod, requestType, acceptContentTypes);
                }
            }
            if (!candidateList.isEmpty() && !subresourcesOnly) {
                break;
            }
        }
        if (!candidateList.isEmpty()) {
            Map.Entry<OperationResourceInfo, MultivaluedMap<String, String>> firstEntry =
                candidateList.entrySet().iterator().next();
            values.clear();
            values.putAll(firstEntry.getValue());
            OperationResourceInfo ori = firstEntry.getKey();
            if (headMethodPossible(ori.getHttpMethod(), httpMethod)) {
                LOG.info(new org.apache.cxf.common.i18n.Message("GET_INSTEAD_OF_HEAD",
                         BUNDLE, resource.getServiceClass().getName(),
                         ori.getMethodToInvoke().getName()).toString());
            }
            LOG.fine(new org.apache.cxf.common.i18n.Message("OPER_SELECTED",
                           BUNDLE, ori.getMethodToInvoke().getName(),
                           resource.getServiceClass().getName()).toString());
            return ori;
        }
       
        int status;
       
        // criteria matched the least number of times will determine the error code;
        // priority : path, method, consumes, produces;
        if (pathMatched == 0) {
            status = 404;
        } else if (methodMatched == 0) {
            status = 405;
        } else if (consumeMatched <= produceMatched) {
            status = 415;
        } else {
            status = 406;
        }
       
        String name = resource.isRoot() ? "NO_OP_EXC" : "NO_SUBRESOURCE_METHOD_FOUND";
        org.apache.cxf.common.i18n.Message errorMsg =
            new org.apache.cxf.common.i18n.Message(name,
                                                   BUNDLE,
                                                   path,
                                                   httpMethod,
                                                   requestType.toString(),
                                                   convertTypesToString(acceptContentTypes));
        if (!"OPTIONS".equalsIgnoreCase(httpMethod) && logNow) {
            LOG.warning(errorMsg.toString());
        }
        ResponseBuilder rb = createResponseBuilder(resource, status, methodMatched == 0);
View Full Code Here


                                           Class<?> pClass, Type genericType,
                                           String defaultValue,
                                           boolean decode) {
       
        MessageContext mc = new MessageContextImpl(m);
        MediaType mt = mc.getHttpHeaders().getMediaType();
       
        @SuppressWarnings("unchecked")
        MultivaluedMap<String, String> params = (MultivaluedMap<String, String>)m.get(FORM_PARAM_MAP);
       
        if (params == null) {
            params = new MetadataMap<String, String>();
            m.put(FORM_PARAM_MAP, params);
       
            if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
                String body = (String)m.get("org.apache.cxf.jaxrs.provider.form.body");
                if (body == null) {
                    body = FormUtils.readBody(m.getContent(InputStream.class), mt);
                    m.put("org.apache.cxf.jaxrs.provider.form.body", body);
                }
                HttpServletRequest request = (HttpServletRequest)m.get(AbstractHTTPDestination.HTTP_REQUEST);
                FormUtils.populateMapFromString(params, (String)body, decode, request);
            } else {
                if (mt != null && "multipart".equalsIgnoreCase(mt.getType())
                    && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) {
                    MultipartBody body = AttachmentUtils.getMultipartBody(mc);
                    FormUtils.populateMapFromMultipart(params, body, decode);
                } else {
                    org.apache.cxf.common.i18n.Message errorMsg =
                        new org.apache.cxf.common.i18n.Message("WRONG_FORM_MEDIA_TYPE",
                                                               BUNDLE,
                                                               mt == null ? "*/*" : mt.toString());
                    LOG.warning(errorMsg.toString());
                    throw new WebApplicationException(415);
                }
            }
        }
View Full Code Here

                            if (!parameters.containsKey(entry.getKey())) {
                                parameters.put(entry.getKey(), entry.getValue());
                            }
                        }
                    }
                    supportedMimeTypeList.add(new MediaType(type, subtype, parameters));
                }
            }
        }

        return new ArrayList<MediaType>(supportedMimeTypeList);
View Full Code Here

       
        if (o == null) {
            return;
        }
       
        MediaType contentType = MediaType.valueOf(headers.getFirst("Content-Type"));
       
        MessageBodyWriter mbw = ProviderFactory.getInstance(outMessage).createMessageBodyWriter(
            cls, type, anns, contentType, outMessage);
        if (mbw != null) {
            try {
View Full Code Here

                || status >= 400) {
                return cls == Response.class ? r : status >= 400 ? inputStream : null;
            }
        }
       
        MediaType contentType = getResponseContentType(r);
       
        MessageBodyReader mbr = ProviderFactory.getInstance(inMessage).createMessageBodyReader(
            cls, type, anns, contentType, inMessage);
        if (mbr != null) {
            try {
View Full Code Here

        // when
        final RestfulResponse<HomePageRepresentation> restfulResponse = RestfulResponse.ofT(resp);

        // then
        final MediaType contentType = restfulResponse.getHeader(Header.CONTENT_TYPE);
        assertThat(contentType, hasType("application"));
        assertThat(contentType, hasSubType("json"));
        assertThat(contentType, hasParameter("profile", "urn:org.restfulobjects:repr-types/homepage"));
        assertThat(contentType, is(RepresentationType.HOME_PAGE.getMediaType()));
    }
View Full Code Here

        // when
        final RestfulResponse<VersionRepresentation> restfulResponse = RestfulResponse.ofT(resp);

        // then
        final MediaType contentType = restfulResponse.getHeader(Header.CONTENT_TYPE);
        assertThat(contentType, hasType("application"));
        assertThat(contentType, hasSubType("json"));
        assertThat(contentType, hasParameter("profile", "urn:org.restfulobjects:repr-types/version"));
        assertThat(contentType, is(RepresentationType.VERSION.getMediaType()));
    }
View Full Code Here

        // when
        final RestfulResponse<ListRepresentation> restfulResponse = RestfulResponse.ofT(resp);

        // then
        final MediaType contentType = restfulResponse.getHeader(Header.CONTENT_TYPE);
        assertThat(contentType, hasType("application"));
        assertThat(contentType, hasSubType("json"));
        assertThat(contentType, hasParameter("profile", "urn:org.restfulobjects:repr-types/list"));
        assertThat(contentType, is(RepresentationType.LIST.getMediaType()));
    }
View Full Code Here

    private final Class<T> returnType;
    private T entity;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static RestfulResponse<JsonRepresentation> of(final Response response) {
        final MediaType jaxRsMediaType = getHeader(response, Header.CONTENT_TYPE);
        final RepresentationType representationType = RepresentationType.lookup(jaxRsMediaType);
        final Class<? extends JsonRepresentation> returnType = representationType.getRepresentationClass();
        return new RestfulResponse(response, returnType);
    }
View Full Code Here

            }
        };
    }

    public static Matcher<? super MediaType> hasProfile(final String expectedMediaTypeAndProfileStr) {
        final MediaType expectedMediaType = Header.CONTENT_TYPE.parse(expectedMediaTypeAndProfileStr);
        final String expectedProfileIfAny = expectedMediaType.getParameters().get("profile");
        return new TypeSafeMatcher<MediaType>() {

            @Override
            public void describeTo(Description description) {
                description.appendText("is a media type 'application/json' with a profile parameter of '" +  expectedMediaTypeAndProfileStr + '"');
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.