1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs;
21  
22  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
23  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
24  import com.liferay.portal.kernel.portlet.LiferayWindowState;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.util.PortletKeys;
29  
30  import java.util.Map;
31  
32  import javax.portlet.PortletMode;
33  import javax.portlet.WindowState;
34  
35  /**
36   * <a href="BlogsFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class BlogsFriendlyURLMapper extends BaseFriendlyURLMapper {
42  
43      public String buildPath(LiferayPortletURL portletURL) {
44          String friendlyURLPath = null;
45  
46          String strutsAction = GetterUtil.getString(
47              portletURL.getParameter("struts_action"));
48  
49          if (strutsAction.equals("/blogs/rss")) {
50              friendlyURLPath = "/blogs/rss";
51          }
52          else if (strutsAction.equals("/blogs/view_entry")) {
53              String entryId = portletURL.getParameter("entryId");
54  
55              String urlTitle = portletURL.getParameter("urlTitle");
56  
57              if (Validator.isNotNull(entryId)) {
58                  friendlyURLPath = "/blogs/" + entryId;
59  
60                  portletURL.addParameterIncludedInPath("entryId");
61              }
62              else if (Validator.isNotNull(urlTitle)) {
63                  friendlyURLPath = "/blogs/" + urlTitle;
64  
65                  portletURL.addParameterIncludedInPath("urlTitle");
66              }
67          }
68  
69          if (Validator.isNotNull(friendlyURLPath)) {
70              portletURL.addParameterIncludedInPath("p_p_id");
71  
72              portletURL.addParameterIncludedInPath("struts_action");
73  
74              WindowState windowState = portletURL.getWindowState();
75  
76              if (windowState.equals(WindowState.MAXIMIZED)) {
77                  friendlyURLPath += StringPool.SLASH + windowState;
78              }
79          }
80  
81          return friendlyURLPath;
82      }
83  
84      public String getMapping() {
85          return _MAPPING;
86      }
87  
88      public String getPortletId() {
89          return _PORTLET_ID;
90      }
91  
92      public void populateParams(
93          String friendlyURLPath, Map<String, String[]> params) {
94  
95          addParam(params, "p_p_id", _PORTLET_ID);
96          addParam(params, "p_p_lifecycle", "0");
97          addParam(params, "p_p_mode", PortletMode.VIEW);
98  
99          int x = friendlyURLPath.indexOf("/", 1);
100         int y = friendlyURLPath.indexOf("/", x + 1);
101 
102         if (y == -1) {
103             y = friendlyURLPath.length();
104         }
105 
106         if ((x + 1) == friendlyURLPath.length()) {
107             addParam(params, "struts_action", "/blogs/view");
108 
109             return;
110         }
111 
112         String type = friendlyURLPath.substring(x + 1, y);
113 
114         if (type.equals("rss")) {
115             addParam(params, "p_p_lifecycle", "1");
116             addParam(params, "p_p_state", LiferayWindowState.EXCLUSIVE);
117 
118             addParam(params, "struts_action", "/blogs/rss");
119         }
120         else if (type.equals("trackback")) {
121             addParam(params, "p_p_lifecycle", "1");
122             addParam(params, "p_p_state", LiferayWindowState.EXCLUSIVE);
123 
124             addParam(params, "struts_action", "/blogs/trackback");
125 
126             type = friendlyURLPath.substring(y + 1);
127 
128             addParam(params, getEntryIdParam(type), type);
129         }
130         else {
131             addParam(params, "struts_action", "/blogs/view_entry");
132 
133             addParam(params, getEntryIdParam(type), type);
134         }
135 
136         if (friendlyURLPath.indexOf("maximized", x) != -1) {
137             addParam(params, "p_p_state", WindowState.MAXIMIZED);
138         }
139     }
140 
141     protected String getEntryIdParam(String type) {
142         if (Validator.isNumber(type)) {
143             return "entryId";
144         }
145         else {
146             return "urlTitle";
147         }
148     }
149 
150     private static final String _MAPPING = "blogs";
151 
152     private static final String _PORTLET_ID = PortletKeys.BLOGS;
153 
154 }