001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webdav.resource;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.ws.rs.OPTIONS;
026import javax.ws.rs.core.Response;
027
028import org.nuxeo.common.utils.Path;
029
030/**
031 * Base class for all resources (existing or not).
032 */
033public class AbstractResource {
034
035    protected String path;
036
037    protected String parentPath;
038
039    protected String name;
040
041    protected HttpServletRequest request;
042
043    public static String getParentPath(String path) {
044        path = new Path(path).removeLastSegments(1).toString();
045        // Ensures that path starts with a "/" and doesn't end with a "/".
046        if (path.endsWith("/")) {
047            path = path.substring(0, path.length() - 1);
048        }
049        if (!path.startsWith("/")) {
050            path = "/" + path;
051        }
052        return path;
053    }
054
055    public static String getNameFromPath(String path) {
056        return new Path(path).lastSegment();
057    }
058
059    protected AbstractResource(String path, HttpServletRequest request) {
060        this.path = path;
061        this.request = request;
062        parentPath = getParentPath(path);
063        name = getNameFromPath(path);
064    }
065
066    @OPTIONS
067    public Response options() {
068        return Response.status(204).entity("").header("DAV", "1,2") // not 1,2 for now.
069        .header("Allow",
070                "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, "
071                        + "PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK").build();
072    }
073
074}