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 * vpasquier <vpasquier@nuxeo.com> 016 * dmetzler <dmetzler@nuxeo.com> 017 */ 018 019package org.nuxeo.box.api; 020 021import org.nuxeo.box.api.marshalling.exceptions.BoxRestException; 022import org.nuxeo.box.api.service.BoxService; 023import org.apache.commons.lang.StringUtils; 024import org.nuxeo.ecm.core.api.DocumentNotFoundException; 025import org.nuxeo.ecm.webengine.model.WebObject; 026import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 027import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException; 028import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 029import org.nuxeo.runtime.api.Framework; 030 031import javax.ws.rs.Path; 032import javax.ws.rs.PathParam; 033import javax.ws.rs.Produces; 034import javax.ws.rs.WebApplicationException; 035import javax.ws.rs.core.Response; 036 037/** 038 * Entry point for Nuxeo Box API 039 * 040 * @since 5.9.2 041 */ 042@Path("/box/2.0{repo : (/repo/[^/]+?)?}") 043@Produces("text/html;charset=UTF-8") 044@WebObject(type = "box") 045public class Box extends ModuleRoot { 046 047 BoxService boxService; 048 049 @Override 050 public void initialize(Object... args) { 051 boxService = Framework.getLocalService(BoxService.class); 052 } 053 054 @Path("/") 055 public Object doGetRepository(@PathParam("repo") final String repositoryParam) throws DocumentNotFoundException { 056 if (StringUtils.isNotBlank(repositoryParam)) { 057 String repoName = repositoryParam.substring("repo/".length() + 1); 058 try { 059 ctx.setRepositoryName(repoName); 060 } catch (IllegalArgumentException e) { 061 throw new WebResourceNotFoundException(e.getMessage()); 062 } 063 } 064 return newObject("repo"); 065 } 066 067 @Path("/folders") 068 public Object doGetFolder() { 069 return newObject("folder"); 070 } 071 072 @Path("/files") 073 public Object doGetFile() { 074 return newObject("file"); 075 } 076 077 @Path("/search") 078 public Object doGetSearch() { 079 return newObject("search"); 080 } 081 082 @Path("/comments") 083 public Object doGetComment() { 084 return newObject("comment"); 085 } 086 087 @Path("/collaborations") 088 public Object doGetCollaborations() { 089 return newObject("collaborations"); 090 } 091 092 /** 093 * Return a Box compat Exception Response in JSON 094 */ 095 @Override 096 public Object handleError(final WebApplicationException e) { 097 if (e instanceof WebSecurityException) { 098 return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).entity( 099 boxService.getJSONBoxException(e, Response.Status.UNAUTHORIZED.getStatusCode())).type( 100 "json/application").build(); 101 } else if (e instanceof WebResourceNotFoundException) { 102 return Response.status(Response.Status.NOT_FOUND.getStatusCode()).entity( 103 boxService.getJSONBoxException(e, Response.Status.NOT_FOUND.getStatusCode())).type( 104 "json/application").build(); 105 } else if (e instanceof BoxRestException) { 106 return Response.status(((BoxRestException) e).getErrorCode()).entity( 107 boxService.getJSONBoxException(e, ((BoxRestException) e).getErrorCode())).type("json/application").build(); 108 } else { 109 return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity( 110 boxService.getJSONBoxException(e, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())).type( 111 "json/application").build(); 112 } 113 } 114 115}