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[]> params) {
89  
90          addParam(params, "p_p_id", _PORTLET_ID);
91          addParam(params, "p_p_lifecycle", "0");
92          addParam(params, "p_p_mode", PortletMode.VIEW);
93  
94          int x = friendlyURLPath.indexOf("/", 1);
95          int y = friendlyURLPath.indexOf("/", x + 1);
96  
97          if (y == -1) {
98              y = friendlyURLPath.length();
99          }
100 
101         if ((x + 1) == friendlyURLPath.length()) {
102             addParam(params, "struts_action", "/blogs/view");
103 
104             return;
105         }
106 
107         String type = friendlyURLPath.substring(x + 1, y);
108 
109         if (type.equals("rss")) {
110             addParam(params, "p_p_lifecycle", "1");
111             addParam(params, "p_p_state", LiferayWindowState.EXCLUSIVE);
112 
113             addParam(params, "struts_action", "/blogs/rss");
114         }
115         else if (type.equals("trackback")) {
116             addParam(params, "p_p_lifecycle", "1");
117             addParam(params, "p_p_state", LiferayWindowState.EXCLUSIVE);
118 
119             addParam(params, "struts_action", "/blogs/trackback");
120 
121             type = friendlyURLPath.substring(y + 1);
122 
123             addParam(params, getEntryIdParam(type), type);
124         }
125         else {
126             addParam(params, "struts_action", "/blogs/view_entry");
127 
128             addParam(params, getEntryIdParam(type), type);
129         }
130 
131         if (friendlyURLPath.indexOf("maximized", x) != -1) {
132             addParam(params, "p_p_state", WindowState.MAXIMIZED);
133         }
134     }
135 
136     protected String getEntryIdParam(String type) {
137         if (Validator.isNumber(type)) {
138             return "entryId";
139         }
140         else {
141             return "urlTitle";
142         }
143     }
144 
145     private static final String _MAPPING = "blogs";
146 
147     private static final String _PORTLET_ID = PortletKeys.BLOGS;
148 
149 }