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;
}
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;
}
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);
}
}
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;
}
Aggregations