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