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