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