How to propagate http response code from back-end to client
Posted
by Manoj Neelapu
on Oracle Blogs
See other posts from Oracle Blogs
or by Manoj Neelapu
Published on Tue, 08 Jun 2010 20:04:52 +0530
Indexed on
2010/06/08
17:13 UTC
Read the original article
Hit count: 607
Hands On
We will try to demonstrate this feature using Oracle Service Bus (11.1.1.3.0. We will also use commons-logging-1.1.1, httpcomponents-client-4.0.1, httpcomponents-core-4.0.1 for writing the client to demonstrate.
First we create a simple JSP which will always set response code to 304.
The JSP snippet will look like
<%@ page language="java"
contentType="text/xml;
charset=UTF-8"
pageEncoding="UTF-8"
%>
<%
System.out.println("Servlet setting Responsecode=304");
response.setStatus(304);
response.flushBuffer();
%>
We will now deploy this JSP on weblogic server with URI=http://localhost:7021/reponsecode/
For this JSP we will create a simple Any XML BS
We will also create proxy service as shown below
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
/**
* @author MNEELAPU
*
*/
public class TestProxy304{
public static void main(String arg[]) throws Exception
{
HttpHost target = new HttpHost("localhost", 7021, "http");
// general setup
SchemeRegistry supportedSchemes = new SchemeRegistry();
// Register the "http" protocol scheme, it is required
// by the default operator to look up socket factories.
supportedSchemes.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 7021));
// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params,
supportedSchemes);
DefaultHttpClient httpclient = new DefaultHttpClient(connMgr, params);
HttpGet req = new HttpGet("/HttpResponseCode/ProxyExposed");
System.out.println("executing request to " + target);
HttpResponse rsp = httpclient.execute(target, req);
HttpEntity entity = rsp.getEntity();
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
On compiling and executing this we see the below output in STDOUT which clearly indicates the response code was propagated from Business Service to Proxy service
executing request to http://localhost:7021
----------------------------------------
HTTP/1.1 304 Not Modified
Date: Tue, 08 Jun 2010 16:13:42 GMT
Content-Type: text/xml; charset=UTF-8
X-Powered-By: Servlet/2.5 JSP/2.1
----------------------------------------
© Oracle Blogs or respective owner