001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.php;
016    
017    import com.liferay.portal.kernel.util.JavaConstants;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.util.Enumeration;
022    import java.util.Map;
023    
024    import javax.portlet.PortletConfig;
025    import javax.portlet.RenderRequest;
026    import javax.portlet.RenderResponse;
027    
028    import javax.servlet.ServletConfig;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletRequestWrapper;
031    
032    /**
033     * @author Jorge Ferrer
034     */
035    public class PHPServletRequest extends HttpServletRequestWrapper {
036    
037            public PHPServletRequest(
038                    HttpServletRequest request, ServletConfig servletConfig,
039                    RenderRequest renderRequest, RenderResponse renderResponse,
040                    PortletConfig portletConfig, String phpURI, boolean addPortletParams) {
041    
042                    super(request);
043    
044                    _servletConfig = servletConfig;
045                    _renderRequest = renderRequest;
046                    _renderResponse = renderResponse;
047                    _portletConfig = portletConfig;
048    
049                    StringBundler sb = new StringBundler();
050    
051                    int pos = phpURI.indexOf(StringPool.QUESTION);
052    
053                    if (pos != -1) {
054                            _path = phpURI.substring(0, pos);
055    
056                            sb.append(phpURI.substring(pos + 1));
057                    }
058                    else {
059                            _path = phpURI;
060                    }
061    
062                    if (addPortletParams) {
063                            sb.append(StringPool.AMPERSAND);
064                            sb.append("portlet_namespace");
065                            sb.append(StringPool.EQUAL);
066                            sb.append(_renderResponse.getNamespace());
067                            sb.append(StringPool.AMPERSAND);
068                            sb.append("portlet_name");
069                            sb.append(StringPool.EQUAL);
070                            sb.append(_portletConfig.getPortletName());
071                    }
072    
073                    _queryString = sb.toString();
074    
075                    request.setAttribute(
076                            JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING, getQueryString());
077                    request.setAttribute(
078                            JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO, getPathInfo());
079                    request.setAttribute(
080                            JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI, getRequestURI());
081                    request.setAttribute(
082                            JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH, _path);
083            }
084    
085            public String getContextPath() {
086                    return StringPool.SLASH;
087            }
088    
089            public String getParameter(String name) {
090                    return _renderRequest.getParameter(name);
091            }
092    
093            public Map<String, String[]> getParameterMap() {
094                    return _renderRequest.getParameterMap();
095            }
096    
097            public Enumeration<String> getParameterNames() {
098                    return _renderRequest.getParameterNames();
099            }
100    
101            public String[] getParameterValues(String name) {
102                    return _renderRequest.getParameterValues(name);
103            }
104    
105            public String getPathInfo() {
106                    return StringPool.BLANK;
107            }
108    
109            public String getPathTranslated() {
110                    return StringPool.BLANK;
111            }
112    
113            public String getQueryString() {
114                    return _queryString;
115            }
116    
117            public String getRealPath(String path) {
118                    return _servletConfig.getServletContext().getRealPath(path);
119            }
120    
121            public String getRequestURI() {
122                    return _path + StringPool.QUESTION + _queryString;
123            }
124    
125            public StringBuffer getRequestURL() {
126                    StringBuffer sb = new StringBuffer();
127    
128                    sb.append(getRequest().getProtocol());
129                    sb.append("://");
130                    sb.append(getRequest().getRemoteHost());
131                    sb.append(StringPool.COLON);
132                    sb.append(getRequest().getServerPort());
133                    sb.append(StringPool.SLASH);
134                    sb.append(getRequestURI());
135    
136                    return sb;
137            }
138    
139            public String getServletPath() {
140                    return _path;
141            }
142    
143            private ServletConfig _servletConfig;
144            private PortletConfig _portletConfig;
145            private RenderRequest _renderRequest;
146            private RenderResponse _renderResponse;
147            private String _queryString;
148            private String _path;
149    
150    }