Search in sources :

Example 1 with ResponseImpl

use of org.apache.cxf.jaxrs.impl.ResponseImpl in project cxf by apache.

the class InboundSseEventProcessor method run.

void run(final Response response) {
    if (closed) {
        throw new IllegalStateException("The SSE Event Processor is already closed");
    }
    final InputStream is = response.readEntity(InputStream.class);
    final ClientProviderFactory factory = ClientProviderFactory.getInstance(endpoint);
    Message message = null;
    if (response instanceof ResponseImpl) {
        message = ((ResponseImpl) response).getOutMessage();
    }
    executor.submit(process(response, is, factory, message));
}
Also used : Message(org.apache.cxf.message.Message) InputStream(java.io.InputStream) ClientProviderFactory(org.apache.cxf.jaxrs.client.ClientProviderFactory) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl)

Example 2 with ResponseImpl

use of org.apache.cxf.jaxrs.impl.ResponseImpl in project cxf by apache.

the class JAXRSUtils method copyResponseIfNeeded.

public static Response copyResponseIfNeeded(Response response) {
    if (!(response instanceof ResponseImpl)) {
        Response r = fromResponse(response).build();
        Field[] declaredFields = ReflectionUtil.getDeclaredFields(response.getClass());
        for (Field f : declaredFields) {
            Class<?> declClass = f.getType();
            if (declClass == Annotation[].class) {
                try {
                    Annotation[] fieldAnnotations = ReflectionUtil.accessDeclaredField(f, response, Annotation[].class);
                    ((ResponseImpl) r).setEntityAnnotations(fieldAnnotations);
                } catch (Throwable ex) {
                    LOG.warning("Custom annotations if any can not be copied");
                }
                break;
            }
        }
        return r;
    }
    return response;
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) Field(java.lang.reflect.Field) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl) Annotation(java.lang.annotation.Annotation)

Example 3 with ResponseImpl

use of org.apache.cxf.jaxrs.impl.ResponseImpl in project cxf by apache.

the class WebClient method handleResponse.

protected Response handleResponse(Message outMessage, Class<?> responseClass, Type genericType) {
    try {
        ResponseBuilder rb = setResponseBuilder(outMessage, outMessage.getExchange());
        Response currentResponse = rb.clone().build();
        ((ResponseImpl) currentResponse).setOutMessage(outMessage);
        Object entity = readBody(currentResponse, outMessage, responseClass, genericType, new Annotation[] {});
        if (entity == null) {
            int status = currentResponse.getStatus();
            if (status >= 400) {
                entity = currentResponse.getEntity();
            }
        }
        rb = JAXRSUtils.fromResponse(currentResponse, false);
        rb.entity(entity instanceof Response ? ((Response) entity).getEntity() : entity);
        Response r = rb.build();
        getState().setResponse(r);
        ((ResponseImpl) r).setOutMessage(outMessage);
        return r;
    } catch (Throwable ex) {
        throw (ex instanceof ProcessingException) ? (ProcessingException) ex : new ProcessingException(ex);
    } finally {
        ClientProviderFactory.getInstance(outMessage).clearThreadLocalProxies();
    }
}
Also used : Response(javax.ws.rs.core.Response) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with ResponseImpl

use of org.apache.cxf.jaxrs.impl.ResponseImpl in project cxf by apache.

the class ClientProxyImpl method handleResponse.

protected Object handleResponse(Message outMessage, Class<?> serviceCls) throws Throwable {
    try {
        Response r = setResponseBuilder(outMessage, outMessage.getExchange()).build();
        ((ResponseImpl) r).setOutMessage(outMessage);
        getState().setResponse(r);
        Method method = outMessage.getExchange().get(Method.class);
        checkResponse(method, r, outMessage);
        if (method.getReturnType() == Void.class || method.getReturnType() == Void.TYPE) {
            return null;
        }
        if (method.getReturnType() == Response.class && (r.getEntity() == null || InputStream.class.isAssignableFrom(r.getEntity().getClass()) && ((InputStream) r.getEntity()).available() == 0)) {
            return r;
        }
        if (PropertyUtils.isTrue(super.getConfiguration().getResponseContext().get(BUFFER_PROXY_RESPONSE))) {
            r.bufferEntity();
        }
        Class<?> returnType = method.getReturnType();
        Type genericType = InjectionUtils.processGenericTypeIfNeeded(serviceCls, returnType, method.getGenericReturnType());
        returnType = InjectionUtils.updateParamClassToTypeIfNeeded(returnType, genericType);
        return readBody(r, outMessage, returnType, genericType, method.getDeclaredAnnotations());
    } finally {
        ClientProviderFactory.getInstance(outMessage).clearThreadLocalProxies();
    }
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl)

Example 5 with ResponseImpl

use of org.apache.cxf.jaxrs.impl.ResponseImpl in project cxf 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 = null;
    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 ? false : 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) {
        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 {
            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);
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Message(org.apache.cxf.message.Message) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl) Annotation(java.lang.annotation.Annotation) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Exchange(org.apache.cxf.message.Exchange) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Produces(javax.ws.rs.Produces) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MediaType(javax.ws.rs.core.MediaType)

Aggregations

ResponseImpl (org.apache.cxf.jaxrs.impl.ResponseImpl)5 Response (javax.ws.rs.core.Response)3 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)2 AsyncResponse (javax.ws.rs.container.AsyncResponse)2 MediaType (javax.ws.rs.core.MediaType)2 Message (org.apache.cxf.message.Message)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 HttpMethod (javax.ws.rs.HttpMethod)1 ProcessingException (javax.ws.rs.ProcessingException)1 Produces (javax.ws.rs.Produces)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 ClientProviderFactory (org.apache.cxf.jaxrs.client.ClientProviderFactory)1 AsyncResponseImpl (org.apache.cxf.jaxrs.impl.AsyncResponseImpl)1