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