1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 != null) &&
126                 !windowState.equals(WindowState.NORMAL)) {
127 
128                 friendlyURLPath += StringPool.SLASH + windowState;
129             }
130 
131             portletURL.addParameterIncludedInPath("p_p_id");
132 
133             portletURL.addParameterIncludedInPath("struts_action");
134         }
135 
136         return friendlyURLPath;
137     }
138 
139     public String getMapping() {
140         return _MAPPING;
141     }
142 
143     public String getPortletId() {
144         return _PORTLET_ID;
145     }
146 
147     public void populateParams(
148         String friendlyURLPath, Map<String, String[]> parameterMap) {
149 
150         addParameter(parameterMap, "p_p_id", _PORTLET_ID);
151         addParameter(parameterMap, "p_p_lifecycle", "0");
152         addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
153 
154         addParameter(parameterMap, "struts_action", "/wiki/view");
155 
156         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
157 
158         String[] urlFragments = StringUtil.split(
159             friendlyURLPath.substring(x + 1), StringPool.SLASH);
160 
161         if (urlFragments.length >= 1) {
162             String urlFragment0 = urlFragments[0];
163 
164             if (urlFragment0.equals("category") || urlFragment0.equals("tag")) {
165                 if (urlFragments.length >= 2) {
166                     addParameter(
167                         parameterMap, "struts_action",
168                         "/wiki/view_tagged_pages");
169 
170                     String tag = HttpUtil.decodeURL(urlFragments[1]);
171 
172                     boolean folksonomy = TagsEntryConstants.FOLKSONOMY_TAG;
173 
174                     if (urlFragment0.equals("category")) {
175                         folksonomy = TagsEntryConstants.FOLKSONOMY_CATEGORY;
176                     }
177 
178                     addParameter(parameterMap, "tag", tag);
179                     addParameter(parameterMap, "folksonomy", folksonomy);
180                 }
181             }
182             else {
183                 if (Validator.isNumber(urlFragment0)) {
184                     addParameter(parameterMap, "nodeId", urlFragment0);
185                     addParameter(parameterMap, "nodeName", StringPool.BLANK);
186                 }
187                 else {
188                     addParameter(parameterMap, "nodeId", StringPool.BLANK);
189                     addParameter(parameterMap, "nodeName", urlFragment0);
190                 }
191 
192                 if (urlFragments.length >= 2) {
193                     String urlFragments1 = HttpUtil.decodeURL(urlFragments[1]);
194 
195                     if (urlFragments1.equals("all_pages") ||
196                         urlFragments1.equals("orphan_pages") ||
197                         urlFragments1.equals("recent_changes")) {
198 
199                         addParameter(
200                             parameterMap, "struts_action",
201                             "/wiki/view_" + urlFragments1);
202                     }
203                     else {
204                         addParameter(parameterMap, "title", urlFragments1);
205                     }
206                 }
207 
208                 addParameter(parameterMap, "tag", StringPool.BLANK);
209             }
210 
211             if (urlFragments.length >= 3) {
212                 String windowState = urlFragments[2];
213 
214                 addParameter(parameterMap, "p_p_state", windowState);
215             }
216         }
217     }
218 
219     private static final String _MAPPING = "wiki";
220 
221     private static final String _PORTLET_ID = PortletKeys.WIKI;
222 
223 }