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