Package org.apache.camel

Examples of org.apache.camel.Exchange


            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            return;
        }
       
        // create exchange and set data on it
        Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);

        if (consumer.getEndpoint().isBridgeEndpoint()) {
            exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
            exchange.setProperty(Exchange.SKIP_WWW_FORM_URLENCODED, Boolean.TRUE);
        }
        if (consumer.getEndpoint().isDisableStreamCache()) {
            exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
        }

        // we override the classloader before building the HttpMessage just in case the binding
        // does some class resolution
        ClassLoader oldTccl = overrideTccl(exchange);
        HttpHelper.setCharsetFromContentType(request.getContentType(), exchange);
        exchange.setIn(new HttpMessage(exchange, request, response));
        // set context path as header
        String contextPath = consumer.getEndpoint().getPath();
        exchange.getIn().setHeader("CamelServletContextPath", contextPath);

        String httpPath = (String)exchange.getIn().getHeader(Exchange.HTTP_PATH);
        // here we just remove the CamelServletContextPath part from the HTTP_PATH
        if (contextPath != null
            && httpPath.startsWith(contextPath)) {
            exchange.getIn().setHeader(Exchange.HTTP_PATH,
                    httpPath.substring(contextPath.length()));
        }

        // we want to handle the UoW
        try {
            consumer.createUoW(exchange);
        } catch (Exception e) {
            log.error("Error processing request", e);
            throw new ServletException(e);
        }

        try {
            if (log.isTraceEnabled()) {
                log.trace("Processing request for exchangeId: {}", exchange.getExchangeId());
            }
            // process the exchange
            consumer.getProcessor().process(exchange);
        } catch (Exception e) {
            exchange.setException(e);
        }

        try {
            // now lets output to the response
            if (log.isTraceEnabled()) {
                log.trace("Writing response for exchangeId: {}", exchange.getExchangeId());
            }
            Integer bs = consumer.getEndpoint().getResponseBufferSize();
            if (bs != null) {
                log.trace("Using response buffer size: {}", bs);
                response.setBufferSize(bs);
View Full Code Here


        assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
    }
   
    @Test
    public void testAddCustomerUniqueResponseCodeWithHttpClientAPI() {
        Exchange exchange
            = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "?httpClientAPI=true", new Processor() {
                public void process(Exchange exchange) throws Exception {
                    exchange.setPattern(ExchangePattern.InOut);
                    Message inMessage = exchange.getIn();
                    // set the Http method
                    inMessage.setHeader(Exchange.HTTP_METHOD, "POST");
                    // set the relative path
                    inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customersUniqueResponseCode");               
                    // create a new customer object
                    Customer customer = new Customer();
                    customer.setId(9999);
                    customer.setName("HttpClient");
                    inMessage.setBody(customer);               
                }
            });
       
        // get the response message
        Response response = (Response) exchange.getOut().getBody();
        assertNotNull("The response should not be null ", response);
        assertNotNull("The response entity should not be null", response.getEntity());
        // check the response code
        assertEquals("Get a wrong response code", 201, response.getStatus());
        // check the response code from message header
        assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
    }
View Full Code Here

        assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
    }
   
    @Test
    public void testAddCustomerUniqueResponseCodeWithProxyAPI() {
        Exchange exchange = template.send("direct://proxy", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.setPattern(ExchangePattern.InOut);
                Message inMessage = exchange.getIn();
                setupDestinationURL(inMessage);
                // set the operation name
                inMessage.setHeader(CxfConstants.OPERATION_NAME, "addCustomerUniqueResponseCode");
                // using the proxy client API
                inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
                // set the parameters , if you just have one parameter
                // camel will put this object into an Object[] itself
                Customer customer = new Customer();
                customer.setId(8888);
                customer.setName("ProxyAPI");
                inMessage.setBody(customer);
            }
        });
       
        // get the response message
        Response response = (Response) exchange.getOut().getBody();
        assertNotNull("The response should not be null ", response);
        assertNotNull("The response entity should not be null", response.getEntity());
        // check the response code
        assertEquals("Get a wrong response code", 201, response.getStatus());
        // check the response code from message header
        assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
       
    }
View Full Code Here

       
    }
   
    @Test
    public void testAddCustomerUniqueResponseCode() {
        Exchange exchange
            = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "?httpClientAPI=true", new Processor() {
                public void process(Exchange exchange) throws Exception {
                    exchange.setPattern(ExchangePattern.InOut);
                    Message inMessage = exchange.getIn();
                    // set the Http method
                    inMessage.setHeader(Exchange.HTTP_METHOD, "POST");
                    // set the relative path
                    inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customersUniqueResponseCode");               
                    // put the response's entity into out message body
                    inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
                    // create a new customer object
                    Customer customer = new Customer();
                    customer.setId(8888);
                    customer.setName("Willem");
                    inMessage.setBody(customer);               
                }
            });
       
        // get the response message
        Customer response = (Customer) exchange.getOut().getBody();
        assertNotNull("The response should not be null ", response);
        assertTrue("Get a wrong customer id ", response.getId() != 8888);
        assertEquals("Get a wrong customer name", response.getName(), "Willem");
        assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
    }
