View Javadoc

1   /*
2    * $Revision: 1.2 $
3    * $Date: 2006/02/28 18:20:38 $
4    *
5    * ====================================================================
6    * Struts back mechanism
7    * Copyright (C) 2006 - Manfred Wolff
8    * 
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   *
21   * created: 21-01-2006 Manfred Wolff (wolff@mwolff.org)
22   */
23  package org.mwolff.struts.back;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.struts.action.Action;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  import org.apache.struts.action.ActionServlet;
38  import org.apache.struts.config.ModuleConfig;
39  import org.apache.struts.tiles.TilesRequestProcessor;
40  
41  /***
42   * Special RequestProcessor to process back and forward mechanism.
43   * 
44   * @author mwolff
45   */
46  public class BackRequestProcessor extends TilesRequestProcessor {
47  
48      /***
49       * <p>
50       * Commons Logging instance.
51       * </p>
52       */
53      protected static Log log = LogFactory.getLog(BackRequestProcessor.class);
54  
55      /***
56       * <p>
57       * Initialize this request processor instance.
58       * </p>
59       * 
60       * @param servlet The ActionServlet we are associated with
61       * @param moduleConfig The ModuleConfig we are associated with.
62       * @throws ServletException If an error occor during initialization
63       */
64      public void init(ActionServlet servlet, ModuleConfig moduleConfig)
65          throws ServletException {
66  
67          super.init(servlet, moduleConfig);
68      }
69      
70      public void setMarker(RingBuffer buffer, HttpServletRequest request) {
71          if (buffer.isForwardPossible()) {
72              request.setAttribute("org.mwolff.struts.isForward", "true"); 
73          } else {
74              request.setAttribute("org.mwolff.struts.isForward", "false"); 
75          }
76  
77          if (buffer.isBackPossible()) {
78              request.setAttribute("org.mwolff.struts.isBack", "true"); 
79          } else {
80              request.setAttribute("org.mwolff.struts.isBack", "false"); 
81          }
82          
83      }
84  
85      /***
86       * <p>
87       * Ask the specified <code>Action</code> instance to handle this request. 
88       * Return the <code>ActionForward</code> instance (if any) returned by the 
89       * called <code>Action</code> for further processing.
90       * </p>
91       * 
92       * @param request The servlet request we are processing
93       * @param response The servlet response we are creating
94       * @param action The Action instance to be used
95       * @param form The ActionForm instance to pass to this Action
96       * @param mapping The ActionMapping instance to pass to this Action
97       * 
98       * @return The <code>ActionForward</code> instance (if any) returned by 
99       * the called <code>Action</code>.
100      * 
101      * @exception IOException if an input/output error occurs
102      * @exception ServletException if a servlet exception occurs
103      */
104     protected ActionForward processActionPerform(HttpServletRequest request,
105         HttpServletResponse response, Action action, ActionForm form,
106         ActionMapping mapping) throws IOException, ServletException {
107 
108         try {
109             ActionForward forward = (action.execute(mapping, form, request,
110                 response));
111 
112             // save the appropriate QueryString for this request
113             RingBuffer buffer = (RingBuffer) request.getSession().getAttribute(
114                 "org.mwolff.struts.back.RingBuffer");
115 
116             if (buffer == null) {
117                 buffer = new RingBuffer(100);
118                 request.getSession().setAttribute(
119                     "org.mwolff.struts.back.RingBuffer", buffer);
120             }
121 
122             buffer.print();
123             String query = calculateQueryString(request);
124 
125             // only push ActionForwards forward declarations not direct jsp forwards.
126             if (query != null && query.indexOf(".do") != -1) {
127                 
128                 if (buffer.isNoPush() == true) {
129                     // returns
130                     buffer.setNoPush(false);
131                     setMarker(buffer, request);
132                     return forward;
133                 }
134                 if (buffer.isWereInvolved() == true) {
135                     // returns
136                     buffer.setWereInvolved(false);
137                     setMarker(buffer, request);
138                     return forward;
139                 }
140                 ArrayEntry entry = new ArrayEntry(query);
141                 buffer.push(entry);
142                 setMarker(buffer, request);
143             }
144 
145             return forward;
146 
147         } catch (Exception e) {
148             return (processException(request, response, e, form, mapping));
149         }
150 
151     }
152 
153     /***
154      * Calculate the query String out of the Reqeust.
155      * 
156      * @param request The actually request
157      * @return The calculated string.
158      */
159     private String calculateQueryString(HttpServletRequest request) {
160 
161         String sourceURL = null;
162 
163         // The alternative Methode request.getRequestURL() has the side-effect
164         // to do an endless loop, so we must stay by this deprecated method.
165         StringBuffer url = javax.servlet.http.HttpUtils.getRequestURL(request);
166 
167         String queryString = request.getQueryString();
168         if ((queryString != null) && (queryString.length() > 0)) {
169             url.append("?");
170             url.append(queryString);
171         }
172         sourceURL = url.toString();
173         return sourceURL;
174     }
175 }