1
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
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 }