1
19
20 package com.liferay.portlet.wsrp;
21
22 import com.liferay.portal.kernel.util.GetterUtil;
23 import com.liferay.util.Encryptor;
24 import com.liferay.util.axis.SimpleHTTPSender;
25
26 import java.security.Key;
27
28 import java.util.Iterator;
29 import java.util.Map;
30
31 import javax.portlet.PortletMode;
32 import javax.portlet.PortletURL;
33 import javax.portlet.RenderResponse;
34 import javax.portlet.WindowState;
35
36 import org.apache.wsrp4j.consumer.URLGenerator;
37 import org.apache.wsrp4j.producer.util.Base64;
38 import org.apache.wsrp4j.util.Constants;
39 import org.apache.wsrp4j.util.Modes;
40 import org.apache.wsrp4j.util.WindowStates;
41
42
48 public class URLGeneratorImpl implements URLGenerator {
49
50 public static String getResourceProxyURL(Map params, Key key) {
51 StringBuilder url = new StringBuilder();
52
53 url.append("/wsrp/resource_proxy/get");
54
55 if (params != null) {
56
57 String paramValue = GetterUtil.getString((String) params
58 .get(Constants.URL));
59
60 url.append(Constants.PARAMS_START);
61 url.append("url");
62 url.append(Constants.EQUALS);
63
64 try {
65 byte[] paramValueBytes =
66 Encryptor.encryptUnencoded(key, paramValue);
67
68 paramValue = Base64.encode(paramValueBytes);
69
70 url.append(paramValue);
71 }
72 catch (Exception e) {
73 e.printStackTrace();
74 }
75 }
76
77 String cookie = SimpleHTTPSender.getCurrentCookie();
78
79 try {
80 byte[] cookieBytes = Encryptor.encryptUnencoded(key, cookie);
81
82 cookie = Base64.encode(cookieBytes);
83 }
84 catch (Exception e) {
85 e.printStackTrace();
86 }
87
88 url.append(Constants.NEXT_PARAM);
89 url.append("cookie");
90 url.append(Constants.EQUALS);
91 url.append(cookie);
92
93 return url.toString();
94
95 }
96
97 public URLGeneratorImpl(RenderResponse response, Key key) {
98 _renderResponse = response;
99 _key = key;
100 }
101
102 public void setRenderResponse(RenderResponse response) {
103 if (response != null) {
104 this._renderResponse = response;
105 }
106 }
107
108 public void setConsumerParameters(Map consumerParameters) {
109 if (consumerParameters != null) {
110 this._consumerParameters = consumerParameters;
111 }
112 }
113
114 public String getBlockingActionURL(Map params) {
115 PortletURL url = _renderResponse.createActionURL();
116
117 if (params != null) {
118
119 Iterator iter = params.keySet().iterator();
120
121 String paramName = "";
122 String paramValue = "";
123
124 while (iter.hasNext()) {
125 paramName = (String) iter.next();
126
127 if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
128 if ((paramValue = (String) params.get(paramName)) != null) {
129
130 setWindowState(url, paramValue);
131
132 }
133 }
134 else if (paramName.equalsIgnoreCase(Constants.PORTLET_MODE)) {
135 if ((paramValue = (String) params.get(paramName)) != null) {
136
137 setPortletMode(url, paramValue);
138
139 }
140 }
141 else {
142 if ((paramValue = (String) params.get(paramName)) != null) {
143
144 url.setParameter(paramName, paramValue);
145
146 }
147 }
148 }
149 }
150
151 if (_consumerParameters != null) {
152
153 Iterator iter2 = _consumerParameters.keySet().iterator();
154 String name = null;
155 String value = null;
156
157 while (iter2.hasNext()) {
158
159 if ((value = (String) _consumerParameters.get(name)) != null) {
160 url.setParameter(name, value);
161 }
162 }
163 }
164
165 url.setParameter(WSRPProxyPortlet.REMOTE_INVOCATION, "true");
166
167 return url.toString();
168
169 }
170
171 public String getRenderURL(Map params) {
172
173 PortletURL url = _renderResponse.createRenderURL();
174
175 if (params != null) {
176
177 Iterator iter = params.keySet().iterator();
178
179 String paramName = "";
180 String paramValue = "";
181
182 while (iter.hasNext()) {
183 paramName = (String) iter.next();
184
185 if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
186 if ((paramValue = (String) params.get(paramName)) != null) {
187
188 setWindowState(url, paramValue);
189
190 }
191 }
192 else if (paramName.equalsIgnoreCase(Constants.PORTLET_MODE)) {
193 if ((paramValue = (String) params.get(paramName)) != null) {
194
195 setPortletMode(url, paramValue);
196
197 }
198 }
199 else {
200 if (!paramName.equalsIgnoreCase(Constants.URL_TYPE)
201 && (paramValue = (String) params.get(paramName)) != null) {
202
203 url.setParameter(paramName, paramValue);
204
205 }
206 }
207 }
208 }
209
210 if (_consumerParameters != null) {
211
212 Iterator iter2 = _consumerParameters.keySet().iterator();
213 String name = null;
214 String value = null;
215
216 while (iter2.hasNext()) {
217
218 if ((value = (String) _consumerParameters.get(name)) != null) {
219 url.setParameter(name, value);
220 }
221 }
222 }
223
224 url.setParameter(WSRPProxyPortlet.REMOTE_INVOCATION, "true");
225
226 return url.toString();
227
228 }
229
230 public String getResourceURL(Map params) {
231 return getResourceProxyURL(params, _key);
232 }
233
234 public String getNamespacedToken(String token) {
235 return _renderResponse.getNamespace();
236 }
237
238
241 private void setWindowState(PortletURL url, String windowState) {
242
243 try {
244 if (windowState.equalsIgnoreCase(WindowStates._maximized)) {
245
246 url.setWindowState(WindowState.MAXIMIZED);
247
248 }
249 else if (windowState.equalsIgnoreCase(WindowStates._minimized)) {
250
251 url.setWindowState(WindowState.MINIMIZED);
252
253 }
254 else if (windowState.equalsIgnoreCase(WindowStates._normal)) {
255
256 url.setWindowState(WindowState.NORMAL);
257
258 }
259 else if (windowState.equalsIgnoreCase(WindowStates._solo)) {
260
261 url.setWindowState(WindowState.MAXIMIZED);
262
263 }
264 }
265 catch (Exception e) {
266 e.printStackTrace();
267 }
268
269 }
270
271
274 private void setPortletMode(PortletURL url, String mode) {
275
276 try {
277 if (mode.equalsIgnoreCase(Modes._edit)) {
278
279 url.setPortletMode(PortletMode.EDIT);
280
281 }
282 else if (mode.equalsIgnoreCase(Modes._view)) {
283
284 url.setPortletMode(PortletMode.VIEW);
285
286 }
287 else if (mode.equalsIgnoreCase(Modes._help)) {
288
289 url.setPortletMode(PortletMode.HELP);
290
291 }
292 else if (mode.equalsIgnoreCase(Modes._preview)) {
293
294 url.setPortletMode(PortletMode.VIEW);
295
296 }
297 }
298 catch (Exception e) {
299 e.printStackTrace();
300 }
301
302 }
303
304 private static URLGeneratorImpl _instance = null;
305
306 private RenderResponse _renderResponse = null;
307
308 private Map _consumerParameters = null;
309
310 private Key _key = null;
311
312 }