1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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 != null) &&
68                  windowState.equals(WindowState.MAXIMIZED)) {
69  
70                  friendlyURLPath += StringPool.SLASH + windowState;
71              }
72  
73              portletURL.addParameterIncludedInPath("p_p_id");
74  
75              portletURL.addParameterIncludedInPath("struts_action");
76          }
77  
78          return friendlyURLPath;
79      }
80  
81      public String getMapping() {
82          return _MAPPING;
83      }
84  
85      public String getPortletId() {
86          return _PORTLET_ID;
87      }
88  
89      public void populateParams(
90          String friendlyURLPath, Map<String, String[]> parameterMap) {
91  
92          addParameter(parameterMap, "p_p_id", _PORTLET_ID);
93          addParameter(parameterMap, "p_p_lifecycle", "0");
94          addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
95  
96          int x = friendlyURLPath.indexOf("/", 1);
97          int y = friendlyURLPath.indexOf("/", x + 1);
98  
99          if (y == -1) {
100             y = friendlyURLPath.length();
101         }
102 
103         if ((x + 1) == friendlyURLPath.length()) {
104             addParameter(parameterMap, "struts_action", "/blogs/view");
105 
106             return;
107         }
108 
109         String type = friendlyURLPath.substring(x + 1, y);
110 
111         if (type.equals("rss")) {
112             addParameter(parameterMap, "p_p_lifecycle", "1");
113             addParameter(
114                 parameterMap, "p_p_state", LiferayWindowState.EXCLUSIVE);
115 
116             addParameter(parameterMap, "struts_action", "/blogs/rss");
117         }
118         else if (type.equals("trackback")) {
119             addParameter(parameterMap, "p_p_lifecycle", "1");
120             addParameter(
121                 parameterMap, "p_p_state", LiferayWindowState.EXCLUSIVE);
122 
123             addParameter(parameterMap, "struts_action", "/blogs/trackback");
124 
125             type = friendlyURLPath.substring(y + 1);
126 
127             String entryIdParam = getEntryIdParam(type);
128 
129             if (entryIdParam.equals("urlTitle")) {
130                 type = HttpUtil.decodeURL(type);
131             }
132 
133             addParameter(parameterMap, entryIdParam, type);
134         }
135         else {
136             addParameter(parameterMap, "struts_action", "/blogs/view_entry");
137 
138             String entryIdParam = getEntryIdParam(type);
139 
140             if (entryIdParam.equals("urlTitle")) {
141                 type = HttpUtil.decodeURL(type);
142             }
143 
144             addParameter(parameterMap, entryIdParam, type);
145         }
146 
147         if (friendlyURLPath.indexOf("maximized", x) != -1) {
148             addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
149         }
150     }
151 
152     protected String getEntryIdParam(String type) {
153         if (Validator.isNumber(type)) {
154             return "entryId";
155         }
156         else {
157             return "urlTitle";
158         }
159     }
160 
161     private static final String _MAPPING = "blogs";
162 
163     private static final String _PORTLET_ID = PortletKeys.BLOGS;
164 
165 }