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