Search in sources :

Example 41 with DavResource

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

the class DavResourceFactoryImpl method createResource.

/**
 * Create a new <code>DavResource</code> from the specified locator and request
 * objects. Note, that in contrast to
 * {@link #createResource(DavResourceLocator, DavSession)} the locator may
 * point to a non-existing resource.
 * <p>
 * If the request contains a {@link org.apache.jackrabbit.webdav.version.DeltaVServletRequest#getLabel()
 * Label header}, the resource is build from the indicated
 * {@link org.apache.jackrabbit.webdav.version.VersionResource version} instead.
 *
 * @param locator
 * @param request
 * @param response
 * @return
 * @see DavResourceFactory#createResource(org.apache.jackrabbit.webdav.DavResourceLocator, org.apache.jackrabbit.webdav.DavServletRequest, org.apache.jackrabbit.webdav.DavServletResponse)
 */
public DavResource createResource(DavResourceLocator locator, DavServletRequest request, DavServletResponse response) throws DavException {
    JcrDavSession.checkImplementation(request.getDavSession());
    JcrDavSession session = (JcrDavSession) request.getDavSession();
    DavResource resource;
    String type = request.getParameter("type");
    if (locator.isRootLocation()) {
        // root
        resource = new RootCollection(locator, session, this);
    } else if ("journal".equals(type) && locator.getResourcePath().equals(locator.getWorkspacePath())) {
        // feed/event journal resource
        try {
            EventJournal ej = session.getRepositorySession().getWorkspace().getObservationManager().getEventJournal();
            if (ej == null) {
                throw new DavException(HttpServletResponse.SC_NOT_IMPLEMENTED, "event journal not supported");
            }
            resource = new EventJournalResourceImpl(ej, locator, session, request, this);
        } catch (AccessDeniedException ex) {
            // EventJournal only allowed for admin?
            throw new DavException(HttpServletResponse.SC_UNAUTHORIZED, ex);
        } catch (RepositoryException ex) {
            throw new DavException(HttpServletResponse.SC_BAD_REQUEST, ex);
        }
    } else if (locator.getResourcePath().equals(locator.getWorkspacePath())) {
        // workspace resource
        resource = new WorkspaceResourceImpl(locator, session, this);
    } else {
        // resource corresponds to a repository item
        try {
            resource = createResourceForItem(locator, session);
            Item item = getItem(session, locator);
            boolean versionable = item.isNode() && ((Node) item).isNodeType(JcrConstants.MIX_VERSIONABLE);
            /* if the created resource is version-controlled and the request
                contains a Label header, the corresponding Version must be used
                instead.*/
            if (request instanceof DeltaVServletRequest && versionable) {
                String labelHeader = ((DeltaVServletRequest) request).getLabel();
                if (labelHeader != null && DavMethods.isMethodAffectedByLabel(request) && isVersionControlled(resource)) {
                    Version v = ((Node) item).getVersionHistory().getVersionByLabel(labelHeader);
                    DavResourceLocator vloc = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), v.getPath(), false);
                    resource = new VersionItemCollection(vloc, session, this, v);
                }
            }
        } catch (PathNotFoundException e) {
            /* item does not exist yet: create the default resources
                Note: MKCOL request forces a collection-resource even if there already
                exists a repository-property with the given path. the MKCOL will
                in that particular case fail with a 405 (method not allowed).*/
            if (DavMethods.getMethodCode(request.getMethod()) == DavMethods.DAV_MKCOL) {
                resource = new VersionControlledItemCollection(locator, session, this, null);
            } else {
                resource = new DefaultItemResource(locator, session, this, null);
            }
        } catch (RepositoryException e) {
            log.error("Failed to build resource from item '" + locator.getRepositoryPath() + "'");
            throw new JcrDavException(e);
        }
    }
    if (request instanceof TransactionDavServletRequest && resource instanceof TransactionResource) {
        ((TransactionResource) resource).init(txMgr, ((TransactionDavServletRequest) request).getTransactionId());
    }
    if (resource instanceof ObservationResource) {
        ((ObservationResource) resource).init(subsMgr);
    }
    return resource;
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) RepositoryException(javax.jcr.RepositoryException) TransactionResource(org.apache.jackrabbit.webdav.transaction.TransactionResource) EventJournal(javax.jcr.observation.EventJournal) Item(javax.jcr.Item) Version(javax.jcr.version.Version) TransactionDavServletRequest(org.apache.jackrabbit.webdav.transaction.TransactionDavServletRequest) ObservationResource(org.apache.jackrabbit.webdav.observation.ObservationResource) DeltaVServletRequest(org.apache.jackrabbit.webdav.version.DeltaVServletRequest) VersionItemCollection(org.apache.jackrabbit.webdav.jcr.version.VersionItemCollection) PathNotFoundException(javax.jcr.PathNotFoundException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 42 with DavResource

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

the class RootCollection method spool.

/**
 * Sets content lengths to '0' and retrieves the modification time.
 *
 * @param outputContext
 * @throws IOException
 * @see DavResource#spool(org.apache.jackrabbit.webdav.io.OutputContext)
 */
@Override
public void spool(OutputContext outputContext) throws IOException {
    if (outputContext.hasStream()) {
        Session session = getRepositorySession();
        Repository rep = session.getRepository();
        String repName = rep.getDescriptor(Repository.REP_NAME_DESC);
        String repURL = rep.getDescriptor(Repository.REP_VENDOR_URL_DESC);
        String repVersion = rep.getDescriptor(Repository.REP_VERSION_DESC);
        String repostr = repName + " " + repVersion;
        StringBuilder sb = new StringBuilder();
        sb.append("<html><head><title>");
        sb.append(repostr);
        sb.append("</title></head>");
        sb.append("<body><h2>").append(repostr).append("</h2>");
        sb.append("<h3>Available Workspace Resources:</h3><ul>");
        DavResourceIterator it = getMembers();
        while (it.hasNext()) {
            DavResource res = it.nextResource();
            sb.append("<li><a href=\"");
            sb.append(res.getHref());
            sb.append("\">");
            sb.append(res.getDisplayName());
            sb.append("</a></li>");
        }
        sb.append("</ul><hr size=\"1\"><em>Powered by <a href=\"");
        sb.append(repURL).append("\">").append(repName);
        sb.append("</a> ").append(repVersion);
        sb.append("</em></body></html>");
        outputContext.setContentLength(sb.length());
        outputContext.setModificationTime(getModificationTime());
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputContext.getOutputStream(), "utf8"));
        writer.print(sb.toString());
        writer.close();
    } else {
        outputContext.setContentLength(0);
        outputContext.setModificationTime(getModificationTime());
    }
}
Also used : DavResourceIterator(org.apache.jackrabbit.webdav.DavResourceIterator) Repository(javax.jcr.Repository) DavResource(org.apache.jackrabbit.webdav.DavResource) OutputStreamWriter(java.io.OutputStreamWriter) Session(javax.jcr.Session) PrintWriter(java.io.PrintWriter)

