001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.webdav.client.methods;
020:
021: import javax.xml.parsers.*;
022:
023: import org.openharmonise.commons.xml.*;
024: import org.openharmonise.commons.xml.namespace.*;
025: import org.openharmonise.webdav.client.*;
026: import org.w3c.dom.*;
027:
028: /**
029: * Lock method.
030: *
031: * @author Matthew Large
032: * @version $Revision: 1.1 $
033: *
034: */
035: public class Lock extends AbstractWebDAVMethod {
036:
037: /**
038: * Method name.
039: */
040: public static String METHOD_NAME = "LOCK";
041:
042: /**
043: * Lock type exclusive identifier.
044: */
045: public static final String LOCK_EXCLUSIVE = "exclusive";
046:
047: /**
048: * Lock type shared identifier.
049: */
050: public static final String LOCK_SHARED = "shared";
051:
052: /**
053: * Lock type.
054: */
055: private String m_sLockType = "exclusive";
056:
057: /**
058: * Owner of lock.
059: */
060: private String m_sOwner = null;
061:
062: /**
063: * Creates a new lock method.
064: *
065: * @param sURL URL for request
066: */
067: public Lock(String sURL) {
068: super (METHOD_NAME, sURL);
069: }
070:
071: /**
072: * Sets the type of the lock.
073: *
074: * @param sLockType Lock type
075: */
076: public void setLockType(String sLockType) {
077: if (sLockType.equals(LOCK_EXCLUSIVE)
078: || sLockType.equals(LOCK_SHARED)) {
079: this .m_sLockType = sLockType;
080: }
081: }
082:
083: /**
084: * Sets the owner of the lock.
085: *
086: * @param sOwner Lock owner
087: */
088: public void setOwner(String sOwner) {
089: this .m_sOwner = sOwner;
090: }
091:
092: public byte[] getData() {
093:
094: DocumentBuilderFactory factory = DocumentBuilderFactory
095: .newInstance();
096: factory.setNamespaceAware(true);
097: Document xmlDoc = null;
098: try {
099: xmlDoc = factory.newDocumentBuilder().newDocument();
100: } catch (ParserConfigurationException e) {
101: e.printStackTrace();
102: }
103:
104: Element elLockInfo = xmlDoc.createElementNS(
105: PropPatch.WEBDAV_NAMESPACE, "lockinfo");
106: xmlDoc.appendChild(elLockInfo);
107:
108: Element elLockScope = xmlDoc.createElementNS(
109: PropPatch.WEBDAV_NAMESPACE, "lockscope");
110: elLockInfo.appendChild(elLockScope);
111: Element elLockScopeVal = xmlDoc.createElementNS(
112: PropPatch.WEBDAV_NAMESPACE, this .m_sLockType);
113: elLockScope.appendChild(elLockScopeVal);
114:
115: Element elLockType = xmlDoc.createElementNS(
116: PropPatch.WEBDAV_NAMESPACE, "locktype");
117: elLockInfo.appendChild(elLockType);
118: Element elLockTypeVal = xmlDoc.createElementNS(
119: PropPatch.WEBDAV_NAMESPACE, "write");
120: elLockType.appendChild(elLockTypeVal);
121:
122: XMLPrettyPrint printer = new XMLPrettyPrint();
123: printer.setNamespaceAware(true);
124:
125: String sXML = null;
126: try {
127: sXML = printer.printNode(xmlDoc.getDocumentElement());
128: } catch (NamespaceClashException e1) {
129: e1.printStackTrace();
130: }
131:
132: return sXML.getBytes();
133: }
134:
135: }
|