1   /**
2    * Copyright (c) 2000-2008 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.StringMaker;
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  
34  import java.util.Map;
35  
36  import javax.portlet.PortletMode;
37  import javax.portlet.WindowState;
38  
39  /**
40   * <a href="WikiFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Jorge Ferrer
43   *
44   */
45  public class WikiFriendlyURLMapper extends BaseFriendlyURLMapper {
46  
47      public String getMapping() {
48          return _MAPPING;
49      }
50  
51      public String getPortletId() {
52          return _PORTLET_ID;
53      }
54  
55      public String buildPath(LiferayPortletURL portletURL) {
56          String friendlyURLPath = null;
57  
58          String strutsAction = GetterUtil.getString(
59              portletURL.getParameter("struts_action"));
60  
61          if (strutsAction.equals("/wiki/view")) {
62              String nodeId = portletURL.getParameter("nodeId");
63              String nodeName = portletURL.getParameter("nodeName");
64              String title = portletURL.getParameter("title");
65  
66              if (Validator.isNotNull(nodeId) || Validator.isNotNull(nodeName)) {
67                  StringMaker sm = new StringMaker();
68  
69                  sm.append(StringPool.SLASH);
70                  sm.append(_MAPPING);
71                  sm.append(StringPool.SLASH);
72  
73                  if (Validator.isNotNull(nodeId)) {
74                      sm.append(nodeId);
75  
76                      portletURL.addParameterIncludedInPath("nodeId");
77                  }
78                  else if (Validator.isNotNull(nodeName)) {
79                      sm.append(nodeName);
80  
81                      portletURL.addParameterIncludedInPath("nodeName");
82                  }
83  
84                  if (Validator.isNotNull(title)) {
85                      sm.append(StringPool.SLASH);
86                      sm.append(title);
87  
88                      portletURL.addParameterIncludedInPath("title");
89  
90                      WindowState windowState = portletURL.getWindowState();
91  
92                      if (!windowState.equals(WindowState.NORMAL)) {
93                          sm.append(StringPool.SLASH);
94                          sm.append(windowState);
95                      }
96                  }
97  
98                  friendlyURLPath = sm.toString();
99              }
100         }
101 
102         if (Validator.isNotNull(friendlyURLPath)) {
103             portletURL.addParameterIncludedInPath("p_p_id");
104             portletURL.addParameterIncludedInPath("struts_action");
105         }
106 
107         return friendlyURLPath;
108     }
109 
110     public void populateParams(
111         String friendlyURLPath, Map<String, String[]> params) {
112 
113         addParam(params, "p_p_id", _PORTLET_ID);
114         addParam(params, "p_p_lifecycle", "0");
115         addParam(params, "p_p_mode", PortletMode.VIEW);
116 
117         addParam(params, "struts_action", "/wiki/view");
118 
119         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
120 
121         String[] urlFragments = StringUtil.split(
122             friendlyURLPath.substring(x + 1), StringPool.SLASH);
123 
124         if (urlFragments.length >= 1) {
125             String nodeId = urlFragments[0];
126 
127             if (Validator.isNumber(nodeId)) {
128                 addParam(params, "nodeId", nodeId);
129             }
130             else {
131                 addParam(params, "nodeName", nodeId);
132             }
133 
134             if (urlFragments.length >= 2) {
135                 String title = urlFragments[1];
136 
137                 addParam(params, "title", title);
138 
139                 if (urlFragments.length >= 3) {
140                     String windowState = urlFragments[2];
141 
142                     addParam(params, "p_p_state", windowState);
143                 }
144             }
145         }
146     }
147 
148     private static final String _MAPPING = "wiki";
149 
150     private static final String _PORTLET_ID = PortletKeys.WIKI;
151 
152 }