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.url;
17  
18  import java.util.Map;
19  
20  import COM.FutureTense.Export.ReferenceException;
21  import COM.FutureTense.Interfaces.ICS;
22  import COM.FutureTense.Util.ftMessage;
23  
24  import com.fatwire.assetapi.data.AssetId;
25  import com.fatwire.gst.foundation.vwebroot.AssetApiVirtualWebrootDao;
26  import com.fatwire.gst.foundation.vwebroot.VirtualWebroot;
27  import com.fatwire.gst.foundation.wra.AssetApiWraCoreFieldDao;
28  import com.fatwire.gst.foundation.wra.VanityAsset;
29  import com.fatwire.gst.foundation.wra.WraCoreFieldDao;
30  import com.openmarket.xcelerate.asset.AssetIdImpl;
31  import com.openmarket.xcelerate.publish.PageRef;
32  import com.openmarket.xcelerate.publish.PubConstants;
33  
34  import org.apache.commons.lang.StringUtils;
35  
36  import static COM.FutureTense.Interfaces.Utilities.goodString;
37  
38  public class WraPagePreviewReference extends PageRef {
39  
40      public static final String GST_DISPATCHER = "GST/Dispatcher";
41      
42  
43      @SuppressWarnings({ "rawtypes", "unchecked" })
44      @Override
45      public void setParameters(Map args, ICS ics) throws ReferenceException {
46  
47          // no processing to do if not serving a page for SS
48          if (shouldModify(args, ics)) {
49              AssetId id = new AssetIdImpl((String) args.get("c"), Long.parseLong((String) args.get("cid")));
50              AssetApiVirtualWebrootDao vwDao = new AssetApiVirtualWebrootDao(ics);
51              WraCoreFieldDao wraDao = new AssetApiWraCoreFieldDao(ics);
52              String currentEnvironment = vwDao.getVirtualWebrootEnvironment();
53              // only look up webroots for WRAs when the environment is configured
54              if (currentEnvironment != null && wraDao.isVanityAsset(id)) {
55                   VanityAsset wra = wraDao.getVanityWra(id);
56                  // get the webroot
57                  VirtualWebroot vw = vwDao.lookupVirtualWebrootForAsset(wra);
58                  if (vw != null) {
59                      // set the special fields
60                      args.put("virtual-webroot", vw.getEnvironmentVirtualWebroot());
61                      args.put("url-path", wra.getPath().substring(vw.getMasterVirtualWebroot().length()));
62                      // has pagename been set? if not, use default.
63                      String pagename = ics.GetProperty(WraPathAssembler.DISPATCHER_PROPNAME,
64                              "ServletRequest.properties", true);
65                      if (!goodString(pagename)) {
66                          pagename = GST_DISPATCHER;
67                      }
68                      // pagename or wrapperpage depending on whether or not we're
69                      // going to use a wrapper.
70                      if (args.get(PubConstants.WRAPPERPAGE) != null)
71                          args.put(PubConstants.WRAPPERPAGE, pagename);
72                      else
73                          args.put("pagename", pagename);
74                  } else {
75                      if (log.isDebugEnabled()) {
76                          log.debug("Not adding WRAPathAssembler args because no matching virtual webroot found for path "
77                                  + wra.getPath() + " and environemnt " + currentEnvironment);
78                      }
79                  }
80              } else {
81                  if (log.isDebugEnabled()) {
82                      if (currentEnvironment == null) {
83                          log.debug("Not adding WraPathAssembler args because virtual webroot environment is not configured");
84                      } else {
85                          log.debug("Not adding WraPathAssembler args because asset " + id + " is not web referenceable.");
86                      }
87                  }
88              }
89          } else {
90              if (log.isDebugEnabled()) {
91                  log.debug("Not adding WRAPathAssembler args because context is not satellite server (it is "
92                          + getSatelliteContext() + ").  Args: " + args);
93              }
94          }
95          super.setParameters(args, ics);
96      }
97  
98      protected boolean shouldModify(Map<String, String> args, ICS ics) {
99          return isRendermodeLive(ics) && getSatelliteContext() == SatelliteContext.SATELLITE_SERVER
100                 && isGetTemplateUrl(args);
101     }
102 
103     protected boolean isRendermodeLive(ICS ics) {
104         String rendermode = ics.GetVar(PubConstants.RENDERMODE);
105         return StringUtils.isBlank(rendermode) || "live".equals(rendermode);
106     }
107 
108     /**
109      * Check to see if the tag being called is a getTemplateUrl tag. If it is
110      * not, we should not be processing this for special links. Note it's not
111      * that easy to figure this out, and there could be missing pieces here.
112      * 
113      * @param args tag args
114      * @return true if it's a gettemplateurl tag, false otherwise.
115      */
116     private boolean isGetTemplateUrl(Map<String, String> args) {
117         if (args.get("c") == null)
118             return false;
119         if (args.get("cid") == null)
120             return false;
121         String pagename = args.get(ftMessage.PageName);
122         if (pagename == null)
123             return false;
124         if (pagename.split("/").length < 2)
125             return false; // need site/type/tname or site/tname at least for a
126                           // valid URL
127         if (args.get(PubConstants.WRAPPERPAGE) != null)
128             return true; // wrapper is only supported for GTU calls
129         else {
130             // possible further checks here just in case
131         }
132         return true;
133     }
134 }