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.HttpUtil;
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 buildPath(LiferayPortletURL portletURL) {
48          String friendlyURLPath = null;
49  
50          String strutsAction = GetterUtil.getString(
51              portletURL.getParameter("struts_action"));
52  
53          if (strutsAction.equals("/wiki/view")) {
54              String nodeId = portletURL.getParameter("nodeId");
55              String nodeName = portletURL.getParameter("nodeName");
56              String title = portletURL.getParameter("title");
57  
58              if (Validator.isNotNull(nodeId) || Validator.isNotNull(nodeName)) {
59                  StringBuilder sb = new StringBuilder();
60  
61                  sb.append(StringPool.SLASH);
62                  sb.append(_MAPPING);
63                  sb.append(StringPool.SLASH);
64  
65                  if (Validator.isNotNull(nodeId)) {
66                      sb.append(nodeId);
67  
68                      portletURL.addParameterIncludedInPath("nodeId");
69                  }
70                  else if (Validator.isNotNull(nodeName)) {
71                      sb.append(nodeName);
72  
73                      portletURL.addParameterIncludedInPath("nodeName");
74                  }
75  
76                  if (Validator.isNotNull(title)) {
77                      sb.append(StringPool.SLASH);
78                      sb.append(HttpUtil.encodeURL(title));
79  
80                      portletURL.addParameterIncludedInPath("title");
81  
82                      WindowState windowState = portletURL.getWindowState();
83  
84                      if (!windowState.equals(WindowState.NORMAL)) {
85                          sb.append(StringPool.SLASH);
86                          sb.append(windowState);
87                      }
88                  }
89  
90                  friendlyURLPath = sb.toString();
91              }
92          }
93  
94          if (Validator.isNotNull(friendlyURLPath)) {
95              portletURL.addParameterIncludedInPath("p_p_id");
96  
97              portletURL.addParameterIncludedInPath("struts_action");
98          }
99  
100         return friendlyURLPath;
101     }
102 
103     public String getMapping() {
104         return _MAPPING;
105     }
106 
107     public String getPortletId() {
108         return _PORTLET_ID;
109     }
110 
111     public void populateParams(
112         String friendlyURLPath, Map<String, String[]> params) {
113 
114         addParam(params, "p_p_id", _PORTLET_ID);
115         addParam(params, "p_p_lifecycle", "0");
116         addParam(params, "p_p_mode", PortletMode.VIEW);
117 
118         addParam(params, "struts_action", "/wiki/view");
119 
120         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
121 
122         String[] urlFragments = StringUtil.split(
123             friendlyURLPath.substring(x + 1), StringPool.SLASH);
124 
125         if (urlFragments.length >= 1) {
126             String nodeId = urlFragments[0];
127 
128             if (Validator.isNumber(nodeId)) {
129                 addParam(params, "nodeId", nodeId);
130             }
131             else {
132                 addParam(params, "nodeName", nodeId);
133             }
134 
135             if (urlFragments.length >= 2) {
136                 String title = HttpUtil.decodeURL(urlFragments[1]);
137 
138                 addParam(params, "title", title);
139 
140                 if (urlFragments.length >= 3) {
141                     String windowState = urlFragments[2];
142 
143                     addParam(params, "p_p_state", windowState);
144                 }
145             }
146         }
147     }
148 
149     private static final String _MAPPING = "wiki";
150 
151     private static final String _PORTLET_ID = PortletKeys.WIKI;
152 
153 }