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    
015    package org.apache.tapestry.portlet;
016    
017    import org.apache.hivemind.util.Defense;
018    import org.apache.tapestry.describe.DescriptionReceiver;
019    import org.apache.tapestry.web.WebRequest;
020    import org.apache.tapestry.web.WebSession;
021    import org.apache.tapestry.web.WebUtils;
022    
023    import javax.portlet.PortletRequest;
024    import javax.portlet.PortletSession;
025    import java.security.Principal;
026    import java.util.List;
027    import java.util.Locale;
028    
029    /**
030     * Implementation of {@link org.apache.tapestry.web.WebRequest} that adapts a
031     * {@link PortletRequest} .
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    public class PortletWebRequest implements WebRequest
037    {
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) return _webSession;
073    
074            PortletSession session = _portletRequest.getPortletSession(create);
075    
076            if (session != null) _webSession = new PortletWebSession(session);
077    
078            return _webSession;
079        }
080    
081        public String getScheme()
082        {
083            return _portletRequest.getScheme();
084        }
085    
086        public String getServerName()
087        {
088            return _portletRequest.getServerName();
089        }
090    
091        public int getServerPort()
092        {
093            return _portletRequest.getServerPort();
094        }
095    
096        /**
097         * Returns "<PortletRequest>", because portlets don't have a notion of
098         * request URI.
099         */
100    
101        public String getRequestURI()
102        {
103            return "<PortletRequest>";
104        }
105    
106        public void forward(String URL)
107        {
108            unsupported("forward");
109        }
110    
111        public String getActivationPath()
112        {
113            return "";
114        }
115    
116        /**
117         * Returns null, always.
118         */
119        public String getPathInfo()
120        {
121            return null;
122        }
123    
124        public List getAttributeNames()
125        {
126            return WebUtils.toSortedList(_portletRequest.getAttributeNames());
127        }
128    
129        public Object getAttribute(String name)
130        {
131            return _portletRequest.getAttribute(name);
132        }
133    
134        public void setAttribute(String name, Object attribute)
135        {
136            if (attribute == null)
137                _portletRequest.removeAttribute(name);
138            else
139                _portletRequest.setAttribute(name, attribute);
140        }
141    
142        protected final void unsupported(String methodName)
143        {
144            throw new UnsupportedOperationException(PortletMessages.unsupportedMethod(methodName));
145        }
146    
147        public void describeTo(DescriptionReceiver receiver)
148        {
149            receiver.describeAlternate(_portletRequest);
150        }
151    
152        public Locale getLocale()
153        {
154            return _portletRequest.getLocale();
155        }
156    
157        public String getHeader(String name)
158        {
159            unsupported("getHeader");
160    
161            return null;
162        }
163    
164        public long getDateHeader(String name)
165        {
166            unsupported("getDateHeader");
167    
168            return -1;
169        }
170    
171        public int getIntHeader(String name)
172        {
173            unsupported("getIntHeader");
174    
175            return -1;
176        }
177    
178        public String getRemoteUser()
179        {
180            return _portletRequest.getRemoteUser();
181        }
182    
183        public Principal getUserPrincipal()
184        {
185            return _portletRequest.getUserPrincipal();
186        }
187    
188        public boolean isUserInRole(String role)
189        {
190            return _portletRequest.isUserInRole(role);
191        }
192        
193        public boolean isSecure()
194        {
195            return _portletRequest.isSecure();
196        }
197    }