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.ICS;
29  import COM.FutureTense.Util.ftErrors;
30  
31  import com.fatwire.gst.foundation.CSRuntimeException;
32  import com.fatwire.gst.foundation.DebugHelper;
33  import com.fatwire.gst.foundation.facade.runtag.render.Unknowndeps;
34  import com.fatwire.gst.foundation.httpstatus.HttpStatusStrings;
35  
36  /**
37   * @author Tony Field
38   * @author Dolf Dijkstra
39   * @since Jun 16, 2010
40   */
41  
42  public abstract class AbstractController  {
43      protected static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.controller");
44  
45      public static final String STATUS_HEADER = HttpStatusStrings.X_FATWIRE_STATUS;
46  
47      protected final ICS ics;
48  
49  
50      /**
51       * @param ics
52       */
53      public AbstractController(ICS ics) {
54          super();
55          this.ics = ics;
56      }
57  
58      /**
59       * Sends the http status code to the user-agent.
60       * 
61       * 
62       * @param code the http response code
63       * @return String to stream
64       */
65  
66      protected final String sendError(final int code, final Exception e) {
67          LOG.debug(code + " status code sent due to exception " + e.toString(), e);
68          if (LOG.isTraceEnabled()) {
69              DebugHelper.dumpVars(ics, LOG);
70          }
71          switch (code) { // all the http status codes, we may restrict the list
72          // to error and redirect
73              case HttpServletResponse.SC_ACCEPTED:
74              case HttpServletResponse.SC_BAD_GATEWAY:
75              case HttpServletResponse.SC_BAD_REQUEST:
76              case HttpServletResponse.SC_CONFLICT:
77              case HttpServletResponse.SC_CONTINUE:
78              case HttpServletResponse.SC_CREATED:
79              case HttpServletResponse.SC_EXPECTATION_FAILED:
80              case HttpServletResponse.SC_FORBIDDEN:
81              case HttpServletResponse.SC_FOUND:
82              case HttpServletResponse.SC_GATEWAY_TIMEOUT:
83              case HttpServletResponse.SC_GONE:
84              case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED:
85              case HttpServletResponse.SC_INTERNAL_SERVER_ERROR:
86              case HttpServletResponse.SC_LENGTH_REQUIRED:
87              case HttpServletResponse.SC_METHOD_NOT_ALLOWED:
88              case HttpServletResponse.SC_MOVED_PERMANENTLY:
89                  // case HttpServletResponse.SC_MOVED_TEMPORARILY : //SC_FOUND is
90                  // preferred
91              case HttpServletResponse.SC_MULTIPLE_CHOICES:
92              case HttpServletResponse.SC_NO_CONTENT:
93              case HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION:
94              case HttpServletResponse.SC_NOT_ACCEPTABLE:
95              case HttpServletResponse.SC_NOT_FOUND:
96              case HttpServletResponse.SC_NOT_IMPLEMENTED:
97              case HttpServletResponse.SC_NOT_MODIFIED:
98              case HttpServletResponse.SC_OK:
99              case HttpServletResponse.SC_PARTIAL_CONTENT:
100             case HttpServletResponse.SC_PAYMENT_REQUIRED:
101             case HttpServletResponse.SC_PRECONDITION_FAILED:
102             case HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED:
103             case HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE:
104             case HttpServletResponse.SC_REQUEST_TIMEOUT:
105             case HttpServletResponse.SC_REQUEST_URI_TOO_LONG:
106             case HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
107             case HttpServletResponse.SC_RESET_CONTENT:
108             case HttpServletResponse.SC_SEE_OTHER:
109             case HttpServletResponse.SC_SERVICE_UNAVAILABLE:
110             case HttpServletResponse.SC_SWITCHING_PROTOCOLS:
111             case HttpServletResponse.SC_TEMPORARY_REDIRECT:
112             case HttpServletResponse.SC_UNAUTHORIZED:
113             case HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE:
114             case HttpServletResponse.SC_USE_PROXY:
115                 ics.StreamHeader(STATUS_HEADER, Integer.toString(code));
116                 break;
117             default:
118                 ics.StreamHeader(STATUS_HEADER, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
119                 break;
120         }
121         Unknowndeps.unknonwDeps(ics);// failure case might be corrected on next
122         // publish or save
123         String element = null;
124 
125         if (goodString(ics.GetVar("site")) && ics.IsElement(ics.GetVar("site") + "/ErrorHandler/" + code)) {
126             element = ics.GetVar("site") + "/ErrorHandler/" + code;
127         } else if (ics.IsElement("GST/ErrorHandler/" + code)) {
128             element = "GST/ErrorHandler/" + code;
129         } else if (ics.IsElement("GST/ErrorHandler")) {
130             element = "GST/ErrorHandler";
131         }
132         if (element != null) {
133             ics.SetObj("com.fatwire.gst.foundation.exception", e);
134             ics.CallElement(element, null);
135             ics.SetObj("com.fatwire.gst.foundation.exception", null);
136         } else {
137             StringWriter sw = new StringWriter();
138             PrintWriter pw = new PrintWriter(sw);
139             e.printStackTrace(pw);
140             pw.flush();
141 
142             ics.StreamText("<h1>ERROR "
143                     + code
144                     + "</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>"
145                     + sw.toString() + "</pre></p>");
146         }
147         ics.SetErrno(ftErrors.exceptionerr);
148 
149         return null;
150 
151     }
152 
153     /**
154      * Executes the core business logic of the controller.
155      * 
156      * @throws CSRuntimeException may throw a CSRuntimeException which is
157      *             handled by handleCSRuntimeException
158      */
159     abstract protected void doExecute();
160 
161     /**
162      * Handles the exception, doing what is required
163      * 
164      * @param e exception
165      */
166     abstract protected void handleException(Exception e);
167 }