KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > deli > DeliImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.deli;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.security.Principal JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.servlet.RequestDispatcher JavaDoc;
30 import javax.servlet.Servlet JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.ServletInputStream JavaDoc;
34 import javax.servlet.http.Cookie JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpSession JavaDoc;
37
38 import org.apache.avalon.framework.activity.Disposable;
39 import org.apache.avalon.framework.activity.Initializable;
40 import org.apache.avalon.framework.context.Context;
41 import org.apache.avalon.framework.context.ContextException;
42 import org.apache.avalon.framework.context.Contextualizable;
43 import org.apache.avalon.framework.logger.AbstractLogEnabled;
44 import org.apache.avalon.framework.parameters.Parameterizable;
45 import org.apache.avalon.framework.parameters.Parameters;
46 import org.apache.avalon.framework.service.ServiceException;
47 import org.apache.avalon.framework.service.ServiceManager;
48 import org.apache.avalon.framework.service.Serviceable;
49 import org.apache.avalon.framework.thread.ThreadSafe;
50 import org.apache.cocoon.Constants;
51 import org.apache.cocoon.environment.Request;
52 import org.apache.excalibur.xml.dom.DOMParser;
53 import org.w3c.dom.Document JavaDoc;
54 import org.w3c.dom.Element JavaDoc;
55 import org.w3c.dom.Text JavaDoc;
56
57 import com.hp.hpl.deli.Profile;
58 import com.hp.hpl.deli.ProfileAttribute;
59 import com.hp.hpl.deli.Workspace;
60
61 /**
62  * Allows the use of <a HREF="http://www-uk.hpl.hp.com/people/marbut/">DELI</a>
63  * to provide <a HREF="http://www.w3.org/Mobile/CCPP/">CC/PP</a> or
64  * <a HREF="http://www1.wapforum.org/tech/terms.asp?doc=WAP-248-UAProf-20010530-p.pdf">UAProf</a>
65  * support. For more details of DELI see the Technical Report
66  * <a HREF="http://www-uk.hpl.hp.com/people/marbut/DeliUserGuideWEB.htm">DELI:
67  * A Delivery Context Library for CC/PP and UAProf</a>.
68  *
69  * @author <a HREF="mailto:marbut@hplb.hpl.hp.com">Mark H. Butler</a>
70  * @version CVS $Id: DeliImpl.java 279802 2005-09-09 15:44:17Z jheymans $
71  */

