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 javax.servlet.http.HttpServletResponse;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Util.ftErrors;
24  
25  import com.fatwire.gst.foundation.CSRuntimeException;
26  import com.fatwire.gst.foundation.DebugHelper;
27  import com.fatwire.gst.foundation.facade.runtag.render.Unknowndeps;
28  import com.fatwire.gst.foundation.httpstatus.HttpStatusStrings;
29  import com.fatwire.gst.foundation.url.WraPathTranslationService;
30  import com.fatwire.gst.foundation.url.db.UrlRegistry2;
31  import com.fatwire.gst.foundation.wra.AliasCoreFieldDao;
32  import com.fatwire.gst.foundation.wra.AssetApiAliasCoreFieldDao;
33  import com.fatwire.gst.foundation.wra.AssetApiWraCoreFieldDao;
34  import com.fatwire.gst.foundation.wra.WraCoreFieldDao;
35  
36  /**
37   * <p>
38   * This an adapter so that Controllers and Actions can call the RenderPage
39   * logic. The RenderPage logic dispatches the request to the correct
40   * asset/template combination based on the path field for a Web Referenceable
41   * Asset.
42   * <p/>
43   * 
44   * 
45   * @author Tony Field
46   * @author Dolf Dijkstra
47   * @since Jun 10, 2010
48   */
49  public class RenderPageAdapter extends WraRenderPage {
50  
51      public static final String STATUS_HEADER = HttpStatusStrings.X_FATWIRE_STATUS;
52  
53      public RenderPageAdapter(ICS ics) {
54          this.ics = ics;
55          pathTranslationService = UrlRegistry2.lookup(ics);
56          wraCoreFieldDao = new AssetApiWraCoreFieldDao(ics);
57          aliasCoreFieldDao = new AssetApiAliasCoreFieldDao(ics, wraCoreFieldDao);
58      }
59  
60      public RenderPageAdapter(ICS ics, WraPathTranslationService pathTranslationService,
61              WraCoreFieldDao wraCoreFieldDao, AliasCoreFieldDao aliasCoreFieldDao) {
62          super();
63          this.ics = ics;
64          this.pathTranslationService = pathTranslationService;
65          this.wraCoreFieldDao = wraCoreFieldDao;
66          this.aliasCoreFieldDao = aliasCoreFieldDao;
67      }
68  
69      public void doExecute() {
70          LOG.trace("RenderPageAdapter execution started");
71          recordCompositionalDependencies();
72          renderPage();
73          LOG.trace("RenderPageAdapter execution complete");
74      }
75  
76      protected void handleException(final Exception e) {
77          if (e instanceof CSRuntimeException) {
78              handleCSRuntimeException((CSRuntimeException) e);
79          } else {
80              sendError(500, e);
81          }
82      }
83  
84      protected final String sendError(final int code, final Exception e) {
85          LOG.debug(code + " status code sent due to exception " + e.toString(), e);
86          if (LOG.isTraceEnabled()) {
87              DebugHelper.dumpVars(ics, LOG);
88          }
89          switch (code) { // all the http status codes, we may restrict the list
90          // to error and redirect
91              case HttpServletResponse.SC_ACCEPTED:
92              case HttpServletResponse.SC_BAD_GATEWAY:
93              case HttpServletResponse.SC_BAD_REQUEST:
94              case HttpServletResponse.SC_CONFLICT:
95              case HttpServletResponse.SC_CONTINUE:
96              case HttpServletResponse.SC_CREATED:
97              case HttpServletResponse.SC_EXPECTATION_FAILED:
98              case HttpServletResponse.SC_FORBIDDEN:
99              case HttpServletResponse.SC_FOUND:
100             case HttpServletResponse.SC_GATEWAY_TIMEOUT:
101             case HttpServletResponse.SC_GONE:
102             case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED:
103             case HttpServletResponse.SC_INTERNAL_SERVER_ERROR:
104             case HttpServletResponse.SC_LENGTH_REQUIRED:
105             case HttpServletResponse.SC_METHOD_NOT_ALLOWED:
106             case HttpServletResponse.SC_MOVED_PERMANENTLY:
107                 // case HttpServletResponse.SC_MOVED_TEMPORARILY : //SC_FOUND is
108                 // preferred
109             case HttpServletResponse.SC_MULTIPLE_CHOICES:
110             case HttpServletResponse.SC_NO_CONTENT:
111             case HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION:
112             case HttpServletResponse.SC_NOT_ACCEPTABLE:
113             case HttpServletResponse.SC_NOT_FOUND:
114             case HttpServletResponse.SC_NOT_IMPLEMENTED:
115             case HttpServletResponse.SC_NOT_MODIFIED:
116             case HttpServletResponse.SC_OK:
117             case HttpServletResponse.SC_PARTIAL_CONTENT:
118             case HttpServletResponse.SC_PAYMENT_REQUIRED:
119             case HttpServletResponse.SC_PRECONDITION_FAILED:
120             case HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED:
121             case HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE:
122             case HttpServletResponse.SC_REQUEST_TIMEOUT:
123             case HttpServletResponse.SC_REQUEST_URI_TOO_LONG:
124             case HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
125             case HttpServletResponse.SC_RESET_CONTENT:
126             case HttpServletResponse.SC_SEE_OTHER:
127             case HttpServletResponse.SC_SERVICE_UNAVAILABLE:
128             case HttpServletResponse.SC_SWITCHING_PROTOCOLS:
129             case HttpServletResponse.SC_TEMPORARY_REDIRECT:
130             case HttpServletResponse.SC_UNAUTHORIZED:
131             case HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE:
132             case HttpServletResponse.SC_USE_PROXY:
133                 ics.StreamHeader(STATUS_HEADER, Integer.toString(code));
134                 break;
135             default:
136                 ics.StreamHeader(STATUS_HEADER, "500");
137                 break;
138         }
139         Unknowndeps.unknonwDeps(ics);// failure case might be corrected on next
140         // publish or save
141         renderErrorPage(code, e);
142 
143         return null;
144 
145     }
146 
147     /**
148      * Renders the error page. This method can be overwritten is other elements
149      * need to be invoked to render an error page.
150      * 
151      * @param code
152      * @param e
153      */
154     protected void renderErrorPage(final int code, final Exception e) {
155         String element = null;
156 
157         if (goodString(ics.GetVar("site")) && ics.IsElement(ics.GetVar("site") + "/ErrorHandler/" + code)) {
158             element = ics.GetVar("site") + "/ErrorHandler/" + code;
159         } else if (ics.IsElement("GST/ErrorHandler/" + code)) {
160             element = "GST/ErrorHandler/" + code;
161         } else if (ics.IsElement("GST/ErrorHandler")) {
162             element = "GST/ErrorHandler";
163         }
164         if (element != null) {
165             ics.SetObj("com.fatwire.gst.foundation.exception", e);
166             ics.CallElement(element, null);
167         }
168         ics.SetErrno(ftErrors.exceptionerr);
169     }
170 
171     /**
172      * This method transforms errno values into http status codes and sets them
173      * using the X-Fatwire-Status header.
174      * <p/>
175      * Only some errnos are handled by this base class.
176      * <p/>
177      * More info coming soon
178      * 
179      * @param e exception
180      */
181     protected void handleCSRuntimeException(final CSRuntimeException e) {
182         switch (e.getErrno()) {
183             case 400:
184             case ftErrors.badparams:
185                 sendError(400, e);
186                 break;
187             case 404:
188             case ftErrors.pagenotfound:
189                 sendError(404, e);
190                 break;
191             case 403:
192             case ftErrors.noprivs:
193                 sendError(403, e);
194                 break;
195             default:
196                 sendError(500, e);
197                 break;
198         }
199     }
200 
201 }