Re: Using common sitemesh decorators for<br> all web apps
Alexandru Popescu <[email protected]> Fri, 17 Jun 2005 00:54:39 +0200
| Newsgroups | gmane.comp.web.sitemesh.general |
|---|---|
| Message-ID | <[email protected]> |
#: by Jonathan's words the mind was *winged* :#
> Well I don't know if this is the right way to do this but, as this this forum does not seem to support tagging we will just have to deal.
>
> Sorry if the comments are too sparse.... A few Side notes:
> 1: I used commons-httpclient v3.0 rc2. I dont think it is really necissary for this class but I had it handy and knew how to use it. If you don't want to have to install "yet another jar" you can get rid of this dependancy and just use something under javax.servlet.
>
> 2: I used commons-logging but not very well if you like you can just remove it or you can expand on it... whatever
>
> 3: You may or may not need commons-codec, I did but I don't remember why.
>
> 4: the code can do either a GET or a POST but is currently hard coded. A definite improvement would be to use the method of the servlet's request as the one for the httpclient request, but this served my purposes so I didn't bother working on it any harder.
>
> 5: It requires a parameter defined in the web.xml. The next post has the web.xml code samples
>
> ******* Code Below *******
> package com.foo.proxy;
>
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
> import java.util.ArrayList;
> import java.util.Iterator;
> import java.util.List;
> import java.util.Map;
>
> import javax.servlet.ServletConfig;
> import javax.servlet.ServletException;
> import javax.servlet.http.HttpServlet;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
>
> import org.apache.commons.httpclient.HttpClient;
> import org.apache.commons.httpclient.HttpException;
> import org.apache.commons.httpclient.HttpStatus;
> import org.apache.commons.httpclient.NameValuePair;
> import org.apache.commons.httpclient.methods.PostMethod;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
>
> public class HttpProxyServlet extends HttpServlet {
>
> private static final Log log = LogFactory.getLog(HttpProxyServlet.class);
>
> private static final int BUFFER_SIZE = 4096;
>
> private String serverBase;
>
> public void init(ServletConfig config) throws ServletException {
> this.serverBase = config.getInitParameter("serverBase");
> if(this.serverBase == null || this.serverBase.equals("")){
> log.fatal("The ServerBase was not initialized for this Servlet.");
> }
> super.init(config);
> }
> /*
> * (non-Javadoc)
> *
> * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
> * javax.servlet.http.HttpServletResponse)
> */
> protected void doGet(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException {
> doPost(request, response);
> }
>
> /*
> * (non-Javadoc)
> *
> * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
> * javax.servlet.http.HttpServletResponse)
> */
> protected void doPost(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException {
> handleRequest(request, response);
> }
>
> /**
> * This will forward the request on to the php application
> *
> * @param request
> * @param response
> */
> private void handleRequest(HttpServletRequest request,
> HttpServletResponse response) {
>
> String path = request.getPathInfo();
> if(path == null){
> path = "";
> }
> String url = this.serverBase + path;
>
> log.info("URL: " + url);
>
> // Create an instance of HttpClient.
> HttpClient client = new HttpClient();
>
> // Create a method instance.
> /*
> //can use GetMethod as well
> org.apache.commons.httpclient.methods.GetMethod method =
> new org.apache.commons.httpclient.methods.GetMethod(url);
> method.setQueryString(getParams(request.getParameterMap()));
> */
> PostMethod method = new PostMethod(url);
> method.addParameters(getParams(request.getParameterMap()));
>
> // Provide custom retry handler if necessary
> //method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
> // new DefaultHttpMethodRetryHandler(3, false));
>
> try {
> // Execute the method.
> int statusCode = client.executeMethod(method);
>
> if (statusCode != HttpStatus.SC_OK) {
> System.err.println("Method failed: " + method.getStatusLine());
> }
>
> response.setContentType("text/html");
>
> copy(method.getResponseBodyAsStream(), response.getOutputStream());
>
> } catch (HttpException e) {
> System.err.println("Fatal protocol violation: " + e.getMessage());
> e.printStackTrace();
> } catch (IOException e) {
> System.err.println("Fatal transport error: " + e.getMessage());
> e.printStackTrace();
> } finally {
> // Release the connection.
> method.releaseConnection();
> }
> }
>
> private NameValuePair[] getParams(Map map) {
> List list = new ArrayList();
> Iterator iter = map.entrySet().iterator();
>
> while (iter.hasNext()) {
> Map.Entry entry = (Map.Entry) iter.next();
>
> //Map.Entry.getValue() reutrns a String[] array type
> String[] vals = (String[]) entry.getValue();
>
> //handles name=val1&name=val2 etc..
> for (int i = vals.length - 1; i >= 0; i--) {
> list.add(new NameValuePair((String) entry.getKey(), vals[i]));
> }
>
> }
> //NameValuePair[] pairs = new NameValuePair[list.size()];
> //int i = 0;
> //for (Iterator iterator = list.iterator(); iterator.hasNext();) {
> // pairs[i++] = (NameValuePair) iterator.next();
> //}
> //return pairs;
> return (NameValuePair[]) list.toArray(new NameValuePair[list.size()]);
> }
>
> /**
> * Copy the contents of the given InputStream to the given OutputStream.
> * Closes both streams when done.
> *
> * @param in
> * the stream to copy from
> * @param out
> * the stream to copy to
> * @return the number of bytes copied
> * @throws IOException
> * in case of I/O errors
> */
> public static int copy(InputStream in, OutputStream out) throws IOException {
> try {
> int byteCount = 0;
> byte[] buffer = new byte[BUFFER_SIZE];
> int bytesRead = -1;
> while ((bytesRead = in.read(buffer)) != -1) {
> out.write(buffer, 0, bytesRead);
> byteCount += bytesRead;
> }
> out.flush();
> return byteCount;
> } finally {
> in.close();
> out.close();
> }
> }
>
> }
>
> ---------------------------------------------------------------------
> Posted via Jive Forums
> http://forums.opensymphony.com/thread.jspa?threadID=1454&messageID=8741#8741
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
thanks Jonathan. I will take a look at this later. many thanks,
:alex |.::the_mindstorm::.|
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]