So here is the scenario which we have/want to implement:
- We have a java spring based web application deployed on apache tomcat.
- In our web application we will provide a 3rd party web application link which is .net based to user. On click of which user will be redirected to .net application but the domain and context root shown in the browser url will remain same that is of java application.
- We cannot ask 3rd party team to change anything in their application.
- Whatever we have to change should be in our code, also as far as we could we should refrain ourself from any apache tomcat level changes.
Here are the things which we implemented so far:
- We tried to implement reverse proxy using following link: https://github.com/mitre/HTTP-Proxy-Servlet
- So the basic funda was to call the 3rd party url as a webservice and edit the httpresponse and add our domain and context path to the urls in any file received as response. Though this is not a good solution and consumes lot of time and space but it does the job.
Issue Which we are facing:
- Everything is working fine except the ajax request in the .net application.
- The Error thrown is: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Here is the code fragment for doing httpresponse modification task:
protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse servletResponse,
HttpRequest proxyRequest, HttpServletRequest servletRequest)throws IOException {
HttpEntity entity = new BufferedHttpEntity(proxyResponse.getEntity());
if (entity.isChunked()) {
InputStream is = entity.getContent();
String proxyBody = EntityUtils.toString(entity);
proxyBody = proxyBody.replaceAll("/.netContextRoot/", "/ourContextRoot/.netContextRoot/");
InputStream stream = new ByteArrayInputStream(proxyBody.getBytes(StandardCharsets.UTF_8));
OutputStream os = servletResponse.getOutputStream();
byte[] buffer = new byte[10 * 1024];
int read;
while ((read = stream.read(buffer)) != -1) {
os.write(buffer, 0, read);
if (doHandleCompression || stream.available() == 0 /* next is.read will block */) {
os.flush();
}
}
// Entity closing/cleanup is done in the caller (#service)
} else {
String proxyBody = EntityUtils.toString(entity);
proxyBody = proxyBody.replaceAll("/.netContextRoot/", "/ourContextRoot/.netContextRoot/");
EntityUtils.updateEntity(proxyResponse, new StringEntity(proxyBody));
HttpEntity entity2 = proxyResponse.getEntity();
OutputStream servletOutputStream = servletResponse.getOutputStream();
entity2.writeTo(servletOutputStream);
}
}
Can anybody help us out with this scenario, Also if you have any other solution without making any changes in apache level then pls do mention.
Thanks in Advance.