001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.LiferayPortletContext;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.ReleaseInfo;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.model.Portlet;
024 import com.liferay.portal.model.PortletApp;
025 import com.liferay.portal.security.lang.DoPrivilegedUtil;
026
027 import java.io.InputStream;
028
029 import java.net.MalformedURLException;
030 import java.net.URL;
031
032 import java.util.Enumeration;
033 import java.util.Set;
034
035 import javax.portlet.PortletRequestDispatcher;
036
037 import javax.servlet.RequestDispatcher;
038 import javax.servlet.ServletContext;
039
040
044 public class PortletContextImpl implements LiferayPortletContext {
045
046 public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
047 _portlet = portlet;
048 _servletContext = servletContext;
049 _servletContextName = GetterUtil.getString(
050 _servletContext.getServletContextName());
051 }
052
053 @Override
054 public Object getAttribute(String name) {
055 if (name == null) {
056 throw new IllegalArgumentException();
057 }
058
059 return _servletContext.getAttribute(name);
060 }
061
062 @Override
063 public Enumeration<String> getAttributeNames() {
064 return _servletContext.getAttributeNames();
065 }
066
067 @Override
068 public Enumeration<String> getContainerRuntimeOptions() {
069 return null;
070 }
071
072 @Override
073 public String getInitParameter(String name) {
074 if (name == null) {
075 throw new IllegalArgumentException();
076 }
077
078 return _servletContext.getInitParameter(name);
079 }
080
081 @Override
082 public Enumeration<String> getInitParameterNames() {
083 return _servletContext.getInitParameterNames();
084 }
085
086 @Override
087 public int getMajorVersion() {
088 return _MAJOR_VERSION;
089 }
090
091 @Override
092 public String getMimeType(String file) {
093 return _servletContext.getMimeType(file);
094 }
095
096 @Override
097 public int getMinorVersion() {
098 return _MINOR_VERSION;
099 }
100
101 @Override
102 public PortletRequestDispatcher getNamedDispatcher(String name) {
103 RequestDispatcher requestDispatcher = null;
104
105 try {
106 requestDispatcher = _servletContext.getNamedDispatcher(name);
107 }
108 catch (Throwable t) {
109 if (_log.isWarnEnabled()) {
110 _log.warn(
111 "Unable to get request dispatcher for name " + name, t);
112 }
113
114 return null;
115 }
116
117 if (requestDispatcher != null) {
118 return DoPrivilegedUtil.wrapWhenActive(
119 new PortletRequestDispatcherImpl(
120 requestDispatcher, true, this));
121 }
122 else {
123 return null;
124 }
125 }
126
127 @Override
128 public Portlet getPortlet() {
129 return _portlet;
130 }
131
132 @Override
133 public String getPortletContextName() {
134 return _servletContextName;
135 }
136
137 @Override
138 public String getRealPath(String path) {
139 return _servletContext.getRealPath(path);
140 }
141
142 @Override
143 public PortletRequestDispatcher getRequestDispatcher(String path) {
144 RequestDispatcher requestDispatcher = null;
145
146 try {
147 requestDispatcher = _servletContext.getRequestDispatcher(path);
148 }
149 catch (Throwable t) {
150 if (_log.isWarnEnabled()) {
151 _log.warn(
152 "Unable to get request dispatcher for path " + path, t);
153 }
154
155 return null;
156 }
157
158 if (requestDispatcher != null) {
159 return DoPrivilegedUtil.wrapWhenActive(
160 new PortletRequestDispatcherImpl(
161 requestDispatcher, false, this, path));
162 }
163 else {
164 return null;
165 }
166 }
167
168 @Override
169 public URL getResource(String path) throws MalformedURLException {
170 if ((path == null) || !path.startsWith(StringPool.SLASH)) {
171 throw new MalformedURLException();
172 }
173
174 return _servletContext.getResource(path);
175 }
176
177 @Override
178 public InputStream getResourceAsStream(String path) {
179 return _servletContext.getResourceAsStream(path);
180 }
181
182 @Override
183 public Set<String> getResourcePaths(String path) {
184 return _servletContext.getResourcePaths(path);
185 }
186
187 @Override
188 public String getServerInfo() {
189 return ReleaseInfo.getServerInfo();
190 }
191
192 @Override
193 public ServletContext getServletContext() {
194 return _servletContext;
195 }
196
197 public boolean isWARFile() {
198 PortletApp portletApp = _portlet.getPortletApp();
199
200 return portletApp.isWARFile();
201 }
202
203 @Override
204 public void log(String msg) {
205 if (_log.isInfoEnabled()) {
206 _log.info(msg);
207 }
208 }
209
210 @Override
211 public void log(String msg, Throwable throwable) {
212 if (_log.isInfoEnabled()) {
213 _log.info(msg, throwable);
214 }
215 }
216
217 @Override
218 public void removeAttribute(String name) {
219 if (name == null) {
220 throw new IllegalArgumentException();
221 }
222
223 _servletContext.removeAttribute(name);
224 }
225
226 @Override
227 public void setAttribute(String name, Object obj) {
228 if (name == null) {
229 throw new IllegalArgumentException();
230 }
231
232 _servletContext.setAttribute(name, obj);
233 }
234
235 private static final int _MAJOR_VERSION = 2;
236
237 private static final int _MINOR_VERSION = 0;
238
239 private static final Log _log = LogFactoryUtil.getLog(
240 PortletContextImpl.class);
241
242 private final Portlet _portlet;
243 private final ServletContext _servletContext;
244 private final String _servletContextName;
245
246 }