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.portlet.wiki;
16  
17  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortletKeys;
25  
26  import java.util.Map;
27  
28  import javax.portlet.PortletMode;
29  import javax.portlet.WindowState;
30  
31  /**
32   * <a href="WikiFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   */
36  public class WikiFriendlyURLMapper extends BaseFriendlyURLMapper {
37  
38      public String buildPath(LiferayPortletURL portletURL) {
39          String friendlyURLPath = null;
40  
41          String strutsAction = GetterUtil.getString(
42              portletURL.getParameter("struts_action"));
43  
44          if (strutsAction.equals("/wiki/view") ||
45              strutsAction.equals("/wiki/view_all_pages") ||
46              strutsAction.equals("/wiki/view_draft_pages") ||
47              strutsAction.equals("/wiki/view_orphan_pages") ||
48              strutsAction.equals("/wiki/view_recent_changes")) {
49  
50              String nodeId = portletURL.getParameter("nodeId");
51              String nodeName = portletURL.getParameter("nodeName");
52  
53              if (Validator.isNotNull(nodeId) || Validator.isNotNull(nodeName)) {
54                  StringBundler sb = new StringBundler();
55  
56                  sb.append(StringPool.SLASH);
57                  sb.append(_MAPPING);
58                  sb.append(StringPool.SLASH);
59  
60                  if (Validator.isNotNull(nodeId)) {
61                      sb.append(nodeId);
62  
63                      portletURL.addParameterIncludedInPath("nodeId");
64                  }
65                  else if (Validator.isNotNull(nodeName)) {
66                      sb.append(nodeName);
67  
68                      portletURL.addParameterIncludedInPath("nodeName");
69                  }
70  
71                  if (strutsAction.equals("/wiki/view")) {
72                      String title = portletURL.getParameter("title");
73  
74                      if (Validator.isNotNull(title)) {
75                          sb.append(StringPool.SLASH);
76                          sb.append(HttpUtil.encodeURL(title));
77  
78                          portletURL.addParameterIncludedInPath("title");
79                      }
80                  }
81                  else {
82                      sb.append(StringPool.SLASH);
83                      sb.append(strutsAction.substring(11));
84                  }
85  
86                  friendlyURLPath = sb.toString();
87              }
88          }
89          else if (strutsAction.equals("/wiki/view_tagged_pages")) {
90              String tag = portletURL.getParameter("tag");
91  
92              if (Validator.isNotNull(tag)) {
93                  StringBundler sb = new StringBundler(6);
94  
95                  sb.append(StringPool.SLASH);
96                  sb.append(_MAPPING);
97                  sb.append(StringPool.SLASH);
98                  sb.append("tag");
99                  sb.append(StringPool.SLASH);
100                 sb.append(HttpUtil.encodeURL(tag));
101 
102                 portletURL.addParameterIncludedInPath("nodeId");
103                 portletURL.addParameterIncludedInPath("nodeName");
104                 portletURL.addParameterIncludedInPath("tag");
105 
106                 friendlyURLPath = sb.toString();
107             }
108             else {
109                 friendlyURLPath = StringPool.BLANK;
110             }
111         }
112 
113         if (Validator.isNotNull(friendlyURLPath)) {
114             WindowState windowState = portletURL.getWindowState();
115 
116             if (!windowState.equals(WindowState.NORMAL)) {
117                 friendlyURLPath += StringPool.SLASH + windowState;
118             }
119 
120             portletURL.addParameterIncludedInPath("p_p_id");
121 
122             portletURL.addParameterIncludedInPath("struts_action");
123         }
124 
125         return friendlyURLPath;
126     }
127 
128     public String getMapping() {
129         return _MAPPING;
130     }
131 
132     public String getPortletId() {
133         return _PORTLET_ID;
134     }
135 
136     public void populateParams(
137         String friendlyURLPath, Map<String, String[]> parameterMap,
138         Map<String, Object> requestContext) {
139 
140         addParameter(parameterMap, "p_p_id", _PORTLET_ID);
141         addParameter(parameterMap, "p_p_lifecycle", "0");
142         addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
143 
144         addParameter(parameterMap, "struts_action", "/wiki/view");
145 
146         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
147 
148         String wikiPath = friendlyURLPath.substring(x + 1);
149 
150         int lastSlash = wikiPath.lastIndexOf(StringPool.SLASH);
151 
152         if (lastSlash != -1) {
153             String lastFragment = wikiPath.substring(lastSlash + 1);
154 
155             if (lastFragment.equalsIgnoreCase("exclusive") ||
156                 lastFragment.equalsIgnoreCase("maximized") ||
157                 lastFragment.equalsIgnoreCase("normal")) {
158 
159                 addParameter(parameterMap, "p_p_state", lastFragment);
160 
161                 wikiPath = wikiPath.substring(0, lastSlash);
162             }
163         }
164 
165         String firstFragment = wikiPath;
166 
167         int firstSlash = wikiPath.indexOf(StringPool.SLASH);
168 
169         if (firstSlash != -1) {
170             firstFragment = wikiPath.substring(0, firstSlash);
171             wikiPath = wikiPath.substring(firstSlash + 1);
172         }
173         else {
174             wikiPath = null;
175         }
176 
177         if (firstFragment.equalsIgnoreCase("tag") && (wikiPath != null)) {
178             addParameter(
179                 parameterMap, "struts_action", "/wiki/view_tagged_pages");
180 
181             String tag = HttpUtil.decodeURL(wikiPath);
182 
183             addParameter(parameterMap, "tag", tag);
184         }
185         else {
186             if (Validator.isNumber(firstFragment)) {
187                 addParameter(parameterMap, "nodeId", firstFragment);
188                 addParameter(parameterMap, "nodeName", StringPool.BLANK);
189             }
190             else {
191                 addParameter(parameterMap, "nodeId", StringPool.BLANK);
192                 addParameter(parameterMap, "nodeName", firstFragment);
193             }
194 
195             if (wikiPath != null) {
196                 if (wikiPath.equals("all_pages") ||
197                     wikiPath.equals("draft_pages") ||
198                     wikiPath.equals("orphan_pages") ||
199                     wikiPath.equals("recent_changes")) {
200 
201                     addParameter(
202                         parameterMap, "struts_action",
203                         "/wiki/view_" + wikiPath);
204                 }
205                 else {
206                     wikiPath = HttpUtil.decodeURL(wikiPath);
207 
208                     addParameter(parameterMap, "title", wikiPath);
209                 }
210             }
211 
212             addParameter(parameterMap, "tag", StringPool.BLANK);
213         }
214     }
215 
216     private static final String _MAPPING = "wiki";
217 
218     private static final String _PORTLET_ID = PortletKeys.WIKI;
219 
220 }