Search in sources :

Example 21 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class VersionControlledResourceImpl method checkin.

/**
 * Calls {@link javax.jcr.Node#checkin()} on the underlying repository node.
 *
 * @throws org.apache.jackrabbit.webdav.DavException
 * @see org.apache.jackrabbit.webdav.version.VersionControlledResource#checkin()
 */
public String checkin() throws DavException {
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    if (!isVersionControlled()) {
        throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED);
    }
    try {
        Version v = getNode().checkin();
        String versionHref = getLocatorFromNode(v).getHref(false);
        return versionHref;
    } catch (RepositoryException e) {
        // UnsupportedRepositoryException should not occur
        throw new JcrDavException(e);
    }
}
Also used : JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) DavException(org.apache.jackrabbit.webdav.DavException) JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) Version(javax.jcr.version.Version) RepositoryException(javax.jcr.RepositoryException)

Example 22 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class AbstractItemResource method move.

/**
 * Moves the underlying repository item to the indicated destination.
 *
 * @param destination
 * @throws DavException
 * @see DavResource#move(DavResource)
 * @see javax.jcr.Session#move(String, String)
 */
@Override
public void move(DavResource destination) throws DavException {
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    DavResourceLocator destLocator = destination.getLocator();
    if (!getLocator().isSameWorkspace(destLocator)) {
        throw new DavException(DavServletResponse.SC_FORBIDDEN);
    }
    try {
        String itemPath = getLocator().getRepositoryPath();
        String destItemPath = destination.getLocator().getRepositoryPath();
        if (getTransactionId() == null) {
            // if not part of a transaction directly import on workspace
            getRepositorySession().getWorkspace().move(itemPath, destItemPath);
        } else {
            // changes will not be persisted unless the tx is completed.
            getRepositorySession().move(itemPath, destItemPath);
        }
    // no use in calling 'complete' that would fail for a moved item anyway.
    } catch (PathNotFoundException e) {
        // according to rfc 2518
        throw new DavException(DavServletResponse.SC_CONFLICT, e.getMessage());
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 23 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class DefaultItemCollection method addMember.

/**
 * 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)
 */
@Override
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 directly import on workspace
                    // since changes would be explicitly 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.");
            }
            // PUT : create new or overwrite existing property.
            String ct = inputContext.getContentType();
            int type = JcrValueType.typeFromContentType(ct);
            if (type != PropertyType.UNDEFINED) {
                // no need to create value/values property. instead
                // prop-value can be retrieved directly:
                int pos = ct.indexOf(';');
                String charSet = (pos > -1) ? ct.substring(pos) : "UTF-8";
                if (type == PropertyType.BINARY) {
                    n.setProperty(memberName, inputContext.getInputStream());
                } else {
                    BufferedReader r = new BufferedReader(new InputStreamReader(inputContext.getInputStream(), charSet));
                    String line;
                    StringBuffer value = new StringBuffer();
                    while ((line = r.readLine()) != null) {
                        value.append(line);
                    }
                    n.setProperty(memberName, value.toString(), type);
                }
            } else {
                // try to parse the request body into a 'values' 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();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DavException(org.apache.jackrabbit.webdav.DavException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ItemExistsException(javax.jcr.ItemExistsException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 24 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class DefaultItemCollection method getMembers.

/**
 * @see org.apache.jackrabbit.webdav.DavResource#getMembers()
 */
@Override
public DavResourceIterator getMembers() {
    ArrayList<DavResource> memberList = new ArrayList<DavResource>();
    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);
}
Also used : NodeIterator(javax.jcr.NodeIterator) DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) DavResourceIteratorImpl(org.apache.jackrabbit.webdav.DavResourceIteratorImpl) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) JcrUserPrivilegesProperty(org.apache.jackrabbit.webdav.jcr.security.JcrUserPrivilegesProperty) DefaultDavProperty(org.apache.jackrabbit.webdav.property.DefaultDavProperty) JcrSupportedPrivilegesProperty(org.apache.jackrabbit.webdav.jcr.security.JcrSupportedPrivilegesProperty) NodeTypeProperty(org.apache.jackrabbit.webdav.jcr.nodetype.NodeTypeProperty) DavProperty(org.apache.jackrabbit.webdav.property.DavProperty) ValuesProperty(org.apache.jackrabbit.webdav.jcr.property.ValuesProperty) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 25 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class DefaultItemCollection method 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)
 */
@Override
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());
            long timeout = reqLockInfo.getTimeout();
            if (timeout == LockInfo.INFINITE_TIMEOUT) {
                timeout = Long.MAX_VALUE;
            } else {
                timeout = timeout / 1000;
            }
            javax.jcr.lock.LockManager lockMgr = getRepositorySession().getWorkspace().getLockManager();
            Lock jcrLock = lockMgr.lock((item).getPath(), reqLockInfo.isDeep(), sessionScoped, timeout, reqLockInfo.getOwner());
            ActiveLock lock = new JcrActiveLock(jcrLock);
            // add reference to DAVSession for this lock
            getSession().addReference(lock.getToken());
            return lock;
        } catch (RepositoryException e) {
            // UnsupportedRepositoryOperationException should not occur...
            throw new JcrDavException(e);
        }
    } else {
        return super.lock(reqLockInfo);
    }
}
Also used : ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock) DavException(org.apache.jackrabbit.webdav.DavException) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock) RepositoryException(javax.jcr.RepositoryException) ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) Lock(javax.jcr.lock.Lock) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock)

Aggregations

DavException (org.apache.jackrabbit.webdav.DavException)157 RepositoryException (javax.jcr.RepositoryException)89 IOException (java.io.IOException)61 HttpResponse (org.apache.http.HttpResponse)47 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)34 DavResource (org.apache.jackrabbit.webdav.DavResource)30 Element (org.w3c.dom.Element)29 ArrayList (java.util.ArrayList)25 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)25 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)18 Node (javax.jcr.Node)16 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)15 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)12 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)12 Path (java.nio.file.Path)11 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)10 Test (org.junit.Test)9 HrefProperty (org.apache.jackrabbit.webdav.property.HrefProperty)8