I have created a ProxyServlet to intercept the Ajax JSON request of a third party tool we have acquired in the company.
My objective is to modify some of the response on the fly.
I hav a first issue, that is I can't intercept the json calls, can't track them. They go through the proxy (when the proxy is done the browser displays an error message), but I see no traces of them (neither in service, nor in onresponsecontent, nor in onresponsesuccess).
public class MyProxy extends ProxyServlet {
private int count = 0;
private final Charset CHARSET = Charset.forName("ISO-8859-1");
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("init done !");
}
private void displayRequest(HttpServletRequest request) throws IOException {
if (request == null)
return;
System.out.println("--- Request ---");
System.out.println("Content type: " + request.getContentType());
System.out.println("Remote address: " + request.getRemoteAddr());
System.out.println(request.getQueryString());
Enumeration<String> headernames = request.getHeaderNames();
while(headernames.hasMoreElements()) {
String headerName = headernames.nextElement();
System.out.println("Header " + headerName + ": " + request.getHeader(headerName));
}
// Attributes
Enumeration<String> atttributesNames = request.getAttributeNames();
while(atttributesNames.hasMoreElements()) {
String attributeName = atttributesNames.nextElement();
System.out.println("Attribute " + attributeName + ": " + request.getAttribute(attributeName));
}
// Parameters
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
System.out.print("Parameter " + paramName + ":");
String[] paramValues = request.getParameterValues(paramName);
for (int i = 0; i < paramValues.length; i++) {
String paramValue = paramValues[i];
System.out.print(paramValue + ";");
}
}
System.out.println("--- End Request ---");
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
System.out.println("Servicing (" + count++ + ")");
displayRequest(arg0);
super.service(arg0, arg1);
}
@Override
protected void onResponseContent(HttpServletRequest request,
HttpServletResponse response, Response proxyResponse,
byte[] buffer, int offset, int length) throws IOException {
//System.out.write(buffer, offset, length);
System.out.println("--- onResponseContent ---" + getRequestId(request));
// This is where I'd like to print the buffer, because this is what is going to be modified by COEOS
super.onResponseContent(request, response, proxyResponse, buffer, offset,
length);
}
@Override
protected void onResponseSuccess(HttpServletRequest request,
HttpServletResponse response, Response proxyResponse) {
// TODO Auto-generated method stub
System.out.println("--- onResponseSuccess ---" + getRequestId(request));
super.onResponseSuccess(request, response, proxyResponse);
}
}
Any idea of what is wrong?
Then I have a second issue. The response content can be sent is several chunk. How can i modify the content in such case?
Thanks for your help
Gilles