001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webdav.resource;
021
022import java.io.IOException;
023import java.io.UnsupportedEncodingException;
024
025import javax.servlet.http.HttpServletRequest;
026import javax.ws.rs.GET;
027import javax.ws.rs.HeaderParam;
028import javax.ws.rs.OPTIONS;
029import javax.ws.rs.Path;
030import javax.ws.rs.PathParam;
031import javax.ws.rs.Produces;
032import javax.ws.rs.WebApplicationException;
033import javax.ws.rs.core.Context;
034import javax.ws.rs.core.Response;
035import javax.ws.rs.core.UriInfo;
036import javax.xml.bind.JAXBException;
037
038import net.java.dev.webdav.jaxrs.methods.PROPFIND;
039
040import org.apache.commons.logging.Log;
041import org.apache.commons.logging.LogFactory;
042import org.nuxeo.ecm.core.api.Blob;
043import org.nuxeo.ecm.core.api.DocumentModel;
044import org.nuxeo.ecm.core.api.DocumentNotFoundException;
045import org.nuxeo.ecm.core.api.NuxeoException;
046import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
047import org.nuxeo.ecm.core.api.security.SecurityConstants;
048import org.nuxeo.ecm.webdav.backend.Backend;
049import org.nuxeo.ecm.webdav.backend.BackendHelper;
050
051//path is set at the servlet level, see the deployment-fragment file
052@Path("")
053public class RootResource {
054
055    private static final Log log = LogFactory.getLog(RootResource.class);
056
057    private HttpServletRequest request;
058
059    public RootResource(@Context HttpServletRequest request) {
060        log.debug(request.getMethod() + " " + request.getRequestURI());
061        this.request = request;
062    }
063
064    @GET
065    @Produces("text/html")
066    public Object getRoot() {
067        Object resource = findResource("");
068        if (resource instanceof FolderResource) {
069            return ((FolderResource) resource).get();
070        } else {
071            return ((VirtualFolderResource) findResource("")).get();
072        }
073    }
074
075    @OPTIONS
076    public Object getRootOptions() {
077        Object resource = findResource("");
078        if (resource instanceof FolderResource) {
079            return ((FolderResource) resource).options();
080        } else {
081            return ((VirtualFolderResource) findResource("")).options();
082        }
083    }
084
085    @PROPFIND
086    public Object getRootPropfind(@Context UriInfo uriInfo, @HeaderParam("depth") String depth) throws IOException,
087            JAXBException {
088        Object resource = findResource("");
089        if (resource instanceof FolderResource) {
090            return ((FolderResource) resource).propfind(uriInfo, depth);
091        } else {
092            return ((VirtualFolderResource) findResource("")).propfind(uriInfo, depth);
093        }
094    }
095
096    @Path("{path:.+}")
097    public Object findResource(@PathParam("path") String path) {
098        try {
099            path = new String(path.getBytes(), "UTF-8");
100        } catch (UnsupportedEncodingException e) {
101            throw new NuxeoException(e);
102        }
103
104        Backend backend = BackendHelper.getBackend(path, request);
105
106        if (backend == null) {
107            throw new WebApplicationException(Response.Status.NOT_FOUND);
108        }
109
110        if (backend.isVirtual()) {
111            return new VirtualFolderResource(path, request, backend.getVirtualFolderNames());
112        }
113
114        DocumentModel doc = null;
115        try {
116            doc = backend.getDocument(path);
117        } catch (DocumentNotFoundException e) {
118            log.error("Error during resolving path: " + path, e);
119            throw new WebApplicationException(Response.Status.CONFLICT);
120        }
121
122        if (doc == null) {
123            return new UnknownResource(path, request, backend);
124        }
125
126        // Send 401 error if not authorised to read.
127        if (!backend.hasPermission(doc.getRef(), SecurityConstants.READ)) {
128            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
129        }
130
131        if (doc.isFolder()) {
132            return new FolderResource(getDocumentPath(doc), doc, request, backend);
133        } else {
134            return new FileResource(getDocumentPath(doc), doc, request, backend);
135        }
136    }
137
138    private String getDocumentPath(DocumentModel source) {
139        if (source.isFolder()) {
140            return source.getPathAsString();
141        } else {
142            BlobHolder bh = source.getAdapter(BlobHolder.class);
143            if (bh != null) {
144                Blob blob = bh.getBlob();
145                if (blob != null) {
146                    return blob.getFilename();
147                }
148            }
149            return String.valueOf(source.getPropertyValue("dc:title"));
150        }
151    }
152
153}