Search in sources :

Example 1 with ServerReaderInterceptorContext

use of org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext in project resteasy by resteasy.

the class MessageBodyParameterInjector method inject.

@Override
public Object inject(HttpRequest request, HttpResponse response, boolean unwrapAsync) {
    Object o = getBody();
    if (o != null) {
        return o;
    }
    MediaType mediaType = request.getHttpHeaders().getMediaType();
    if (mediaType == null) {
        mediaType = MediaType.WILDCARD_TYPE;
    // throw new BadRequestException("content-type was null and expecting to extract a body into " + this.target);
    }
    InputStream is = null;
    if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(mediaType)) {
        if (request.formParametersRead()) {
            MultivaluedMap<String, String> map = request.getDecodedFormParameters();
            if (map != null) {
                StringBuilder sb = new StringBuilder();
                for (Entry<String, List<String>> entry : map.entrySet()) {
                    String key = entry.getKey();
                    sb.append(key);
                    List<String> values = entry.getValue();
                    for (String value : values) {
                        if (!("".equals(value))) {
                            sb.append("=").append(value);
                        }
                        sb.append("&");
                    }
                }
                if (sb.length() > 0 && '&' == sb.charAt(sb.length() - 1)) {
                    sb.deleteCharAt(sb.length() - 1);
                }
                String charset = "UTF-8";
                if (mediaType.getParameters().get("charset") != null) {
                    charset = mediaType.getParameters().get("charset");
                }
                try {
                    is = new ByteArrayInputStream(sb.toString().getBytes(charset));
                } catch (Exception e) {
                    LogMessages.LOGGER.charsetUnavailable(charset);
                }
            }
        }
    }
    try {
        if (is == null) {
            is = request.getInputStream();
        }
        if (isMarshalledEntity) {
            is = new InputStreamToByteArray(is);
        }
        AbstractReaderInterceptorContext messageBodyReaderContext = new ServerReaderInterceptorContext(getReaderInterceptors(), factory, type, genericType, annotations, mediaType, request.getMutableHeaders(), is, request);
        RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
        final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
        final Object obj;
        try {
            obj = messageBodyReaderContext.proceed();
        } finally {
            tracingLogger.logDuration("RI_SUMMARY", timestamp, messageBodyReaderContext.getProcessedInterceptorCount());
        }
        if (isMarshalledEntity) {
            InputStreamToByteArray isba = (InputStreamToByteArray) is;
            final byte[] bytes = isba.toByteArray();
            return new MarshalledEntity() {

                @Override
                public byte[] getMarshalledBytes() {
                    return bytes;
                }

                @Override
                public Object getEntity() {
                    return obj;
                }
            };
        } else {
            return obj;
        }
    } catch (Exception e) {
        if (e instanceof ReaderException) {
            throw (ReaderException) e;
        } else {
            throw new ReaderException(e);
        }
    }
}
Also used : AbstractReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarshalledEntity(org.jboss.resteasy.spi.MarshalledEntity) ReaderException(org.jboss.resteasy.spi.ReaderException) ReaderException(org.jboss.resteasy.spi.ReaderException) InputStreamToByteArray(org.jboss.resteasy.util.InputStreamToByteArray) ByteArrayInputStream(java.io.ByteArrayInputStream) ServerReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext) MediaType(jakarta.ws.rs.core.MediaType) AccessibleObject(java.lang.reflect.AccessibleObject) List(java.util.List) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger)

Example 2 with ServerReaderInterceptorContext

use of org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext in project resteasy by resteasy.

the class GZIPDecodingInterceptor method aroundReadFrom.

@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
    LogMessages.LOGGER.debugf("Interceptor : %s,  Method : aroundReadFrom", getClass().getName());
    Object encoding = context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING);
    if (encoding != null && encoding.toString().equalsIgnoreCase("gzip")) {
        InputStream old = context.getInputStream();
        FinishableGZIPInputStream is = new FinishableGZIPInputStream(old, context instanceof ServerReaderInterceptorContext, getMaxSize());
        context.setInputStream(is);
        try {
            return context.proceed();
        } finally {
            // see RESTEASY-554 for more details
            if (!context.getType().equals(InputStream.class))
                is.finish();
            context.setInputStream(old);
        }
    } else {
        return context.proceed();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) ServerReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext)

Aggregations

InputStream (java.io.InputStream)2 ServerReaderInterceptorContext (org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext)2 MediaType (jakarta.ws.rs.core.MediaType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 List (java.util.List)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 AbstractReaderInterceptorContext (org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext)1 MarshalledEntity (org.jboss.resteasy.spi.MarshalledEntity)1 ReaderException (org.jboss.resteasy.spi.ReaderException)1 RESTEasyTracingLogger (org.jboss.resteasy.tracing.RESTEasyTracingLogger)1 InputStreamToByteArray (org.jboss.resteasy.util.InputStreamToByteArray)1