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