1 /*
  2  * Copyright ©2012 SARA bv, The Netherlands
  3  *
  4  * This file is part of js-webdav-client.
  5  *
  6  * js-webdav-client is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published
  8  * by the Free Software Foundation, either version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * js-webdav-client is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public License
 17  * along with js-webdav-client.  If not, see <http://www.gnu.org/licenses/>.
 18  */
 19 "use strict";
 20 
 21 /**
 22  * This plugin adds acl capabilities to the WebDAV client class
 23  */
 24 
 25 /**
 26  * Perform a WebDAV ACL request
 27  *
 28  * @param    {String}                         path      The path to perform ACL on
 29  * @param    {Function(status,body,headers)}  callback  Querying the server is done asynchronously, this callback function is called when the results are in
 30  * @param    {nl.sara.webdav.Acl}             acl       The ACL to submit
 31  * @param    {Array}                          headers   Optional; Additional headers to set
 32  * @returns  {nl.sara.webdav.Client}                    The client itself for chaining methods
 33  */
 34 nl.sara.webdav.Client.prototype.acl = function(path, callback, acl, headers) {
 35   if ((path === undefined) || (callback === undefined)) {
 36     throw new nl.sara.webdav.Exception('ACL requires the parameters path, callback and acl', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
 37   }
 38   if (!(typeof path === "string") || (!nl.sara.webdav.Ie.isIE && !(acl instanceof nl.sara.webdav.Acl))) {
 39     throw new nl.sara.webdav.Exception('ACL parameter; path should be a string, acl should be an instance of Acl', nl.sara.webdav.Exception.WRONG_TYPE);
 40   }
 41 
 42   // Get the full URL, based on the specified path
 43   var url = this.getUrl(path);
 44 
 45   var aclBody = document.implementation.createDocument("DAV:", "acl", null);
 46   aclBody = nl.sara.webdav.codec.AclCodec.toXML(acl, aclBody);
 47 
 48   // Create the request body string
 49   var serializer = new XMLSerializer();
 50   var body = '<?xml version="1.0" encoding="utf-8" ?>' + serializer.serializeToString(aclBody);
 51 
 52   // And then send the request
 53   var ajax = null;
 54   if (nl.sara.webdav.Ie.isIE) {
 55     if (url.lastIndexOf('?') !== -1) {
 56       url = url + '&_method=acl';
 57     }else{
 58       url = url + '?_method=acl';
 59     }
 60     ajax = this.getAjax('POST', url, callback, headers);
 61   }else{
 62     ajax = this.getAjax("ACL", url, callback, headers);
 63   }
 64   ajax.setRequestHeader('Content-Type', 'application/xml; charset="utf-8"');
 65   ajax.send(body);
 66 
 67   return this;
 68 };
 69 
 70 /**
 71  * Perform a WebDAV REPORT request
 72  *
 73  * @param    {String}                         path         The path to perform REPORT on
 74  * @param    {Function(status,body,headers)}  callback     Querying the server is done asynchronously, this callback function is called when the results are in
 75  * @param    {Document}                       body         The (XML DOM) document to parse and send as the request body
 76  * @param    {Array}                          headers      Optional; Additional headers to set
 77  * @returns  {nl.sara.webdav.Client}                       The client itself for chaining methods
 78  */
 79 nl.sara.webdav.Client.prototype.report = function(path, callback, body, headers) {
 80   if ((path === undefined) || (callback === undefined) || (body === undefined)) {
 81     throw new nl.sara.webdav.Exception('REPORT requires the parameters path, callback and body', nl.sara.webdav.Exception.MISSING_REQUIRED_PARAMETER);
 82   }
 83   if ((typeof path !== "string") || (!nl.sara.webdav.Ie.isIE && !(body instanceof Document))) {
 84     throw new nl.sara.webdav.Exception('REPORT parameter; path should be a string, body should be an instance of Document', nl.sara.webdav.Exception.WRONG_TYPE);
 85   }
 86 
 87   // Get the full URL, based on the specified path
 88   var url = this.getUrl(path);
 89 
 90   // Parse the body
 91   var serializer = new XMLSerializer();
 92   var body = '<?xml version="1.0" encoding="utf-8" ?>' + serializer.serializeToString(body);
 93 
 94   // And then send the request
 95   var ajax = null;
 96   if (nl.sara.webdav.Ie.isIE) {
 97     if (url.lastIndexOf('?') !== -1) {
 98       url = url + '&_method=report';
 99     }else{
100       url = url + '?_method=report';
101     }
102     ajax = this.getAjax('POST', url, callback, headers);
103   }else{
104     ajax = this.getAjax("REPORT", url, callback, headers);
105   }
106   ajax.setRequestHeader('Content-Type', 'application/xml; charset="utf-8"');
107   ajax.send(body);
108 
109   return this;
110 };
111 
112 // End of library
113