1   /**
2    * Copyright (c) 2000-2007 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;
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.impl.PortletImpl;
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   */
44  public class WAIFriendlyURLMapper implements FriendlyURLMapper {
45  
46      public String getMapping() {
47          return _MAPPING;
48      }
49  
50      public String buildPath(LiferayPortletURL portletURL) {
51          String portletId = portletURL.getPortletId();
52  
53          String prefix = portletId;
54  
55          int pos = portletId.indexOf(PortletImpl.WAR_SEPARATOR);
56  
57          if (pos != -1) {
58              prefix = portletId.substring(0, pos);
59          }
60  
61          String appUrl = GetterUtil.getString(portletURL.getParameter("appURL"));
62  
63          portletURL.addParameterIncludedInPath("p_p_id");
64  
65          return StringPool.SLASH + _MAPPING + StringPool.SLASH + prefix +
66              StringPool.SLASH + appUrl;
67      }
68  
69      public void populateParams(String friendlyURLPath, Map params) {
70          int x = friendlyURLPath.indexOf(_MAPPING);
71          int y = friendlyURLPath.indexOf("/", x + _MAPPING.length() + 1);
72  
73          if (x == -1) {
74              return;
75          }
76  
77          String prefix = friendlyURLPath.substring(x + _MAPPING.length() + 1, y);
78  
79          String portletId = prefix + PortletImpl.WAR_SEPARATOR + prefix;
80  
81          params.put("p_p_id", portletId);
82  
83          params.put("p_p_action", "0");
84  
85          if (hasBinaryExtension(friendlyURLPath)) {
86              params.put("p_p_state", LiferayWindowState.EXCLUSIVE.toString());
87          }
88          else {
89              params.put("p_p_state", WindowState.MAXIMIZED.toString());
90          }
91  
92          params.put("p_p_mode", PortletMode.VIEW.toString());
93  
94          String namespace = PortalUtil.getPortletNamespace(portletId);
95  
96          String path = friendlyURLPath.substring(y);
97  
98          params.put(namespace + "appURL", path);
99      }
100 
101     protected boolean hasBinaryExtension(String friendlyURLPath) {
102         for (int i = 0; i < _BINARY_EXTENSIONS.length; i++) {
103             String binaryExtension = _BINARY_EXTENSIONS[i];
104 
105             if (friendlyURLPath.endsWith(binaryExtension)) {
106                 return true;
107             }
108         }
109 
110         return false;
111     }
112 
113     private static final String _MAPPING = "waiapp";
114 
115     private static final String[] _BINARY_EXTENSIONS = new String[] {
116         ".css", ".doc", ".gif", ".jpeg", ".jpg", ".js", ".odp", ".png", ".ppt",
117         ".tgz", ".xls", ".zip",
118     };
119 
120 }