Search in sources :

Example 1 with ProxyOutputStream

use of org.apache.cxf.transport.http.ProxyOutputStream in project cxf by apache.

the class ClientRequestFilterInterceptor method handleMessage.

public void handleMessage(Message outMessage) throws Fault {
    ClientProviderFactory pf = ClientProviderFactory.getInstance(outMessage);
    if (pf == null) {
        return;
    }
    // create an empty proxy output stream that the filter can interact with
    // and save a reference for later
    ProxyOutputStream pos = new ProxyOutputStream();
    outMessage.setContent(OutputStream.class, pos);
    outMessage.setContent(ProxyOutputStream.class, pos);
    List<ProviderInfo<ClientRequestFilter>> filters = pf.getClientRequestFilters();
    if (!filters.isEmpty()) {
        final Exchange exchange = outMessage.getExchange();
        final ClientRequestContext context = new ClientRequestContextImpl(outMessage, false);
        for (ProviderInfo<ClientRequestFilter> filter : filters) {
            InjectionUtils.injectContexts(filter.getProvider(), filter, outMessage);
            try {
                filter.getProvider().filter(context);
                @SuppressWarnings("unchecked") Map<String, List<Object>> headers = CastUtils.cast((Map<String, List<Object>>) outMessage.get(Message.PROTOCOL_HEADERS));
                HttpUtils.convertHeaderValuesToString(headers, false);
                Response response = outMessage.getExchange().get(Response.class);
                if (response != null) {
                    outMessage.getInterceptorChain().abort();
                    Message inMessage = new MessageImpl();
                    inMessage.setExchange(exchange);
                    inMessage.put(Message.RESPONSE_CODE, response.getStatus());
                    inMessage.put(Message.PROTOCOL_HEADERS, response.getMetadata());
                    exchange.setInMessage(inMessage);
                    MessageObserver observer = exchange.get(MessageObserver.class);
                    observer.onMessage(inMessage);
                    return;
                }
            } catch (IOException ex) {
                throw new ProcessingException(ex);
            }
        }
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) IOException(java.io.IOException) ClientProviderFactory(org.apache.cxf.jaxrs.client.ClientProviderFactory) Exchange(org.apache.cxf.message.Exchange) Response(javax.ws.rs.core.Response) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ProxyOutputStream(org.apache.cxf.transport.http.ProxyOutputStream) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

IOException (java.io.IOException)1 List (java.util.List)1 ProcessingException (javax.ws.rs.ProcessingException)1 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)1 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)1 Response (javax.ws.rs.core.Response)1 ClientProviderFactory (org.apache.cxf.jaxrs.client.ClientProviderFactory)1 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)1 Exchange (org.apache.cxf.message.Exchange)1 Message (org.apache.cxf.message.Message)1 MessageImpl (org.apache.cxf.message.MessageImpl)1 MessageObserver (org.apache.cxf.transport.MessageObserver)1 ProxyOutputStream (org.apache.cxf.transport.http.ProxyOutputStream)1