72 public final class DeliImpl extends AbstractLogEnabled
73     implements Parameterizable, Deli, Serviceable, Disposable, Initializable,
74                ThreadSafe, Contextualizable {
75                
76      /** The name of the main DELI configuration file */
77      private String JavaDoc deliConfig = "deli/config/deliConfig.xml";
78  
79     /** The service manager */
80     protected ServiceManager manager = null;
81
82     /** Parser used to construct the DOM tree to import the profile to a stylesheet */
83     protected DOMParser parser;
84
85     /** A context, used to retrieve the path to the configuration file */
86     protected CocoonServletContext servletContext;
87
88     /** Contextualize this class */
89     public void contextualize(Context JavaDoc context) throws ContextException {
90         org.apache.cocoon.environment.Context ctx =
91             (org.apache.cocoon.environment.Context)context.get(
92                 Constants.CONTEXT_ENVIRONMENT_CONTEXT);
93         this.servletContext = new CocoonServletContext(ctx);
94     }
95
96     /** Service this class */
97     public void service(ServiceManager manager) throws ServiceException {
98         this.manager = manager;
99         try {
100             this.parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
101         } catch (ServiceException e) {
102             getLogger().error("DELI Exception while creating parser: ", e);
103             throw e;
104         }
105     }
106
107     /** Configure this class */
108     public void parameterize(Parameters params) {
109         this.deliConfig = params.getParameter("deli-config-file", this.deliConfig);
110     }
111
112     /**
113      * Initialize
114      */

115     public void initialize() throws Exception JavaDoc {
116         try {
117             Workspace.getInstance().configure(this.servletContext, this.deliConfig);
118         } catch (Exception JavaDoc e) {
119             getLogger().error("DELI Exception while creating workspace: ", e);
120             throw e;
121         }
122     }
123
124     /** Dispose of this class */
125     public void dispose() {
126         if (parser != null) {
127             this.manager.release(parser);
128         }
129         this.parser = null;
130     }
131
132     /** Process a HttpServletRequest and either extract
133      * CC/PP or UAProf information from it and use this information
134      * to resolve a profile or examine the user agent string, match
135      * this using the DELI legacy device database, and use this
136      * information to retrieve the appropriate CC/PP profile.
137      *
138      * @param theRequest The Request.
139      * @return The profile as a vector of profile attributes.
140      * @throws IOException
141      * @throws ServletException
142      * @throws Exception
143      */

144     public Profile getProfile(Request JavaDoc theRequest)
145         throws IOException JavaDoc, ServletException JavaDoc, Exception JavaDoc {
146         Profile theProfile = null;
147         try {
148             CocoonServletRequest servletRequest = new CocoonServletRequest(theRequest);
149             theProfile = new Profile(servletRequest);
150         } catch (Exception JavaDoc e) {
151             getLogger().error("DELI Exception while retrieving profile: ", e);
152             throw e;
153         }
154         return theProfile;
155     }
156
157     /** Convert a profile stored as a vector of profile attributes
158      * to a DOM tree.
159      *
160      * @param theProfile The profile as a vector of profile attributes.
161      * @return The DOM tree.
162      */

163     public Document JavaDoc getUACapabilities(Profile theProfile) throws Exception JavaDoc {
164         Document JavaDoc document = null;
165         try {
166             Element JavaDoc rootElement;
167             Element JavaDoc attributeNode;
168             Element JavaDoc complexAttributeNode;
169             Text JavaDoc text;
170
171             document = parser.createDocument();
172             rootElement = document.createElementNS(null, "browser");
173             document.appendChild(rootElement);
174
175             Iterator JavaDoc profileIter = theProfile.iterator();
176             while (profileIter.hasNext()) {
177                 ProfileAttribute p = (ProfileAttribute)profileIter.next();
178                 attributeNode = document.createElementNS(null, p.getAttribute());
179                 rootElement.appendChild(attributeNode);
180                 Vector JavaDoc attributeValue = p.get();
181         if (attributeValue != null)
182         {
183             Iterator JavaDoc complexValueIter = attributeValue.iterator();
184             if (p.getCollectionType().equals("Simple")) {
185                 // Simple attribute
186
String JavaDoc value = (String JavaDoc)complexValueIter.next();
187                 text = document.createTextNode(value);
188                 attributeNode.appendChild(text);
189             } else {
190                 // Complex attribute e.g. Seq or Bag
191
while (complexValueIter.hasNext()) {
192                     String JavaDoc value = (String JavaDoc)complexValueIter.next();
193                     complexAttributeNode = document.createElementNS(null, "li");
194                     attributeNode.appendChild(complexAttributeNode);
195                     text = document.createTextNode(value);
196                     complexAttributeNode.appendChild(text);
197                 }
198             }
199                     }
200                 }
201         } catch (Exception JavaDoc e) {
202             getLogger().error("DELI Exception while converting profile to DOM fragment: ", e);
203             throw e;
204         }
205         return document;
206     }
207
208     public Document JavaDoc getUACapabilities(Request JavaDoc theRequest)
209         throws IOException JavaDoc, Exception JavaDoc {
210         return this.getUACapabilities(this.getProfile(theRequest));
211     }
212
213     /**
214      * Stub implementation of Servlet Context
215      */

216     public static class CocoonServletContext implements ServletContext JavaDoc {
217
218         org.apache.cocoon.environment.Context envContext;
219
220         public CocoonServletContext(org.apache.cocoon.environment.Context context) {
221             this.envContext = context;
222         }
223
224         public Object JavaDoc getAttribute(String JavaDoc name) { return envContext.getAttribute(name); }
225         public void setAttribute(String JavaDoc name, Object JavaDoc value) { envContext.setAttribute(name, value); }
226         public Enumeration JavaDoc getAttributeNames() { return envContext.getAttributeNames(); }
227         public java.net.URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc { return envContext.getResource(path); }
228         public String JavaDoc getRealPath(String JavaDoc path) { return envContext.getRealPath(path); }
229         public String JavaDoc getMimeType(String JavaDoc file) { return envContext.getMimeType(file); }
230         public String JavaDoc getInitParameter(String JavaDoc name) { return envContext.getInitParameter(name); }
231         public java.io.InputStream JavaDoc getResourceAsStream(String JavaDoc path) { return envContext.getResourceAsStream(path); }
232
233         public ServletContext JavaDoc getContext(String JavaDoc uripath) { return (null); }
234         public Enumeration JavaDoc getInitParameterNames() { return (null); }
235         public int getMajorVersion() { return (2); }
236         public int getMinorVersion() { return (3); }
237         public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name) { return (null); }
238         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) { return (null); }
239         public Set JavaDoc getResourcePaths(String JavaDoc path) { return null; }
240         public String JavaDoc getServerInfo() { return (null); }
241         /**
242          * @deprecated The method DeliImpl.CocoonServletContext.getServlet(String)
243          * overrides a deprecated method from ServletContext.
244          * @see <a HREF="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getServlet(java.lang.String)">ServletContext#getServlet(java.lang.String)</a>
245          */

246         public Servlet JavaDoc getServlet(String JavaDoc name) throws ServletException JavaDoc { return (null); }
247         public String JavaDoc getServletContextName() { return (null); }
248         /**
249          * @deprecated The method DeliImpl.CocoonServletContext.getServletNames()
250          * overrides a deprecated method from ServletContext.
251          * @see <a HREF="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getServletNames()">ServletContext#getServletNames()</a>
252          */

253         public Enumeration JavaDoc getServletNames() { return (null); }
254         /**
255          * @deprecated The method DeliImpl.CocoonServletContext.getServlets()
256          * overrides a deprecated method from ServletContext.
257          * @see <a HREF="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getServlets()">ServletContext#getServlets()</a>
258          */

