use of org.apache.cxf.io.CachedOutputStream in project tomee by apache.
the class XSLTJaxbProvider method unmarshalFromReader.
@Override
protected Object unmarshalFromReader(Unmarshaller unmarshaller, XMLStreamReader reader, Annotation[] anns, MediaType mt) throws JAXBException {
CachedOutputStream out = new CachedOutputStream();
try {
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
StaxUtils.copy(new StaxSource(reader), writer);
writer.writeEndDocument();
writer.flush();
writer.close();
return unmarshalFromInputStream(unmarshaller, out.getInputStream(), anns, mt);
} catch (Exception ex) {
throw ExceptionUtils.toBadRequestException(ex, null);
}
}
use of org.apache.cxf.io.CachedOutputStream in project tomee by apache.
the class XSLTJaxbProvider method marshalToWriter.
@Override
protected void marshalToWriter(Marshaller ms, Object obj, XMLStreamWriter writer, Annotation[] anns, MediaType mt) throws Exception {
CachedOutputStream out = new CachedOutputStream();
marshalToOutputStream(ms, obj, out, anns, mt);
StaxUtils.copy(new StreamSource(out.getInputStream()), writer);
}
use of org.apache.cxf.io.CachedOutputStream in project tomee by apache.
the class JAXRSOutInterceptor method serializeMessage.
private void serializeMessage(ServerProviderFactory providerFactory, Message message, Response theResponse, OperationResourceInfo ori, boolean firstTry) {
ResponseImpl response = (ResponseImpl) JAXRSUtils.copyResponseIfNeeded(theResponse);
final Exchange exchange = message.getExchange();
boolean headResponse = response.getStatus() == 200 && firstTry && ori != null && HttpMethod.HEAD.equals(ori.getHttpMethod());
Object entity = response.getActualEntity();
if (headResponse && entity != null) {
LOG.info(new org.apache.cxf.common.i18n.Message("HEAD_WITHOUT_ENTITY", BUNDLE).toString());
entity = null;
}
Method invoked = ori == null ? null : ori.getAnnotatedMethod() != null ? ori.getAnnotatedMethod() : ori.getMethodToInvoke();
Annotation[] annotations;
Annotation[] staticAnns = ori != null ? ori.getOutAnnotations() : new Annotation[] {};
Annotation[] responseAnns = response.getEntityAnnotations();
if (responseAnns != null) {
annotations = new Annotation[staticAnns.length + responseAnns.length];
System.arraycopy(staticAnns, 0, annotations, 0, staticAnns.length);
System.arraycopy(responseAnns, 0, annotations, staticAnns.length, responseAnns.length);
} else {
annotations = staticAnns;
}
response.setStatus(getActualStatus(response.getStatus(), entity));
response.setEntity(entity, annotations);
// Prepare the headers
MultivaluedMap<String, Object> responseHeaders = prepareResponseHeaders(message, response, entity, firstTry);
// Run the filters
try {
JAXRSUtils.runContainerResponseFilters(providerFactory, response, message, ori, invoked);
} catch (Throwable ex) {
handleWriteException(providerFactory, message, ex, firstTry);
return;
}
// Write the entity
entity = InjectionUtils.getEntity(response.getActualEntity());
setResponseStatus(message, getActualStatus(response.getStatus(), entity));
if (entity == null) {
if (!headResponse) {
responseHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, "0");
if (MessageUtils.getContextualBoolean(message, "remove.content.type.for.empty.response", false)) {
responseHeaders.remove(HttpHeaders.CONTENT_TYPE);
message.remove(Message.CONTENT_TYPE);
}
}
HttpUtils.convertHeaderValuesToString(responseHeaders, true);
return;
}
Object ignoreWritersProp = exchange.get(JAXRSUtils.IGNORE_MESSAGE_WRITERS);
boolean ignoreWriters = ignoreWritersProp != null && Boolean.valueOf(ignoreWritersProp.toString());
if (ignoreWriters) {
writeResponseToStream(message.getContent(OutputStream.class), entity);
return;
}
MediaType responseMediaType = getResponseMediaType(responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE));
Class<?> serviceCls = invoked != null ? ori.getClassResourceInfo().getServiceClass() : null;
Class<?> targetType = InjectionUtils.getRawResponseClass(entity);
Type genericType = InjectionUtils.getGenericResponseType(invoked, serviceCls, response.getActualEntity(), targetType, exchange);
targetType = InjectionUtils.updateParamClassToTypeIfNeeded(targetType, genericType);
annotations = response.getEntityAnnotations();
List<WriterInterceptor> writers = providerFactory.createMessageBodyWriterInterceptor(targetType, genericType, annotations, responseMediaType, message, ori == null ? null : ori.getNameBindings());
OutputStream outOriginal = message.getContent(OutputStream.class);
if (writers == null || writers.isEmpty()) {
writeResponseErrorMessage(message, outOriginal, "NO_MSG_WRITER", targetType, responseMediaType);
return;
}
try {
boolean checkWriters = false;
if (responseMediaType.isWildcardSubtype()) {
Produces pM = AnnotationUtils.getMethodAnnotation(ori == null ? null : ori.getAnnotatedMethod(), Produces.class);
Produces pC = AnnotationUtils.getClassAnnotation(serviceCls, Produces.class);
checkWriters = pM == null && pC == null;
}
responseMediaType = checkFinalContentType(responseMediaType, writers, checkWriters);
} catch (Throwable ex) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, ex.getMessage(), ex);
}
handleWriteException(providerFactory, message, ex, firstTry);
return;
}
String finalResponseContentType = JAXRSUtils.mediaTypeToString(responseMediaType);
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Response content type is: " + finalResponseContentType);
}
responseHeaders.putSingle(HttpHeaders.CONTENT_TYPE, finalResponseContentType);
message.put(Message.CONTENT_TYPE, finalResponseContentType);
boolean enabled = checkBufferingMode(message, writers, firstTry);
try {
try {
// NOPMD
JAXRSUtils.writeMessageBody(writers, entity, targetType, genericType, annotations, responseMediaType, responseHeaders, message);
if (isResponseRedirected(message)) {
return;
}
checkCachedStream(message, outOriginal, enabled);
} finally {
if (enabled) {
OutputStream os = message.getContent(OutputStream.class);
if (os != outOriginal && os instanceof CachedOutputStream) {
os.close();
}
message.setContent(OutputStream.class, outOriginal);
message.put(XMLStreamWriter.class.getName(), null);
}
}
} catch (Throwable ex) {
logWriteError(firstTry, targetType, responseMediaType);
handleWriteException(providerFactory, message, ex, firstTry);
}
}
use of org.apache.cxf.io.CachedOutputStream in project tomee 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();
if (streamingOn) {
m.setContent(XMLStreamWriter.class, new CachingXmlEventWriter());
} else {
m.setContent(OutputStream.class, new CachedOutputStream());
}
}
return enabled;
}
use of org.apache.cxf.io.CachedOutputStream in project tomee by apache.
the class JAXRSOutInterceptor method checkCachedStream.
private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled) throws Exception {
final XMLStreamWriter writer;
if (enabled) {
writer = m.getContent(XMLStreamWriter.class);
} else {
writer = (XMLStreamWriter) m.get(XMLStreamWriter.class.getName());
}
if (writer instanceof CachingXmlEventWriter) {
CachingXmlEventWriter cache = (CachingXmlEventWriter) writer;
if (!cache.getEvents().isEmpty()) {
XMLStreamWriter origWriter = null;
try {
origWriter = StaxUtils.createXMLStreamWriter(osOriginal);
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origWriter);
}
} finally {
StaxUtils.close(origWriter);
}
}
m.setContent(XMLStreamWriter.class, null);
return;
}
if (enabled) {
OutputStream os = m.getContent(OutputStream.class);
if (os != osOriginal && os instanceof CachedOutputStream) {
CachedOutputStream cos = (CachedOutputStream) os;
if (cos.size() != 0) {
cos.writeCacheTo(osOriginal);
}
}
}
}
Aggregations