Java Code Examples for javax.ws.rs.core.MediaType#WILDCARD_TYPE

The following examples show how to use javax.ws.rs.core.MediaType#WILDCARD_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpResponseWriter.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
default Map<String, String> join(Map<String, String> original, MediaType contentMediaType, MediaType[] additional) {
	if (contentMediaType == null) {
		contentMediaType = MediaType.WILDCARD_TYPE;
	}

	MediaType first = null; // to be used in case no content type matches requested content type
	if (additional != null && additional.length > 0) {

		for (MediaType produces : additional) {
			if (first == null) {
				first = produces;
			}

			if (MediaTypeHelper.matches(contentMediaType, produces)) {
				original.put(HttpHeaders.CONTENT_TYPE.toString(), MediaTypeHelper.toString(produces));
				break;
			}
		}
	}

	if (original.get(HttpHeaders.CONTENT_TYPE.toString()) == null && first != null) {
		original.put(HttpHeaders.CONTENT_TYPE.toString(), MediaTypeHelper.toString(first));
	}

	return original;
}
 
Example 2
Source File: RESTITCase.java    From syncope with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultContentType() {
    // manualy instantiate SyncopeClient so that media type can be set to */*
    SyncopeClientFactoryBean factory = new SyncopeClientFactoryBean().setAddress(ADDRESS);
    SyncopeClient client = new SyncopeClient(
            MediaType.WILDCARD_TYPE,
            factory.getRestClientFactoryBean(),
            factory.getExceptionMapper(),
            new BasicAuthenticationHandler(ADMIN_UNAME, ADMIN_PWD),
            false,
            null);

    // perform operation
    AnyTypeClassService service = client.getService(AnyTypeClassService.class);
    service.list();

    // check that */* was actually sent
    MultivaluedMap<String, String> requestHeaders = WebClient.client(service).getHeaders();
    assertEquals(MediaType.WILDCARD, requestHeaders.getFirst(HttpHeaders.ACCEPT));

    // check that application/json was received
    String contentType = WebClient.client(service).getResponse().getHeaderString(HttpHeaders.CONTENT_TYPE);
    assertTrue(contentType.startsWith(MediaType.APPLICATION_JSON));
}
 
Example 3
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
public <T> ContextResolver<T> createContextResolver(Type contextType,
                                                    Message m) {
    boolean isRequestor = MessageUtils.isRequestor(m);
    Message requestMessage = isRequestor ? m.getExchange().getOutMessage()
                                         : m.getExchange().getInMessage();

    Message responseMessage = isRequestor ? m.getExchange().getInMessage()
                                          : m.getExchange().getOutMessage();
    Object ctProperty = null;
    if (responseMessage != null) {
        ctProperty = responseMessage.get(Message.CONTENT_TYPE);
    } else {
        ctProperty = requestMessage.get(Message.CONTENT_TYPE);
    }
    MediaType mt = ctProperty != null ? JAXRSUtils.toMediaType(ctProperty.toString())
        : MediaType.WILDCARD_TYPE;
    return createContextResolver(contextType, m, mt);

}
 
Example 4
Source File: OperationResourceInfoTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparator2() throws Exception {
    Message m = createMessage();

    OperationResourceInfo ori1 = new OperationResourceInfo(
                                                           TestClass.class.getMethod("doIt", new Class[]{}),
                                                           new ClassResourceInfo(TestClass.class));
    ori1.setURITemplate(new URITemplate("/"));

    OperationResourceInfo ori2 = new OperationResourceInfo(
                                                           TestClass.class.getMethod("doThat", new Class[]{}),
                                                           new ClassResourceInfo(TestClass.class));

    ori2.setURITemplate(new URITemplate("/"));

    OperationResourceInfoComparator cmp = new OperationResourceInfoComparator(m, "POST", false,
        MediaType.WILDCARD_TYPE, Collections.singletonList(MediaType.WILDCARD_TYPE));


    int result = cmp.compare(ori1,  ori2);
    assertEquals(0, result);
}
 
Example 5
Source File: MediaTypeHeaderProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static MediaType handleMediaTypeWithoutSubtype(String mType) {
    if (mType.startsWith(MediaType.MEDIA_TYPE_WILDCARD)) {
        String mTypeNext = mType.length() == 1 ? "" : mType.substring(1).trim();
        boolean mTypeNextEmpty = StringUtils.isEmpty(mTypeNext);
        if (mTypeNextEmpty || mTypeNext.startsWith(";")) {
            if (!mTypeNextEmpty) {
                Map<String, String> parameters = new LinkedHashMap<>();
                StringTokenizer st = new StringTokenizer(mType.substring(2).trim(), ";");
                while (st.hasMoreTokens()) {
                    addParameter(parameters, st.nextToken());
                }
                return new MediaType(MediaType.MEDIA_TYPE_WILDCARD,
                                     MediaType.MEDIA_TYPE_WILDCARD,
                                     parameters);
            }
            return MediaType.WILDCARD_TYPE;

        }
    }
    Message message = PhaseInterceptorChain.getCurrentMessage();
    if (message != null
        && !MessageUtils.getContextualBoolean(message, STRICT_MEDIA_TYPE_CHECK, false)) {
        MediaType mt = null;
        if (mType.equals(MediaType.TEXT_PLAIN_TYPE.getType())) {
            mt = MediaType.TEXT_PLAIN_TYPE;
        } else if (mType.equals(MediaType.APPLICATION_XML_TYPE.getSubtype())) {
            mt = MediaType.APPLICATION_XML_TYPE;
        } else {
            mt = MediaType.WILDCARD_TYPE;
        }
        LOG.fine("Converting a malformed media type '" + mType + "' to '" + typeToString(mt) + "'");
        return mt;
    }
    throw new IllegalArgumentException("Media type separator is missing");
}
 
