REST proxy servlet ver. 1.2


This servlet lets you transparently pass HTTP GET requests to the hosts, provided as a parameter. For example, you are developing some application with Google Maps API. And you are going to request some XML file from the remote host with function GDownloadUrl. This function is a wrapper for XMLHttpRequest and accepts two parameters: URL for XML data and user-defined callback. XMLHttpRequest has so called sandbox restrictions. 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? So normally you cannot use here data from the remote hosts. This component lets you call some local servlet and provide an actual URL as a parameter. Requested data from the remote host (e.g. XML, JSON etc.) will be passed back to your JavaScript application.

How to use proxy servlet:

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

b) describe this servlet in web.xml file. Servlet accepts the following initial parameters (optional):
proxyHost - Optional parameter. Describes a proxy host for your internet connection
proxyPort - Optional parameter. Describes a proxy port for your internet connection
encoding - Optional parameter. Describes a target encoding
rewriteHost - Optional parameter. Describes a boolean value. If this value is true than servlet will rewrite Host header
remoteHost - Optional parameter. Describes an URL for the remote host. Alternatively, you can pass it as GET parameter.

For example:
 


<servlet>
  <servlet-name>RestProxy</servlet-name>
  <servlet-class>com.jsos.restproxy.RestProxyServlet</servlet-class>
</servlet>

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


<servlet-mapping>
  <servlet-name>RestProxy</servlet-name>
  <url-pattern>/servlet/proxy</url-pattern>
</servlet-mapping>

and now in your applications you can use local requests like
 


/servlet/proxy?http://some_remore_host

Alternatively, you can describe remote host as an initial parameter and use just /servlet/proxy call.

E.g. your XMLHttpRequest calls will be addressed now to the local servlet and transparently redirected to the actual remote site site. And this chain lets you avoid the violation of browser cross-domain security policy.

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


<servlet>
  <servlet-name>RestProxy</servlet-name>
  <servlet-class>com.jsos.restproxy.RestProxyServlet</servlet-class>
<init-param>
  <param-name>proxyHost</param-name>
  <param-value>192.168.0.1</param-value>
</init-param>

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

   For downloading:

    REST proxy package:  restProxyPackage.jar
 

 ©  Coldbeans     Comments?

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

     

Also in JSOS: