001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.wsrp;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.util.Encryptor;
026: import com.liferay.util.axis.SimpleHTTPSender;
027:
028: import java.security.Key;
029:
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import javax.portlet.PortletMode;
034: import javax.portlet.PortletURL;
035: import javax.portlet.RenderResponse;
036: import javax.portlet.WindowState;
037:
038: import org.apache.wsrp4j.consumer.URLGenerator;
039: import org.apache.wsrp4j.producer.util.Base64;
040: import org.apache.wsrp4j.util.Constants;
041: import org.apache.wsrp4j.util.Modes;
042: import org.apache.wsrp4j.util.WindowStates;
043:
044: /**
045: * <a href="URLGeneratorImpl.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Michael Young
048: *
049: */
050: public class URLGeneratorImpl implements URLGenerator {
051:
052: public static String getResourceProxyURL(Map params, Key key) {
053: StringMaker url = new StringMaker();
054:
055: url.append("/wsrp/resource_proxy/get");
056:
057: if (params != null) {
058:
059: String paramValue = GetterUtil.getString((String) params
060: .get(Constants.URL));
061:
062: url.append(Constants.PARAMS_START);
063: url.append("url");
064: url.append(Constants.EQUALS);
065:
066: try {
067: byte[] paramValueBytes = Encryptor.encryptRaw(key,
068: paramValue);
069:
070: paramValue = Base64.encode(paramValueBytes);
071:
072: url.append(paramValue);
073: } catch (Exception e) {
074: e.printStackTrace();
075: }
076: }
077:
078: String cookie = SimpleHTTPSender.getCurrentCookie();
079:
080: try {
081: byte[] cookieBytes = Encryptor.encryptRaw(key, cookie);
082:
083: cookie = Base64.encode(cookieBytes);
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087:
088: url.append(Constants.NEXT_PARAM);
089: url.append("cookie");
090: url.append(Constants.EQUALS);
091: url.append(cookie);
092:
093: return url.toString();
094:
095: }
096:
097: public URLGeneratorImpl(RenderResponse response, Key key) {
098: _renderResponse = response;
099: _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: } else if (paramName
134: .equalsIgnoreCase(Constants.PORTLET_MODE)) {
135: if ((paramValue = (String) params.get(paramName)) != null) {
136:
137: setPortletMode(url, paramValue);
138:
139: }
140: } else {
141: if ((paramValue = (String) params.get(paramName)) != null) {
142:
143: url.setParameter(paramName, paramValue);
144:
145: }
146: }
147: }
148: }
149:
150: if (_consumerParameters != null) {
151:
152: Iterator iter2 = _consumerParameters.keySet().iterator();
153: String name = null;
154: String value = null;
155:
156: while (iter2.hasNext()) {
157:
158: if ((value = (String) _consumerParameters.get(name)) != null) {
159: url.setParameter(name, value);
160: }
161: }
162: }
163:
164: url.setParameter(WSRPProxyPortlet.REMOTE_INVOCATION, "true");
165:
166: return url.toString();
167:
168: }
169:
170: public String getRenderURL(Map params) {
171:
172: PortletURL url = _renderResponse.createRenderURL();
173:
174: if (params != null) {
175:
176: Iterator iter = params.keySet().iterator();
177:
178: String paramName = "";
179: String paramValue = "";
180:
181: while (iter.hasNext()) {
182: paramName = (String) iter.next();
183:
184: if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
185: if ((paramValue = (String) params.get(paramName)) != null) {
186:
187: setWindowState(url, paramValue);
188:
189: }
190: } else if (paramName
191: .equalsIgnoreCase(Constants.PORTLET_MODE)) {
192: if ((paramValue = (String) params.get(paramName)) != null) {
193:
194: setPortletMode(url, paramValue);
195:
196: }
197: } else {
198: if (!paramName.equalsIgnoreCase(Constants.URL_TYPE)
199: && (paramValue = (String) params
200: .get(paramName)) != null) {
201:
202: url.setParameter(paramName, paramValue);
203:
204: }
205: }
206: }
207: }
208:
209: if (_consumerParameters != null) {
210:
211: Iterator iter2 = _consumerParameters.keySet().iterator();
212: String name = null;
213: String value = null;
214:
215: while (iter2.hasNext()) {
216:
217: if ((value = (String) _consumerParameters.get(name)) != null) {
218: url.setParameter(name, value);
219: }
220: }
221: }
222:
223: url.setParameter(WSRPProxyPortlet.REMOTE_INVOCATION, "true");
224:
225: return url.toString();
226:
227: }
228:
229: public String getResourceURL(Map params) {
230: return getResourceProxyURL(params, _key);
231: }
232:
233: public String getNamespacedToken(String token) {
234: return _renderResponse.getNamespace();
235: }
236:
237: /**
238: * Maps wsrp-windowStates to pluto-windowStates.
239: */
240: private void setWindowState(PortletURL url, String windowState) {
241:
242: try {
243: if (windowState.equalsIgnoreCase(WindowStates._maximized)) {
244:
245: url.setWindowState(WindowState.MAXIMIZED);
246:
247: } else if (windowState
248: .equalsIgnoreCase(WindowStates._minimized)) {
249:
250: url.setWindowState(WindowState.MINIMIZED);
251:
252: } else if (windowState
253: .equalsIgnoreCase(WindowStates._normal)) {
254:
255: url.setWindowState(WindowState.NORMAL);
256:
257: } else if (windowState.equalsIgnoreCase(WindowStates._solo)) {
258:
259: url.setWindowState(WindowState.MAXIMIZED);
260:
261: }
262: } catch (Exception e) {
263: e.printStackTrace();
264: }
265:
266: }
267:
268: /**
269: * Maps wsrp-portletModes to pluto-portletModes.
270: */
271: private void setPortletMode(PortletURL url, String mode) {
272:
273: try {
274: if (mode.equalsIgnoreCase(Modes._edit)) {
275:
276: url.setPortletMode(PortletMode.EDIT);
277:
278: } else if (mode.equalsIgnoreCase(Modes._view)) {
279:
280: url.setPortletMode(PortletMode.VIEW);
281:
282: } else if (mode.equalsIgnoreCase(Modes._help)) {
283:
284: url.setPortletMode(PortletMode.HELP);
285:
286: } else if (mode.equalsIgnoreCase(Modes._preview)) {
287:
288: url.setPortletMode(PortletMode.VIEW);
289:
290: }
291: } catch (Exception e) {
292: e.printStackTrace();
293: }
294:
295: }
296:
297: private static URLGeneratorImpl _instance = null;
298:
299: private RenderResponse _renderResponse = null;
300:
301: private Map _consumerParameters = null;
302:
303: private Key _key = null;
304:
305: }
|