View Full Code Here

    }
   
    @Test
    public void testProducerWithQueryParameters() {
        // START SNIPPET: QueryExample
        Exchange exchange = template.send("cxfrs://http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/testQuery?httpClientAPI=true&q1=12&q2=13"
        // END SNIPPET: QueryExample                                  
            , new Processor() {       
                public void process(Exchange exchange) throws Exception {
                    exchange.setPattern(ExchangePattern.InOut);
                    Message inMessage = exchange.getIn();
                    // set the Http method
                    inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
                    inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, InputStream.class);
                    inMessage.setBody(null);               
                }
           
            });
    
        // get the response message
        String response = exchange.getOut().getBody(String.class);
        assertNotNull("The response should not be null ", response);
        assertEquals("The response value is wrong", "q1=12&q2=13", response);
    }
View Full Code Here

        assertEquals("The response value is wrong", "q1=12&q2=13", response);
    }
   
    @Test
    public void testProducerWithQueryParametersHeader() {
        Exchange exchange = template.send("cxfrs://http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/testQuery?httpClientAPI=true&q1=12&q2=13"
            , new Processor() {
                public void process(Exchange exchange) throws Exception {
                    exchange.setPattern(ExchangePattern.InOut);
                    Message inMessage = exchange.getIn();
                    // set the Http method
                    inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
                    inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, InputStream.class);
                    // override the parameter setting from URI
                    // START SNIPPET: QueryMapExample
                    Map<String, String> queryMap = new LinkedHashMap<String, String>();                   
                    queryMap.put("q1", "new");
                    queryMap.put("q2", "world");                   
                    inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap);
                    // END SNIPPET: QueryMapExample
                    inMessage.setBody(null);               
                }
           
            });
    
        // get the response message
        String response = exchange.getOut().getBody(String.class);
        assertNotNull("The response should not be null ", response);
        assertEquals("The response value is wrong", "q1=new&q2=world", response);
    }
View Full Code Here

                // we get the all the content now, so call super to process the received message
                super.messageReceived(ctx, messageEvent);
            }
        } else if (msg instanceof HttpResponse) {
            response = (HttpResponse) msg;
            Exchange exchange = super.getExchange(ctx);
            if (!HttpHeaders.isKeepAlive(response)) {
                // just want to make sure we close the channel if the keepAlive is not true
                exchange.setProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
            }
            if (LOG.isTraceEnabled()) {
                LOG.trace("HttpResponse received: {} chunked:", response, response.isChunked());
            }
            if (response.getStatus().getCode() == HttpResponseStatus.CONTINUE.getCode()) {
View Full Code Here

            total = maxMessagesPerPoll;
        }

        for (int index = 0; index < total && isBatchAllowed(); index++) {
            // only loop if we are started (allowed to run)
            Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
            // add current index and total as properties
            exchange.setProperty(Exchange.BATCH_INDEX, index);
            exchange.setProperty(Exchange.BATCH_SIZE, total);
            exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1);

            // update pending number of exchanges
            pendingExchanges = total - index - 1;

            // must use the original message in case we need to workaround a charset issue when extracting mail content
            final Message mail = exchange.getIn(MailMessage.class).getOriginalMessage();

            // need to call setPeek on java-mail to avoid the message being flagged eagerly as SEEN on the server in case
            // we process the message and rollback due an exception
            if (getEndpoint().getConfiguration().isPeek()) {
                peekMessage(mail);
            }

            // add on completion to handle after work when the exchange is done
            exchange.addOnCompletion(new SynchronizationAdapter() {
                public void onComplete(Exchange exchange) {
                    processCommit(mail, exchange);
                }

                public void onFailure(Exchange exchange) {
View Full Code Here

            if (LOG.isTraceEnabled()) {
                LOG.trace("Mail #{} is of type: {} - {}", new Object[]{i, ObjectHelper.classCanonicalName(message), message});
            }

            if (!message.getFlags().contains(Flags.Flag.DELETED)) {
                Exchange exchange = getEndpoint().createExchange(message);
                if (getEndpoint().getConfiguration().isMapMailMessage()) {
                    // ensure the mail message is mapped, which can be ensured by touching the body/header/attachment
                    LOG.trace("Mapping #{} from javax.mail.Message to Camel MailMessage", i);
                    exchange.getIn().getBody();
                    exchange.getIn().getHeaders();
                    exchange.getIn().getAttachments();
                }

                // If the protocol is POP3 we need to remember the uid on the exchange
                // so we can find the mail message again later to be able to delete it
                if (getEndpoint().getConfiguration().getProtocol().startsWith("pop3")) {
                    String uid = generatePop3Uid(message);
                    if (uid != null) {
                        exchange.setProperty(POP3_UID, uid);
                        LOG.trace("POP3 mail message using uid {}", uid);
                    }
                }
                answer.add(exchange);
            } else {
View Full Code Here

    @Override
    protected int poll() throws Exception {
        int messagesPolled = 0;
        while (isPollAllowed()) {
            final Exchange exchange = executor.submit(pollTask).get();
            if (exchange == null) {
                break;
            }

            ++messagesPolled;
View Full Code Here

TOP

Related Classes of org.apache.camel.Exchange

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.