KickJava   Java API By Example, From Geeks To Geeks.

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


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

14
15 package com.ibm.webdav.protocol.http;
16
17
18 import com.ibm.webdav.*;
19 import com.ibm.webdav.impl.*;
20 import com.ibm.webdav.impl.ResourceImpl;
21
22 import javax.servlet.http.*;
23 import javax.xml.parsers.*;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25
26 import java.io.*;
27 import java.util.Enumeration JavaDoc;
28 import org.w3c.dom.*;
29
30 /**
31  * Executes the WebDAV BIND method.
32  *
33  * @author Michael Bell
34  * @since November 13, 2003
35  */

36 public class BindMethod extends WebDAVMethod {
37     /**
38      * Construct a BindMethod.
39      *
40      * @param request
41      * the servlet request
42      * @param response
43      * the servlet response
44      * @exception com.ibm.webdav.WebDAVException
45      */

46     public BindMethod(HttpServletRequest request, HttpServletResponse response)
47             throws WebDAVException {
48         super(request, response);
49         methodName = "BIND";
50     }
51
52     /**
53      * Execute the method.
54      *
55      * @return the result status code
56      */

57     public WebDAVStatus execute() {
58         setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
59
MultiStatus multiStatus = null;
60         try {
61             context.setMethodName("BIND");
62
63             Document contents = null;
64
65             if (context.getRequestContext().contentLength() > 0) {
66                 // get the request entity body and parse it
67
WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
68                         resource.getURL().toString());
69
70                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
71                         .newInstance();
72                 factory.setNamespaceAware(true);
73
74                 DocumentBuilder docbuilder = factory.newDocumentBuilder();
75                 docbuilder.setErrorHandler(errorHandler);
76                 contents = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(request
77                         .getReader()));
78
79                 if (errorHandler.getErrorCount() > 0) {
80                     throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
81                             "Syntax error in PROPFIND request entity body");
82                 }
83             }
84
85             if (contents == null) {
86                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
87                         "No body in request");
88             }
89
90             Element bindEl = contents.getDocumentElement();
91
92             if (bindEl.getLocalName().equals("bind") == false) {
93                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
94                         "Invalid root element in request body");
95             }
96
97             NodeList segmentNL = bindEl.getElementsByTagNameNS("DAV:",
98                     "segment");
99
100             NodeList hrefNL = bindEl.getElementsByTagNameNS("DAV:", "href");
101
102             if (segmentNL.getLength() != 1 || hrefNL.getLength() != 1) {
103                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
104                         "Invalid request body");
105             }
106
107             String JavaDoc segment = segmentNL.item(0).getFirstChild().getNodeValue();
108
109             String JavaDoc href = hrefNL.item(0).getFirstChild().getNodeValue();
110
111             multiStatus = resource.createBinding(context, segment, href);
112             Enumeration JavaDoc responses = multiStatus.getResponses();
113             if (responses.hasMoreElements()) {
114                 MethodResponse methodResponse = (MethodResponse) responses
115                         .nextElement();
116                 if (responses.hasMoreElements()) {
117                     // there's more than one response, so return a multistatus
118
context.getResponseContext().contentType("text/xml");
119                     setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
120                     setResponseHeaders();
121
122                     // output the results as an XML document
123
Document results = multiStatus.asXML();
124                     //((Document) results).setEncoding(getResponseCharset());
125
if (ResourceImpl.debug) {
126                         System.err.println("move results:");
127                         PrintWriter pout = new PrintWriter(System.err);
128                         pout.print(XMLUtility.printNode(results
129                                 .getDocumentElement()));
130                         //((Document) results).printWithFormat(pout);
131
}
132                     PrintWriter pout = new PrintWriter(response.getWriter(),
133                             false);
134                     pout.print(XMLUtility.printNode(results
135                             .getDocumentElement()));
136                     //((Document) results).print(pout);
137
pout.close();
138                 } else {
139                     // there was just one MethodResponse, so return it directly
140
// instead
141
// of wrapped in a multistatus
142
setStatusCode(methodResponse.getStatus());
143                     setResponseHeaders();
144                 }
145             } else {
146                 setResponseHeaders();
147             }
148         } catch (WebDAVException exc) {
149             setStatusCode(exc.getStatusCode());
150
151         } catch (Exception JavaDoc exc) {
152             setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
153         }
154         return context.getStatusCode();
155     }
156 }
Popular Tags