1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.portlet.Router;
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 Router getRouter() {
62          return router;
63      }
64  
65      public boolean isCheckMappingWithPrefix() {
66          return _CHECK_MAPPING_WITH_PREFIX;
67      }
68  
69      public void populateParams(
70          String friendlyURLPath, Map<String, String[]> parameterMap,
71          Map<String, Object> requestContext) {
72  
73          int x = friendlyURLPath.indexOf(_MAPPING);
74          int y = friendlyURLPath.indexOf("/", x + _MAPPING.length() + 1);
75  
76          if (x == -1) {
77              return;
78          }
79  
80          String prefix = friendlyURLPath.substring(x + _MAPPING.length() + 1, y);
81  
82          String portletId = prefix + PortletConstants.WAR_SEPARATOR + prefix;
83  
84          parameterMap.put("p_p_id", new String[] {portletId});
85          parameterMap.put("p_p_lifecycle", new String[] {"0"});
86  
87          if (hasBinaryExtension(friendlyURLPath)) {
88              parameterMap.put(
89                  "p_p_state",
90                  new String[] {LiferayWindowState.EXCLUSIVE.toString()});
91          }
92          else {
93              parameterMap.put(
94                  "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
95          }
96  
97          parameterMap.put(
98              "p_p_mode", new String[] {PortletMode.VIEW.toString()});
99  
100         String namespace = PortalUtil.getPortletNamespace(portletId);
101 
102         String path = friendlyURLPath.substring(y);
103 
104         parameterMap.put(namespace + "appURL", new String[] {path});
105     }
106 
107     public void setRouter(Router router) {
108         this.router = router;
109     }
110 
111     protected boolean hasBinaryExtension(String friendlyURLPath) {
112         for (int i = 0; i < _BINARY_EXTENSIONS.length; i++) {
113             String binaryExtension = _BINARY_EXTENSIONS[i];
114 
115             if (friendlyURLPath.endsWith(binaryExtension)) {
116                 return true;
117             }
118         }
119 
120         return false;
121     }
122 
123     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
124 
125     private static final String _MAPPING = "waiapp";
126 
127     private static final String[] _BINARY_EXTENSIONS = new String[] {
128         ".css", ".doc", ".gif", ".jpeg", ".jpg", ".js", ".odp", ".png", ".ppt",
129         ".tgz", ".xls", ".zip",
130     };
131 
132     protected Router router;
133 
134 }