2

I need to copy the HttpServletRequest of client1, as it is, to the HttpServletResponse object created for client2. According to this HttpRequest has a ResuestLine, then headers, then body. I have copied the headers like this :

 Enumeration<?> headerNames = client1Req.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();

        Enumeration<?> headers = client1Req.getHeaders(headerName);
        while (headers.hasMoreElements()) {
            String headerValue = (String) headers.nextElement();
            responseToClient2.addHeader(headerName, headerValue);
            responseToClient2.setHeader(headerName, headerValue);
            System.out.println("headername : "  + headerName + "value : " + headerValue);
        }

and body like this:

input = clientReq.getInputStream();
        output = responseToRIC.getOutputStream();
        if((len=input.available()) > 0){
            len = input.read(outputByte);
            System.out.println("len : "+len);
            output.write(outputByte);
            output.flush();
        }   
        else{
            System.out.println("no body");
        }

However, i need to copy the request line too. If i try to read the input stream instead in the beginning instead of reading headers and body separately, it return -1. There is no api for reading the entire request line from an HttpServletRequest. If I read the URL using getRequestURL() and getQuesrystring(), where should I place it in the responseToClient2? In a custom header REQUESTLINE or should i write it on the outputstream? Thanks

EDIT : since it is semantically wrong to copy a request to a response, i have decided to copy the request tothe payload or body of the response. However, since the HttpServletRequest is not serializable, i am unable to write it on the outputstream of response, any suggestions as to how I should achieve this? Thanks

shalakha
  • 159
  • 2
  • 9
  • 3
    I'm not sure I understand what you are trying to do here. Request and response have different purposes and different specs so what is your real requirement? To just copy one to the other seems wrong. – redge Apr 24 '15 at 06:49
  • 1
    This doesn't make sense. A valid request is not a valid response in HTTP. – user207421 Apr 24 '15 at 06:57
  • Client2 is behind a firewall which blocks incoming requests but allows outgoing requests. I have to relay client1's request to an appserver on client2's side. Client1 cannot directly send a request to the appserver because of the firewall. Hence, I have placed my own server outside the firewall and client1 sends requests for the appserver to my server. My server also receives requests from Client2 and responds to it with the request of client1. I agree that requests and responses have totally different purposes. But then how do I achieve what i want to? – shalakha Apr 24 '15 at 09:07
  • Sounds like you're trying to write some sort of proxy? – Evan Knowles Apr 24 '15 at 14:50

0 Answers0