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.blogs;
16  
17  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.portlet.LiferayWindowState;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.StringPool;
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="BlogsFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class BlogsFriendlyURLMapper 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("/blogs/rss")) {
45              friendlyURLPath = "/blogs/rss";
46          }
47          else if (strutsAction.equals("/blogs/view_entry")) {
48              String entryId = portletURL.getParameter("entryId");
49  
50              String urlTitle = portletURL.getParameter("urlTitle");
51  
52              if (Validator.isNotNull(entryId)) {
53                  friendlyURLPath = "/blogs/" + entryId;
54  
55                  portletURL.addParameterIncludedInPath("entryId");
56              }
57              else if (Validator.isNotNull(urlTitle)) {
58                  friendlyURLPath = "/blogs/" + HttpUtil.encodeURL(urlTitle);
59  
60                  portletURL.addParameterIncludedInPath("urlTitle");
61              }
62          }
63  
64          if (Validator.isNotNull(friendlyURLPath)) {
65              WindowState windowState = portletURL.getWindowState();
66  
67              if (windowState.equals(WindowState.MAXIMIZED)) {
68                  friendlyURLPath += StringPool.SLASH + windowState;
69              }
70  
71              portletURL.addParameterIncludedInPath("p_p_id");
72  
73              portletURL.addParameterIncludedInPath("struts_action");
74          }
75  
76          return friendlyURLPath;
77      }
78  
79      public String getMapping() {
80          return _MAPPING;
81      }
82  
83      public String getPortletId() {
84          return _PORTLET_ID;
85      }
86  
87      public void populateParams(
88          String friendlyURLPath, Map<String, String[]> parameterMap,
89          Map<String, Object> requestContext) {
90  
91          addParameter(parameterMap, "p_p_id", _PORTLET_ID);
92          addParameter(parameterMap, "p_p_lifecycle", "0");
93          addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
94  
95          int x = friendlyURLPath.indexOf("/", 1);
96          int y = friendlyURLPath.indexOf("/", x + 1);
97  
98          if (y == -1) {
99              y = friendlyURLPath.length();
100         }
101 
102         if ((x + 1) == friendlyURLPath.length()) {
103             addParameter(parameterMap, "struts_action", "/blogs/view");
104 
105             return;
106         }
107 
108         String type = friendlyURLPath.substring(x + 1, y);
109 
110         if (type.equals("rss")) {
111             addParameter(parameterMap, "p_p_lifecycle", "1");
112             addParameter(
113                 parameterMap, "p_p_state", LiferayWindowState.EXCLUSIVE);
114 
115             addParameter(parameterMap, "struts_action", "/blogs/rss");
116         }
117         else if (type.equals("trackback")) {
118             addParameter(parameterMap, "p_p_lifecycle", "1");
119             addParameter(
120                 parameterMap, "p_p_state", LiferayWindowState.EXCLUSIVE);
121 
122             addParameter(parameterMap, "struts_action", "/blogs/trackback");
123 
124             type = friendlyURLPath.substring(y + 1);
125 
126             addParameter(parameterMap, getEntryIdParam(type), type);
127         }
128         else {
129             addParameter(parameterMap, "struts_action", "/blogs/view_entry");
130 
131             addParameter(parameterMap, getEntryIdParam(type), type);
132         }
133 
134         if (friendlyURLPath.indexOf("maximized", x) != -1) {
135             addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
136         }
137     }
138 
139     protected String getEntryIdParam(String type) {
140         if (Validator.isNumber(type)) {
141             return "entryId";
142         }
143         else {
144             return "urlTitle";
145         }
146     }
147 
148     private static final String _MAPPING = "blogs";
149 
150     private static final String _PORTLET_ID = PortletKeys.BLOGS;
151 
152 }