1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.ReleaseInfo;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.model.Portlet;
31 import com.liferay.portal.model.PortletApp;
32
33 import java.io.InputStream;
34
35 import java.net.MalformedURLException;
36 import java.net.URL;
37
38 import java.util.Enumeration;
39 import java.util.Set;
40
41 import javax.portlet.PortletContext;
42 import javax.portlet.PortletRequestDispatcher;
43
44 import javax.servlet.RequestDispatcher;
45 import javax.servlet.ServletContext;
46
47
53 public class PortletContextImpl implements PortletContext {
54
55 public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
56 _portlet = portlet;
57 _servletContext = servletContext;
58 _servletContextName = GetterUtil.getString(
59 _servletContext.getServletContextName());
60 }
61
62 public Object getAttribute(String name) {
63 if (name == null) {
64 throw new IllegalArgumentException();
65 }
66
67 return _servletContext.getAttribute(name);
68 }
69
70 public Enumeration<String> getAttributeNames() {
71 return _servletContext.getAttributeNames();
72 }
73
74 public Enumeration<String> getContainerRuntimeOptions() {
75 return null;
76 }
77
78 public String getInitParameter(String name) {
79 if (name == null) {
80 throw new IllegalArgumentException();
81 }
82
83 return _servletContext.getInitParameter(name);
84 }
85
86 public Enumeration<String> getInitParameterNames() {
87 return _servletContext.getInitParameterNames();
88 }
89
90 public int getMajorVersion() {
91 return _MAJOR_VERSION;
92 }
93
94 public String getMimeType(String file) {
95 return _servletContext.getMimeType(file);
96 }
97
98 public int getMinorVersion() {
99 return _MINOR_VERSION;
100 }
101
102 public PortletRequestDispatcher getNamedDispatcher(String name) {
103 RequestDispatcher requestDispatcher = null;
104
105 try {
106 requestDispatcher = _servletContext.getNamedDispatcher(name);
107 }
108 catch (IllegalArgumentException iae) {
109 return null;
110 }
111
112 if (requestDispatcher != null) {
113 return new PortletRequestDispatcherImpl(
114 requestDispatcher, true, this);
115 }
116 else {
117 return null;
118 }
119 }
120
121 public Portlet getPortlet() {
122 return _portlet;
123 }
124
125 public String getPortletContextName() {
126 return _servletContextName;
127 }
128
129 public String getRealPath(String path) {
130 return _servletContext.getRealPath(path);
131 }
132
133 public PortletRequestDispatcher getRequestDispatcher(String path) {
134 RequestDispatcher requestDispatcher = null;
135
136 try {
137 requestDispatcher = _servletContext.getRequestDispatcher(path);
138 }
139 catch (IllegalArgumentException iae) {
140 return null;
141 }
142
143
146 if ((requestDispatcher != null) &&
147 (requestDispatcher.getClass().getName().equals(
148 "org.mortbay.jetty.servlet.Dispatcher"))) {
149
150
152 String rdToString = requestDispatcher.toString();
153
154 String rdPath = rdToString.substring(11, rdToString.indexOf(","));
155
156 if (rdPath.equals(StringPool.SLASH) &&
157 !path.equals(StringPool.SLASH)) {
158
159 requestDispatcher = null;
160 }
161 }
162
163 if (requestDispatcher != null) {
164 return new PortletRequestDispatcherImpl(
165 requestDispatcher, false, this, path);
166 }
167 else {
168 return null;
169 }
170 }
171
172 public URL getResource(String path) throws MalformedURLException {
173 if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
174 throw new MalformedURLException();
175 }
176
177 return _servletContext.getResource(path);
178 }
179
180 public InputStream getResourceAsStream(String path) {
181 return _servletContext.getResourceAsStream(path);
182 }
183
184 public Set<String> getResourcePaths(String path) {
185 return _servletContext.getResourcePaths(path);
186 }
187
188 public String getServerInfo() {
189 return ReleaseInfo.getServerInfo();
190 }
191
192 public ServletContext getServletContext() {
193 return _servletContext;
194 }
195
196 public boolean isWARFile() {
197 PortletApp portletApp = _portlet.getPortletApp();
198
199 return portletApp.isWARFile();
200 }
201
202 public void log(String msg) {
203 if (_log.isInfoEnabled()) {
204 _log.info(msg);
205 }
206 }
207
208 public void log(String msg, Throwable throwable) {
209 if (_log.isInfoEnabled()) {
210 _log.info(msg, throwable);
211 }
212 }
213
214 public void removeAttribute(String name) {
215 if (name == null) {
216 throw new IllegalArgumentException();
217 }
218
219 _servletContext.removeAttribute(name);
220 }
221
222 public void setAttribute(String name, Object obj) {
223 if (name == null) {
224 throw new IllegalArgumentException();
225 }
226
227 _servletContext.setAttribute(name, obj);
228 }
229
230 private static int _MAJOR_VERSION = 2;
231
232 private static int _MINOR_VERSION = 0;
233
234 private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
235
236 private Portlet _portlet;
237 private ServletContext _servletContext;
238 private String _servletContextName;
239
240 }