001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     dmetzler
016 */
017
018package org.nuxeo.ecm.restapi.server.jaxrs;
019
020import javax.ws.rs.Path;
021import javax.ws.rs.PathParam;
022import javax.ws.rs.Produces;
023import javax.ws.rs.WebApplicationException;
024
025import org.apache.commons.lang.StringUtils;
026import org.nuxeo.ecm.automation.server.jaxrs.RestOperationException;
027import org.nuxeo.ecm.core.api.DocumentNotFoundException;
028import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper;
029import org.nuxeo.ecm.webengine.WebException;
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
032import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
033
034/**
035 * The root entry for the WebEngine module.
036 *
037 * @since 5.7.2
038 */
039@Path("/api/v1{repo : (/repo/[^/]+?)?}")
040@Produces("text/html;charset=UTF-8")
041@WebObject(type = "APIRoot")
042public class APIRoot extends ModuleRoot {
043
044    @Path("/")
045    public Object doGetRepository(@PathParam("repo") String repositoryParam) throws DocumentNotFoundException {
046        if (StringUtils.isNotBlank(repositoryParam)) {
047            String repoName = repositoryParam.substring("repo/".length() + 1);
048            try {
049                ctx.setRepositoryName(repoName);
050            } catch (IllegalArgumentException e) {
051                throw new WebResourceNotFoundException(e.getMessage());
052            }
053
054        }
055        return newObject("repo");
056    }
057
058    @Path("/user")
059    public Object doGetUser() {
060        return newObject("users");
061    }
062
063    @Path("/group")
064    public Object doGetGroup() {
065        return newObject("groups");
066    }
067
068    @Path("/automation")
069    public Object getAutomationEndPoint() {
070        return newObject("automation");
071    }
072
073    @Path("/directory")
074    public Object doGetDirectory() {
075        return newObject("directory");
076    }
077
078    @Path("/doc")
079    public Object doGetDocumentation() {
080        return newObject("doc");
081    }
082
083    @Path("/query")
084    public Object doQuery() {
085        return newObject("query");
086    }
087
088    @Path("/config")
089    public Object doGetConfig() {
090        return newObject("config");
091    }
092
093    @Path("/conversion")
094    public Object doGetConversion() {
095        return newObject("conversions");
096    }
097
098    @Override
099    public Object handleError(final WebApplicationException cause) {
100        Throwable unWrapException = ExceptionHelper.unwrapException(cause);
101        if (unWrapException instanceof RestOperationException) {
102            int customHttpStatus = ((RestOperationException) unWrapException)
103                    .getStatus();
104            return WebException.newException(
105                    cause.getMessage(), cause, customHttpStatus);
106        }
107        return WebException.newException(
108                cause.getMessage(), unWrapException);
109    }
110
111    /**
112     * @since 7.2
113     */
114    @Path("/ext/{otherPath}")
115    public Object route(@PathParam("otherPath") String otherPath) {
116        return newObject(otherPath);
117    }
118}