Author: adrianc
Date: Wed Jun 24 05:58:56 2009 New Revision: 787915 URL: http://svn.apache.org/viewvc?rev=787915&view=rev Log: A rudimentary WebDAV servlet. This is in preparation for the forthcoming iCalendar improvements. It is only a beginning - it needs to be built out. I put this in the framework because it is reusable and extensible. For example, a WebDAV interface to the Content component could be useful. Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java (with props) ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java (with props) ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java (with props) ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java (with props) ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java (with props) ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java (with props) Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java?rev=787915&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java Wed Jun 24 05:58:56 2009 @@ -0,0 +1,86 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.webdav; + +import java.io.IOException; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; + +import javolution.util.FastList; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** PROPFIND HTTP method helper class. This class provides helper methods for + * working with WebDAV PROPFIND requests and responses.*/ +public class PropFindHelper extends ResponseHelper { + + protected final Document requestDocument; + + public PropFindHelper(Document requestDocument) throws IOException, SAXException, ParserConfigurationException { + this.requestDocument = requestDocument; + } + + public Element createPropElement(List<Element> propList) { + Element element = this.responseDocument.createElementNS(DAV_NAMESPACE_URI, "D:prop"); + if (propList != null && propList.size() > 0) { + for (Element propElement : propList) { + element.appendChild(propElement); + } + } + return element; + } + + public Element createPropStatElement(Element prop, String stat) { + Element element = this.responseDocument.createElementNS(DAV_NAMESPACE_URI, "D:propstat"); + element.appendChild(prop); + element.appendChild(createStatusElement(stat)); + return element; + } + + public List<Element> getFindPropsList(String nameSpaceUri) { + List<Element> result = FastList.newInstance(); + NodeList nodeList = this.requestDocument.getElementsByTagNameNS(nameSpaceUri == null ? "*" : nameSpaceUri, "prop"); + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i).getFirstChild(); + while (node != null) { + if (node.getNodeType() == Node.ELEMENT_NODE) { + result.add((Element) node); + } + node = node.getNextSibling(); + } + } + return result; + } + + public boolean isAllProp() { + NodeList nodeList = this.requestDocument.getElementsByTagNameNS(DAV_NAMESPACE_URI, "allprop"); + return nodeList.getLength() > 0; + } + + public boolean isPropName() { + NodeList nodeList = this.requestDocument.getElementsByTagNameNS(DAV_NAMESPACE_URI, "propname"); + return nodeList.getLength() > 0; + } + +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/PropFindHelper.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java?rev=787915&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java Wed Jun 24 05:58:56 2009 @@ -0,0 +1,39 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.webdav; + +import java.io.IOException; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public interface RequestHandler { + /** Called by the the WebDAV servlet to handle a WebDAV request. + * <p>The <code>HttpServletRequest</code> contains the following attributes:<br/> + * <table cellspacing="0" cellpadding="2" border="1"> + * <tr><td>delegator</td><td>A <code>GenericDelgator</code> instance</td></tr> + * <tr><td>dispatcher</td><td>A <code>LocalDispatcher</code> instance</td></tr> + * <tr><td>security</td><td>A <code>Security</code> instance</td></tr> + * <tr><td>authz</td><td>An <code>Authorization</code> instance</td></tr> + * </table></p> + */ + public void handleRequest(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException, IOException; +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java?rev=787915&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java Wed Jun 24 05:58:56 2009 @@ -0,0 +1,32 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.webdav; + +public interface RequestHandlerFactory { + /** Returns a <code>RequestHandler<code> instance appropriate + * for the WebDAV HTTP method. + * + * @param method The WebDAV HTTP method. Implementations MUST + * provide handlers for the following methods: PROPFIND, PROPPATCH, + * MKCOL, GET, HEAD, POST, DELETE, PUT, COPY, MOVE, LOCK, UNLOCK. + * @return A <code>RequestHandler</code> instance. Implementations + * of this interface MUST NOT return <code>null</code>. + */ + public RequestHandler getHandler(String method); +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/RequestHandlerFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java?rev=787915&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java Wed Jun 24 05:58:56 2009 @@ -0,0 +1,105 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.webdav; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.Writer; + +import javax.servlet.http.HttpServletResponse; +import javax.xml.parsers.ParserConfigurationException; + +import org.ofbiz.base.util.UtilXml; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +/** WebDAV response helper class. This class provides helper methods for + * working with WebDAV PROPFIND requests and responses.*/ +public class ResponseHelper { + + public static final String DAV_NAMESPACE_URI = "DAV:"; + public static final String STATUS_200 = "HTTP/1.1 200 OK"; + public static final String STATUS_400 = "HTTP/1.1 400 Bad Request"; + public static final String STATUS_401 = "HTTP/1.1 401 Unauthorized"; + public static final String STATUS_403 = "HTTP/1.1 403 Forbidden"; + public static final String STATUS_404 = "HTTP/1.1 404 Not Found"; + + @SuppressWarnings("deprecation") + public static void prepareResponse(HttpServletResponse response, int statusCode, String statusString) { + response.setContentType("application/xml"); + response.setCharacterEncoding("UTF-8"); + if (statusString == null) { + response.setStatus(statusCode); + } else { + response.setStatus(statusCode, statusString); + } + } + + protected final Document responseDocument; + + public ResponseHelper() throws IOException, SAXException, ParserConfigurationException { + this.responseDocument = UtilXml.makeEmptyXmlDocument(); + } + + public Element createElementSetValue(String elementName, String value) { + Element element = this.responseDocument.createElementNS(DAV_NAMESPACE_URI, elementName); + element.appendChild(element.getOwnerDocument().createTextNode(value)); + element.setNodeValue(value); + return element; + } + + public Element createHrefElement(String hrefUrl) { + return createElementSetValue("D:href", hrefUrl); + } + + public Element createMultiStatusElement() { + return this.responseDocument.createElementNS(DAV_NAMESPACE_URI, "D:multistatus"); + } + + public Element createResponseDescriptionElement(String description, String lang) { + Element element = createElementSetValue("D:responsedescription", description); + if (lang != null) { + element.setAttribute("xml:lang", lang); + } + return element; + } + + public Element createResponseElement() { + return this.responseDocument.createElementNS(DAV_NAMESPACE_URI, "D:response"); + } + + public Element createStatusElement(String statusText) { + return createElementSetValue("D:status", statusText); + } + + public Document getResponseDocument() { + return this.responseDocument; + } + + public void writeResponse(Writer writer) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + try { + UtilXml.writeXmlDocument(os, this.responseDocument, "UTF-8", true, true); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + writer.write(os.toString()); + } +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/ResponseHelper.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java?rev=787915&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java Wed Jun 24 05:58:56 2009 @@ -0,0 +1,115 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.webdav; + +import java.io.IOException; + +import javax.servlet.GenericServlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.entity.GenericDelegator; +import org.ofbiz.security.Security; +import org.ofbiz.security.SecurityFactory; +import org.ofbiz.security.authz.Authorization; +import org.ofbiz.security.authz.AuthorizationFactory; +import org.ofbiz.service.GenericDispatcher; +import org.ofbiz.service.LocalDispatcher; + +/** Implements a WebDAV servlet. The servlet simply forwards WebDAV requests + * to a <code>RequestHandlerFactory</code> instance, whose class is specified + * in the web application's web.xml file:<p><code> + * <context-param><br> + * <param-name>requestHandlerFactoryClass</param-name><br> + * <param-value>com.mydomain.MyWebDAVFactory</param-value><br> + * </context-param></code></p> + */ +@SuppressWarnings("serial") +public class WebDavServlet extends GenericServlet { + + public static final String module = WebDavServlet.class.getName(); + + protected Authorization authz = null; + protected GenericDelegator delegator = null; + protected LocalDispatcher dispatcher = null; + protected RequestHandlerFactory handlerFactory = null; + protected Security security = null; + + @Override + public void init(ServletConfig config) throws ServletException{ + try { + super.init(config); + ServletContext context = this.getServletContext(); + String delegatorName = context.getInitParameter("entityDelegatorName"); + this.delegator = GenericDelegator.getGenericDelegator(delegatorName); + String dispatcherName = context.getInitParameter("localDispatcherName"); + this.dispatcher = GenericDispatcher.getLocalDispatcher(dispatcherName, this.delegator); + this.security = SecurityFactory.getInstance(this.delegator); + this.authz = AuthorizationFactory.getInstance(this.delegator); + String factoryClassName = context.getInitParameter("requestHandlerFactoryClass"); + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + this.handlerFactory = (RequestHandlerFactory) loader.loadClass(factoryClassName).newInstance(); + } catch (Exception e) { + Debug.logError(e, "Error while initializing WebDAV servlet: ", module); + throw new ServletException(e); + } + if (Debug.verboseOn()) { + StringBuilder buff = new StringBuilder("WebDAV servlet initialized: delegator = "); + buff.append(this.delegator.getDelegatorName()); + buff.append(", dispatcher = "); + buff.append(this.dispatcher.getName()); + buff.append(", security = "); + buff.append(this.security.getClass().getName()); + buff.append(", authz = "); + buff.append(this.authz.getClass().getName()); + buff.append(", handler factory = "); + buff.append(this.handlerFactory.getClass().getName()); + Debug.logVerbose(buff.toString(), module); + } + } + + public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { + request.setAttribute("delegator", this.delegator); + request.setAttribute("dispatcher", this.dispatcher); + request.setAttribute("security", this.security); + request.setAttribute("authz", this.authz); + HttpServletRequest httpRequest = (HttpServletRequest) request; + RequestHandler handler = this.handlerFactory.getHandler(httpRequest.getMethod()); + try { + handler.handleRequest(httpRequest, (HttpServletResponse) response, this.getServletContext()); + } catch (IOException e) { + throw e; + } catch (ServletException e) { + throw e; + } catch (Exception e) { + throw new ServletException(e); + } finally { + HttpSession session = httpRequest.getSession(); + session.invalidate(); + } + } + +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java?rev=787915&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java Wed Jun 24 05:58:56 2009 @@ -0,0 +1,54 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.webdav; + +import java.io.IOException; +import java.io.InputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import javax.servlet.http.HttpServletRequest; +import javax.xml.parsers.ParserConfigurationException; + +import org.ofbiz.base.util.UtilXml; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +/** Utility methods needed to implement a WebDAV servlet. */ +public class WebDavUtil { + + public static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT"); + public static final String RFC1123_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz"; + + public static String formatDate(String formatString, Date date) { + DateFormat df = new SimpleDateFormat(formatString); + df.setTimeZone(GMT_TIMEZONE); + return df.format(date); + } + + public static Document getDocumentFromRequest(HttpServletRequest request) throws IOException, SAXException, ParserConfigurationException { + InputStream is = request.getInputStream(); + Document document = UtilXml.readXmlDocument(is, false, "WebDAV request"); + is.close(); + return document; + } + +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavUtil.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |