001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.struts;
022:
023: import com.liferay.portal.kernel.portlet.LiferayPortletURL;
024: import com.liferay.portal.kernel.servlet.URLEncoder;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.util.HttpUtil;
030:
031: import java.util.HashMap;
032:
033: import javax.portlet.PortletMode;
034: import javax.portlet.PortletModeException;
035: import javax.portlet.WindowState;
036: import javax.portlet.WindowStateException;
037:
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: /**
044: * <a href="StrutsURLEncoder.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class StrutsURLEncoder implements URLEncoder {
050:
051: public static void setParameters(LiferayPortletURL portletURL,
052: String queryString) {
053:
054: String[] params = StringUtil.split(queryString, "&");
055:
056: for (int i = 0; i < params.length; i++) {
057: int pos = params[i].indexOf("=");
058:
059: if (pos != -1) {
060: String param = params[i].substring(0, pos);
061: String value = params[i].substring(pos + 1, params[i]
062: .length());
063:
064: if (param.equals("windowState")) {
065: try {
066: portletURL
067: .setWindowState(new WindowState(value));
068: } catch (WindowStateException wse) {
069: wse.printStackTrace();
070: }
071: } else if (param.equals("portletMode")) {
072: try {
073: portletURL
074: .setPortletMode(new PortletMode(value));
075: } catch (PortletModeException pme) {
076: pme.printStackTrace();
077: }
078: } else if (param.equals("actionURL")) {
079: portletURL.setAction(GetterUtil.getBoolean(value));
080: } else {
081: portletURL.setParameter(param, HttpUtil
082: .decodeURL(value), true);
083: }
084: }
085: }
086: }
087:
088: public StrutsURLEncoder(String contextPath, String mainPath,
089: String servletMapping, LiferayPortletURL portletURL) {
090:
091: _contextPath = contextPath;
092: _mainPath = mainPath;
093: _setServletMapping(servletMapping);
094: _portletURL = portletURL;
095: _windowState = portletURL.getWindowState();
096: _portletMode = portletURL.getPortletMode();
097: }
098:
099: public String encodeURL(HttpServletResponse res, String path) {
100: if (_log.isDebugEnabled()) {
101: _log.debug("Path " + path);
102: _log.debug("Context path " + _contextPath);
103: _log.debug("Servlet mapping " + _servletMapping);
104: }
105:
106: String encodedURL = path;
107:
108: if (path.startsWith("//") || path.startsWith(_contextPath)
109: || path.startsWith(_servletMapping)) {
110:
111: // Struts uses & instead of & to delimit parameter key value
112: // pairs when you set the "name" attribute for html:link.
113:
114: path = StringUtil.replace(path, "&", "&");
115:
116: // Reset portlet URL settings so it can be reused
117:
118: try {
119: _portletURL.setWindowState(_windowState);
120: } catch (WindowStateException wse) {
121: }
122:
123: try {
124: _portletURL.setPortletMode(_portletMode);
125: } catch (PortletModeException pme) {
126: }
127:
128: _portletURL.setParameters(new HashMap());
129: _portletURL.setAction(false);
130:
131: // Separate the Struts action from the query string
132:
133: String strutsAction = path;
134: String queryString = StringPool.BLANK;
135:
136: int pos = strutsAction.indexOf(StringPool.QUESTION);
137:
138: if (pos != -1) {
139: strutsAction = path.substring(0, pos);
140: queryString = path.substring(pos + 1, path.length());
141: }
142:
143: // Set the Struts action
144:
145: if (strutsAction.startsWith("c/")) {
146: strutsAction = strutsAction.substring(1);
147: } else if (strutsAction.startsWith("/c/")) {
148: strutsAction = strutsAction.substring(2);
149: }
150:
151: if (Validator.isNotNull(_contextPath)) {
152: strutsAction = strutsAction.substring(_contextPath
153: .length(), strutsAction.length());
154: }
155:
156: if (strutsAction.startsWith(_servletMapping)) {
157: strutsAction = strutsAction.substring(_servletMapping
158: .length(), strutsAction.length());
159: }
160:
161: if (!strutsAction.startsWith(StringPool.SLASH)) {
162: strutsAction = StringPool.SLASH + strutsAction;
163: }
164:
165: if (_log.isDebugEnabled()) {
166: _log.debug("Struts action " + strutsAction);
167: }
168:
169: _portletURL.setParameter("struts_action", strutsAction);
170:
171: // Set the query string
172:
173: setParameters(_portletURL, queryString);
174:
175: // Return the portlet URL
176:
177: encodedURL = _portletURL.toString();
178:
179: if (_log.isDebugEnabled()) {
180: _log.debug("Encoded portlet URL " + encodedURL);
181: }
182: }
183:
184: return encodedURL;
185: }
186:
187: private void _setServletMapping(String servletMapping) {
188: if (servletMapping != null) {
189:
190: // See org.apache.struts.util.RequestUtils.getActionMappingURL
191:
192: if (servletMapping.endsWith("/*")) {
193: int pos = 0;
194:
195: if (servletMapping.startsWith(_mainPath)) {
196: pos = _mainPath.length() - 2;
197: }
198:
199: _servletMapping = servletMapping.substring(pos,
200: servletMapping.length() - 1);
201: }
202: }
203: }
204:
205: private static Log _log = LogFactory.getLog(StrutsURLEncoder.class);
206:
207: private String _contextPath;
208: private String _mainPath;
209: private String _servletMapping = StringPool.BLANK;
210: private LiferayPortletURL _portletURL;
211: private WindowState _windowState;
212: private PortletMode _portletMode;
213:
214: }
|