View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.controller;
17  
18  import static COM.FutureTense.Interfaces.Utilities.goodString;
19  
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import COM.FutureTense.Interfaces.FTVAL;
29  import COM.FutureTense.Interfaces.FTValList;
30  import COM.FutureTense.Interfaces.ICS;
31  import COM.FutureTense.Interfaces.IPS;
32  import COM.FutureTense.Util.ftErrors;
33  import COM.FutureTense.XML.Template.Seed2;
34  
35  import com.fatwire.gst.foundation.CSRuntimeException;
36  import com.fatwire.gst.foundation.DebugHelper;
37  import com.fatwire.gst.foundation.facade.runtag.render.Unknowndeps;
38  
39  /**
40   * @author Tony Field
41   * @author Dolf Dijkstra
42   * @since Jun 16, 2010
43   */
44  
45  public abstract class AbstractController implements Seed2 {
46      protected static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.controller");
47  
48      public static final String STATUS_HEADER = "X-Fatwire-Status";
49  
50      protected ICS ics;
51      private FTValList vIn;
52  
53      /*
54       * (non-Javadoc)
55       * 
56       * @see
57       * COM.FutureTense.XML.Template.Seed2#SetAppLogic(COM.FutureTense.Interfaces
58       * .IPS)
59       */
60  
61      public void SetAppLogic(final IPS ips) {
62          ics = ips.GetICSObject();
63  
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see
70       * COM.FutureTense.XML.Template.Seed#Execute(COM.FutureTense.Interfaces.
71       * FTValList, COM.FutureTense.Interfaces.FTValList)
72       */
73  
74      public final String Execute(final FTValList vIn, final FTValList vOut) {
75          this.vIn = vIn;
76          try {
77              doExecute();
78          } catch (final Exception e) {
79              handleException(e);
80          }
81  
82          return "";
83      }
84  
85      protected final FTVAL getInputArgument(String name) {
86          return vIn == null ? null : vIn.getVal(name);
87      }
88  
89      protected final String getInputArgumentAsString(String name) {
90          return vIn == null ? null : vIn.getValString(name);
91      }
92  
93      /**
94       * Sends the http status code to the user-agent.
95       * 
96       * 
97       * @param code the http response code
98       * @return String to stream
99       */
100 
101     protected final String sendError(final int code, final Exception e) {
102         LOG.debug(code + " status code sent due to exception " + e.toString(), e);
103         if (LOG.isTraceEnabled()) {
104             DebugHelper.dumpVars(ics, LOG);
105         }
106         switch (code) { // all the http status codes, we may restrict the list
107         // to error and redirect
108             case HttpServletResponse.SC_ACCEPTED:
109             case HttpServletResponse.SC_BAD_GATEWAY:
110             case HttpServletResponse.SC_BAD_REQUEST:
111             case HttpServletResponse.SC_CONFLICT:
112             case HttpServletResponse.SC_CONTINUE:
113             case HttpServletResponse.SC_CREATED:
114             case HttpServletResponse.SC_EXPECTATION_FAILED:
115             case HttpServletResponse.SC_FORBIDDEN:
116             case HttpServletResponse.SC_FOUND:
117             case HttpServletResponse.SC_GATEWAY_TIMEOUT:
118             case HttpServletResponse.SC_GONE:
119             case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED:
120             case HttpServletResponse.SC_INTERNAL_SERVER_ERROR:
121             case HttpServletResponse.SC_LENGTH_REQUIRED:
122             case HttpServletResponse.SC_METHOD_NOT_ALLOWED:
123             case HttpServletResponse.SC_MOVED_PERMANENTLY:
124                 // case HttpServletResponse.SC_MOVED_TEMPORARILY : //SC_FOUND is
125                 // preferred
126             case HttpServletResponse.SC_MULTIPLE_CHOICES:
127             case HttpServletResponse.SC_NO_CONTENT:
128             case HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION:
129             case HttpServletResponse.SC_NOT_ACCEPTABLE:
130             case HttpServletResponse.SC_NOT_FOUND:
131             case HttpServletResponse.SC_NOT_IMPLEMENTED:
132             case HttpServletResponse.SC_NOT_MODIFIED:
133             case HttpServletResponse.SC_OK:
134             case HttpServletResponse.SC_PARTIAL_CONTENT:
135             case HttpServletResponse.SC_PAYMENT_REQUIRED:
136             case HttpServletResponse.SC_PRECONDITION_FAILED:
137             case HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED:
138             case HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE:
139             case HttpServletResponse.SC_REQUEST_TIMEOUT:
140             case HttpServletResponse.SC_REQUEST_URI_TOO_LONG:
141             case HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
142             case HttpServletResponse.SC_RESET_CONTENT:
143             case HttpServletResponse.SC_SEE_OTHER:
144             case HttpServletResponse.SC_SERVICE_UNAVAILABLE:
145             case HttpServletResponse.SC_SWITCHING_PROTOCOLS:
146             case HttpServletResponse.SC_TEMPORARY_REDIRECT:
147             case HttpServletResponse.SC_UNAUTHORIZED:
148             case HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE:
149             case HttpServletResponse.SC_USE_PROXY:
150                 ics.StreamHeader(STATUS_HEADER, Integer.toString(code));
151                 break;
152             default:
153                 ics.StreamHeader(STATUS_HEADER, "500");
154                 break;
155         }
156         Unknowndeps.unknonwDeps(ics);// failure case might be corrected on next
157         // publish or save
158         String element = null;
159 
160         if (goodString(ics.GetVar("site")) && ics.IsElement(ics.GetVar("site") + "/ErrorHandler/" + code)) {
161             element = ics.GetVar("site") + "/ErrorHandler/" + code;
162         } else if (ics.IsElement("GST/ErrorHandler/" + code)) {
163             element = "GST/ErrorHandler/" + code;
164         } else if (ics.IsElement("GST/ErrorHandler")) {
165             element = "GST/ErrorHandler";
166         }
167         if (element != null) {
168             ics.SetObj("com.fatwire.gst.foundation.exception", e);
169             ics.CallElement(element, null);
170             ics.SetObj("com.fatwire.gst.foundation.exception", null);
171         } else {
172             StringWriter sw = new StringWriter();
173             PrintWriter pw = new PrintWriter(sw);
174             e.printStackTrace(pw);
175             pw.flush();
176 
177             ics.StreamText("<h1>ERROR "+ code +"</h1><p>An error has been raised. <br/>Please add an element at GST/ErrorHandler to handle the display of this message differently.<br/></br><pre>"
178                     + sw.toString()
179                     + "</pre></p>");
180         }
181         ics.SetErrno(ftErrors.exceptionerr);
182 
183         return null;
184 
185     }
186 
187     /**
188      * Executes the core business logic of the controller.
189      * 
190      * @throws CSRuntimeException may throw a CSRuntimeException which is
191      *             handled by handleCSRuntimeException
192      */
193     abstract protected void doExecute();
194 
195     /**
196      * Handles the exception, doing what is required
197      * 
198      * @param e exception
199      */
200     abstract protected void handleException(Exception e);
201 }