HTTP proxy servlet ver. 3.4


This servlet lets you transparently pass all the incoming requests to the some predefined host. Servlet simply performs a role of HTTP proxy for your requests.

Normally you can hide the actual location for your servers with this proxy servlet. One interesting usage belongs to Ajax programming. You browser may impose a security restriction on calls to XMLHttpRequest. This restriction prevents a script or application from making a connection to any web server other than your web page originally came from. But what if you are going to obtain XML data from the Net? In our example we will show how you can request web services from Yahoo in your Ajax applications.

How to use proxy servlet:

a) download httpProxyPackage.jar and save it in WEB-INF/lib

b) describe this servlet in web.xml file. Servlet accepts the following initial parameters:

host - describes a host for the redirection
proxyHost - Optional parameter. Describes a proxy host for your internet connection
proxyPort - Optional parameter. Describes a proxy port for your internet connection
rewriteHost - Optional parameter. If this value is true than HTTP header Host will be changed. Default value is false
uri - Optional parameter. If this value is not empty, than proxy will use the request's URI exclude the provided value in the final request. Default value is empty (do not rewrite URI).
path - Optional parameter. Describes a boolean value. If this value is true, than proxy will use path info from the original request in the final one. Default value if false (do not translate path).
encoding - Optional parameter. Describes a target encoding
headers - Optional parameter. If this value is true, than proxy will keep headers from the original request as well as read them from the requested party. Otherwise headers will be skipped. Default value is true (keep headers).
combineHeaders - Optional parameter. If this value is false, than proxy will send multiple headers with the same key "as is". Default value is true (combine multiple headers with the same key into one).
gzip - Optional parameter. If this value is false, than proxy disables gzip compression (disables ACCEPT-ENCODING header). Default value is true (keeps ACCEPT-ENCODING header "as is").
log - Optional parameter. If this value is true, than proxy prints trace log file on the system console. Default value is false (servlet does not print trace).
certificate - Optional parameter. If this value is true, than proxy accepts any SSL certificate. Default value is false.
keepAlive - Optional parameter. If this value is false, than proxy will set keepAlive attribute to false. Default value is true.
user - Optional parameter. Describes a user name for basic authentication
password - Optional parameter. Describes a password for basic authentication

For example in this case servlet will redirect requests to Yahoo search:
 


<servlet>
  <servlet-name>HttpProxy</servlet-name>
  <servlet-class>com.jsos.httpproxy.HttpProxyServlet</servlet-class>
<init-param>
  <param-name>host</param-name>
  <param-value>http://api.search.yahoo.com/WebSearchService/V1/webSearch</param-value>
</init-param>
</servlet>

c) describe a mapping for this servlet in web.xml file. E.g.:
 


<servlet-mapping>
  <servlet-name>HttpProxy</servlet-name>
  <url-pattern>/servlet/yahoo</url-pattern>
</servlet-mapping>

So in the above-mentioned example any local request like this /servlet/yahoo?appid=Your_Yahoo_ID&query=Coldtags&results=10 (search for the word Coldtags on Yahoo) will be actually redirected (and processed there) to Yahoo site. It is also an example how easily you can use Yahoo search API in your Ajax applications. Your XMLHttpRequest Calls could be addressed to the local servlet and transparently redirected to the actual Yahoo site. And all this chain lets you avoid the violation of browser's cross-domain security policy.

Note that you do not need proxy of course for the direct (not Ajax based) request to Yahoo. But HTTP proxy may help again when/if you will need hide that requests or create a facade etc.

Also you can describe the proxy settings for the servlet itself. Parameters are proxyHost and proxyPort. E.g.:
 


<servlet>
  <servlet-name>HttpProxy</servlet-name>
  <servlet-class>com.jsos.httpproxy.HttpProxyServlet</servlet-class>
<init-param>
  <param-name>host</param-name>
  <param-value>http://api.search.yahoo.com/WebSearchService/V1/webSearch</param-value>
</init-param>

<init-param>
  <param-name>proxyHost</param-name>
  <param-value>10.114.7.95</param-value>
</init-param>

<init-param>
  <param-name>proxyPort</param-name>
  <param-value>80</param-value>
</init-param>
</servlet>

Parameter uri lets you support RESTful requests. Let us provide an example:

Suppose you are describing this parameter so:
 


<init-param>
  <param-name>uri</param-name>
  <param-value>/c/delete*</param-value>
</init-param>

and your mapping for servlet looks so:
 


<servlet-mapping>
  <servlet-name>HttpProxy</servlet-name>
  <url-pattern>/c/delete/*</url-pattern>
</servlet-mapping>

then for your original request that looks so: http://original_host/c/delete/1 proxy will cut a part of the request that corresponds uri parameter (/c/delete – everything before *) and use the rest (/1 in this case) for adding to the target request. So the real request finally looks so: http://real_host/1

In the simplest case you can set an intial parameter path to true and translate path info (the extra path information follows the servlet path but precedes the query string and will start with a "/" character) from the original request to the final one.

At this version proxy supports the following HTTP (HTTPS) requests:

GET
POST
PUT
HEAD
DELETE

   For downloading:

    HTTP proxy package:  httpProxyPackage.jar
 

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.


     

Also in JSOS: