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 java.security.Principal; 018 import java.util.List; 019 import java.util.Locale; 020 021 import javax.portlet.PortletRequest; 022 import javax.portlet.PortletSession; 023 024 import org.apache.hivemind.util.Defense; 025 import org.apache.tapestry.describe.DescriptionReceiver; 026 import org.apache.tapestry.web.WebRequest; 027 import org.apache.tapestry.web.WebSession; 028 import 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 */ 037 public 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 public List getAttributeNames() 118 { 119 return WebUtils.toSortedList(_portletRequest.getAttributeNames()); 120 } 121 122 public Object getAttribute(String name) 123 { 124 return _portletRequest.getAttribute(name); 125 } 126 127 public void setAttribute(String name, Object attribute) 128 { 129 if (attribute == null) 130 _portletRequest.removeAttribute(name); 131 else 132 _portletRequest.setAttribute(name, attribute); 133 } 134 135 protected final void unsupported(String methodName) 136 { 137 throw new UnsupportedOperationException(PortletMessages.unsupportedMethod(methodName)); 138 } 139 140 public void describeTo(DescriptionReceiver receiver) 141 { 142 receiver.describeAlternate(_portletRequest); 143 } 144 145 public Locale getLocale() 146 { 147 return _portletRequest.getLocale(); 148 } 149 150 public String getHeader(String name) 151 { 152 unsupported("getHeader"); 153 154 return null; 155 } 156 157 public String getRemoteUser() 158 { 159 return _portletRequest.getRemoteUser(); 160 } 161 162 public Principal getUserPrincipal() 163 { 164 return _portletRequest.getUserPrincipal(); 165 } 166 167 public boolean isUserInRole(String role) 168 { 169 return _portletRequest.isUserInRole(role); 170 } 171 }