KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > protocol > http > MkcolMethod


1 package com.ibm.webdav.protocol.http;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.io.*;
18 import java.util.*;
19 import java.util.logging.*;
20
21 import javax.servlet.http.*;
22 import javax.xml.parsers.*;
23
24 import org.w3c.dom.*;
25
26 import com.ibm.webdav.*;
27 import com.ibm.webdav.impl.*;
28
29 /** Executes the WebDAV MKCOL method.
30  * @author Jim Amsden <jamsden@us.ibm.com>
31  */

32 public class MkcolMethod extends WebDAVMethod {
33     
34     private static Logger m_logger = Logger.getLogger(MkcolMethod.class.getName());
35
36     public static final String JavaDoc METHOD_NAME = "MKCOL";
37 /** Construct a MkcolMethod.
38 * @param request the servlet request
39 * @param response the servlet response
40 * @exception com.ibm.webdav.WebDAVException
41 */

42 public MkcolMethod(HttpServletRequest request, HttpServletResponse response) throws WebDAVException {
43     super(request, response);
44     methodName = METHOD_NAME;
45 }
46 /** Execute the method.
47 * @return the result status code
48 */

49 public WebDAVStatus execute() {
50     setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
51
MultiStatus multiStatus = null;
52     try {
53         // get the request entity body and parse it (the contents may be empty)
54
WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(resource.getURL().toString());
55         /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
56         xmlParser.setWarningNoDoctypeDecl(false);
57         xmlParser.setProcessNamespace(true);*/

58                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
59                 factory.setNamespaceAware(true);
60                 DocumentBuilder docbuilder = factory.newDocumentBuilder();
61         docbuilder.setErrorHandler(errorHandler);
62                 Document contents = null;
63         if (context.getRequestContext().contentLength() > 0) {
64             //contents = xmlParser.readStream(request.getReader());
65
contents = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(request.getReader()));
66             if (errorHandler.getErrorCount() > 0) {
67                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in MKCOL request entity body");
68             }
69         }
70         multiStatus = ((CollectionImpl) resource).createCollection(context, contents);
71         Enumeration stats = multiStatus.getResponses();
72         if (stats.hasMoreElements()) { stats.nextElement(); }
73         if (stats.hasMoreElements()) {
74             // only do this if there is more than one status. This is unlikely, and some
75
// clients, like IE5 don't support Multi-Status responses to MKCOL requests.
76
context.getResponseContext().contentType("text/xml");
77             setResponseHeaders();
78             setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
79             Document results = multiStatus.asXML();
80             //((Document) results).setEncoding(getResponseCharset());
81
PrintWriter pout = new PrintWriter(response.getWriter(), false);
82             //((Document) results).print(pout);
83
pout.print(multiStatus.toString());
84             pout.close();
85         } else {
86             int rc = getStatusCode();
87             if (rc == WebDAVStatus.SC_MULTI_STATUS) {
88                 stats = multiStatus.getResponses();
89                 MethodResponse response = (MethodResponse) stats.nextElement();
90                 rc = response.getStatus();
91             }
92             setStatusCode( rc );
93             setResponseHeaders();
94         }
95     } catch (WebDAVException exc) {
96         m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - " + request.getQueryString());
97         setStatusCode(exc.getStatusCode());
98                 
99     } catch (Exception JavaDoc exc) {
100         m_logger.log(Level.WARNING, exc.getMessage(), exc);
101         setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
102     }
103     return context.getStatusCode();
104 }
105 }
106
Popular Tags