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