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.net.URI;
19  import java.net.URISyntaxException;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Properties;
23  
24  import com.fatwire.cs.core.uri.Assembler;
25  import com.fatwire.cs.core.uri.Definition;
26  import com.fatwire.cs.core.uri.QueryAssembler;
27  import com.fatwire.cs.core.uri.Simple;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * A dispatching assembler, dispatching to Web-referenceable asset path
35   * assembler only invoked if rendermode=live of blank, in other cases to the
36   * QueryAssembler.
37   * 
38   * @author Dolf Dijkstra
39   * @since November 16,2011
40   */
41  public final class WraPathPreviewAssembler extends LightweightAbstractAssembler {
42      protected static final Log LOG = LogFactory.getLog(WraPathPreviewAssembler.class.getName());
43      /**
44       * Name of query string parameter for virtual webroot
45       */
46      private static final String VIRTUAL_WEBROOT = "virtual-webroot";
47  
48      /**
49       * Name of query string parameter for url-path
50       */
51      private static final String URL_PATH = "url-path";
52  
53      /**
54       * Name of packedargs param
55       */
56      private static final String PACKEDARGS = "packedargs";
57  
58      private static final Collection<String> BAD_ARGS = Arrays.asList(VIRTUAL_WEBROOT, URL_PATH);
59      /**
60       * The assembler to use in case the input does not support the WRAPath
61       * approach
62       */
63      private Assembler queryAssembler;
64  
65      private Assembler wraPathAssembler;
66  
67      /**
68       * Set properties, initializing the assembler
69       * 
70       * @param properties configuration properties
71       */
72      public void setProperties(Properties properties) {
73          wraPathAssembler = _instantiateAssembler(WraPathAssembler.class);
74          wraPathAssembler.setProperties(properties);
75  
76          queryAssembler = _instantiateAssembler(QueryAssembler.class);
77          queryAssembler.setProperties(properties);
78  
79      }
80  
81      private Assembler _instantiateAssembler(Class<?> c) {
82          try {
83              Object o = c.newInstance();
84              return (Assembler) o;
85  
86          } catch (InstantiationException e) {
87              throw new IllegalStateException("Could not instantiate assembler: " + c.getName(), e);
88          } catch (IllegalAccessException e) {
89              throw new IllegalStateException("Could not instantiate assembler: " + c.getName(), e);
90          } catch (ClassCastException e) {
91              throw new IllegalArgumentException("Assembler class is not an instance of Assembler: " + c.getName(), e);
92          }
93      }
94  
95      /**
96       * Looks for virtual-webroot and url-path. If found, concatenates
97       * virtual-webroot and url-path. Once core query params are suppressed, the
98       * remaining params are appended to the URL.
99       * 
100      * @param definition
101      * @return valid URI
102      * @throws URISyntaxException
103      */
104     public URI assemble(Definition definition) throws URISyntaxException {
105         String rendermode = definition.getParameter("rendermode");
106         LOG.trace("rendermode: " + rendermode);
107         if (StringUtils.isBlank(rendermode) || "live".equals(rendermode)) {
108             // rendermode is not set or live
109             return wraPathAssembler.assemble(definition);
110         }
111         // preview etc
112         Simple copy = new Simple(definition.sessionEncode(), definition.getSatelliteContext(),
113                 definition.getContainerType(), definition.getScheme(), definition.getAuthority(),
114                 definition.getAppType(), definition.getFragment());
115         for (Object o : definition.getParameterNames()) {
116             String key = (String) o;
117 
118             if (PACKEDARGS.equals(key)) {
119                 String[] pa = excludeFromPackedargs(definition.getParameters(PACKEDARGS), BAD_ARGS);
120                 if (pa != null && pa.length > 0 && StringUtils.isNotBlank(pa[0])) {
121                     copy.setQueryStringParameter(PACKEDARGS, pa);
122                 }
123             } else if (VIRTUAL_WEBROOT.equals(key)) {
124                 // ignore
125             } else if (URL_PATH.equals(key)) {
126                 // ignore
127             } else {
128                 copy.setQueryStringParameter(key, definition.getParameters(key));
129             }
130 
131         }
132 
133         return queryAssembler.assemble(copy);
134     }
135 
136     public Definition disassemble(URI uri, Definition.ContainerType containerType) throws URISyntaxException {
137         return wraPathAssembler.disassemble(uri, containerType);
138     }
139 
140     public Assembler getTheBackupAssembler() {
141         return queryAssembler;
142     }
143 }