Search in sources :

Example 1 with CacheAndWriteOutputStream

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);
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) OutputStream(java.io.OutputStream) WriterInterceptorContextImpl(org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream)

Example 2 with CacheAndWriteOutputStream

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);
    }
}
Also used : WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) OutputStream(java.io.OutputStream) WriterInterceptorContextImpl(org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream)

Example 3 with CacheAndWriteOutputStream

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));
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Logger(java.util.logging.Logger) PrintWriter(java.io.PrintWriter) FilterWriter(java.io.FilterWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream)

Example 4 with CacheAndWriteOutputStream

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;
}
Also used : CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream)

Example 5 with CacheAndWriteOutputStream

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");
        }
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) StringWriter(java.io.StringWriter) ExchangeData(org.apache.cxf.management.persistence.ExchangeData) OutputStream(java.io.OutputStream) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Service(org.apache.cxf.service.Service) Date(java.util.Date) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream) PrintWriter(java.io.PrintWriter)

Aggregations

CacheAndWriteOutputStream (org.apache.cxf.io.CacheAndWriteOutputStream)6 OutputStream (java.io.OutputStream)5 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 LoadingByteArrayOutputStream (org.apache.cxf.helpers.LoadingByteArrayOutputStream)2 WriterInterceptorContextImpl (org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl)2 WriterInterceptorMBW (org.apache.cxf.jaxrs.impl.WriterInterceptorMBW)2 WriterInterceptor (jakarta.ws.rs.ext.WriterInterceptor)1 WriterInterceptorContext (jakarta.ws.rs.ext.WriterInterceptorContext)1 FilterWriter (java.io.FilterWriter)1 Writer (java.io.Writer)1 Date (java.util.Date)1 Logger (java.util.logging.Logger)1 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)1 WriterInterceptorContext (javax.ws.rs.ext.WriterInterceptorContext)1 ExchangeData (org.apache.cxf.management.persistence.ExchangeData)1 Service (org.apache.cxf.service.Service)1 OperationInfo (org.apache.cxf.service.model.OperationInfo)1