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   */
46  public class WikiFriendlyURLMapper extends BaseFriendlyURLMapper {
47  
48      public String buildPath(LiferayPortletURL portletURL) {
49          String friendlyURLPath = null;
50  
51          String strutsAction = GetterUtil.getString(
52              portletURL.getParameter("struts_action"));
53  
54          if (strutsAction.equals("/wiki/view") ||
55              strutsAction.equals("/wiki/view_all_pages") ||
56              strutsAction.equals("/wiki/view_orphan_pages") ||
57              strutsAction.equals("/wiki/view_recent_changes")) {
58  
59              String nodeId = portletURL.getParameter("nodeId");
60              String nodeName = portletURL.getParameter("nodeName");
61  
62              if (Validator.isNotNull(nodeId) || Validator.isNotNull(nodeName)) {
63                  StringBuilder sb = new StringBuilder();
64  
65                  sb.append(StringPool.SLASH);
66                  sb.append(_MAPPING);
67                  sb.append(StringPool.SLASH);
68  
69                  if (Validator.isNotNull(nodeId)) {
70                      sb.append(nodeId);
71  
72                      portletURL.addParameterIncludedInPath("nodeId");
73                  }
74                  else if (Validator.isNotNull(nodeName)) {
75                      sb.append(nodeName);
76  
77                      portletURL.addParameterIncludedInPath("nodeName");
78                  }
79  
80                  if (strutsAction.equals("/wiki/view")) {
81                      String title = portletURL.getParameter("title");
82  
83                      if (Validator.isNotNull(title)) {
84                          sb.append(StringPool.SLASH);
85                          sb.append(HttpUtil.encodeURL(title));
86  
87                          portletURL.addParameterIncludedInPath("title");
88  
89                          WindowState windowState = portletURL.getWindowState();
90  
91                          if (!windowState.equals(WindowState.NORMAL)) {
92                              sb.append(StringPool.SLASH);
93                              sb.append(windowState);
94                          }
95                      }
96                  }
97                  else {
98                      sb.append(StringPool.SLASH);
99                      sb.append(strutsAction.substring(11));
100                 }
101 
102                 friendlyURLPath = sb.toString();
103             }
104         }
105         else if (strutsAction.equals("/wiki/view_tagged_pages")) {
106             boolean folksonomy = GetterUtil.getBoolean(
107                 portletURL.getParameter("folksonomy"));
108 
109             String tag = portletURL.getParameter("tag");
110 
111             StringBuilder sb = new StringBuilder();
112 
113             if (Validator.isNotNull(tag)) {
114                 sb.append(StringPool.SLASH);
115                 sb.append(_MAPPING);
116                 sb.append(StringPool.SLASH);
117 
118                 if (folksonomy) {
119                     sb.append("tag");
120                 }
121                 else {
122                     sb.append("category");
123                 }
124 
125                 sb.append(StringPool.SLASH);
126 
127                 sb.append(HttpUtil.encodeURL(tag));
128 
129                 portletURL.addParameterIncludedInPath("nodeId");
130                 portletURL.addParameterIncludedInPath("nodeName");
131                 portletURL.addParameterIncludedInPath("tag");
132             }
133 
134             friendlyURLPath = sb.toString();
135         }
136 
137         if (Validator.isNotNull(friendlyURLPath)) {
138             portletURL.addParameterIncludedInPath("p_p_id");
139 
140             portletURL.addParameterIncludedInPath("struts_action");
141         }
142 
143         return friendlyURLPath;
144     }
145 
146     public String getMapping() {
147         return _MAPPING;
148     }
149 
150     public String getPortletId() {
151         return _PORTLET_ID;
152     }
153 
154     public void populateParams(
155         String friendlyURLPath, Map<String, String[]> params) {
156 
157         addParam(params, "p_p_id", _PORTLET_ID);
158         addParam(params, "p_p_lifecycle", "0");
159         addParam(params, "p_p_mode", PortletMode.VIEW);
160 
161         addParam(params, "struts_action", "/wiki/view");
162 
163         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
164 
165         String[] urlFragments = StringUtil.split(
166             friendlyURLPath.substring(x + 1), StringPool.SLASH);
167 
168         if (urlFragments.length >= 1) {
169             String urlFragment0 = urlFragments[0];
170 
171             if (urlFragment0.equals("category") || urlFragment0.equals("tag")) {
172                 if (urlFragments.length >= 2) {
173                     addParam(
174                         params, "struts_action", "/wiki/view_tagged_pages");
175 
176                     String tag = HttpUtil.decodeURL(urlFragments[1]);
177 
178                     boolean folksonomy = TagsEntryConstants.FOLKSONOMY_TAG;
179 
180                     if (urlFragment0.equals("category")) {
181                         folksonomy = TagsEntryConstants.FOLKSONOMY_CATEGORY;
182                     }
183 
184                     addParam(params, "tag", tag);
185                     addParam(params, "folksonomy", folksonomy);
186                 }
187             }
188             else {
189                 if (Validator.isNumber(urlFragment0)) {
190                     addParam(params, "nodeId", urlFragment0);
191                 }
192                 else {
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                         if (urlFragments.length >= 3) {
211                             String windowState = urlFragments[2];
212 
213                             addParam(params, "p_p_state", windowState);
214                         }
215                     }
216                 }
217             }
218         }
219     }
220 
221     private static final String _MAPPING = "wiki";
222 
223     private static final String _PORTLET_ID = PortletKeys.WIKI;
224 
225 }