use of org.apache.cxf.io.CacheAndWriteOutputStream 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 ("org.apache.cxf.jaxrs.reactivestreams.server.StreamingAsyncSubscriber$StreamingResponseImpl".equals(entity.getClass().getName())) {
// cache the OutputStream when it's reactive response
entityStream = new CacheAndWriteOutputStream(entityStream);
}
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.io.CacheAndWriteOutputStream in project tomee 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 ("org.apache.cxf.jaxrs.reactivestreams.server.StreamingAsyncSubscriber$StreamingResponseImpl".equals(entity.getClass().getName())) {
// cache the OutputStream when it's reactive response
entityStream = new CacheAndWriteOutputStream(entityStream);
}
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.io.CacheAndWriteOutputStream in project cxf by apache.
the class LoggingOutInterceptor method handleMessage.
public void handleMessage(Message message) {
final OutputStream os = message.getContent(OutputStream.class);
final Writer iowriter = message.getContent(Writer.class);
if (os == null && iowriter == null) {
return;
}
Logger logger = getMessageLogger(message);
if (logger != null && (logger.isLoggable(Level.INFO) || writer != null)) {
// Write the output while caching it for the log message
boolean hasLogged = message.containsKey(LOG_SETUP);
if (!hasLogged) {
message.put(LOG_SETUP, Boolean.TRUE);
if (os != null) {
final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);
if (threshold > 0) {
newOut.setThreshold(threshold);
}
if (limit > 0) {
newOut.setCacheLimit(limit);
}
message.setContent(OutputStream.class, newOut);
newOut.registerCallback(new LoggingCallback(logger, message, os));
} else {
message.setContent(Writer.class, new LogWriter(logger, message, iowriter));
}
}
}
}
use of org.apache.cxf.io.CacheAndWriteOutputStream in project cxf by apache.
the class LoggingOutInterceptor method createCachingOut.
private OutputStream createCachingOut(Message message, final OutputStream os, CachedOutputStreamCallback callback) {
final CacheAndWriteOutputStream newOut = new LoggingOutputStream(os);
if (threshold > 0) {
newOut.setThreshold(threshold);
}
if (limit > 0) {
// make the limit for the cache greater than the limit for the truncated payload in the log event,
// this is necessary for finding out that the payload was truncated
// (see boolean isTruncated = cos.size() > limit && limit != -1;) in method copyPayload
newOut.setCacheLimit(getCacheLimit());
}
newOut.registerCallback(callback);
return newOut;
}
use of org.apache.cxf.io.CacheAndWriteOutputStream in project cxf by apache.
the class PersistOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
ExchangeData exchangeData = message.getExchange().getInMessage().getContent(ExchangeData.class);
if (exchangeData != null) {
final OutputStream os = message.getContent(OutputStream.class);
if (os == null) {
return;
}
try {
Service service = message.getExchange().getService();
String serviceName = String.valueOf(service.getName());
OperationInfo opInfo = message.getExchange().getBindingOperationInfo().getOperationInfo();
String operationName = opInfo == null ? null : opInfo.getName().getLocalPart();
if (operationName == null) {
Object nameProperty = message.getExchange().get("org.apache.cxf.resource.operation.name");
if (nameProperty != null) {
operationName = "\"" + nameProperty.toString() + "\"";
}
}
exchangeData.setServiceName(serviceName);
exchangeData.setOperation(operationName);
// add all additional properties
addPropertiesFrom(exchangeData, message.getExchange().getInMessage());
addPropertiesFrom(exchangeData, message);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
// Write the output while caching it for the log message
final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);
message.setContent(OutputStream.class, newOut);
newOut.registerCallback(new PersistOutInterceptorCallback(message, os, exchangeData));
exchangeData.setOutDate(new Date());
if (message.getContent(Exception.class) != null) {
exchangeData.setStatus("ERROR");
Exception exception = message.getContent(Exception.class);
StringWriter stringWriter = new StringWriter();
if (exception.getCause() != null) {
exchangeData.setExceptionType(exception.getCause().getClass().getName());
exception.getCause().printStackTrace(new PrintWriter(stringWriter));
} else {
exchangeData.setExceptionType(exception.getClass().getName());
exception.printStackTrace(new PrintWriter(stringWriter));
}
exchangeData.setStackTrace(stringWriter.toString());
} else {
exchangeData.setStatus("OK");
}
}
}
Aggregations