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.util.bridges.php;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.JavaConstants;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.Validator;
027:
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import javax.portlet.PortletConfig;
033: import javax.portlet.RenderRequest;
034: import javax.portlet.RenderResponse;
035:
036: import javax.servlet.ServletConfig;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletRequestWrapper;
039:
040: /**
041: * <a href="PHPServletRequest.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Jorge Ferrer
044: */
045: public class PHPServletRequest extends HttpServletRequestWrapper {
046:
047: public PHPServletRequest(HttpServletRequest req,
048: ServletConfig config, RenderRequest renderReq,
049: RenderResponse renderRes, PortletConfig portletConfig,
050: String phpURI) {
051:
052: super (req);
053:
054: _config = config;
055: _renderReq = renderReq;
056: _renderRes = renderRes;
057: _portletConfig = portletConfig;
058:
059: StringBuffer sb = new StringBuffer();
060:
061: int pos = phpURI.indexOf(StringPool.QUESTION);
062:
063: if (pos != -1) {
064: _path = phpURI.substring(0, pos);
065:
066: sb.append(phpURI.substring(pos + 1));
067: } else {
068: _path = phpURI;
069: }
070:
071: if (GetterUtil.getBoolean(portletConfig
072: .getInitParameter("add-portlet-params"), true)) {
073:
074: sb.append(StringPool.AMPERSAND);
075: sb.append("portlet_namespace");
076: sb.append(StringPool.EQUAL);
077: sb.append(_renderRes.getNamespace());
078: sb.append(StringPool.AMPERSAND);
079: sb.append("portlet_name");
080: sb.append(StringPool.EQUAL);
081: sb.append(_portletConfig.getPortletName());
082: }
083:
084: _queryString = sb.toString();
085:
086: req.setAttribute(
087: JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING,
088: getQueryString());
089: req.setAttribute(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO,
090: getPathInfo());
091: req.setAttribute(
092: JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI,
093: getRequestURI());
094: req
095: .setAttribute(
096: JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH,
097: _path);
098: }
099:
100: public String getContextPath() {
101: return StringPool.SLASH;
102: }
103:
104: public String getParameter(String name) {
105: return _renderReq.getParameter(name);
106: }
107:
108: public Map getParameterMap() {
109: return _renderReq.getParameterMap();
110: }
111:
112: public Enumeration getParameterNames() {
113: return _renderReq.getParameterNames();
114: }
115:
116: public String[] getParameterValues(String name) {
117: return _renderReq.getParameterValues(name);
118: }
119:
120: public String getPathInfo() {
121: return StringPool.BLANK;
122: }
123:
124: public String getPathTranslated() {
125: return StringPool.BLANK;
126: }
127:
128: public String getQueryString() {
129: return _queryString;
130: }
131:
132: public String getRealPath(String path) {
133: return _config.getServletContext().getRealPath(path);
134: }
135:
136: public String getRequestURI() {
137: return _path + StringPool.QUESTION + _queryString;
138: }
139:
140: public StringBuffer getRequestURL() {
141: StringBuffer sb = new StringBuffer();
142:
143: sb.append(getRequest().getProtocol());
144: sb.append("://");
145: sb.append(getRequest().getRemoteHost());
146: sb.append(StringPool.COLON);
147: sb.append(getRequest().getServerPort());
148: sb.append(StringPool.SLASH);
149: sb.append(getRequestURI());
150:
151: return sb;
152: }
153:
154: public String getServletPath() {
155: return _path;
156: }
157:
158: protected Map parseQueryString(String queryString) {
159: Map params = new HashMap();
160:
161: if (Validator.isNull(queryString)) {
162: return params;
163: }
164:
165: int ampersandIndex;
166:
167: do {
168: ampersandIndex = queryString.indexOf(StringPool.AMPERSAND);
169:
170: String nameValuePair;
171:
172: if (ampersandIndex == -1) {
173: nameValuePair = queryString;
174: } else {
175: nameValuePair = queryString
176: .substring(0, ampersandIndex);
177: queryString = queryString.substring(ampersandIndex + 1);
178: }
179:
180: int equalIndex = nameValuePair.indexOf(StringPool.EQUAL);
181:
182: String key = nameValuePair;
183: String value = StringPool.BLANK;
184:
185: if (equalIndex != -1) {
186: key = nameValuePair.substring(0, equalIndex);
187: value = nameValuePair.substring(equalIndex + 1);
188: }
189:
190: params.put(key, value);
191:
192: } while (ampersandIndex != -1);
193:
194: return params;
195: }
196:
197: private ServletConfig _config;
198: private PortletConfig _portletConfig;
199: private RenderRequest _renderReq;
200: private RenderResponse _renderRes;
201: private String _queryString;
202: private String _path;
203:
204: }
|