1   /**
2    * Copyright (c) 2000-2008 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(
70          String friendlyURLPath, Map<String, String[]> params) {
71  
72          int x = friendlyURLPath.indexOf(_MAPPING);
73          int y = friendlyURLPath.indexOf("/", x + _MAPPING.length() + 1);
74  
75          if (x == -1) {
76              return;
77          }
78  
79          String prefix = friendlyURLPath.substring(x + _MAPPING.length() + 1, y);
80  
81          String portletId = prefix + PortletImpl.WAR_SEPARATOR + prefix;
82  
83          params.put("p_p_id", new String[] {portletId});
84          params.put("p_p_lifecycle", new String[] {"0"});
85  
86          if (hasBinaryExtension(friendlyURLPath)) {
87              params.put(
88                  "p_p_state",
89                  new String[] {LiferayWindowState.EXCLUSIVE.toString()});
90          }
91          else {
92              params.put(
93                  "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
94          }
95  
96          params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
97  
98          String namespace = PortalUtil.getPortletNamespace(portletId);
99  
100         String path = friendlyURLPath.substring(y);
101 
102         params.put(namespace + "appURL", new String[] {path});
103     }
104 
105     protected boolean hasBinaryExtension(String friendlyURLPath) {
106         for (int i = 0; i < _BINARY_EXTENSIONS.length; i++) {
107             String binaryExtension = _BINARY_EXTENSIONS[i];
108 
109             if (friendlyURLPath.endsWith(binaryExtension)) {
110                 return true;
111             }
112         }
113 
114         return false;
115     }
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 }