KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > helper > servlet > ParamGateway


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ParamGateway.java,v 1.17 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.helper.servlet;
21
22 import java.io.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import org.apache.log4j.*;
27
28 import org.enhydra.barracuda.core.helper.state.*;
29 import org.enhydra.barracuda.plankton.http.*;
30
31
32 /**
33  * <p>The purpose of this servlet is take a set of req parameters
34  * and persist them in the users session. Then return the real target
35  * url back to the calling application
36  */

37 public class ParamGateway extends HttpServlet {
38
39     protected static final Logger logger = Logger.getLogger(ParamGateway.class.getName());
40
41     //these are intentionally non-final, so that you can programatically
42
//change them if need be
43
public static String JavaDoc PARAM_TARGET = "pt_";
44     public static String JavaDoc PARAM_EXT = ".param_map";
45
46
47     //-------------------- ParamGateway --------------------------
48
/**
49      * <p>Handle the default HttpRequest.
50      *
51      * @param req the servlet request
52      * @param resp the servlet response
53      * @throws ServletException
54      * @throws IOException
55      */

56     public void handleDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
57         //persist the params
58
if (logger.isDebugEnabled()) {
59             logger.debug("Persisting parameter information");
60             ServletUtil.showParams(req, logger);
61         }
62         ParamPersister.persistReqParamState(req);
63
64         //find the client context id
65
String JavaDoc cid = req.getParameter("pm_cid");
66
67         //csc_061202.1 - added
68
//set the caching hdrs to prevent caching
69
resp.setHeader("Cache-Control","no-cache");
70         resp.setDateHeader("Expires", System.currentTimeMillis());
71
72         //figure out where we need to go from here by deriving it from
73
//the actual URL used to get us here
74
String JavaDoc src = req.getRequestURI();
75         if (logger.isDebugEnabled()) logger.debug("Request URI:"+src);
76         int spos = src.indexOf(PARAM_TARGET);
77         int epos = src.indexOf(PARAM_EXT);
78         String JavaDoc base = src.substring(spos+PARAM_TARGET.length(), epos);
79         String JavaDoc param = src.substring(epos+PARAM_EXT.length());
80         String JavaDoc target = URLRewriter.encodeURL(req, resp, base+param);
81         if (logger.isDebugEnabled()) logger.debug("Param Target:"+target);
82         resp.setContentType("text/html");
83         PrintWriter out = resp.getWriter();
84
85         out.println("<html>");
86         out.println(" <head>");
87         out.println(" <title></title>");
88         out.println(" <script type=\"text/javascript\">window.onerror=new Function('return true');</script>");
89         out.println(" </head>");
90         out.print (" <body onload=\"p=document.layers?parentLayer:window.parent;p.jsrsLoaded('");out.print(cid);out.println("');\">");
91         out.print (" <h3>Redirecting to real target...</h3><p>If you are not automatically redirected, please click <a HREF=\"");out.print(target);out.println("\" onclick=\"location.replace(this.href);return false;\">here</a></p>");
92         out.print (" <form name=\"jsrs_Form\"><div style=\"visibility:hidden;\"><textarea name=\"jsrs_Payload\" rows=\"2\" cols=\"20\">");out.print(target);out.println("</textarea></div></form>");
93         out.println(" </body>");
94         out.println("</html>");
95
96         //out.flush();
97
resp.flushBuffer(); //instead of out.flush for Servlet 2.2 compatibility (Craig McClanahan http://w4.metronet.com/~wjm/tomcat/2000/Nov/msg00174.html)
98
}
99
100
101     //-------------------- HTTPServlet ---------------------------
102
/**
103      * <p>By default the GET request is mapped to the handleDefault method
104      *
105      * @param req the servlet request
106      * @param resp the servlet response
107      * @throws ServletException
108      * @throws IOException
109      */

110     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
111         handleDefault(req, resp);
112     }
113
114     /**
115      * <p>By default the POST request is mapped to the handleDefault method
116      *
117      * @param req the servlet request
118      * @param resp the servlet response
119      * @throws ServletException
120      * @throws IOException
121      */

122     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
123         handleDefault(req, resp);
124     }
125
126     //-------------------- Servlet -------------------------------
127
/**
128      * <p>Here's where we initialize the servlet.
129      */

130     public void init() throws ServletException {
131         logger.info("initializing servlet");
132     }
133
134 }
135
Popular Tags