1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wsrp;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.util.Encryptor;
28  import com.liferay.util.axis.SimpleHTTPSender;
29  
30  import java.security.Key;
31  
32  import java.util.Iterator;
33  import java.util.Map;
34  
35  import javax.portlet.PortletMode;
36  import javax.portlet.PortletURL;
37  import javax.portlet.RenderResponse;
38  import javax.portlet.WindowState;
39  
40  import org.apache.wsrp4j.consumer.URLGenerator;
41  import org.apache.wsrp4j.producer.util.Base64;
42  import org.apache.wsrp4j.util.Constants;
43  import org.apache.wsrp4j.util.Modes;
44  import org.apache.wsrp4j.util.WindowStates;
45  
46  /**
47   * <a href="URLGeneratorImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Michael Young
50   *
51   */
52  public class URLGeneratorImpl implements URLGenerator {
53  
54      public static String getResourceProxyURL(Map params, Key key) {
55          StringMaker url = new StringMaker();
56  
57          url.append("/wsrp/resource_proxy/get");
58  
59          if (params != null) {
60  
61              String paramValue = GetterUtil.getString((String) params
62                      .get(Constants.URL));
63  
64              url.append(Constants.PARAMS_START);
65              url.append("url");
66              url.append(Constants.EQUALS);
67  
68              try {
69                  byte[] paramValueBytes = Encryptor.encryptRaw(key, paramValue);
70  
71                  paramValue = Base64.encode(paramValueBytes);
72  
73                  url.append(paramValue);
74              }
75              catch (Exception e) {
76                  e.printStackTrace();
77              }
78          }
79  
80          String cookie = SimpleHTTPSender.getCurrentCookie();
81  
82          try {
83              byte[] cookieBytes = Encryptor.encryptRaw(key, cookie);
84  
85              cookie = Base64.encode(cookieBytes);
86          }
87          catch (Exception e) {
88              e.printStackTrace();
89          }
90  
91          url.append(Constants.NEXT_PARAM);
92          url.append("cookie");
93          url.append(Constants.EQUALS);
94          url.append(cookie);
95  
96          return url.toString();
97  
98      }
99  
100     public URLGeneratorImpl(RenderResponse response, Key key) {
101         _renderResponse = response;
102         _key = key;
103     }
104 
105     public void setRenderResponse(RenderResponse response) {
106         if (response != null) {
107             this._renderResponse = response;
108         }
109     }
110 
111     public void setConsumerParameters(Map consumerParameters) {
112         if (consumerParameters != null) {
113             this._consumerParameters = consumerParameters;
114         }
115     }
116 
117     public String getBlockingActionURL(Map params) {
118         PortletURL url = _renderResponse.createActionURL();
119 
120         if (params != null) {
121 
122             Iterator iter = params.keySet().iterator();
123 
124             String paramName = "";
125             String paramValue = "";
126 
127             while (iter.hasNext()) {
128                 paramName = (String) iter.next();
129 
130                 if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
131                     if ((paramValue = (String) params.get(paramName)) != null) {
132 
133                         setWindowState(url, paramValue);
134 
135                     }
136                 }
137                 else if (paramName.equalsIgnoreCase(Constants.PORTLET_MODE)) {
138                     if ((paramValue = (String) params.get(paramName)) != null) {
139 
140                         setPortletMode(url, paramValue);
141 
142                     }
143                 }
144                 else {
145                     if ((paramValue = (String) params.get(paramName)) != null) {
146 
147                         url.setParameter(paramName, paramValue);
148 
149                     }
150                 }
151             }
152         }
153 
154         if (_consumerParameters != null) {
155 
156             Iterator iter2 = _consumerParameters.keySet().iterator();
157             String name = null;
158             String value = null;
159 
160             while (iter2.hasNext()) {
161 
162                 if ((value = (String) _consumerParameters.get(name)) != null) {
163                     url.setParameter(name, value);
164                 }
165             }
166         }
167 
168         url.setParameter(WSRPProxyPortlet.REMOTE_INVOCATION, "true");
169 
170         return url.toString();
171 
172     }
173 
174     public String getRenderURL(Map params) {
175 
176         PortletURL url = _renderResponse.createRenderURL();
177 
178         if (params != null) {
179 
180             Iterator iter = params.keySet().iterator();
181 
182             String paramName = "";
183             String paramValue = "";
184 
185             while (iter.hasNext()) {
186                 paramName = (String) iter.next();
187 
188                 if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
189                     if ((paramValue = (String) params.get(paramName)) != null) {
190 
191                         setWindowState(url, paramValue);
192 
193                     }
194                 }
195                 else if (paramName.equalsIgnoreCase(Constants.PORTLET_MODE)) {
196                     if ((paramValue = (String) params.get(paramName)) != null) {
197 
198                         setPortletMode(url, paramValue);
199 
200                     }
201                 }
202                 else {
203                     if (!paramName.equalsIgnoreCase(Constants.URL_TYPE)
204                             && (paramValue = (String) params.get(paramName)) != null) {
205 
206                         url.setParameter(paramName, paramValue);
207 
208                     }
209                 }
210             }
211         }
212 
213         if (_consumerParameters != null) {
214 
215             Iterator iter2 = _consumerParameters.keySet().iterator();
216             String name = null;
217             String value = null;
218 
219             while (iter2.hasNext()) {
220 
221                 if ((value = (String) _consumerParameters.get(name)) != null) {
222                     url.setParameter(name, value);
223                 }
224             }
225         }
226 
227         url.setParameter(WSRPProxyPortlet.REMOTE_INVOCATION, "true");
228 
229         return url.toString();
230 
231     }
232 
233     public String getResourceURL(Map params) {
234         return getResourceProxyURL(params, _key);
235     }
236 
237     public String getNamespacedToken(String token) {
238         return _renderResponse.getNamespace();
239     }
240 
241     /**
242      * Maps wsrp-windowStates to pluto-windowStates.
243      */
244     private void setWindowState(PortletURL url, String windowState) {
245 
246         try {
247             if (windowState.equalsIgnoreCase(WindowStates._maximized)) {
248 
249                 url.setWindowState(WindowState.MAXIMIZED);
250 
251             }
252             else if (windowState.equalsIgnoreCase(WindowStates._minimized)) {
253 
254                 url.setWindowState(WindowState.MINIMIZED);
255 
256             }
257             else if (windowState.equalsIgnoreCase(WindowStates._normal)) {
258 
259                 url.setWindowState(WindowState.NORMAL);
260 
261             }
262             else if (windowState.equalsIgnoreCase(WindowStates._solo)) {
263 
264                 url.setWindowState(WindowState.MAXIMIZED);
265 
266             }
267         }
268         catch (Exception e) {
269             e.printStackTrace();
270         }
271 
272     }
273 
274     /**
275      * Maps wsrp-portletModes to pluto-portletModes.
276      */
277     private void setPortletMode(PortletURL url, String mode) {
278 
279         try {
280             if (mode.equalsIgnoreCase(Modes._edit)) {
281 
282                 url.setPortletMode(PortletMode.EDIT);
283 
284             }
285             else if (mode.equalsIgnoreCase(Modes._view)) {
286 
287                 url.setPortletMode(PortletMode.VIEW);
288 
289             }
290             else if (mode.equalsIgnoreCase(Modes._help)) {
291 
292                 url.setPortletMode(PortletMode.HELP);
293 
294             }
295             else if (mode.equalsIgnoreCase(Modes._preview)) {
296 
297                 url.setPortletMode(PortletMode.VIEW);
298 
299             }
300         }
301         catch (Exception e) {
302             e.printStackTrace();
303         }
304 
305     }
306 
307     private static URLGeneratorImpl _instance = null;
308 
309     private RenderResponse _renderResponse = null;
310 
311     private Map _consumerParameters = null;
312 
313     private Key _key = null;
314 
315 }