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 javax.servlet.http.HttpServletRequest;
023import javax.ws.rs.OPTIONS;
024import javax.ws.rs.core.Response;
025
026import org.nuxeo.common.utils.Path;
027
028/**
029 * Base class for all resources (existing or not).
030 */
031public class AbstractResource {
032
033    protected String path;
034
035    protected String parentPath;
036
037    protected String name;
038
039    protected HttpServletRequest request;
040
041    public static String getParentPath(String path) {
042        path = new Path(path).removeLastSegments(1).toString();
043        // Ensures that path starts with a "/" and doesn't end with a "/".
044        if (path.endsWith("/")) {
045            path = path.substring(0, path.length() - 1);
046        }
047        if (!path.startsWith("/")) {
048            path = "/" + path;
049        }
050        return path;
051    }
052
053    public static String getNameFromPath(String path) {
054        return new Path(path).lastSegment();
055    }
056
057    protected AbstractResource(String path, HttpServletRequest request) {
058        this.path = path;
059        this.request = request;
060        parentPath = getParentPath(path);
061        name = getNameFromPath(path);
062    }
063
064    @OPTIONS
065    public Response options() {
066        return Response.status(204).entity("").header("DAV", "1,2") // not 1,2 for now.
067        .header("Allow",
068                "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, "
069                        + "PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK").build();
070    }
071
072}