1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.wsrp;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.wsrp.util.WSRPUtil;
25  import com.liferay.portlet.ActionRequestImpl;
26  import com.liferay.portlet.PortletRequestImpl;
27  import com.liferay.portlet.PortletURLImpl;
28  
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.Map;
32  
33  import javax.portlet.PortletMode;
34  import javax.portlet.WindowState;
35  
36  import oasis.names.tc.wsrp.v1.types.GetMarkup;
37  import oasis.names.tc.wsrp.v1.types.PerformBlockingInteraction;
38  import oasis.names.tc.wsrp.v1.types.PortletContext;
39  import oasis.names.tc.wsrp.v1.types.RuntimeContext;
40  import oasis.names.tc.wsrp.v1.types.UserContext;
41  
42  import org.apache.wsrp4j.producer.provider.Provider;
43  import org.apache.wsrp4j.producer.provider.URLComposer;
44  import org.apache.wsrp4j.producer.util.Base64;
45  import org.apache.wsrp4j.producer.util.ObjectSerializer;
46  
47  /**
48   * <a href="WSRPPortletURLImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Michael Young
51   *
52   */
53  public class WSRPPortletURLImpl extends PortletURLImpl {
54  
55      public WSRPPortletURLImpl(PerformBlockingInteraction pbo, Provider wsrpProvider,
56                                PortletRequestImpl req, String portletName,
57                                long plid, String lifecycle) {
58  
59          super(req, portletName, plid, lifecycle);
60  
61          _init(pbo, wsrpProvider);
62      }
63  
64      public WSRPPortletURLImpl(GetMarkup getMarkup, Provider wsrpProvider,
65                                PortletRequestImpl req, String portletName,
66                                long plid, String lifecycle) {
67  
68          super(req, portletName, plid, lifecycle);
69  
70          _init(getMarkup, wsrpProvider);
71      }
72  
73      public String toString() {
74          PortletMode portletMode = getPortletMode();
75          WindowState windowState = getWindowState();
76          boolean action = getLifecycle().equals("1");
77          boolean secure = isSecure();
78  
79          if (portletMode == null) {
80              portletMode = PortletMode.VIEW;
81          }
82  
83          if (windowState == null) {
84              windowState = WindowState.NORMAL;
85          }
86  
87          URLComposer urlComposer = _wsrpProvider.getURLComposer();
88          String encodedParams = _getEncodedParameters();
89          String url = StringPool.BLANK;
90  
91          if (action) {
92              url = urlComposer.createBlockingActionURL(WSRPUtil
93                      .toWsrpMode(portletMode.toString()), null, encodedParams,
94                      WSRPUtil.toWsrpWindowState(windowState.toString()), secure,
95                      _runtimeContext, _portletContext, _userContext);
96          }
97          else {
98              url = urlComposer.createRenderURL(WSRPUtil.toWsrpMode(portletMode
99                      .toString()), encodedParams, WSRPUtil
100                     .toWsrpWindowState(windowState.toString()), secure,
101                     _runtimeContext, _portletContext, _userContext);
102         }
103         return url;
104     }
105 
106     private void _init(GetMarkup getMarkup, Provider wsrpProvider) {
107         _runtimeContext = getMarkup.getRuntimeContext();
108         _portletContext = getMarkup.getPortletContext();
109         _userContext = getMarkup.getUserContext();
110         _wsrpProvider = wsrpProvider;
111     }
112 
113     private void _init(PerformBlockingInteraction pbo, Provider wsrpProvider) {
114         _runtimeContext = pbo.getRuntimeContext();
115         _portletContext = pbo.getPortletContext();
116         _userContext = pbo.getUserContext();
117         _wsrpProvider = wsrpProvider;
118     }
119 
120     private String _getEncodedParameters() {
121         Map renderParams = getParameterMap();
122         Map params = new HashMap();
123 
124         String[] plid = {String.valueOf(getPlid())};
125         params.put("p_l_id", plid);
126 
127         String[] portletId = {getPortletId()};
128         params.put("p_p_id", portletId);
129 
130         String[] lifecycle = {getLifecycle()};
131         params.put("p_p_lifecycle", lifecycle);
132 
133         WindowState windowState = getWindowState();
134         if (getWindowState() != null) {
135             String[] stateStr = {windowState.toString()};
136             params.put("p_p_state", stateStr);
137         }
138 
139         PortletMode portletMode = getPortletMode();
140         if (getPortletMode() != null) {
141             String[] modeStr = {portletMode.toString()};
142             params.put("p_p_mode", modeStr);
143         }
144 
145         Iterator itr = renderParams.entrySet().iterator();
146 
147         while (itr.hasNext()) {
148             Map.Entry entry = (Map.Entry)itr.next();
149 
150             String name =
151                 PortalUtil.getPortletNamespace(portletId[0]) +
152                 (String)entry.getKey();
153             String[] values = (String[])entry.getValue();
154 
155             for (int i = 0; i < values.length; i++) {
156                 params.put(name, values);
157             }
158         }
159 
160         String encodedParams = null;
161 
162         try {
163             encodedParams = Base64.encode(ObjectSerializer
164                     .serialize(params));
165         }
166         catch (Exception e) {
167         }
168 
169         return encodedParams;
170     }
171 
172     private RuntimeContext _runtimeContext;
173     private PortletContext _portletContext;
174     private UserContext _userContext;
175     private Provider _wsrpProvider;
176 
177 }