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.wiki;
24  
25  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
26  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PortletKeys;
33  
34  import java.util.Map;
35  
36  import javax.portlet.PortletMode;
37  import javax.portlet.WindowState;
38  
39  /**
40   * <a href="WikiFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Jorge Ferrer
43   *
44   */
45  public class WikiFriendlyURLMapper extends BaseFriendlyURLMapper {
46  
47      public String getMapping() {
48          return _MAPPING;
49      }
50  
51      public String getPortletId() {
52          return _PORTLET_ID;
53      }
54  
55      public String buildPath(LiferayPortletURL portletURL) {
56          String friendlyURLPath = null;
57  
58          String strutsAction = GetterUtil.getString(
59              portletURL.getParameter("struts_action"));
60  
61          if (strutsAction.equals("/wiki/view")) {
62              String title = portletURL.getParameter("title");
63              String nodeId = portletURL.getParameter("nodeId");
64  
65              if (Validator.isNotNull(nodeId)) {
66                  StringMaker sm = new StringMaker();
67  
68                  sm.append(StringPool.SLASH);
69                  sm.append(_MAPPING);
70                  sm.append(StringPool.SLASH);
71                  sm.append(nodeId);
72  
73                  portletURL.addParameterIncludedInPath("nodeId");
74  
75                  if (Validator.isNotNull(title)) {
76                      sm.append(StringPool.SLASH);
77                      sm.append(title);
78  
79                      portletURL.addParameterIncludedInPath("title");
80                  }
81  
82                  friendlyURLPath = sm.toString();
83              }
84          }
85  
86          if (Validator.isNotNull(friendlyURLPath)) {
87              portletURL.addParameterIncludedInPath("p_p_id");
88              portletURL.addParameterIncludedInPath("struts_action");
89          }
90  
91          return friendlyURLPath;
92      }
93  
94      public void populateParams(String friendlyURLPath, Map params) {
95          params.put("p_p_id", _PORTLET_ID);
96          params.put("p_p_action", "0");
97          params.put("p_p_state", WindowState.MAXIMIZED.toString());
98          params.put("p_p_mode", PortletMode.VIEW.toString());
99  
100         addParam(params, "struts_action", "/wiki/view");
101 
102         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
103 
104         String[] urlFragments = StringUtil.split(
105             friendlyURLPath.substring(x + 1), StringPool.SLASH);
106 
107         if (urlFragments.length >= 1) {
108             String nodeId = urlFragments[0];
109 
110             addParam(params, "nodeId", nodeId);
111 
112             if (urlFragments.length >= 2) {
113                 String title = urlFragments[1];
114 
115                 addParam(params, "title", title);
116             }
117         }
118     }
119 
120     private static final String _MAPPING = "wikipage";
121 
122     private static final String _PORTLET_ID = PortletKeys.WIKI;
123 
124 }