Example 43 with DavResource

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

the class WorkspaceResourceImpl method spool.

/**
 * @param outputContext
 * @throws IOException
 */
@Override
public void spool(OutputContext outputContext) throws IOException {
    outputContext.setProperty("Link", "<?" + EventJournalResourceImpl.RELURIFROMWORKSPACE + ">; title=\"Event Journal\"; rel=alternate; type=\"" + AtomFeedConstants.MEDIATYPE + "\"");
    if (outputContext.hasStream()) {
        Session session = getRepositorySession();
        Repository rep = session.getRepository();
        String repName = rep.getDescriptor(Repository.REP_NAME_DESC);
        String repURL = rep.getDescriptor(Repository.REP_VENDOR_URL_DESC);
        String repVersion = rep.getDescriptor(Repository.REP_VERSION_DESC);
        String repostr = repName + " " + repVersion;
        StringBuilder sb = new StringBuilder();
        sb.append("<html><head><title>");
        sb.append(repostr);
        sb.append("</title>");
        sb.append("<link rel=alternate type=\"" + AtomFeedConstants.MEDIATYPE + "\" title=\"Event Journal\" href=\"?" + EventJournalResourceImpl.RELURIFROMWORKSPACE + "\">");
        sb.append("</head>");
        sb.append("<body><h2>").append(repostr).append("</h2><ul>");
        sb.append("<li><a href=\"..\">..</a></li>");
        DavResourceIterator it = getMembers();
        while (it.hasNext()) {
            DavResource res = it.nextResource();
            sb.append("<li><a href=\"");
            sb.append(res.getHref());
            sb.append("\">");
            sb.append(res.getDisplayName());
            sb.append("</a></li>");
        }
        sb.append("</ul><hr size=\"1\"><em>Powered by <a href=\"");
        sb.append(repURL).append("\">").append(repName);
        sb.append("</a> ").append(repVersion);
        sb.append("</em></body></html>");
        outputContext.setContentLength(sb.length());
        outputContext.setModificationTime(getModificationTime());
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputContext.getOutputStream(), "utf8"));
        writer.print(sb.toString());
        writer.close();
    } else {
        outputContext.setContentLength(0);
        outputContext.setModificationTime(getModificationTime());
    }
}
Also used : DavResourceIterator(org.apache.jackrabbit.webdav.DavResourceIterator) Repository(javax.jcr.Repository) DavResource(org.apache.jackrabbit.webdav.DavResource) OutputStreamWriter(java.io.OutputStreamWriter) Session(javax.jcr.Session) PrintWriter(java.io.PrintWriter)

