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.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.tags.model.TagsEntryConstants;
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                  StringBuilder sb = new StringBuilder();
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              boolean folksonomy = GetterUtil.getBoolean(
91                  portletURL.getParameter("folksonomy"),
92                  TagsEntryConstants.FOLKSONOMY_TAG);
93  
94              String tag = portletURL.getParameter("tag");
95  
96              StringBuilder sb = new StringBuilder();
97  
98              if (Validator.isNotNull(tag)) {
99                  sb.append(StringPool.SLASH);
100                 sb.append(_MAPPING);
101                 sb.append(StringPool.SLASH);
102 
103                 if (folksonomy) {
104                     sb.append("tag");
105                 }
106                 else {
107                     sb.append("category");
108                 }
109 
110                 sb.append(StringPool.SLASH);
111 
112                 sb.append(HttpUtil.encodeURL(tag));
113 
114                 portletURL.addParameterIncludedInPath("nodeId");
115                 portletURL.addParameterIncludedInPath("nodeName");
116                 portletURL.addParameterIncludedInPath("tag");
117             }
118 
119             friendlyURLPath = sb.toString();
120         }
121 
122         if (Validator.isNotNull(friendlyURLPath)) {
123             WindowState windowState = portletURL.getWindowState();
124 
125             if (!windowState.equals(WindowState.NORMAL)) {
126                 friendlyURLPath += StringPool.SLASH + windowState;
127             }
128 
129             portletURL.addParameterIncludedInPath("p_p_id");
130 
131             portletURL.addParameterIncludedInPath("struts_action");
132         }
133 
134         return friendlyURLPath;
135     }
136 
137     public String getMapping() {
138         return _MAPPING;
139     }
140 
141     public String getPortletId() {
142         return _PORTLET_ID;
143     }
144 
145     public void populateParams(
146         String friendlyURLPath, Map<String, String[]> params) {
147 
148         addParam(params, "p_p_id", _PORTLET_ID);
149         addParam(params, "p_p_lifecycle", "0");
150         addParam(params, "p_p_mode", PortletMode.VIEW);
151 
152         addParam(params, "struts_action", "/wiki/view");
153 
154         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
155 
156         String[] urlFragments = StringUtil.split(
157             friendlyURLPath.substring(x + 1), StringPool.SLASH);
158 
159         if (urlFragments.length >= 1) {
160             String urlFragment0 = urlFragments[0];
161 
162             if (urlFragment0.equals("category") || urlFragment0.equals("tag")) {
163                 if (urlFragments.length >= 2) {
164                     addParam(
165                         params, "struts_action", "/wiki/view_tagged_pages");
166 
167                     String tag = HttpUtil.decodeURL(urlFragments[1]);
168 
169                     boolean folksonomy = TagsEntryConstants.FOLKSONOMY_TAG;
170 
171                     if (urlFragment0.equals("category")) {
172                         folksonomy = TagsEntryConstants.FOLKSONOMY_CATEGORY;
173                     }
174 
175                     addParam(params, "tag", tag);
176                     addParam(params, "folksonomy", folksonomy);
177                 }
178             }
179             else {
180                 if (Validator.isNumber(urlFragment0)) {
181                     addParam(params, "nodeId", urlFragment0);
182                     addParam(params, "nodeName", StringPool.BLANK);
183                 }
184                 else {
185                     addParam(params, "nodeId", StringPool.BLANK);
186                     addParam(params, "nodeName", urlFragment0);
187                 }
188 
189                 if (urlFragments.length >= 2) {
190                     String urlFragments1 = HttpUtil.decodeURL(urlFragments[1]);
191 
192                     if (urlFragments1.equals("all_pages") ||
193                         urlFragments1.equals("orphan_pages") ||
194                         urlFragments1.equals("recent_changes")) {
195 
196                         addParam(
197                             params, "struts_action",
198                             "/wiki/view_" + urlFragments1);
199                     }
200                     else {
201                         addParam(params, "title", urlFragments1);
202                     }
203                 }
204 
205                 addParam(params, "tag", StringPool.BLANK);
206             }
207 
208             if (urlFragments.length >= 3) {
209                 String windowState = urlFragments[2];
210 
211                 addParam(params, "p_p_state", windowState);
212             }
213         }
214     }
215 
216     private static final String _MAPPING = "wiki";
217 
218     private static final String _PORTLET_ID = PortletKeys.WIKI;
219 
220 }