1

I have wrote a proxy in my spring mvc service, which by passes any request that client making through a specific format to respective services. This is working fine, with some problems. Here is my code.

        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        InetSocketAddress address = new InetSocketAddress(this.serviceUrl, Integer.parseInt(this.servicePort));
        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
        factory.setProxy(proxy);
        factory.setBufferRequestBody(true);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(factory);

        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(uri, method, new HttpEntity<Object>(body,headers), byte[].class);
        return responseEntity;

This code breaks if the target server sends response with the following header.

Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:3636
Content-Type:application/json
Date:Thu, 26 Nov 2015 06:51:26 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.9 (Unix) PHP/5.5.23
X-Powered-By:PHP/5.5.23

And which is working fine if the target response is

Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:application/json
Date:Thu, 26 Nov 2015 06:54:14 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.9 (Unix) PHP/5.5.23
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.23

The only difference I could Able to find the Transfer-Encoding:chunked and Content-Length

The proxy service is not transferring the whole data to the client. rather it breaks after getting some amount of data.

If someone know what how can I fix it. I am sure its problem with the proxy configuration. But unable to fix it.

Ysak
  • 2,601
  • 6
  • 29
  • 53

0 Answers0