001: /*
002: * (C) Copyright Simulacra Media Ltd, 2004. All rights reserved.
003: *
004: * The program is provided "AS IS" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * Simulacra Media Ltd will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will Simulacra Media Ltd be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * Simulacra Media Ltd has been advised of the possibility of their occurrence.
011: * Simulacra Media Ltd will not be liable for any third party claims against you.
012: *
013: */
014:
015: package com.ibm.webdav.protocol.http;
016:
017: import com.ibm.webdav.*;
018: import com.ibm.webdav.impl.*;
019: import com.ibm.webdav.impl.ResourceImpl;
020:
021: import javax.servlet.http.*;
022: import javax.xml.parsers.*;
023: import javax.xml.parsers.DocumentBuilderFactory;
024:
025: import java.io.*;
026: import java.util.Enumeration;
027: import org.w3c.dom.*;
028:
029: /**
030: * Executes the WebDAV BIND method.
031: *
032: * @author Michael Bell
033: * @since November 13, 2003
034: */
035: public class BindMethod extends WebDAVMethod {
036: /**
037: * Construct a BindMethod.
038: *
039: * @param request
040: * the servlet request
041: * @param response
042: * the servlet response
043: * @exception com.ibm.webdav.WebDAVException
044: */
045: public BindMethod(HttpServletRequest request,
046: HttpServletResponse response) throws WebDAVException {
047: super (request, response);
048: methodName = "BIND";
049: }
050:
051: /**
052: * Execute the method.
053: *
054: * @return the result status code
055: */
056: public WebDAVStatus execute() {
057: setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
058: MultiStatus multiStatus = null;
059: try {
060: context.setMethodName("BIND");
061:
062: Document contents = null;
063:
064: if (context.getRequestContext().contentLength() > 0) {
065: // get the request entity body and parse it
066: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
067: resource.getURL().toString());
068:
069: DocumentBuilderFactory factory = DocumentBuilderFactory
070: .newInstance();
071: factory.setNamespaceAware(true);
072:
073: DocumentBuilder docbuilder = factory
074: .newDocumentBuilder();
075: docbuilder.setErrorHandler(errorHandler);
076: contents = docbuilder
077: .parse(new org.xml.sax.InputSource(request
078: .getReader()));
079:
080: if (errorHandler.getErrorCount() > 0) {
081: throw new WebDAVException(
082: WebDAVStatus.SC_BAD_REQUEST,
083: "Syntax error in PROPFIND request entity body");
084: }
085: }
086:
087: if (contents == null) {
088: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
089: "No body in request");
090: }
091:
092: Element bindEl = contents.getDocumentElement();
093:
094: if (bindEl.getLocalName().equals("bind") == false) {
095: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
096: "Invalid root element in request body");
097: }
098:
099: NodeList segmentNL = bindEl.getElementsByTagNameNS("DAV:",
100: "segment");
101:
102: NodeList hrefNL = bindEl.getElementsByTagNameNS("DAV:",
103: "href");
104:
105: if (segmentNL.getLength() != 1 || hrefNL.getLength() != 1) {
106: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
107: "Invalid request body");
108: }
109:
110: String segment = segmentNL.item(0).getFirstChild()
111: .getNodeValue();
112:
113: String href = hrefNL.item(0).getFirstChild().getNodeValue();
114:
115: multiStatus = resource
116: .createBinding(context, segment, href);
117: Enumeration responses = multiStatus.getResponses();
118: if (responses.hasMoreElements()) {
119: MethodResponse methodResponse = (MethodResponse) responses
120: .nextElement();
121: if (responses.hasMoreElements()) {
122: // there's more than one response, so return a multistatus
123: context.getResponseContext()
124: .contentType("text/xml");
125: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
126: setResponseHeaders();
127:
128: // output the results as an XML document
129: Document results = multiStatus.asXML();
130: //((Document) results).setEncoding(getResponseCharset());
131: if (ResourceImpl.debug) {
132: System.err.println("move results:");
133: PrintWriter pout = new PrintWriter(System.err);
134: pout.print(XMLUtility.printNode(results
135: .getDocumentElement()));
136: //((Document) results).printWithFormat(pout);
137: }
138: PrintWriter pout = new PrintWriter(response
139: .getWriter(), false);
140: pout.print(XMLUtility.printNode(results
141: .getDocumentElement()));
142: //((Document) results).print(pout);
143: pout.close();
144: } else {
145: // there was just one MethodResponse, so return it directly
146: // instead
147: // of wrapped in a multistatus
148: setStatusCode(methodResponse.getStatus());
149: setResponseHeaders();
150: }
151: } else {
152: setResponseHeaders();
153: }
154: } catch (WebDAVException exc) {
155: setStatusCode(exc.getStatusCode());
156:
157: } catch (Exception exc) {
158: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
159: }
160: return context.getStatusCode();
161: }
162: }
|