001    /**
002     * Copyright (c) 2000-2013 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.CharPool;
018    import com.liferay.portal.kernel.util.JavaConstants;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.util.Enumeration;
023    import java.util.Map;
024    
025    import javax.portlet.PortletConfig;
026    import javax.portlet.RenderRequest;
027    import javax.portlet.RenderResponse;
028    
029    import javax.servlet.ServletConfig;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletRequestWrapper;
032    
033    /**
034     * @author Jorge Ferrer
035     */
036    public class PHPServletRequest extends HttpServletRequestWrapper {
037    
038            public PHPServletRequest(
039                    HttpServletRequest request, ServletConfig servletConfig,
040                    RenderRequest renderRequest, RenderResponse renderResponse,
041                    PortletConfig portletConfig, String phpURI, boolean addPortletParams) {
042    
043                    super(request);
044    
045                    _servletConfig = servletConfig;
046                    _renderRequest = renderRequest;
047                    _renderResponse = renderResponse;
048                    _portletConfig = portletConfig;
049    
050                    StringBundler sb = new StringBundler();
051    
052                    int pos = phpURI.indexOf(CharPool.QUESTION);
053    
054                    if (pos != -1) {
055                            _path = phpURI.substring(0, pos);
056    
057                            sb.append(phpURI.substring(pos + 1));
058                    }
059                    else {
060                            _path = phpURI;
061                    }
062    
063                    if (addPortletParams) {
064                            sb.append(StringPool.AMPERSAND);
065                            sb.append("portlet_namespace");
066                            sb.append(StringPool.EQUAL);
067                            sb.append(_renderResponse.getNamespace());
068                            sb.append(StringPool.AMPERSAND);
069                            sb.append("portlet_name");
070                            sb.append(StringPool.EQUAL);
071                            sb.append(_portletConfig.getPortletName());
072                    }
073    
074                    _queryString = sb.toString();
075    
076                    request.setAttribute(
077                            JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING, getQueryString());
078                    request.setAttribute(
079                            JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO, getPathInfo());
080                    request.setAttribute(
081                            JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI, getRequestURI());
082                    request.setAttribute(
083                            JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH, _path);
084            }
085    
086            @Override
087            public String getContextPath() {
088                    return StringPool.SLASH;
089            }
090    
091            @Override
092            public String getParameter(String name) {
093                    return _renderRequest.getParameter(name);
094            }
095    
096            @Override
097            public Map<String, String[]> getParameterMap() {
098                    return _renderRequest.getParameterMap();
099            }
100    
101            @Override
102            public Enumeration<String> getParameterNames() {
103                    return _renderRequest.getParameterNames();
104            }
105    
106            @Override
107            public String[] getParameterValues(String name) {
108                    return _renderRequest.getParameterValues(name);
109            }
110    
111            @Override
112            public String getPathInfo() {
113                    return StringPool.BLANK;
114            }
115    
116            @Override
117            public String getPathTranslated() {
118                    return StringPool.BLANK;
119            }
120    
121            @Override
122            public String getQueryString() {
123                    return _queryString;
124            }
125    
126            @Override
127            public String getRealPath(String path) {
128                    return _servletConfig.getServletContext().getRealPath(path);
129            }
130    
131            @Override
132            public String getRequestURI() {
133                    return _path + StringPool.QUESTION + _queryString;
134            }
135    
136            @Override
137            public StringBuffer getRequestURL() {
138                    StringBuffer sb = new StringBuffer();
139    
140                    sb.append(getRequest().getProtocol());
141                    sb.append("://");
142                    sb.append(getRequest().getRemoteHost());
143                    sb.append(StringPool.COLON);
144                    sb.append(getRequest().getServerPort());
145                    sb.append(StringPool.SLASH);
146                    sb.append(getRequestURI());
147    
148                    return sb;
149            }
150    
151            @Override
152            public String getServletPath() {
153                    return _path;
154            }
155    
156            private String _path;
157            private PortletConfig _portletConfig;
158            private String _queryString;
159            private RenderRequest _renderRequest;
160            private RenderResponse _renderResponse;
161            private ServletConfig _servletConfig;
162    
163    }