Java Code Examples for org.apache.cxf.jaxrs.utils.JAXRSUtils#toMediaType()

The following examples show how to use org.apache.cxf.jaxrs.utils.JAXRSUtils#toMediaType() . 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: 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 2
Source File: MultipartProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
private <T> DataHandler getHandlerForObject(T obj,
                                        Class<T> cls, Type genericType,
                                        Annotation[] anns,
                                        String mimeType) {
    MediaType mt = JAXRSUtils.toMediaType(mimeType);
    mc.put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY, this);

    MessageBodyWriter<T> r = null;
    try {
        r = mc.getProviders().getMessageBodyWriter(cls, genericType, anns, mt);
    } finally {
        mc.put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY, null);
    }
    if (r == null) {
        org.apache.cxf.common.i18n.Message message =
            new org.apache.cxf.common.i18n.Message("NO_MSG_WRITER",
                                               BUNDLE,
                                               cls);
        LOG.severe(message.toString());
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }

    return new MessageBodyWriterDataHandler<T>(r, obj, cls, genericType, anns, mt);
}
 
Example 3
Source File: HttpServletRequestFilter.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void readFromParamsIfNeeded() {
    if (formParams == null) {
        if (m.containsKey(FormUtils.FORM_PARAM_MAP)) {
            formParams = (MultivaluedMap<String, String>)m.get(FormUtils.FORM_PARAM_MAP);
        } else {
            formParams = new MetadataMap<>();
            MediaType mt = JAXRSUtils.toMediaType((String)m.get(Message.CONTENT_TYPE));
            String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
            String body = FormUtils.readBody(m.getContent(InputStream.class), enc);
            FormUtils.populateMapFromString(formParams, m, body, enc, true);
        }
    }

}
 
Example 4
Source File: WriterInterceptorMBW.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void aroundWriteTo(WriterInterceptorContext c) throws IOException, WebApplicationException {

    MultivaluedMap<String, Object> headers = c.getHeaders();
    Object mtObject = headers.getFirst(HttpHeaders.CONTENT_TYPE);
    MediaType entityMt = mtObject == null ? c.getMediaType() : JAXRSUtils.toMediaType(mtObject.toString());
    m.put(Message.CONTENT_TYPE, entityMt.toString());

    Class<?> entityCls = c.getType();
    Type entityType = c.getGenericType();
    Annotation[] entityAnns = c.getAnnotations();

    if (writer == null
        || m.get(ProviderFactory.PROVIDER_SELECTION_PROPERTY_CHANGED) == Boolean.TRUE
        && !writer.isWriteable(entityCls, entityType, entityAnns, entityMt)) {

        writer = (MessageBodyWriter<Object>)ProviderFactory.getInstance(m)
            .createMessageBodyWriter(entityCls, entityType, entityAnns, entityMt, m);
    }

    if (writer == null) {
        String errorMessage = JAXRSUtils.logMessageHandlerProblem("NO_MSG_WRITER", entityCls, entityMt);
        throw new ProcessingException(errorMessage);
    }
    
    HttpUtils.convertHeaderValuesToString(headers, true);

    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Response EntityProvider is: " + writer.getClass().getName());
    }
    
    writer.writeTo(c.getEntity(),
                   c.getType(),
                   c.getGenericType(),
                   c.getAnnotations(),
                   entityMt,
                   headers,
                   c.getOutputStream());
}
 
Example 5
Source File: ClientRequestContextImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public MediaType getMediaType() {
    if (!hasEntity()) {
        return null;
    }
    Object mt = HttpUtils.getModifiableHeaders(m).getFirst(HttpHeaders.CONTENT_TYPE);
    return mt instanceof MediaType ? (MediaType)mt : JAXRSUtils.toMediaType(mt.toString());
}
 
Example 6
Source File: WriterInterceptorContextImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public MediaType getMediaType() {
    Object value = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
    return value instanceof MediaType ? (MediaType)value : JAXRSUtils.toMediaType((String)value);
}
 
Example 7
Source File: ResponseImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public MediaType getMediaType() {
    Object header = metadata.getFirst(HttpHeaders.CONTENT_TYPE);
    return header == null || header instanceof MediaType ? (MediaType)header
        : (MediaType)JAXRSUtils.toMediaType(header.toString());
}
 
