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