Example 6
Source File: CrossOriginResourceSharingFilter.java    From cxf with Apache License 2.0 5 votes vote down vote up
private OperationResourceInfo findPreflightMethod(
    Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources,
                                                  String requestUri,
                                                  String httpMethod,
                                                  MultivaluedMap<String, String> values,
                                                  Message m) {
    final String contentType = MediaType.WILDCARD;
    final MediaType acceptType = MediaType.WILDCARD_TYPE;
    OperationResourceInfo ori = JAXRSUtils.findTargetMethod(matchedResources,
                                m, httpMethod, values,
                                contentType,
                                Collections.singletonList(acceptType),
                                false,
                                false);
    if (ori == null) {
        return null;
    }
    if (ori.isSubResourceLocator()) {
        Class<?> cls = ori.getMethodToInvoke().getReturnType();
        ClassResourceInfo subcri = ori.getClassResourceInfo().getSubResource(cls, cls);
        if (subcri == null) {
            return null;
        }
        MultivaluedMap<String, String> newValues = new MetadataMap<>();
        newValues.putAll(values);
        return findPreflightMethod(Collections.singletonMap(subcri, newValues),
                                   values.getFirst(URITemplate.FINAL_MATCH_GROUP),
                                   httpMethod,
                                   newValues,
                                   m);
    }
    return ori;
}
 
Example 7
Source File: SseEventBuilder.java    From cxf with Apache License 2.0 5 votes vote down vote up
private MediaType guessMediaType(String dataString) {
    if (dataString != null) {
        if (dataString.startsWith("<")) {
            return MediaType.APPLICATION_XML_TYPE;
        }
        if (dataString.startsWith("{")) {
            return MediaType.APPLICATION_JSON_TYPE;
        }
    }
    return MediaType.WILDCARD_TYPE;
}
 
Example 8
Source File: HttpSBControllerImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
private MediaType typeOfMediaType(String type) {
    switch (type) {
    case XML:
        return MediaType.APPLICATION_XML_TYPE;
    case JSON:
        return MediaType.APPLICATION_JSON_TYPE;
    case MediaType.WILDCARD:
        return MediaType.WILDCARD_TYPE;
    default:
        throw new IllegalArgumentException("Unsupported media type " + type);

    }
}
 
Example 9
Source File: ResponseImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException,
    IllegalStateException {

    checkEntityIsClosed();

    if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass())
        && !(lastEntity instanceof InputStream)) {
        return cls.cast(lastEntity);
    }

    MediaType mediaType = getMediaType();
    if (mediaType == null) {
        mediaType = MediaType.WILDCARD_TYPE;
    }

    // the stream is available if entity is IS or
    // message contains XMLStreamReader or Reader
    boolean entityStreamAvailable = entityStreamAvailable();
    InputStream entityStream = null;
    if (!entityStreamAvailable) {
        // try create a stream if the entity is String or Number
        entityStream = convertEntityToStreamIfPossible();
        entityStreamAvailable = entityStream != null;
    } else if (entity instanceof InputStream) {
        entityStream = InputStream.class.cast(entity);
    } else {
        Message inMessage = getResponseMessage();
        Reader reader = inMessage.getContent(Reader.class);
        if (reader != null) {
            entityStream = InputStream.class.cast(new ReaderInputStream(reader));
        }
    }

    // we need to check for readers even if no IS is set - the readers may still do it
    List<ReaderInterceptor> readers = outMessage == null ? null : ProviderFactory.getInstance(outMessage)
        .createMessageBodyReaderInterceptor(cls, t, anns, mediaType, outMessage, entityStreamAvailable, null);

    if (readers != null) {
        try {
            if (entityBufferred) {
                InputStream.class.cast(entity).reset();
            }

            Message responseMessage = getResponseMessage();
            responseMessage.put(Message.PROTOCOL_HEADERS, getHeaders());

            lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t,
                                                                   anns,
                                                                   entityStream,
                                                                   mediaType,
                                                                   responseMessage);
            autoClose(cls, false);
            return castLastEntity();
        } catch (Exception ex) {
            autoClose(cls, true);
            reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
        } finally {
            ProviderFactory pf = ProviderFactory.getInstance(outMessage);
            if (pf != null) {
                pf.clearThreadLocalProxies();
            }
        }
    } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
        lastEntity = entity;
        return castLastEntity();
    } else if (entityStreamAvailable) {
        reportMessageHandlerProblem("NO_MSG_READER", cls, mediaType, null);
    }

    throw new IllegalStateException("The entity is not backed by an input stream, entity class is : "
        + (entity != null ? entity.getClass().getName() : cls.getName()));

}
 
Example 10
Source File: MediaTypeHelper.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
public MediaTypeRange(MediaType type) {
    next = (type == null) ? MediaType.WILDCARD_TYPE : type;
}