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