Example 8
Source File: HttpHeadersImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public MediaType getMediaType() {
    List<String> values = getListValues(HttpHeaders.CONTENT_TYPE);
    return values.isEmpty() ? null : JAXRSUtils.toMediaType(values.get(0));
}
 
Example 9
Source File: ReaderInterceptorContextImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public MediaType getMediaType() {
    return JAXRSUtils.toMediaType(getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
}
 
Example 10
Source File: Attachment.java    From cxf with Apache License 2.0 4 votes vote down vote up
public MediaType getContentType() {
    String value = handler != null && handler.getContentType() != null ? handler.getContentType()
        : headers.getFirst("Content-Type");
    return value == null ? MediaType.TEXT_PLAIN_TYPE : JAXRSUtils.toMediaType(value);
}
 
Example 11
Source File: JweWriterInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void setJoseMediaType(WriterInterceptorContext ctx) {
    MediaType joseMediaType = JAXRSUtils.toMediaType(JoseConstants.MEDIA_TYPE_JOSE);
    ctx.setMediaType(joseMediaType);
}
 
Example 12
Source File: JweJsonWriterInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void setJoseMediaType(WriterInterceptorContext ctx) {
    MediaType joseMediaType = JAXRSUtils.toMediaType(JoseConstants.MEDIA_TYPE_JOSE_JSON);
    ctx.setMediaType(joseMediaType);
}
 
Example 13
Source File: JwsWriterInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void setJoseMediaType(WriterInterceptorContext ctx) {
    MediaType joseMediaType = JAXRSUtils.toMediaType(JoseConstants.MEDIA_TYPE_JOSE);
    ctx.setMediaType(joseMediaType);
}
 
Example 14
Source File: AbstractClient.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected <T> void writeBody(T o, Message outMessage, Class<?> cls, Type type, Annotation[] anns,
                             OutputStream os) {

    if (o == null) {
        return;
    }
    @SuppressWarnings("unchecked")
    MultivaluedMap<String, Object> headers =
        (MultivaluedMap<String, Object>)outMessage.get(Message.PROTOCOL_HEADERS);

    @SuppressWarnings("unchecked")
    Class<T> theClass = (Class<T>)cls;

    Object contentTypeHeader = headers.getFirst(HttpHeaders.CONTENT_TYPE);
    if (contentTypeHeader == null) {
        contentTypeHeader = MediaType.WILDCARD;
    }
    MediaType contentType = JAXRSUtils.toMediaType(contentTypeHeader.toString());

    List<WriterInterceptor> writers = ClientProviderFactory.getInstance(outMessage)
        .createMessageBodyWriterInterceptor(theClass, type, anns, contentType, outMessage, null);
    if (writers != null) {
        try {
            JAXRSUtils.writeMessageBody(writers,
                                        o,
                                        theClass,
                                        type,
                                        anns,
                                        contentType,
                                        headers,
                                        outMessage);

            OutputStream realOs = outMessage.get(OutputStream.class);
            if (realOs != null) {
                realOs.flush();
            }
        } catch (Exception ex) {
            reportMessageHandlerProblem("MSG_WRITER_PROBLEM", cls, contentType, ex);
        }
    } else {
        reportMessageHandlerProblem("NO_MSG_WRITER", cls, contentType, null);
    }

}
 
Example 15
Source File: WadlGenerator.java    From cxf with Apache License 2.0 2 votes vote down vote up
/**
 * Set the default WADL response media type.
 * For example, a browser may display WADL better if Content-Type
 * is set to application/xml which is a default response content type.
 * Users may set it to application/vnd.sun.wadl+xml or other type.
 * @param mt WADL response media type
 */
public void setDefaultMediaType(String mt) {
    this.defaultWadlResponseMediaType = JAXRSUtils.toMediaType(mt);
}
 
Example 16
Source File: WadlGenerator.java    From cxf with Apache License 2.0 2 votes vote down vote up
/**
 * Set the default representation media type to be used
 * if JAX-RS Produces or Consumes annotation is missing.
 * Wild-card media type is used by default in such cases.
 * @param mt the default representation media type
 */
public void setDefaultRepresentationMediaType(String mt) {
    this.defaultWadlResponseMediaType = JAXRSUtils.toMediaType(mt);
}