001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.portlet;
016
017import java.security.Principal;
018import java.util.List;
019import java.util.Locale;
020
021import javax.portlet.PortletRequest;
022import javax.portlet.PortletSession;
023
024import org.apache.hivemind.util.Defense;
025import org.apache.tapestry.describe.DescriptionReceiver;
026import org.apache.tapestry.web.WebRequest;
027import org.apache.tapestry.web.WebSession;
028import org.apache.tapestry.web.WebUtils;
029
030/**
031 * Implementation of {@link org.apache.tapestry.web.WebRequest} that adapts a
032 * {@link PortletRequest).
033 * 
034 * @author Howard M. Lewis Ship
035 * @since 4.0
036 */
037public class PortletWebRequest implements WebRequest
038{
039    private final PortletRequest _portletRequest;
040
041    private WebSession _webSession;
042
043    public PortletWebRequest(PortletRequest portletRequest)
044    {
045        Defense.notNull(portletRequest, "portletRequest");
046
047        _portletRequest = portletRequest;
048    }
049
050    public List getParameterNames()
051    {
052        return WebUtils.toSortedList(_portletRequest.getParameterNames());
053    }
054
055    public String getParameterValue(String parameterName)
056    {
057        return _portletRequest.getParameter(parameterName);
058    }
059
060    public String[] getParameterValues(String parameterName)
061    {
062        return _portletRequest.getParameterValues(parameterName);
063    }
064
065    public String getContextPath()
066    {
067        return _portletRequest.getContextPath();
068    }
069
070    public WebSession getSession(boolean create)
071    {
072        if (_webSession != null)
073            return _webSession;
074
075        PortletSession session = _portletRequest.getPortletSession(create);
076
077        if (session != null)
078            _webSession = new PortletWebSession(session);
079
080        return _webSession;
081    }
082
083    public String getScheme()
084    {
085        return _portletRequest.getScheme();
086    }
087
088    public String getServerName()
089    {
090        return _portletRequest.getServerName();
091    }
092
093    public int getServerPort()
094    {
095        return _portletRequest.getServerPort();
096    }
097
098    /**
099     * Returns "<PortletRequest>", because portlets don't have a notion of request URI.
100     */
101
102    public String getRequestURI()
103    {
104        return "<PortletRequest>";
105    }
106
107    public void forward(String URL)
108    {
109        unsupported("forward");
110    }
111
112    public String getActivationPath()
113    {
114        return "";
115    }
116
117    /**
118     * Returns null, always.
119     */
120    public String getPathInfo()
121    {
122        return null;
123    }
124
125    public List getAttributeNames()
126    {
127        return WebUtils.toSortedList(_portletRequest.getAttributeNames());
128    }
129
130    public Object getAttribute(String name)
131    {
132        return _portletRequest.getAttribute(name);
133    }
134
135    public void setAttribute(String name, Object attribute)
136    {
137        if (attribute == null)
138            _portletRequest.removeAttribute(name);
139        else
140            _portletRequest.setAttribute(name, attribute);
141    }
142
143    protected final void unsupported(String methodName)
144    {
145        throw new UnsupportedOperationException(PortletMessages.unsupportedMethod(methodName));
146    }
147
148    public void describeTo(DescriptionReceiver receiver)
149    {
150        receiver.describeAlternate(_portletRequest);
151    }
152
153    public Locale getLocale()
154    {
155        return _portletRequest.getLocale();
156    }
157
158    public String getHeader(String name)
159    {
160        unsupported("getHeader");
161
162        return null;
163    }
164
165    public String getRemoteUser()
166    {
167        return _portletRequest.getRemoteUser();
168    }
169
170    public Principal getUserPrincipal()
171    {
172        return _portletRequest.getUserPrincipal();
173    }
174
175    public boolean isUserInRole(String role)
176    {
177        return _portletRequest.isUserInRole(role);
178    }
179}