webdav.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 204 行

JS
204
字号
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
importClass(java.util.ArrayList);

importClass(Packages.nl.hippo.cocoon.webdav.WebDAVHelper);
importClass(Packages.nl.hippo.cocoon.webdav.Property);

importClass(Packages.org.apache.excalibur.source.SourceResolver);


// ---------- Public functions ----------


/**
 * WebDAV DELETE
 */
function remove(uri, success, failure) {
  var status = WebDAVHelper.remove(uri, cocoon.session.httpstate);
  if (status == 204) {
     _act(success, status);
     return true;
  }
  else {
    _act(failure, status);
    return false;
  }
}

/**
 * WebDAV MKCOL
 */
function mkcol(uri, success, failure) {
  var status = WebDAVHelper.mkcol(uri, cocoon.session.httpstate);
  if (status == 201) {
  	 _act(success, status);
  	 return true;
  }
  else {
 	  _act(failure, status);
 	  return false;
  }
}

/**
 * WebDAV PUT
 */
function put(uri, src, success, failure) {
  var resolver = null;
  var source = null;
  var input = null;
  try {
    resolver = cocoon.getComponent(SourceResolver.ROLE);
    source = resolver.resolveURI(src);
    input = source.getInputStream();
    var status = WebDAVHelper.put(uri, input, cocoon.session.httpstate);
    if (status == 201 || status == 204 || status == 200) {
       _act(success, status);
       return true;
    }
    else {
       _act(failure, status);
       return false;
    }
  }
  finally {
    if (input != null) {
      try {
       input.close();
      }
      catch (e) {}
    }
    if (source != null) {
      resolver.release(source);
    }
    if (resolver != null) {
      cocoon.releaseComponent(resolver);
    }
  }
}

function proppatch(uri, set, remove, success, failure) {
  var status = WebDAVHelper.proppatch(uri, set, remove, cocoon.session.httpstate);
  if (status == 403 || status == 409 || status == 423 || status == 404) {
    _act(failure, status);
    return false;
  }
  else {
    _act(success, status);
    //_act(success, "207");
    return true;
  }
}

function propfindAsString(uri, namespace, localName) {
  importClass(org.apache.cocoon.components.store.impl.EventAwareTransientStore);
  importClass(org.apache.excalibur.store.Store);
  var cache = cocoon.getComponent(Store.ROLE+"/EventAware");
  
  var result = WebDAVHelper.propfindAsString(cache,uri, namespace, localName, cocoon.session.httpstate);
  return result;
}

function propfindAsElement(uri, namespace, localName) {

  var result = WebDAVHelper.propfindAsElement(uri, namespace, localName, cocoon.session.httpstate);
  return result;
}



// ---------- Sitemenu functions ----------


function setproperty() {
  var uri = cocoon.parameters["uri"];
  var namespace = cocoon.parameters["namespace"];
  var name = cocoon.parameters["name"];
  var value = cocoon.parameters["value"];
  var set = new ArrayList();
  set.add(new Property("property", namespace, name, value));
  var status = WebDAVHelper.proppatch(uri, set, null, cocoon.session.httpstate);
  cocoon.sendPage("/dav/status", {status: status});
}

function removeproperty() {
  var uri = cocoon.parameters["uri"];
  var namespace = cocoon.parameters["namespace"];
  var name = cocoon.parameters["name"];
  var value = cocoon.parameters["value"];
  var remove = new ArrayList();
  remove.add(new Property("property", namespace, name, value));
  var status = WebDAVHelper.proppatch(uri, null, remove, cocoon.session.httpstate);
  cocoon.sendPage("/dav/status", {status: status});
}


function checkAncestorCollectionsExist(m_baseUri,location)
{

	var NOT_FOUND_INDEX = -1;
  var lastIndexOfSlash = location.lastIndexOf('/');
  
  if (lastIndexOfSlash != NOT_FOUND_INDEX)
  {

    var parentPath = "" + location.substring(0, lastIndexOfSlash);
    var pPlength = parentPath.length;
    
    if (pPlength > 0)
    {      
      checkAncestorCollectionsExist(m_baseUri,parentPath);
      
      var mkcolResult = WebDAVHelper.mkcol(m_baseUri + parentPath, cocoon.session.httpstate);
      if (mkcolResult != 201)
      {
        //print("failure making: " + m_baseUri + parentPath + ": " + mkcolResult);
        // TODO: handle result
      }
      
    }
  }
  
}



// ---------- Internal functions ----------


function _act(action, status) {
  if (action == undefined) {
    cocoon.sendPage("/dav/status", {status: status});  
  }
  if (action == null) {
    return;
  }
  if (action) {
    action.call(this, status);
    //action.apply(this, new Array(status));
    return;
  }
}

function success(status) {
  cocoon.sendPage("/dav/status", {status: status});
}

function failure(status) {
  cocoon.sendPage("/dav/status", {status: status});
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?