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

Reply via email to