1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39
40
41
42
43
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
61
62
63
64
65
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
82
83
84
85
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
110
111 @Override
112 protected void renderPage() {
113
114
115
116 String pa = unpackPackedArgs();
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
127
128 if (ics.GetVar(PubConstants.CHILDPAGENAME) != null) {
129
130 callPage(id, ics.GetVar(PubConstants.CHILDPAGENAME), pa);
131 } else {
132
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
144
145
146
147
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
156
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
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 }