1   /**
2    * Copyright (c) 2000-2008 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.portal.wsrp;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.wsrp.util.WSRPUtil;
28  import com.liferay.portlet.ActionRequestImpl;
29  import com.liferay.portlet.PortletRequestImpl;
30  import com.liferay.portlet.PortletURLImpl;
31  
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  import javax.portlet.PortletMode;
37  import javax.portlet.WindowState;
38  
39  import oasis.names.tc.wsrp.v1.types.GetMarkup;
40  import oasis.names.tc.wsrp.v1.types.PerformBlockingInteraction;
41  import oasis.names.tc.wsrp.v1.types.PortletContext;
42  import oasis.names.tc.wsrp.v1.types.RuntimeContext;
43  import oasis.names.tc.wsrp.v1.types.UserContext;
44  
45  import org.apache.wsrp4j.producer.provider.Provider;
46  import org.apache.wsrp4j.producer.provider.URLComposer;
47  import org.apache.wsrp4j.producer.util.Base64;
48  import org.apache.wsrp4j.producer.util.ObjectSerializer;
49  
50  /**
51   * <a href="WSRPPortletURLImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Michael Young
54   *
55   */
56  public class WSRPPortletURLImpl extends PortletURLImpl {
57  
58      public WSRPPortletURLImpl(PerformBlockingInteraction pbo, Provider wsrpProvider,
59                                PortletRequestImpl req, String portletName,
60                                long plid, String lifecycle) {
61  
62          super(req, portletName, plid, lifecycle);
63  
64          _init(pbo, wsrpProvider);
65      }
66  
67      public WSRPPortletURLImpl(GetMarkup getMarkup, Provider wsrpProvider,
68                                PortletRequestImpl req, String portletName,
69                                long plid, String lifecycle) {
70  
71          super(req, portletName, plid, lifecycle);
72  
73          _init(getMarkup, wsrpProvider);
74      }
75  
76      public String toString() {
77          PortletMode portletMode = getPortletMode();
78          WindowState windowState = getWindowState();
79          boolean action = getLifecycle().equals("1");
80          boolean secure = isSecure();
81  
82          if (portletMode == null) {
83              portletMode = PortletMode.VIEW;
84          }
85  
86          if (windowState == null) {
87              windowState = WindowState.NORMAL;
88          }
89  
90          URLComposer urlComposer = _wsrpProvider.getURLComposer();
91          String encodedParams = _getEncodedParameters();
92          String url = StringPool.BLANK;
93  
94          if (action) {
95              url = urlComposer.createBlockingActionURL(WSRPUtil
96                      .toWsrpMode(portletMode.toString()), null, encodedParams,
97                      WSRPUtil.toWsrpWindowState(windowState.toString()), secure,
98                      _runtimeContext, _portletContext, _userContext);
99          }
100         else {
101             url = urlComposer.createRenderURL(WSRPUtil.toWsrpMode(portletMode
102                     .toString()), encodedParams, WSRPUtil
103                     .toWsrpWindowState(windowState.toString()), secure,
104                     _runtimeContext, _portletContext, _userContext);
105         }
106         return url;
107     }
108 
109     private void _init(GetMarkup getMarkup, Provider wsrpProvider) {
110         _runtimeContext = getMarkup.getRuntimeContext();
111         _portletContext = getMarkup.getPortletContext();
112         _userContext = getMarkup.getUserContext();
113         _wsrpProvider = wsrpProvider;
114     }
115 
116     private void _init(PerformBlockingInteraction pbo, Provider wsrpProvider) {
117         _runtimeContext = pbo.getRuntimeContext();
118         _portletContext = pbo.getPortletContext();
119         _userContext = pbo.getUserContext();
120         _wsrpProvider = wsrpProvider;
121     }
122 
123     private String _getEncodedParameters() {
124         Map renderParams = getParameterMap();
125         Map params = new HashMap();
126 
127         String[] plid = {String.valueOf(getPlid())};
128         params.put("p_l_id", plid);
129 
130         String[] portletId = {getPortletId()};
131         params.put("p_p_id", portletId);
132 
133         String[] lifecycle = {getLifecycle()};
134         params.put("p_p_lifecycle", lifecycle);
135 
136         WindowState windowState = getWindowState();
137         if (getWindowState() != null) {
138             String[] stateStr = {windowState.toString()};
139             params.put("p_p_state", stateStr);
140         }
141 
142         PortletMode portletMode = getPortletMode();
143         if (getPortletMode() != null) {
144             String[] modeStr = {portletMode.toString()};
145             params.put("p_p_mode", modeStr);
146         }
147 
148         Iterator itr = renderParams.entrySet().iterator();
149 
150         while (itr.hasNext()) {
151             Map.Entry entry = (Map.Entry)itr.next();
152 
153             String name =
154                 PortalUtil.getPortletNamespace(portletId[0]) +
155                 (String)entry.getKey();
156             String[] values = (String[])entry.getValue();
157 
158             for (int i = 0; i < values.length; i++) {
159                 params.put(name, values);
160             }
161         }
162 
163         String encodedParams = null;
164 
165         try {
166             encodedParams = Base64.encode(ObjectSerializer
167                     .serialize(params));
168         }
169         catch (Exception e) {
170         }
171 
172         return encodedParams;
173     }
174 
175     private RuntimeContext _runtimeContext;
176     private PortletContext _portletContext;
177     private UserContext _userContext;
178     private Provider _wsrpProvider;
179 
180 }