1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.util.bridges.wai;
16  
17  import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.portlet.LiferayWindowState;
20  import com.liferay.portal.kernel.util.CharPool;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.model.PortletConstants;
24  import com.liferay.portal.util.PortalUtil;
25  
26  import java.util.Map;
27  
28  import javax.portlet.PortletMode;
29  import javax.portlet.WindowState;
30  
31  /**
32   * <a href="WAIFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   */
36  public class WAIFriendlyURLMapper implements FriendlyURLMapper {
37  
38      public String buildPath(LiferayPortletURL portletURL) {
39          String portletId = portletURL.getPortletId();
40  
41          String prefix = portletId;
42  
43          int pos = portletId.indexOf(PortletConstants.WAR_SEPARATOR);
44  
45          if (pos != -1) {
46              prefix = portletId.substring(0, pos);
47          }
48  
49          String appUrl = GetterUtil.getString(portletURL.getParameter("appURL"));
50  
51          portletURL.addParameterIncludedInPath("p_p_id");
52  
53          return StringPool.SLASH + _MAPPING + StringPool.SLASH + prefix +
54              StringPool.SLASH + appUrl;
55      }
56  
57      public String getMapping() {
58          return _MAPPING;
59      }
60  
61      public boolean isCheckMappingWithPrefix() {
62          return _CHECK_MAPPING_WITH_PREFIX;
63      }
64  
65      public void populateParams(
66          String friendlyURLPath, Map<String, String[]> parameterMap) {
67  
68          int x = friendlyURLPath.indexOf(_MAPPING);
69          int y = friendlyURLPath.indexOf(
70              CharPool.SLASH, x + _MAPPING.length() + 1);
71  
72          if (x == -1) {
73              return;
74          }
75  
76          String prefix = friendlyURLPath.substring(x + _MAPPING.length() + 1, y);
77  
78          String portletId = prefix + PortletConstants.WAR_SEPARATOR + prefix;
79  
80          parameterMap.put("p_p_id", new String[] {portletId});
81          parameterMap.put("p_p_lifecycle", new String[] {"0"});
82  
83          if (hasBinaryExtension(friendlyURLPath)) {
84              parameterMap.put(
85                  "p_p_state",
86                  new String[] {LiferayWindowState.EXCLUSIVE.toString()});
87          }
88          else {
89              parameterMap.put(
90                  "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
91          }
92  
93          parameterMap.put(
94              "p_p_mode", new String[] {PortletMode.VIEW.toString()});
95  
96          String namespace = PortalUtil.getPortletNamespace(portletId);
97  
98          String path = friendlyURLPath.substring(y);
99  
100         parameterMap.put(namespace + "appURL", new String[] {path});
101     }
102 
103     protected boolean hasBinaryExtension(String friendlyURLPath) {
104         for (int i = 0; i < _BINARY_EXTENSIONS.length; i++) {
105             String binaryExtension = _BINARY_EXTENSIONS[i];
106 
107             if (friendlyURLPath.endsWith(binaryExtension)) {
108                 return true;
109             }
110         }
111 
112         return false;
113     }
114 
115     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
116 
117     private static final String _MAPPING = "waiapp";
118 
119     private static final String[] _BINARY_EXTENSIONS = new String[] {
120         ".css", ".doc", ".gif", ".jpeg", ".jpg", ".js", ".odp", ".png", ".ppt",
121         ".tgz", ".xls", ".zip",
122     };
123 
124 }