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.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.util.PortletKeys;
26  
27  import java.util.Map;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="WikiFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   */
37  public class WikiFriendlyURLMapper extends BaseFriendlyURLMapper {
38  
39      public String buildPath(LiferayPortletURL portletURL) {
40          String friendlyURLPath = null;
41  
42          String strutsAction = GetterUtil.getString(
43              portletURL.getParameter("struts_action"));
44  
45          if (strutsAction.equals("/wiki/view") ||
46              strutsAction.equals("/wiki/view_all_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[]> params) {
138 
139         addParam(params, "p_p_id", _PORTLET_ID);
140         addParam(params, "p_p_lifecycle", "0");
141         addParam(params, "p_p_mode", PortletMode.VIEW);
142 
143         addParam(params, "struts_action", "/wiki/view");
144 
145         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
146 
147         String[] urlFragments = StringUtil.split(
148             friendlyURLPath.substring(x + 1), StringPool.SLASH);
149 
150         if (urlFragments.length >= 1) {
151             String urlFragment0 = urlFragments[0];
152 
153             if (urlFragment0.equals("tag")) {
154                 if (urlFragments.length >= 2) {
155                     addParam(
156                         params, "struts_action", "/wiki/view_tagged_pages");
157 
158                     String tag = HttpUtil.decodeURL(urlFragments[1]);
159 
160                     addParam(params, "tag", tag);
161                 }
162             }
163             else {
164                 if (Validator.isNumber(urlFragment0)) {
165                     addParam(params, "nodeId", urlFragment0);
166                     addParam(params, "nodeName", StringPool.BLANK);
167                 }
168                 else {
169                     addParam(params, "nodeId", StringPool.BLANK);
170                     addParam(params, "nodeName", urlFragment0);
171                 }
172 
173                 if (urlFragments.length >= 2) {
174                     String urlFragments1 = HttpUtil.decodeURL(urlFragments[1]);
175 
176                     if (urlFragments1.equals("all_pages") ||
177                         urlFragments1.equals("orphan_pages") ||
178                         urlFragments1.equals("recent_changes")) {
179 
180                         addParam(
181                             params, "struts_action",
182                             "/wiki/view_" + urlFragments1);
183                     }
184                     else {
185                         addParam(params, "title", urlFragments1);
186                     }
187                 }
188 
189                 addParam(params, "tag", StringPool.BLANK);
190             }
191 
192             if (urlFragments.length >= 3) {
193                 String windowState = urlFragments[2];
194 
195                 addParam(params, "p_p_state", windowState);
196             }
197         }
198     }
199 
200     private static final String _MAPPING = "wiki";
201 
202     private static final String _PORTLET_ID = PortletKeys.WIKI;
203 
204 }