1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.assetpublisher;
16  
17  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.PortletConstants;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.PortletKeys;
26  
27  import java.util.Map;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="AssetPublisherFriendlyURLMapper.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Julio Camarero
37   */
38  public class AssetPublisherFriendlyURLMapper extends BaseFriendlyURLMapper {
39  
40      public String buildPath(LiferayPortletURL portletURL) {
41          String friendlyURLPath = null;
42  
43          String strutsAction = GetterUtil.getString(
44              portletURL.getParameter("struts_action"));
45  
46          WindowState windowState = portletURL.getWindowState();
47  
48          if (strutsAction.equals("/asset_publisher/rss")) {
49              String portletId = portletURL.getPortletId();
50  
51              if (Validator.isNotNull(portletId)) {
52                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
53                      portletId = _PORTLET_ID;
54                  }
55  
56                  int pos = portletId.indexOf(
57                      PortletConstants.INSTANCE_SEPARATOR);
58  
59                  String instanceId = null;
60  
61                  if (pos > 0) {
62                      instanceId = portletId.substring(pos + 10);
63                  }
64                  else {
65                      instanceId = portletId;
66                  }
67  
68                  friendlyURLPath = "/asset_publisher/" + instanceId + "/rss";
69              }
70          }
71          else if (strutsAction.equals("/asset_publisher/view_content")) {
72              String portletId = portletURL.getPortletId();
73              String assetEntryId = portletURL.getParameter("assetEntryId");
74              String type = GetterUtil.getString(
75                  portletURL.getParameter("type"), "content");
76              long groupId = GetterUtil.getLong(
77                  portletURL.getParameter("groupId"));
78              String urlTitle = portletURL.getParameter("urlTitle");
79  
80              if (Validator.isNotNull(portletId) &&
81                  Validator.isNotNull(assetEntryId)) {
82  
83                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
84                      portletId = _PORTLET_ID;
85                  }
86  
87                  int pos = portletId.indexOf(
88                      PortletConstants.INSTANCE_SEPARATOR);
89  
90                  String instanceId = null;
91  
92                  if (pos > 0) {
93                      instanceId = portletId.substring(pos + 10);
94                  }
95                  else {
96                      instanceId = portletId;
97                  }
98  
99                  friendlyURLPath =
100                     "/asset_publisher/" + instanceId + StringPool.SLASH + type +
101                         StringPool.SLASH;
102 
103                 if (Validator.isNotNull(urlTitle)) {
104                     friendlyURLPath += urlTitle;
105 
106                     portletURL.addParameterIncludedInPath("urlTitle");
107 
108                     if (groupId > 0) {
109                         friendlyURLPath += StringPool.SLASH + groupId;
110 
111                         portletURL.addParameterIncludedInPath("groupId");
112                     }
113                 }
114                 else {
115                     friendlyURLPath += "id/" + assetEntryId;
116                 }
117 
118                 portletURL.addParameterIncludedInPath("type");
119                 portletURL.addParameterIncludedInPath("assetEntryId");
120             }
121         }
122 
123         if (Validator.isNotNull(friendlyURLPath)) {
124             if (windowState.equals(WindowState.MAXIMIZED)) {
125                 friendlyURLPath += StringPool.SLASH + windowState;
126             }
127 
128             portletURL.addParameterIncludedInPath("p_p_id");
129 
130             portletURL.addParameterIncludedInPath("struts_action");
131         }
132 
133         return friendlyURLPath;
134     }
135 
136     public String getMapping() {
137         return _MAPPING;
138     }
139 
140     public String getPortletId() {
141         return _PORTLET_ID;
142     }
143 
144     public void populateParams(
145         String friendlyURLPath, Map<String, String[]> parameterMap,
146         Map<String, Object> requestContext) {
147 
148         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
149 
150         String[] urlFragments = StringUtil.split(
151             friendlyURLPath.substring(x + 1), StringPool.SLASH);
152 
153         if (urlFragments.length >= 2) {
154             String instanceId = urlFragments[0];
155             String type = urlFragments[1];
156             String assetEntryId = null;
157             long groupId = 0;
158             String urlTitle = null;
159 
160             String portletId =
161                 _PORTLET_ID + PortletConstants.INSTANCE_SEPARATOR + instanceId;
162 
163             String namespace = PortalUtil.getPortletNamespace(portletId);
164 
165             parameterMap.put("p_p_id", new String[] {portletId});
166 
167             if (friendlyURLPath.indexOf("maximized", x) != -1) {
168                 addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
169             }
170 
171             parameterMap.put(
172                 "p_p_mode", new String[] {PortletMode.VIEW.toString()});
173 
174             if (type.equals("rss")) {
175                 parameterMap.put(
176                     namespace + "struts_action",
177                     new String[] {"/asset_publisher/rss"});
178 
179                 parameterMap.put("p_p_lifecycle", new String[] {"2"});
180             }
181             else {
182                 if ((urlFragments.length > 3) && urlFragments[2].equals("id")) {
183                     assetEntryId = urlFragments[3];
184                 }
185                 else if (urlFragments.length > 3) {
186                     urlTitle = urlFragments[2];
187 
188                     groupId = GetterUtil.getLong(urlFragments[3]);
189                 }
190                 else {
191                     urlTitle = urlFragments[2];
192                 }
193 
194                 parameterMap.put("p_p_lifecycle", new String[] {"0"});
195 
196                 parameterMap.put(
197                     namespace + "struts_action",
198                     new String[] {"/asset_publisher/view_content"});
199                 parameterMap.put(namespace + "type", new String[] {type});
200 
201                 if (Validator.isNotNull(assetEntryId)) {
202                     parameterMap.put(
203                         namespace + "assetEntryId",
204                         new String[] {assetEntryId});
205                 }
206                 else {
207                     if (groupId > 0) {
208                         parameterMap.put(
209                             namespace + "groupId",
210                             new String[] {String.valueOf(groupId)});
211                     }
212 
213                     parameterMap.put(
214                         namespace + "urlTitle", new String[] {urlTitle});
215                 }
216             }
217         }
218     }
219 
220     private static final String _MAPPING = "asset_publisher";
221 
222     private static final String _PORTLET_DEFAULT_INSTANCE =
223         PortletKeys.ASSET_PUBLISHER + "_INSTANCE_0000";
224 
225     private static final String _PORTLET_ID = PortletKeys.ASSET_PUBLISHER;
226 
227 }