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.vwebroot;
17  
18  import java.util.Comparator;
19  import java.util.SortedSet;
20  import java.util.TreeSet;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Interfaces.IList;
24  
25  import com.fatwire.assetapi.data.AssetData;
26  import com.fatwire.gst.foundation.facade.assetapi.AssetDataUtils;
27  import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
28  import com.fatwire.gst.foundation.facade.assetapi.AttributeDataUtils;
29  import com.fatwire.gst.foundation.facade.runtag.asset.AssetList;
30  import com.fatwire.gst.foundation.facade.sql.IListIterable;
31  import com.fatwire.gst.foundation.facade.sql.Row;
32  import com.fatwire.gst.foundation.wra.VanityAsset;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * DAO for working with Virtual Webroots
39   * 
40   * @author Tony Field
41   * @since Jul 22, 2010
42   */
43  public final class AssetApiVirtualWebrootDao implements VirtualWebrootDao {
44  
45      private static final Log LOG = LogFactory.getLog(AssetApiVirtualWebrootDao.class.getName());
46  
47      private final ICS ics;
48  
49      public AssetApiVirtualWebrootDao(ICS ics) {
50          this.ics = ics;
51      }
52  
53      public VirtualWebroot getVirtualWebroot(long cid) {
54          String sCid = Long.toString(cid);
55          if (LOG.isTraceEnabled())
56              LOG.trace("Loading virtual webroot data for for GSTVirtualWebroot:" + sCid);
57          AssetData ad = AssetDataUtils.getAssetData(ics, AssetIdUtils.createAssetId("GSTVirtualWebroot", sCid), "master_vwebroot", "env_vwebroot",
58                  "env_name");
59          return new VWebrootBeanImpl(cid, AttributeDataUtils.getWithFallback(ad, "master_vwebroot"),
60                  AttributeDataUtils.getWithFallback(ad, "env_vwebroot"), AttributeDataUtils.getWithFallback(ad,
61                          "env_name"));
62  
63      }
64  
65      /**
66       * Get all of the virtual webroots, sorted by URL length.
67       * 
68       * @return list of virtual webroots
69       */
70      public SortedSet<VirtualWebroot> getAllVirtualWebroots() {
71          AssetList al = new AssetList();
72          al.setExcludeVoided(true);
73          al.setList("pr-out");
74          al.setType("GSTVirtualWebroot");
75          ics.RegisterList("pr-out", null);
76          al.execute(ics);
77          IList ilist = ics.GetList("pr-out");
78          ics.RegisterList("pr-out", null);
79          if (ilist == null)
80              throw new IllegalStateException("No GSTVirtualWebroots are registered");
81  
82          SortedSet<VirtualWebroot> result = new TreeSet<VirtualWebroot>(new UrlInfoComparator());
83          for (Row r : new IListIterable(ilist)) {
84              result.add(getVirtualWebroot(r.getLong("id")));
85          }
86          return result;
87      }
88  
89      /**
90       * Get the current virtual webroot environment as defined by the
91       * configuration properties. Null indicates that none is configured.
92       * 
93       * @return virtual webroot environment or null if not set.
94       */
95      public String getVirtualWebrootEnvironment() {
96          String environmentName = System.getProperty("com.fatwire.gst.foundation.env-name", null);
97  
98          // avoid configuration problem trickery
99          if (environmentName != null) {
100             environmentName = environmentName.trim();
101             if (environmentName.length() == 0)
102                 environmentName = null;
103         }
104 
105         if (environmentName == null) {
106             // allow user to have accidentally mis-configured things
107             environmentName = ics.GetProperty("com.fatwire.gst.foundation.env-name");
108             // avoid configuration problem trickery
109             if (environmentName != null) {
110                 environmentName = environmentName.trim();
111                 if (environmentName.length() == 0)
112                     environmentName = null;
113             }
114         }
115         if (environmentName == null)
116             LOG.debug("Virtual webroot environment is not configured.");
117         return environmentName;
118     }
119 
120     /**
121      * Look up and return the VirtualWebroot corresponding to the specified
122      * WebReferenceableAsset, for the current environment. If the current
123      * environment is not configured, no match can be found.
124      * 
125      * @param wra web-referenceable asset
126      * @return matching VirtualWebroot or null if no match is found.
127      */
128     public VirtualWebroot lookupVirtualWebrootForAsset(VanityAsset wra) {
129         if (LOG.isDebugEnabled())
130             LOG.debug("Looking up virtual webroot for WRA " + wra.getId());
131         String wraPath = wra.getPath();
132         return lookupVirtualWebrootForUri(wraPath);
133     }
134 
135     public VirtualWebroot lookupVirtualWebrootForUri(String wraPath) {
136         if (wraPath == null) {
137             LOG.trace("WRA does not have a path set - cannot locate virtual webroot");
138             return null;
139         }
140         String env = getVirtualWebrootEnvironment();
141         if (env == null)
142             return null;
143         for (VirtualWebroot vw : getAllVirtualWebroots()) {
144             // find longest first one that is found in the prefix of path. that
145             // is virtual-webroot
146             // the path in the asset must start with the MASTER virtual webroot
147             // for this to work. This could
148             // be loosened up but there is no real reason to right now.
149             if (env.equals(vw.getEnvironmentName()) && wraPath.startsWith(vw.getMasterVirtualWebroot())) {
150                 return vw;
151             }
152         }
153         return null; // no match
154     }
155 
156     /**
157      * Comparator that compares virtual webroots by webroot. Uses
158      * reverse-natural ordering to ensure that overlapping virtual webroots
159      * resolve properly.
160      */
161     public static class UrlInfoComparator implements Comparator<VirtualWebroot> {
162 
163         public int compare(VirtualWebroot o1, VirtualWebroot o2) {
164             int i = -o1.getMasterVirtualWebroot().compareTo(o2.getMasterVirtualWebroot());
165             if (i == 0) {
166                 int j = -o1.getEnvironmentName().compareTo(o2.getEnvironmentName());
167                 if (j == 0) {
168                     int k = -o1.getEnvironmentVirtualWebroot().compareTo(o2.getEnvironmentVirtualWebroot());
169                     if (k == 0) {
170                         return (int) (o1.getId().getId() - o2.getId().getId());
171                     }
172                     return k;
173                 }
174                 return j;
175             }
176             return i;
177         }
178     }
179 }