Example 44 with DavResource

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

the class WorkspaceResourceImpl method getCollection.

/**
 * Retrieve the collection that has all workspace collections
 * as internal members.
 *
 * @see org.apache.jackrabbit.webdav.DavResource#getCollection()
 */
@Override
public DavResource getCollection() {
    DavResource collection = null;
    // create location with 'null' values for workspace-path and resource-path
    DavResourceLocator parentLoc = getLocator().getFactory().createResourceLocator(getLocator().getPrefix(), null, null, false);
    try {
        collection = createResourceFromLocator(parentLoc);
    } catch (DavException e) {
        log.error("Unexpected error while retrieving collection: " + e.getMessage());
    }
    return collection;
}
Also used : DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 45 with DavResource

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

the class AbstractItemResource method getCollection.

/**
 * Returns the resource representing the parent item of the repository item
 * represented by this resource. If this resoure represents the root item
 * a {@link RootCollection} is returned.
 *
 * @return the collection this resource is internal member of. Except for the
 * repository root, the returned collection always represent the parent
 * repository node.
 * @see org.apache.jackrabbit.webdav.DavResource#getCollection()
 */
@Override
public DavResource getCollection() {
    DavResource collection = null;
    String parentPath = Text.getRelativeParent(getResourcePath(), 1);
    DavResourceLocator parentLoc = getLocator().getFactory().createResourceLocator(getLocator().getPrefix(), getLocator().getWorkspacePath(), parentPath);
    try {
        collection = createResourceFromLocator(parentLoc);
    } catch (DavException e) {
        log.error("Unexpected error while retrieving collection: " + e.getMessage());
    }
    return collection;
}
Also used : DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Aggregations

DavResource (org.apache.jackrabbit.webdav.DavResource)50 DavException (org.apache.jackrabbit.webdav.DavException)30 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)22 RepositoryException (javax.jcr.RepositoryException)16 ArrayList (java.util.ArrayList)9 Path (java.nio.file.Path)7 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)7 Node (javax.jcr.Node)6 VersionHistory (javax.jcr.version.VersionHistory)6 DavResourceIteratorImpl (org.apache.jackrabbit.webdav.DavResourceIteratorImpl)6 IOException (java.io.IOException)5 Version (javax.jcr.version.Version)4 DavResourceIterator (org.apache.jackrabbit.webdav.DavResourceIterator)4 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Item (javax.jcr.Item)3 NodeIterator (javax.jcr.NodeIterator)3 Repository (javax.jcr.Repository)3 ManagedRepository (org.apache.archiva.repository.ManagedRepository)3 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)3