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.util.Map;
21  
22  import COM.FutureTense.Util.ftErrors;
23  
24  import com.fatwire.gst.foundation.CSRuntimeException;
25  import com.fatwire.gst.foundation.url.WraPathTranslationService;
26  import com.fatwire.gst.foundation.wra.Alias;
27  import com.fatwire.gst.foundation.wra.AliasCoreFieldDao;
28  import com.fatwire.gst.foundation.wra.VanityAsset;
29  import com.fatwire.gst.foundation.wra.VanityAssetBean;
30  import com.fatwire.gst.foundation.wra.WraCoreFieldDao;
31  import com.openmarket.xcelerate.publish.PubConstants;
32  
33  /**
34   * 
35   * The BaseRenderPage class in the implementation that renders an asset. It's
36   * intended use is to be called from a Controller (outer wrapper).
37   * <p>
38   * For the passing of arguments it makes heavy use of page criteria on the
39   * calling Template
40   * 
41   * @author Tony Field
42   * @author Dolf Dijkstra
43   * @since June 2010
44   * 
45   */
46  public class WraRenderPage extends BaseRenderPage {
47      public static final String URL_PATH = "url-path";
48  
49      public static final String VIRTUAL_WEBROOT = "virtual-webroot";
50  
51      protected WraPathTranslationService pathTranslationService;
52      protected WraCoreFieldDao wraCoreFieldDao;
53      protected AliasCoreFieldDao aliasCoreFieldDao;
54  
55      public WraRenderPage() {
56          super();
57      }
58  
59      /**
60       * Add p to the input parameters, if it is known or knowable. First check to
61       * see if it has been explicitly set, then look it up if it hasn't been. The
62       * variable is not guaranteed to be found.
63       * 
64       * @param id asset id with site
65       * @param arguments calltemplate arguments
66       */
67      @Override
68      protected final void findAndSetP(final AssetIdWithSite id, final Map<String, String> arguments) {
69          final String pVar = ics.GetVar("p");
70          if (pVar != null && pVar.length() > 0) {
71              arguments.put("p", pVar);
72          } else {
73              final long p = wraCoreFieldDao.findP(id);
74              if (p > 0L) {
75                  arguments.put("p", Long.toString(p));
76              }
77          }
78      }
79  
80      /**
81       * Load the WRA, or the alias, and return it for use by the controller. If
82       * the asset is not found, an exception is thrown
83       * 
84       * @param id asset id
85       * @return WRA, never null. May be an instance of an Alias
86       */
87      protected VanityAsset getWraAndResolveAlias(AssetIdWithSite id) {
88          try {
89              if (Alias.ALIAS_ASSET_TYPE_NAME.equals(id.getType())) {
90                  if (LOG.isTraceEnabled())
91                      LOG.trace("Loading alias: " + id);
92                  Alias alias = aliasCoreFieldDao.getAlias(id);
93                  VanityAssetBean wra = new VanityAssetBean(alias);
94  
95                  if (LOG.isDebugEnabled())
96                      LOG.debug("Loaded alias: " + id + " which resolved to " + wra.getId());
97                  return wra;
98              } else {
99                  if (LOG.isTraceEnabled())
100                     LOG.trace("Loading wra: " + id);
101                 return wraCoreFieldDao.getVanityWra(id);
102             }
103         } catch (IllegalArgumentException e) {
104             throw new CSRuntimeException("Web-Referenceable Asset " + id + " is not valid", ftErrors.pagenotfound);
105         }
106     }
107 
108     /**
109      * This method is the entry of the class, it does the rendering of the page.
110      */
111     @Override
112     protected void renderPage() {
113         // the preview code path is adding all the additional arguments in
114         // packedargs.
115         // unpack here so we can use
116         String pa = unpackPackedArgs();//ics.GetVar(PubConstants.PACKEDARGS);
117         
118         final AssetIdWithSite id = resolveAssetId();
119         if (id == null || id.getSite() == null) {
120             throw new CSRuntimeException("Asset or site not found: '" + id + "' for url " + ics.pageURL(),
121                     ftErrors.pagenotfound);
122         }
123         if (LOG.isDebugEnabled())
124             LOG.debug("RenderPage found a valid asset and site: " + id);
125 
126         // if childpagename is passed in (preview code path) we use this and do
127         // a render:satellitepage
128         if (ics.GetVar(PubConstants.CHILDPAGENAME) != null) {
129             // preview UI support
130             callPage(id, ics.GetVar(PubConstants.CHILDPAGENAME), pa);
131         } else {
132             // alternatively, render the live logic.
133             VanityAsset wra = getWraAndResolveAlias(id);
134 
135             callTemplate(new AssetIdWithSite(wra.getId().getType(), wra.getId().getId(), id.getSite()),
136                     wra.getTemplate());
137 
138         }
139         LOG.debug("WraRenderPage execution complete");
140     }
141 
142     /**
143      * In this method the AssetId and the site are resolved. Based on either
144      * c/cid or the virtual-webroot/url-path arguments the site which this asset
145      * belongs to is discovered from the AssetPublication table.
146      * 
147      * @return the AssetId and the site that the asset belongs to.
148      */
149     @Override
150     protected AssetIdWithSite resolveAssetId() {
151         final AssetIdWithSite id;
152         if (goodString(ics.GetVar(VIRTUAL_WEBROOT)) && goodString(ics.GetVar(URL_PATH))) {
153             id = pathTranslationService.resolveAsset(ics.GetVar(VIRTUAL_WEBROOT), ics.GetVar(URL_PATH));
154         } else if (goodString(ics.GetVar("c")) && goodString(ics.GetVar("cid"))) {
155             // handle these to be nice
156             // Look up site because we can't trust the wrapper's resarg.
157             String site = wraCoreFieldDao.resolveSite(ics.GetVar("c"), ics.GetVar("cid"));
158 
159             if (site == null)
160                 throw new CSRuntimeException("No site found for asset (" + ics.GetVar("c") + ":" + ics.GetVar("cid")
161                         + " ).", ftErrors.pagenotfound);
162 
163             id = new AssetIdWithSite(ics.GetVar("c"), Long.parseLong(ics.GetVar("cid")), site);
164         } else if (goodString(ics.GetVar(VIRTUAL_WEBROOT)) || goodString(ics.GetVar(URL_PATH))) {
165             // (but not both)
166             throw new CSRuntimeException("Missing required param virtual-webroot & url-path.", ftErrors.pagenotfound);
167         } else {
168             throw new CSRuntimeException("Missing required param c, cid.", ftErrors.pagenotfound);
169         }
170         return id;
171     }
172 
173 }