1   /**
2    * Copyright (c) 2000-2009 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.assetpublisher;
24  
25  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
26  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
27  import com.liferay.portal.kernel.portlet.LiferayWindowState;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
34  
35  import java.util.Map;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.WindowState;
39  
40  /**
41   * <a href="AssetPublisherFriendlyURLMapper.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Julio Camarero
45   *
46   */
47  public class AssetPublisherFriendlyURLMapper extends BaseFriendlyURLMapper {
48  
49      public String buildPath(LiferayPortletURL portletURL) {
50          String friendlyURLPath = null;
51  
52          String strutsAction = GetterUtil.getString(
53              portletURL.getParameter("struts_action"));
54  
55          WindowState windowState = portletURL.getWindowState();
56  
57          if ((strutsAction.equals("/asset_publisher/view_content"))  &&
58              ((windowState == null) ||
59               (!windowState.equals(LiferayWindowState.EXCLUSIVE)))) {
60  
61              String portletId = portletURL.getPortletId();
62              String assetId = portletURL.getParameter("assetId");
63              String type = GetterUtil.getString(
64                  portletURL.getParameter("type"), "content");
65  
66              if (Validator.isNotNull(portletId) &&
67                  Validator.isNotNull(assetId)) {
68  
69                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
70                      portletId = _PORTLET_ID;
71                  }
72  
73                  int pos = portletId.indexOf("_INSTANCE_");
74  
75                  String instanceId = null;
76  
77                  if (pos > 0) {
78                      instanceId = portletId.substring(pos + 10);
79                  }
80                  else {
81                      instanceId = portletId;
82                  }
83  
84                  friendlyURLPath =
85                      "/asset_publisher/" + instanceId + StringPool.SLASH + type +
86                          StringPool.SLASH + assetId;
87  
88                  portletURL.addParameterIncludedInPath("type");
89                  portletURL.addParameterIncludedInPath("assetId");
90              }
91          }
92  
93          if (Validator.isNotNull(friendlyURLPath)) {
94              portletURL.addParameterIncludedInPath("p_p_id");
95  
96              portletURL.addParameterIncludedInPath("struts_action");
97          }
98  
99          return friendlyURLPath;
100     }
101 
102     public String getMapping() {
103         return _MAPPING;
104     }
105 
106     public String getPortletId() {
107         return _PORTLET_ID;
108     }
109 
110     public void populateParams(
111         String friendlyURLPath, Map<String, String[]> params) {
112 
113         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
114 
115         String[] urlFragments = StringUtil.split(
116             friendlyURLPath.substring(x + 1), StringPool.SLASH);
117 
118         if (urlFragments.length > 2) {
119             String instanceId = urlFragments[0];
120             String type = urlFragments[1];
121             String assetId = urlFragments[2];
122 
123             String portletId = _PORTLET_ID + "_INSTANCE_" + instanceId;
124 
125            if (Validator.equals(portletId, _PORTLET_ID)) {
126                 portletId = _PORTLET_DEFAULT_INSTANCE;
127 
128                 params.put("p_p_id", new String[] {portletId});
129                 params.put(
130                     "p_p_state",
131                     new String[] {WindowState.MAXIMIZED.toString()});
132             }
133             else {
134                 params.put("p_p_id", new String[] {portletId});
135                 params.put(
136                     "p_p_state", new String[] {WindowState.NORMAL.toString()});
137             }
138 
139             params.put("p_p_lifecycle", new String[] {"0"});
140             params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
141 
142             String namespace =
143                 StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
144 
145             params.put(
146                 namespace + "struts_action",
147                 new String[] {"/asset_publisher/view_content"});
148 
149             if (Validator.isNumber(assetId) &&
150                 !type.equals(AssetPublisherUtil.TYPE_CONTENT)) {
151 
152                 params.put(namespace + "assetId", new String[] {assetId});
153             }
154             else {
155                 params.put(namespace + "type", new String[] {type});
156                 params.put(namespace + "urlTitle", new String[] {assetId});
157             }
158         }
159     }
160 
161     private static final String _MAPPING = "asset_publisher";
162 
163     private static final String _PORTLET_DEFAULT_INSTANCE =
164         PortletKeys.ASSET_PUBLISHER + "_INSTANCE_0000";
165 
166     private static final String _PORTLET_ID = PortletKeys.ASSET_PUBLISHER;
167 
168 }