001: package com.ibm.webdav.protocol.http;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import java.io.*;
018: import java.util.*;
019: import java.util.logging.*;
020:
021: import javax.servlet.http.*;
022: import javax.xml.parsers.*;
023:
024: import org.w3c.dom.*;
025:
026: import com.ibm.webdav.*;
027: import com.ibm.webdav.impl.*;
028:
029: /** Executes the WebDAV MKCOL method.
030: * @author Jim Amsden <jamsden@us.ibm.com>
031: */
032: public class MkcolMethod extends WebDAVMethod {
033:
034: private static Logger m_logger = Logger.getLogger(MkcolMethod.class
035: .getName());
036:
037: public static final String METHOD_NAME = "MKCOL";
038:
039: /** Construct a MkcolMethod.
040: * @param request the servlet request
041: * @param response the servlet response
042: * @exception com.ibm.webdav.WebDAVException
043: */
044: public MkcolMethod(HttpServletRequest request,
045: HttpServletResponse response) throws WebDAVException {
046: super (request, response);
047: methodName = METHOD_NAME;
048: }
049:
050: /** Execute the method.
051: * @return the result status code
052: */
053: public WebDAVStatus execute() {
054: setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
055: MultiStatus multiStatus = null;
056: try {
057: // get the request entity body and parse it (the contents may be empty)
058: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
059: resource.getURL().toString());
060: /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
061: xmlParser.setWarningNoDoctypeDecl(false);
062: xmlParser.setProcessNamespace(true);*/
063: DocumentBuilderFactory factory = DocumentBuilderFactory
064: .newInstance();
065: factory.setNamespaceAware(true);
066: DocumentBuilder docbuilder = factory.newDocumentBuilder();
067: docbuilder.setErrorHandler(errorHandler);
068: Document contents = null;
069: if (context.getRequestContext().contentLength() > 0) {
070: //contents = xmlParser.readStream(request.getReader());
071: contents = docbuilder
072: .parse(new org.xml.sax.InputSource(request
073: .getReader()));
074: if (errorHandler.getErrorCount() > 0) {
075: throw new WebDAVException(
076: WebDAVStatus.SC_BAD_REQUEST,
077: "Syntax error in MKCOL request entity body");
078: }
079: }
080: multiStatus = ((CollectionImpl) resource).createCollection(
081: context, contents);
082: Enumeration stats = multiStatus.getResponses();
083: if (stats.hasMoreElements()) {
084: stats.nextElement();
085: }
086: if (stats.hasMoreElements()) {
087: // only do this if there is more than one status. This is unlikely, and some
088: // clients, like IE5 don't support Multi-Status responses to MKCOL requests.
089: context.getResponseContext().contentType("text/xml");
090: setResponseHeaders();
091: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
092: Document results = multiStatus.asXML();
093: //((Document) results).setEncoding(getResponseCharset());
094: PrintWriter pout = new PrintWriter(
095: response.getWriter(), false);
096: //((Document) results).print(pout);
097: pout.print(multiStatus.toString());
098: pout.close();
099: } else {
100: int rc = getStatusCode();
101: if (rc == WebDAVStatus.SC_MULTI_STATUS) {
102: stats = multiStatus.getResponses();
103: MethodResponse response = (MethodResponse) stats
104: .nextElement();
105: rc = response.getStatus();
106: }
107: setStatusCode(rc);
108: setResponseHeaders();
109: }
110: } catch (WebDAVException exc) {
111: m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - "
112: + request.getQueryString());
113: setStatusCode(exc.getStatusCode());
114:
115: } catch (Exception exc) {
116: m_logger.log(Level.WARNING, exc.getMessage(), exc);
117: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
118: }
119: return context.getStatusCode();
120: }
121: }
|