001/* 002 * (C) Copyright 2013 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 */ 019package org.nuxeo.ecm.restapi.server.jaxrs.adapters; 020 021import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; 022 023import java.util.ArrayList; 024import java.util.List; 025 026import javax.ws.rs.Consumes; 027import javax.ws.rs.GET; 028import javax.ws.rs.POST; 029import javax.ws.rs.PUT; 030import javax.ws.rs.Path; 031import javax.ws.rs.PathParam; 032import javax.ws.rs.Produces; 033import javax.ws.rs.core.GenericEntity; 034import javax.ws.rs.core.MediaType; 035 036import org.nuxeo.ecm.automation.core.operations.business.adapter.BusinessAdapter; 037import org.nuxeo.ecm.automation.core.util.Paginable; 038import org.nuxeo.ecm.automation.io.services.codec.ObjectCodec; 039import org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService; 040import org.nuxeo.ecm.automation.jaxrs.DefaultJsonAdapter; 041import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableWithDelegate; 042import org.nuxeo.ecm.core.api.CoreSession; 043import org.nuxeo.ecm.core.api.DocumentModel; 044import org.nuxeo.ecm.core.api.DocumentModelList; 045import org.nuxeo.ecm.core.api.NuxeoException; 046import org.nuxeo.ecm.core.rest.DocumentObject; 047import org.nuxeo.ecm.webengine.model.WebAdapter; 048import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 049import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter; 050import org.nuxeo.runtime.api.Framework; 051 052/** 053 * Basic CRUD with a BusinessAdapter 054 * 055 * @since 5.7.2 056 */ 057@WebAdapter(name = BOAdapter.NAME, type = "BOService", targetType = "Document") 058@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+esentity" }) 059public class BOAdapter extends DefaultAdapter { 060 061 public static final String NAME = "bo"; 062 063 @GET 064 @Path("{adapterName}") 065 public Object doGetAdapter(@PathParam("adapterName") String adapterName) { 066 DocumentModel doc = getTarget().getAdapter(DocumentModel.class); 067 if (doc != null) { 068 BusinessAdapter adapter = getAdapter(adapterName, doc); 069 return new DefaultJsonAdapter(adapter); 070 } 071 072 DocumentModelList list = getTarget().getAdapter(DocumentModelList.class); 073 if (list != null) { 074 return doGetAdapterOnList(list, adapterName); 075 } 076 077 throw new NuxeoException("Adapter can only be executed on Document or DocumentList", SC_BAD_REQUEST); 078 } 079 080 /** 081 * @since 5.8 082 */ 083 @SuppressWarnings({ "unchecked", "rawtypes" }) 084 private Object doGetAdapterOnList(DocumentModelList list, String adapterName) { 085 086 List<BusinessAdapter> adapters; 087 if (list instanceof Paginable) { 088 adapters = new PaginableWithDelegate((Paginable<DocumentModel>) list); 089 } else { 090 adapters = new ArrayList<>(); 091 } 092 093 for (DocumentModel docItem : list) { 094 adapters.add(getAdapter(adapterName, docItem)); 095 } 096 GenericEntity<List<BusinessAdapter>> entity = new GenericEntity<List<BusinessAdapter>>(adapters) { 097 }; 098 return entity; 099 } 100 101 @PUT 102 @Path("{adapterName}") 103 @Consumes(MediaType.APPLICATION_JSON) 104 public Object doPostAdapter(@PathParam("adapterName") String adapterName, BusinessAdapter input) { 105 ctx.getCoreSession().saveDocument(input.getDocument()); 106 107 ctx.getCoreSession().save(); 108 109 return new DefaultJsonAdapter(input); 110 } 111 112 @POST 113 @Path("{adapterName}/{docName}") 114 public Object doPutAdapter(@PathParam("adapterName") String adapterName, @PathParam("docName") String docName, 115 BusinessAdapter input) { 116 DocumentModel document = input.getDocument(); 117 118 DocumentObject dobj = (DocumentObject) getTarget(); 119 DocumentModel parentDoc = dobj.getDocument(); 120 121 document.setPathInfo(parentDoc.getPathAsString(), docName); 122 CoreSession session = ctx.getCoreSession(); 123 document = session.createDocument(document); 124 session.save(); 125 BusinessAdapter adapter = document.getAdapter(input.getClass()); 126 return new DefaultJsonAdapter(adapter); 127 } 128 129 private BusinessAdapter getAdapter(String adapterName, DocumentModel doc) { 130 ObjectCodecService cs = Framework.getService(ObjectCodecService.class); 131 ObjectCodec<?> codec = cs.getCodec(adapterName); 132 if (codec != null) { 133 return (BusinessAdapter) doc.getAdapter(codec.getJavaType()); 134 } else { 135 throw new WebResourceNotFoundException(String.format("Unable to find [%s] adapter", adapterName)); 136 } 137 } 138 139}