Posted By

bcmoney on 12/10/10


Tagged

http java servlet proxy


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kubaitis


Proxy.java


 / Published in: Java
 

URL: http://bcmoney-mobiletv.com/blog/2009/05/01/the-server-side-proxy/

Java Servlet for making an HTTP request cross-domain.

Nice and simple tutorial: http://www.tidytutorials.com/2009/12/simple-java-proxy-servlet-using.html

  1. package proxy;
  2.  
  3. import java.io.*;
  4. import java.util.Enumeration;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.List;
  8. import java.util.logging.*;
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12.  
  13. import java.net.URL;
  14. import java.net.HttpURLConnection;
  15.  
  16. public class Proxy extends HttpServlet {
  17.  
  18. private ServletContext servletContext;
  19. private Logger log;
  20.  
  21. public void init(ServletConfig servletConfig) throws ServletException {
  22. servletContext = servletConfig.getServletContext();
  23. log = Logger.getLogger(Proxy.class.getName());
  24. }
  25.  
  26. public void doGet(HttpServletRequest request, HttpServletResponse response){
  27. doPost(request, response);
  28. }
  29.  
  30. public void doPost(HttpServletRequest request, HttpServletResponse response){
  31.  
  32. BufferedInputStream webToProxyBuf = null;
  33. BufferedOutputStream proxyToClientBuf = null;
  34.  
  35. try{
  36. int statusCode;
  37. int oneByte;
  38. String methodName;
  39. String headerText;
  40.  
  41. String urlString = request.getRequestURL().toString();
  42. String queryString = request.getQueryString();
  43.  
  44. urlString += queryString==null?"":"?"+queryString;
  45. URL url = new URL(urlString);
  46.  
  47. log.info("Fetching >"+url.toString());
  48.  
  49. con =(HttpURLConnection) url.openConnection();
  50.  
  51. methodName = request.getMethod();
  52. con.setRequestMethod(methodName);
  53. con.setDoOutput(true);
  54. con.setDoInput(true);
  55. con.setFollowRedirects(false);
  56. con.setUseCaches(true);
  57.  
  58. for( Enumeration e = request.getHeaderNames() ; e.hasMoreElements();){
  59. String headerName = e.nextElement().toString();
  60. con.setRequestProperty(headerName, request.getHeader(headerName));
  61. }
  62.  
  63. con.connect();
  64.  
  65. if(methodName.equals("POST")){
  66. BufferedInputStream clientToProxyBuf = new BufferedInputStream(request.getInputStream());
  67. BufferedOutputStream proxyToWebBuf = new BufferedOutputStream(con.getOutputStream());
  68.  
  69. while ((oneByte = clientToProxyBuf.read()) != -1)
  70. proxyToWebBuf.write(oneByte);
  71.  
  72. proxyToWebBuf.flush();
  73. proxyToWebBuf.close();
  74. clientToProxyBuf.close();
  75. }
  76.  
  77. statusCode = con.getResponseCode();
  78. response.setStatus(statusCode);
  79.  
  80. for( Iterator i = con.getHeaderFields().entrySet().iterator() ; i.hasNext() ;){
  81. Map.Entry mapEntry = (Map.Entry)i.next();
  82. if(mapEntry.getKey()!=null)
  83. response.setHeader(mapEntry.getKey().toString(), ((List)mapEntry.getValue()).get(0).toString());
  84. }
  85.  
  86. webToProxyBuf = new BufferedInputStream(con.getInputStream());
  87. proxyToClientBuf = new BufferedOutputStream(response.getOutputStream());
  88.  
  89. while ((oneByte = webToProxyBuf.read()) != -1)
  90. proxyToClientBuf.write(oneByte);
  91.  
  92. proxyToClientBuf.flush();
  93. proxyToClientBuf.close();
  94.  
  95. webToProxyBuf.close();
  96. con.disconnect();
  97.  
  98. }catch(Exception e){
  99. System.out.println(e.getMessage());
  100. e.printStackTrace();
  101. }
  102. finally{
  103. }
  104. }
  105. }

Report this snippet  

You need to login to post a comment.