You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Shing Hing Man <ma...@yahoo.com> on 2013/10/06 20:51:27 UTC

How to return an image for a http get request without the original query params ?


 Hi ,

I am trying to set  up a route that 
1) receives a http get request  
2) extract a query parameter from the HttpRequest.
3) forward the extracted query parameter  to another endpoint. 
4) Return an  1 by 1 pixel image to the http get request sender.

I managed to do all the above, but the returned 1 by 1 image also contains the original query parameter. 


Below is my code.

public class JettyHttpServerDemo {
    private CamelContext context;

    public void run() throws Exception {
        context = new DefaultCamelContext();

        // add our route to the CamelContext
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {

                from("jetty:http://localhost:8081/test?matchOnUriPrefix=true")
                        .process(new ImageProcessor()).to("stream:out");

            }

        });

        context.start();
        
        Thread.sleep(1000000);

    }

    private static class ImageProcessor implements Processor {
        private BufferedImage pixel;

        public ImageProcessor() {
            pixel = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            pixel.setRGB(0, 0, (0xFF));
        }

        public void process(Exchange exchange) throws Exception {
            
            HttpServletRequest request = exchange.getIn().getBody(
                    HttpServletRequest.class);
            String name = request.getParameter("name");
            
            exchange.getIn().setBody("name=" + name);
            
            // send a response with 1 pixel.
            HttpServletResponse response = exchange.getIn().getBody(
                    HttpServletResponse.class);
            response.setContentType("image/png");

            OutputStream os = response.getOutputStream();

            ImageIO.write(pixel, "png", os);
            
        }
    }

    public static void main(String[] args) throws Exception {
         JettyHttpServerDemo demo = new JettyHttpServerDemo();
         demo.run();
    }
-----------------


public class JettyHttpGetClient  {

    private CamelContext context;

    public void send() throws Exception{

        context = new DefaultCamelContext();


        ProducerTemplate template = context.createProducerTemplate();

        Exchange exchange = template.send("http://localhost:8081/test", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "name=foo");
            }
        });
        
        Message out = exchange.getOut();

        InputStream reply = (InputStream) exchange.getOut().getBody();
      // replyStr also contains the query parameter name=foo !
        String replyStr= inputStreamToString(reply);
        System.out.println("Response from http server has size :"+ replyStr.length());
        System.out.println("Response from http server is :"+ replyStr);
        System.out.println("status header is "+out.getHeader(Exchange.HTTP_RESPONSE_CODE));
        
        
        
    }

    private String inputStreamToString(InputStream inputStream) throws Exception{
        BufferedReader in = new BufferedReader(
                new InputStreamReader(inputStream));
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        return response.toString();
        
    }
    
    
    public static void main(String [] args) throws Exception{
         JettyHttpGetClient client = new JettyHttpGetClient();
         client.send();
        
        
    }
    
}
-----------------------------

In debug mode, I can see that replyStr also contains "name=foo". 
Is there a way to return a 1 by 1 pixel image without the original query parameter ?

Thanks in advance for any assistance !

Shing

Re: How to return an image for a http get request without the original query params ?

Posted by Shing Hing Man <ma...@yahoo.com>.
From  the "Message Body" section of  http://camel.apache.org/http.html :


Camel will store the HTTP response from the external server on the OUT 
body. All headers from the IN message will be copied to the OUT message,
 so headers are preserved during routing. Additionally Camel will add 
the HTTP response headers as well to the OUT message headers.
 

The above explains why I am getting the original query parameters  in the reply. Somehow I need remove the original parameters from the out message.

Shing 



________________________________
 From: Shing Hing Man <ma...@yahoo.com>
To: "users@camel.apache.org" <us...@camel.apache.org> 
Sent: Sunday, October 6, 2013 7:51 PM
Subject: How to return an image for a http get request without the original query params ?
 



 Hi ,

I am trying to set  up a route that 
1) receives a http get request  
2) extract a query parameter from the HttpRequest.
3) forward the extracted query parameter  to another endpoint. 
4) Return an  1 by 1 pixel image to the http get request sender.

I managed to do all the above, but the returned 1 by 1 image also contains the original query parameter. 


Below is my code.

public class JettyHttpServerDemo {
    private CamelContext context;

    public void run() throws Exception {
        context = new DefaultCamelContext();

        // add our route to the CamelContext
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {

                from("jetty:http://localhost:8081/test?matchOnUriPrefix=true")
                        .process(new ImageProcessor()).to("stream:out");

            }

        });

        context.start();
        
        Thread.sleep(1000000);

    }

    private static class ImageProcessor implements Processor {
        private BufferedImage pixel;

        public ImageProcessor() {
            pixel = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            pixel.setRGB(0, 0, (0xFF));
        }

        public void process(Exchange exchange) throws Exception {
            
            HttpServletRequest request = exchange.getIn().getBody(
                    HttpServletRequest.class);
            String name = request.getParameter("name");
            
            exchange.getIn().setBody("name=" + name);
            
            // send a response with 1 pixel.
            HttpServletResponse response = exchange.getIn().getBody(
                    HttpServletResponse.class);
            response.setContentType("image/png");

            OutputStream os = response.getOutputStream();

            ImageIO.write(pixel, "png", os);
            
        }
    }

    public static void main(String[] args) throws Exception {
         JettyHttpServerDemo demo = new JettyHttpServerDemo();
         demo.run();
    }
-----------------


public class JettyHttpGetClient  {

    private CamelContext context;

    public void send() throws Exception{

        context = new DefaultCamelContext();


        ProducerTemplate template = context.createProducerTemplate();

        Exchange exchange = template.send("http://localhost:8081/test", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "name=foo");
            }
        });
        
        Message out = exchange.getOut();

        InputStream reply = (InputStream) exchange.getOut().getBody();
      // replyStr also contains the query parameter name=foo !
        String replyStr= inputStreamToString(reply);
        System.out.println("Response from http server has size :"+ replyStr.length());
        System.out.println("Response from http server is :"+ replyStr);
        System.out.println("status header is "+out.getHeader(Exchange.HTTP_RESPONSE_CODE));
        
        
        
    }

    private String inputStreamToString(InputStream inputStream) throws Exception{
        BufferedReader in = new BufferedReader(
                new InputStreamReader(inputStream));
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        return response.toString();
        
    }
    
    
    public static void main(String [] args) throws Exception{
         JettyHttpGetClient client = new JettyHttpGetClient();
         client.send();
        
        
    }
    
}
-----------------------------

In debug mode, I can see that replyStr also contains "name=foo". 
Is there a way to return a 1 by 1 pixel image without the original query parameter ?

Thanks in advance for any assistance !

Shing