259         public Enumeration JavaDoc getServlets() { return (null); }
260         public void log(String JavaDoc message) {}
261         /** @deprecated use {@link #log(String message, Throwable throwable)} instead. */
262         public void log(Exception JavaDoc exception, String JavaDoc message) {}
263         public void log(String JavaDoc message, Throwable JavaDoc throwable) {}
264         public void removeAttribute(String JavaDoc name) {}
265     }
266
267     /**
268      * Stub implementation of HttpServletRequest
269      */

270     public static class CocoonServletRequest implements HttpServletRequest JavaDoc {
271         Request JavaDoc request;
272
273         public CocoonServletRequest(Request JavaDoc request) {
274             this.request = request;
275         }
276
277         public String JavaDoc getAuthType() { return request.getAuthType(); }
278         public long getDateHeader(String JavaDoc s) { return request.getDateHeader(s); }
279         public String JavaDoc getHeader(String JavaDoc s) { return request.getHeader(s); }
280         public Enumeration JavaDoc getHeaders(String JavaDoc s) { return request.getHeaders(s); }
281         public Enumeration JavaDoc getHeaderNames() { return request.getHeaderNames(); }
282         public String JavaDoc getMethod() { return request.getMethod(); }
283         public String JavaDoc getPathInfo() { return request.getPathInfo(); }
284         public String JavaDoc getPathTranslated() { return request.getPathTranslated(); }
285         public String JavaDoc getContextPath() { return request.getContextPath(); }
286         public String JavaDoc getQueryString() { return request.getQueryString(); }
287         public String JavaDoc getRemoteUser() { return request.getRemoteUser(); }
288         public boolean isUserInRole(String JavaDoc s) { return request.isUserInRole(s); }
289         public String JavaDoc getRequestedSessionId() { return request.getRequestedSessionId(); }
290         public String JavaDoc getRequestURI() { return request.getRequestURI(); }
291         public String JavaDoc getServletPath() { return request.getServletPath(); }
292         public boolean isRequestedSessionIdValid() { return request.isRequestedSessionIdValid(); }
293         public boolean isRequestedSessionIdFromCookie() { return request.isRequestedSessionIdFromCookie(); }
294         public Object JavaDoc getAttribute(String JavaDoc s) { return request.getAttribute(s); }
295         public Enumeration JavaDoc getAttributeNames() { return request.getAttributeNames(); }
296         public String JavaDoc getCharacterEncoding() { return request.getCharacterEncoding(); }
297         public int getContentLength() { return request.getContentLength(); }
298         public String JavaDoc getContentType() { return request.getContentType(); }
299         public String JavaDoc getParameter(String JavaDoc s) { return request.getParameter(s); }
300         public Enumeration JavaDoc getParameterNames() { return request.getParameterNames(); }
301         public String JavaDoc[] getParameterValues(String JavaDoc s) { return request.getParameterValues(s); }
302         public String JavaDoc getProtocol() { return request.getProtocol(); }
303         public String JavaDoc getScheme() { return request.getScheme(); }
304         public String JavaDoc getServerName() { return request.getServerName(); }
305         public int getServerPort() { return request.getServerPort(); }
306         public String JavaDoc getRemoteAddr() { return request.getRemoteAddr(); }
307         public String JavaDoc getRemoteHost() { return request.getRemoteHost(); }
308         public void setAttribute(String JavaDoc s, Object JavaDoc obj) { request.setAttribute(s, obj); }
309         public void removeAttribute(String JavaDoc s) { request.removeAttribute(s); }
310         public boolean isSecure() { return request.isSecure(); }
311         public StringBuffer JavaDoc getRequestURL() { return null; }
312         public Map JavaDoc getParameterMap() { return null; }
313         public void setCharacterEncoding(String JavaDoc s) {}
314         public Principal JavaDoc getUserPrincipal() { return request.getUserPrincipal(); }
315         public Locale JavaDoc getLocale() { return request.getLocale(); }
316         public Enumeration JavaDoc getLocales() { return request.getLocales(); }
317
318         /** @deprecated use {@link org.apache.cocoon.components.deli.DeliImpl.CocoonServletContext#getRealPath(java.lang.String)} instead. */
319         public String JavaDoc getRealPath(String JavaDoc s) { return null; }
320         public Cookie JavaDoc[] getCookies() { return null; }
321         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc s) { return null; }
322         public BufferedReader JavaDoc getReader() throws IOException JavaDoc { return null; }
323         public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc { return null; }
324         public HttpSession JavaDoc getSession(boolean flag) { return null; }
325         public HttpSession JavaDoc getSession() { return null; }
326         public boolean isRequestedSessionIdFromURL() { return false; }
327         /** @deprecated use {@link #isRequestedSessionIdFromURL()} instead */
328         public boolean isRequestedSessionIdFromUrl() { return false; }
329         public int getIntHeader(String JavaDoc s) { return 0; }
330     }
331
332 }
333
334
Popular Tags