1 16 package org.apache.cocoon.components.deli; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.security.Principal ; 22 import java.util.Enumeration ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.Vector ; 28 29 import javax.servlet.RequestDispatcher ; 30 import javax.servlet.Servlet ; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.ServletInputStream ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpSession ; 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 ; 54 import org.w3c.dom.Element ; 55 import org.w3c.dom.Text ; 56 57 import com.hp.hpl.deli.Profile; 58 import com.hp.hpl.deli.ProfileAttribute; 59 import com.hp.hpl.deli.Workspace; 60 61 72 public final class DeliImpl extends AbstractLogEnabled 73 implements Parameterizable, Deli, Serviceable, Disposable, Initializable, 74 ThreadSafe, Contextualizable { 75 76 77 private String deliConfig = "deli/config/deliConfig.xml"; 78 79 80 protected ServiceManager manager = null; 81 82 83 protected DOMParser parser; 84 85 86 protected CocoonServletContext servletContext; 87 88 89 public void contextualize(Context 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 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 108 public void parameterize(Parameters params) { 109 this.deliConfig = params.getParameter("deli-config-file", this.deliConfig); 110 } 111 112 115 public void initialize() throws Exception { 116 try { 117 Workspace.getInstance().configure(this.servletContext, this.deliConfig); 118 } catch (Exception e) { 119 getLogger().error("DELI Exception while creating workspace: ", e); 120 throw e; 121 } 122 } 123 124 125 public void dispose() { 126 if (parser != null) { 127 this.manager.release(parser); 128 } 129 this.parser = null; 130 } 131 132 144 public Profile getProfile(Request theRequest) 145 throws IOException , ServletException , Exception { 146 Profile theProfile = null; 147 try { 148 CocoonServletRequest servletRequest = new CocoonServletRequest(theRequest); 149 theProfile = new Profile(servletRequest); 150 } catch (Exception e) { 151 getLogger().error("DELI Exception while retrieving profile: ", e); 152 throw e; 153 } 154 return theProfile; 155 } 156 157 163 public Document getUACapabilities(Profile theProfile) throws Exception { 164 Document document = null; 165 try { 166 Element rootElement; 167 Element attributeNode; 168 Element complexAttributeNode; 169 Text text; 170 171 document = parser.createDocument(); 172 rootElement = document.createElementNS(null, "browser"); 173 document.appendChild(rootElement); 174 175 Iterator 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 attributeValue = p.get(); 181 if (attributeValue != null) 182 { 183 Iterator complexValueIter = attributeValue.iterator(); 184 if (p.getCollectionType().equals("Simple")) { 185 String value = (String )complexValueIter.next(); 187 text = document.createTextNode(value); 188 attributeNode.appendChild(text); 189 } else { 190 while (complexValueIter.hasNext()) { 192 String value = (String )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 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 getUACapabilities(Request theRequest) 209 throws IOException , Exception { 210 return this.getUACapabilities(this.getProfile(theRequest)); 211 } 212 213 216 public static class CocoonServletContext implements ServletContext { 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 getAttribute(String name) { return envContext.getAttribute(name); } 225 public void setAttribute(String name, Object value) { envContext.setAttribute(name, value); } 226 public Enumeration getAttributeNames() { return envContext.getAttributeNames(); } 227 public java.net.URL getResource(String path) throws MalformedURLException { return envContext.getResource(path); } 228 public String getRealPath(String path) { return envContext.getRealPath(path); } 229 public String getMimeType(String file) { return envContext.getMimeType(file); } 230 public String getInitParameter(String name) { return envContext.getInitParameter(name); } 231 public java.io.InputStream getResourceAsStream(String path) { return envContext.getResourceAsStream(path); } 232 233 public ServletContext getContext(String uripath) { return (null); } 234 public Enumeration getInitParameterNames() { return (null); } 235 public int getMajorVersion() { return (2); } 236 public int getMinorVersion() { return (3); } 237 public RequestDispatcher getNamedDispatcher(String name) { return (null); } 238 public RequestDispatcher getRequestDispatcher(String path) { return (null); } 239 public Set getResourcePaths(String path) { return null; } 240 public String getServerInfo() { return (null); } 241 246 public Servlet getServlet(String name) throws ServletException { return (null); } 247 public String getServletContextName() { return (null); } 248 253 public Enumeration getServletNames() { return (null); } 254 259 public Enumeration getServlets() { return (null); } 260 public void log(String message) {} 261 262 public void log(Exception exception, String message) {} 263 public void log(String message, Throwable throwable) {} 264 public void removeAttribute(String name) {} 265 } 266 267 270 public static class CocoonServletRequest implements HttpServletRequest { 271 Request request; 272 273 public CocoonServletRequest(Request request) { 274 this.request = request; 275 } 276 277 public String getAuthType() { return request.getAuthType(); } 278 public long getDateHeader(String s) { return request.getDateHeader(s); } 279 public String getHeader(String s) { return request.getHeader(s); } 280 public Enumeration getHeaders(String s) { return request.getHeaders(s); } 281 public Enumeration getHeaderNames() { return request.getHeaderNames(); } 282 public String getMethod() { return request.getMethod(); } 283 public String getPathInfo() { return request.getPathInfo(); } 284 public String getPathTranslated() { return request.getPathTranslated(); } 285 public String getContextPath() { return request.getContextPath(); } 286 public String getQueryString() { return request.getQueryString(); } 287 public String getRemoteUser() { return request.getRemoteUser(); } 288 public boolean isUserInRole(String s) { return request.isUserInRole(s); } 289 public String getRequestedSessionId() { return request.getRequestedSessionId(); } 290 public String getRequestURI() { return request.getRequestURI(); } 291 public String getServletPath() { return request.getServletPath(); } 292 public boolean isRequestedSessionIdValid() { return request.isRequestedSessionIdValid(); } 293 public boolean isRequestedSessionIdFromCookie() { return request.isRequestedSessionIdFromCookie(); } 294 public Object getAttribute(String s) { return request.getAttribute(s); } 295 public Enumeration getAttributeNames() { return request.getAttributeNames(); } 296 public String getCharacterEncoding() { return request.getCharacterEncoding(); } 297 public int getContentLength() { return request.getContentLength(); } 298 public String getContentType() { return request.getContentType(); } 299 public String getParameter(String s) { return request.getParameter(s); } 300 public Enumeration getParameterNames() { return request.getParameterNames(); } 301 public String [] getParameterValues(String s) { return request.getParameterValues(s); } 302 public String getProtocol() { return request.getProtocol(); } 303 public String getScheme() { return request.getScheme(); } 304 public String getServerName() { return request.getServerName(); } 305 public int getServerPort() { return request.getServerPort(); } 306 public String getRemoteAddr() { return request.getRemoteAddr(); } 307 public String getRemoteHost() { return request.getRemoteHost(); } 308 public void setAttribute(String s, Object obj) { request.setAttribute(s, obj); } 309 public void removeAttribute(String s) { request.removeAttribute(s); } 310 public boolean isSecure() { return request.isSecure(); } 311 public StringBuffer getRequestURL() { return null; } 312 public Map getParameterMap() { return null; } 313 public void setCharacterEncoding(String s) {} 314 public Principal getUserPrincipal() { return request.getUserPrincipal(); } 315 public Locale getLocale() { return request.getLocale(); } 316 public Enumeration getLocales() { return request.getLocales(); } 317 318 319 public String getRealPath(String s) { return null; } 320 public Cookie [] getCookies() { return null; } 321 public RequestDispatcher getRequestDispatcher(String s) { return null; } 322 public BufferedReader getReader() throws IOException { return null; } 323 public ServletInputStream getInputStream() throws IOException { return null; } 324 public HttpSession getSession(boolean flag) { return null; } 325 public HttpSession getSession() { return null; } 326 public boolean isRequestedSessionIdFromURL() { return false; } 327 328 public boolean isRequestedSessionIdFromUrl() { return false; } 329 public int getIntHeader(String s) { return 0; } 330 } 331 332 } 333 334 | Popular Tags |