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