1   /**
2    * Copyright (c) 2000-2009 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  import com.liferay.portlet.tags.model.TagsEntryConstants;
34  
35  import java.util.Map;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.WindowState;
39  
40  /**
41   * <a href="WikiFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Jorge Ferrer
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              strutsAction.equals("/wiki/view_all_pages") ||
55              strutsAction.equals("/wiki/view_orphan_pages") ||
56              strutsAction.equals("/wiki/view_recent_changes")) {
57  
58              String nodeId = portletURL.getParameter("nodeId");
59              String nodeName = portletURL.getParameter("nodeName");
60  
61              if (Validator.isNotNull(nodeId) || Validator.isNotNull(nodeName)) {
62                  StringBuilder sb = new StringBuilder();
63  
64                  sb.append(StringPool.SLASH);
65                  sb.append(_MAPPING);
66                  sb.append(StringPool.SLASH);
67  
68                  if (Validator.isNotNull(nodeId)) {
69                      sb.append(nodeId);
70  
71                      portletURL.addParameterIncludedInPath("nodeId");
72                  }
73                  else if (Validator.isNotNull(nodeName)) {
74                      sb.append(nodeName);
75  
76                      portletURL.addParameterIncludedInPath("nodeName");
77                  }
78  
79                  if (strutsAction.equals("/wiki/view")) {
80                      String title = portletURL.getParameter("title");
81  
82                      if (Validator.isNotNull(title)) {
83                          sb.append(StringPool.SLASH);
84                          sb.append(HttpUtil.encodeURL(title));
85  
86                          portletURL.addParameterIncludedInPath("title");
87                      }
88                  }
89                  else {
90                      sb.append(StringPool.SLASH);
91                      sb.append(strutsAction.substring(11));
92                  }
93  
94                  friendlyURLPath = sb.toString();
95              }
96          }
97          else if (strutsAction.equals("/wiki/view_tagged_pages")) {
98              boolean folksonomy = GetterUtil.getBoolean(
99                  portletURL.getParameter("folksonomy"));
100 
101             String tag = portletURL.getParameter("tag");
102 
103             StringBuilder sb = new StringBuilder();
104 
105             if (Validator.isNotNull(tag)) {
106                 sb.append(StringPool.SLASH);
107                 sb.append(_MAPPING);
108                 sb.append(StringPool.SLASH);
109 
110                 if (folksonomy) {
111                     sb.append("tag");
112                 }
113                 else {
114                     sb.append("category");
115                 }
116 
117                 sb.append(StringPool.SLASH);
118 
119                 sb.append(HttpUtil.encodeURL(tag));
120 
121                 portletURL.addParameterIncludedInPath("nodeId");
122                 portletURL.addParameterIncludedInPath("nodeName");
123                 portletURL.addParameterIncludedInPath("tag");
124             }
125 
126             friendlyURLPath = sb.toString();
127         }
128 
129         if (Validator.isNotNull(friendlyURLPath)) {
130             portletURL.addParameterIncludedInPath("p_p_id");
131 
132             portletURL.addParameterIncludedInPath("struts_action");
133 
134             WindowState windowState = portletURL.getWindowState();
135 
136             if (!windowState.equals(WindowState.NORMAL)) {
137                 friendlyURLPath += StringPool.SLASH + windowState;
138             }
139         }
140 
141         return friendlyURLPath;
142     }
143 
144     public String getMapping() {
145         return _MAPPING;
146     }
147 
148     public String getPortletId() {
149         return _PORTLET_ID;
150     }
151 
152     public void populateParams(
153         String friendlyURLPath, Map<String, String[]> params) {
154 
155         addParam(params, "p_p_id", _PORTLET_ID);
156         addParam(params, "p_p_lifecycle", "0");
157         addParam(params, "p_p_mode", PortletMode.VIEW);
158 
159         addParam(params, "struts_action", "/wiki/view");
160 
161         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
162 
163         String[] urlFragments = StringUtil.split(
164             friendlyURLPath.substring(x + 1), StringPool.SLASH);
165 
166         if (urlFragments.length >= 1) {
167             String urlFragment0 = urlFragments[0];
168 
169             if (urlFragment0.equals("category") || urlFragment0.equals("tag")) {
170                 if (urlFragments.length >= 2) {
171                     addParam(
172                         params, "struts_action", "/wiki/view_tagged_pages");
173 
174                     String tag = HttpUtil.decodeURL(urlFragments[1]);
175 
176                     boolean folksonomy = TagsEntryConstants.FOLKSONOMY_TAG;
177 
178                     if (urlFragment0.equals("category")) {
179                         folksonomy = TagsEntryConstants.FOLKSONOMY_CATEGORY;
180                     }
181 
182                     addParam(params, "tag", tag);
183                     addParam(params, "folksonomy", folksonomy);
184                 }
185             }
186             else {
187                 if (Validator.isNumber(urlFragment0)) {
188                     addParam(params, "nodeId", urlFragment0);
189                     addParam(params, "nodeName", StringPool.BLANK);
190                 }
191                 else {
192                     addParam(params, "nodeId", StringPool.BLANK);
193                     addParam(params, "nodeName", urlFragment0);
194                 }
195 
196                 if (urlFragments.length >= 2) {
197                     String urlFragments1 = HttpUtil.decodeURL(urlFragments[1]);
198 
199                     if (urlFragments1.equals("all_pages") ||
200                         urlFragments1.equals("orphan_pages") ||
201                         urlFragments1.equals("recent_changes")) {
202 
203                         addParam(
204                             params, "struts_action",
205                             "/wiki/view_" + urlFragments1);
206                     }
207                     else {
208                         addParam(params, "title", urlFragments1);
209                     }
210                 }
211             }
212 
213             if (urlFragments.length >= 3) {
214                 String windowState = urlFragments[2];
215 
216                 addParam(params, "p_p_state", windowState);
217             }
218         }
219     }
220 
221     private static final String _MAPPING = "wiki";
222 
223     private static final String _PORTLET_ID = PortletKeys.WIKI;
224 
225 }