Search in sources :

Example 1 with WriterInterceptorMBW

use of org.apache.cxf.jaxrs.impl.WriterInterceptorMBW in project cxf by apache.

the class JAXRSOutInterceptor method checkBufferingMode.

private boolean checkBufferingMode(Message m, List<WriterInterceptor> writers, boolean firstTry) {
    if (!firstTry) {
        return false;
    }
    WriterInterceptor last = writers.get(writers.size() - 1);
    MessageBodyWriter<Object> w = ((WriterInterceptorMBW) last).getMBW();
    Object outBuf = m.getContextualProperty(OUT_BUFFERING);
    boolean enabled = PropertyUtils.isTrue(outBuf);
    boolean configurableProvider = w instanceof AbstractConfigurableProvider;
    if (!enabled && outBuf == null && configurableProvider) {
        enabled = ((AbstractConfigurableProvider) w).getEnableBuffering();
    }
    if (enabled) {
        boolean streamingOn = configurableProvider ? ((AbstractConfigurableProvider) w).getEnableStreaming() : false;
        if (streamingOn) {
            m.setContent(XMLStreamWriter.class, new CachingXmlEventWriter());
        } else {
            m.setContent(OutputStream.class, new CachedOutputStream());
        }
    }
    return enabled;
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) AbstractConfigurableProvider(org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 2 with WriterInterceptorMBW

use of org.apache.cxf.jaxrs.impl.WriterInterceptorMBW in project cxf by apache.

the class JAXRSOutInterceptor method checkFinalContentType.

private MediaType checkFinalContentType(MediaType mt, List<WriterInterceptor> writers, boolean checkWriters) {
    if (checkWriters) {
        int mbwIndex = writers.size() == 1 ? 0 : writers.size() - 1;
        MessageBodyWriter<Object> writer = ((WriterInterceptorMBW) writers.get(mbwIndex)).getMBW();
        Produces pm = writer.getClass().getAnnotation(Produces.class);
        if (pm != null) {
            List<MediaType> sorted = JAXRSUtils.sortMediaTypes(JAXRSUtils.getMediaTypes(pm.value()), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
            mt = JAXRSUtils.intersectMimeTypes(sorted, mt).get(0);
        }
    }
    if (mt.isWildcardType() || mt.isWildcardSubtype()) {
        if ("application".equals(mt.getType()) || mt.isWildcardType()) {
            mt = MediaType.APPLICATION_OCTET_STREAM_TYPE;
        } else {
            throw ExceptionUtils.toNotAcceptableException(null, null);
        }
    }
    return mt;
}
Also used : WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) Produces(javax.ws.rs.Produces) MediaType(javax.ws.rs.core.MediaType)

Example 3 with WriterInterceptorMBW

use of org.apache.cxf.jaxrs.impl.WriterInterceptorMBW in project cxf by apache.

the class JAXRSUtils method writeMessageBody.

// CHECKSTYLE:OFF
public static void writeMessageBody(List<WriterInterceptor> writers, Object entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Message message) throws WebApplicationException, IOException {
    OutputStream entityStream = message.getContent(OutputStream.class);
    if (writers.size() > 1) {
        WriterInterceptor first = writers.remove(0);
        WriterInterceptorContext context = new WriterInterceptorContextImpl(entity, type, genericType, annotations, entityStream, message, writers);
        first.aroundWriteTo(context);
    } else {
        MessageBodyWriter<Object> writer = ((WriterInterceptorMBW) writers.get(0)).getMBW();
        if (type == byte[].class) {
            long size = writer.getSize(entity, type, genericType, annotations, mediaType);
            if (size != -1) {
                httpHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
            }
        }
        HttpUtils.convertHeaderValuesToString(httpHeaders, true);
        writer.writeTo(entity, type, genericType, annotations, mediaType, httpHeaders, entityStream);
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) OutputStream(java.io.OutputStream) WriterInterceptorContextImpl(org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl)

Example 4 with WriterInterceptorMBW

use of org.apache.cxf.jaxrs.impl.WriterInterceptorMBW in project cxf by apache.

the class ProviderFactory method createMessageBodyWriterInterceptor.

public <T> List<WriterInterceptor> createMessageBodyWriterInterceptor(Class<T> bodyType, Type parameterType, Annotation[] parameterAnnotations, MediaType mediaType, Message m, Set<String> names) {
    MessageBodyWriter<T> mw = createMessageBodyWriter(bodyType, parameterType, parameterAnnotations, mediaType, m);
    int size = writerInterceptors.size();
    if (mw != null || size > 0) {
        @SuppressWarnings({ "unchecked", "rawtypes" }) WriterInterceptor mbwWriter = new WriterInterceptorMBW((MessageBodyWriter) mw, m);
        List<WriterInterceptor> interceptors = null;
        if (size > 0) {
            interceptors = new ArrayList<>(size + 1);
            List<ProviderInfo<WriterInterceptor>> writers = getBoundFilters(writerInterceptors, names);
            for (ProviderInfo<WriterInterceptor> p : writers) {
                injectContextValues(p, m);
                interceptors.add(p.getProvider());
            }
            interceptors.add(mbwWriter);
        } else {
            interceptors = Collections.singletonList(mbwWriter);
        }
        return interceptors;
    }
    return null;
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Aggregations

WriterInterceptorMBW (org.apache.cxf.jaxrs.impl.WriterInterceptorMBW)4 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)3 OutputStream (java.io.OutputStream)1 Produces (javax.ws.rs.Produces)1 MediaType (javax.ws.rs.core.MediaType)1 WriterInterceptorContext (javax.ws.rs.ext.WriterInterceptorContext)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 WriterInterceptorContextImpl (org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl)1 FilterProviderInfo (org.apache.cxf.jaxrs.model.FilterProviderInfo)1 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)1 AbstractConfigurableProvider (org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider)1 CachingXmlEventWriter (org.apache.cxf.staxutils.CachingXmlEventWriter)1