⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultitemcollection.java

📁 jsr170接口的java实现。是个apache的开源项目。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        while (it.hasNext()) {            Object propEntry = it.next();            if (propEntry instanceof DavPropertyName) {                // use the internal remove method in order to prevent premature 'save'                DavPropertyName propName = (DavPropertyName) propEntry;                internalRemoveProperty(propName);            } else if (propEntry instanceof DavProperty) {                // use the internal set method in order to prevent premature 'save'                DavProperty prop = (DavProperty) propEntry;                internalSetProperty(prop);            } else {                throw new IllegalArgumentException("unknown object in change list: " + propEntry.getClass().getName());            }        }        // TODO: missing undo of successful set/remove if subsequent operation fails        // NOTE, that this is relevant with transactions only.        // success: save all changes together if no error occured        complete();        return new MultiStatusResponse(getHref(), DavServletResponse.SC_OK);    }    /**     * If the specified resource represents a collection, a new node is {@link Node#addNode(String)     * added} to the item represented by this resource. If an input stream is specified     * together with a collection resource {@link Session#importXML(String, java.io.InputStream, int)}     * is called instead and this resource path is used as <code>parentAbsPath</code> argument.     * <p/>     * However, if the specified resource is not of resource type collection a     * new {@link Property} is set or an existing one is changed by modifying its     * value.<br>     * NOTE: with the current implementation it is not possible to create or     * modify multivalue JCR properties.<br>     * NOTE: if the JCR property represented by the specified resource has an     * {@link PropertyType#UNDEFINED undefined} resource type, its value will be     * changed/set to type {@link PropertyType#BINARY binary}.     *     * @param resource     * @param inputContext     * @throws org.apache.jackrabbit.webdav.DavException     * @see org.apache.jackrabbit.webdav.DavResource#addMember(org.apache.jackrabbit.webdav.DavResource, InputContext)     * @see Node#addNode(String)     * @see Node#setProperty(String, java.io.InputStream)     */    public void addMember(DavResource resource, InputContext inputContext)            throws DavException {        /* RFC 2815 states that all 'parents' must exist in order all addition of members */        if (!exists()) {            throw new DavException(DavServletResponse.SC_CONFLICT);        }        File tmpFile = null;        try {            Node n = (Node) item;            InputStream in = (inputContext != null) ? inputContext.getInputStream() : null;            String itemPath = getLocator().getRepositoryPath();            String memberName = getItemName(resource.getLocator().getRepositoryPath());            if (resource.isCollection()) {                if (in == null) {                    // MKCOL without a request body, try if a default-primary-type is defined.                    n.addNode(memberName);                } else {                    // MKCOL, which is not allowed for existing resources                    int uuidBehavior = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;                    String str = inputContext.getProperty(IMPORT_UUID_BEHAVIOR);                    if (str != null) {                        try {                            uuidBehavior = Integer.parseInt(str);                        } catch (NumberFormatException e) {                            throw new DavException(DavServletResponse.SC_BAD_REQUEST);                        }                    }                    if (getTransactionId() == null) {                        // if not part of a transaction directely import on workspace                        // since changes would be explicitely saved in the                        // complete-call.                        getRepositorySession().getWorkspace().importXML(itemPath, in, uuidBehavior);                    } else {                        // changes will not be persisted unless the tx is completed.                        getRepositorySession().importXML(itemPath, in, uuidBehavior);                    }                }            } else {                if (in == null) {                    // PUT: not possible without request body                    throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Cannot create a new non-collection resource without request body.");                } else {                    // PUT : create new or overwrite existing property.                    tmpFile = File.createTempFile(TMP_PREFIX + Text.escape(memberName), null, null);                    FileOutputStream out = new FileOutputStream(tmpFile);                    IOUtil.spool(in, out);                    out.close();                    // try to parse the request body into a 'values' property.                    ValuesProperty vp = buildValuesProperty(new FileInputStream(tmpFile));                    if (vp != null) {                        if (JCR_VALUE.equals(vp.getName())) {                            n.setProperty(memberName, vp.getJcrValue());                        } else {                            n.setProperty(memberName, vp.getJcrValues());                        }                    } else {                        // request body cannot be parsed into a 'values' property.                        // fallback: try to import as single value from stream.                        n.setProperty(memberName, new FileInputStream(tmpFile));                    }                }            }            if (resource.exists() && resource instanceof AbstractItemResource) {                // PUT may modify value of existing jcr property. thus, this                // node is not modified by the 'addMember' call.                ((AbstractItemResource)resource).complete();            } else {                complete();            }        } catch (ItemExistsException e) {            // according to RFC 2518: MKCOL only possible on non-existing/deleted resource            throw new JcrDavException(e, DavServletResponse.SC_METHOD_NOT_ALLOWED);        } catch (RepositoryException e) {            throw new JcrDavException(e);        } catch (IOException e) {            throw new DavException(DavServletResponse.SC_UNPROCESSABLE_ENTITY, e.getMessage());        } finally {            if (tmpFile != null) {                tmpFile.delete();            }        }    }    /**     * @see org.apache.jackrabbit.webdav.DavResource#getMembers()     */    public DavResourceIterator getMembers() {        ArrayList memberList = new ArrayList();        if (exists()) {            try {                Node n = (Node)item;                // add all node members                NodeIterator it = n.getNodes();                while (it.hasNext()) {                    Node node = it.nextNode();                    DavResourceLocator loc = getLocatorFromItem(node);                    memberList.add(createResourceFromLocator(loc));                }                // add all property members                PropertyIterator propIt = n.getProperties();                while (propIt.hasNext()) {                    Property prop = propIt.nextProperty();                    DavResourceLocator loc = getLocatorFromItem(prop);                    memberList.add(createResourceFromLocator(loc));                }            } catch (RepositoryException e) {                // ignore                log.error(e.getMessage());            } catch (DavException e) {                // should never occur.                log.error(e.getMessage());            }        }        return new DavResourceIteratorImpl(memberList);    }    /**     * Removes the repository item represented by the specified member     * resource.     *     * @throws DavException if this resource does not exist or if an error occurs     * while deleting the underlying item.     * @see DavResource#removeMember(DavResource)     * @see javax.jcr.Item#remove()     */    public void removeMember(DavResource member) throws DavException {        Session session = getRepositorySession();        try {            String itemPath = member.getLocator().getRepositoryPath();            if (!exists() || !session.itemExists(itemPath)) {                throw new DavException(DavServletResponse.SC_NOT_FOUND);            }            if (!getResourcePath().equals(Text.getRelativeParent(member.getResourcePath(), 1))) {                throw new DavException(DavServletResponse.SC_CONFLICT, member.getResourcePath() + "is not member of this resource (" + getResourcePath() + ")");            }            getRepositorySession().getItem(itemPath).remove();            complete();        } catch (RepositoryException e) {            log.error("Unexpected error: " + e.getMessage());            throw new JcrDavException(e);        }    }    /**     * @param type     * @param scope     * @return true if a lock with the specified type and scope is present on     * this resource, false otherwise. If retrieving the corresponding information     * fails, false is returned.     * @see org.apache.jackrabbit.webdav.DavResource#hasLock(org.apache.jackrabbit.webdav.lock.Type, org.apache.jackrabbit.webdav.lock.Scope)     */    public boolean hasLock(Type type, Scope scope) {        if (isLockable(type, scope)) {            if (Type.WRITE.equals(type)) {                try {                    return ((Node) item).isLocked();                } catch (RepositoryException e) {                    log.error(e.getMessage());                }            } else {                return super.hasLock(type, scope);            }        }        return false;    }    /**     * Retrieve the lock with the specified type and scope.     *     * @param type     * @param scope     * @return lock with the specified type and scope is present on this     * resource or <code>null</code>. NOTE: If retrieving the write lock present     * on the underlying repository item fails, <code>null</code> is return.     * @see org.apache.jackrabbit.webdav.DavResource#getLock(org.apache.jackrabbit.webdav.lock.Type, org.apache.jackrabbit.webdav.lock.Scope)     * @see javax.jcr.Node#getLock() for the write locks.     */    public ActiveLock getLock(Type type, Scope scope) {        ActiveLock lock = null;        if (Type.WRITE.equals(type)) {            try {                if (!exists()) {                    log.warn("Unable to retrieve lock: no item found at '" + getResourcePath() + "'");                } else if (((Node) item).isLocked()) {                    Lock jcrLock = ((Node) item).getLock();                    lock = new JcrActiveLock(jcrLock);                }            } catch (AccessDeniedException e) {                log.error("Error while accessing resource lock: "+e.getMessage());            } catch (UnsupportedRepositoryOperationException e) {                log.error("Error while accessing resource lock: "+e.getMessage());            } catch (RepositoryException e) {                log.error("Error while accessing resource lock: "+e.getMessage());            }        } else {            lock = super.getLock(type, scope);        }        return lock;    }    /**     * Creates a lock on this resource by locking the underlying     * {@link javax.jcr.Node node}. Except for the {@link org.apache.jackrabbit.webdav.lock.LockInfo#isDeep()} }     * all information included in the <code>LockInfo</code> object is ignored.     * Lock timeout is defined by JCR implementation.     *     * @param reqLockInfo     * @return lock object representing the lock created on this resource.     * @throws org.apache.jackrabbit.webdav.DavException     * @see org.apache.jackrabbit.webdav.DavResource#lock(org.apache.jackrabbit.webdav.lock.LockInfo)     * @see Node#lock(boolean, boolean)     */    public ActiveLock lock(LockInfo reqLockInfo) throws DavException {        if (!isLockable(reqLockInfo.getType(), reqLockInfo.getScope())) {            throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);        }        if (Type.WRITE.equals(reqLockInfo.getType())) {            if (!exists()) {                log.warn("Cannot create a write lock for non-existing JCR node (" + getResourcePath() + ")");                throw new DavException(DavServletResponse.SC_NOT_FOUND);            }            try {                boolean sessionScoped = EXCLUSIVE_SESSION.equals(reqLockInfo.getScope());                Lock jcrLock = ((Node)item).lock(reqLockInfo.isDeep(), sessionScoped);                // add reference to DAVSession for this lock                getSession().addReference(jcrLock.getLockToken());                return new JcrActiveLock(jcrLock, sessionScoped);            } catch (RepositoryException e) {                // UnsupportedRepositoryOperationException should not occur...                throw new JcrDavException(e);            }        } else {            return super.lock(reqLockInfo);        }    }    /**     * Refreshes the lock on this resource. With this implementation the     * {@link javax.jcr.lock lock} present on the underlying {@link javax.jcr.Node node}     * is refreshed. The timeout indicated by the <code>LockInfo</code>     * object is ignored.     *     * @param reqLockInfo LockInfo as build from the request.     * @param lockToken     * @return the updated lock info object.     * @throws org.apache.jackrabbit.webdav.DavException in case the lock could not be refreshed.     * @see org.apache.jackrabbit.webdav.DavResource#refreshLock(org.apache.jackrabbit.webdav.lock.LockInfo, String)     * @see javax.jcr.lock.Lock#refresh()     */    public ActiveLock refreshLock(LockInfo reqLockInfo, String lockToken)            throws DavException {        if (lockToken == null) {            throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);        }        ActiveLock lock = getLock(reqLockInfo.getType(), reqLockInfo.getScope());        if (lock == null) {            throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given scope/type present on this resource.");        }        if (Type.WRITE.equals(lock.getType())) {            try {                Lock jcrLock = ((Node) item).getLock();                jcrLock.refresh();                return new JcrActiveLock(jcrLock, EXCLUSIVE_SESSION.equals(lock.getScope()));            } catch (RepositoryException e) {                /*                  NOTE: LockException is only thrown by Lock.refresh()

⌨️ 快捷键说明

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