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 * @param list 082 * @param adapterName 083 * @return 084 * @since 5.8 085 */ 086 @SuppressWarnings({ "unchecked", "rawtypes" }) 087 private Object doGetAdapterOnList(DocumentModelList list, String adapterName) { 088 089 List<BusinessAdapter> adapters; 090 if (list instanceof Paginable) { 091 adapters = new PaginableWithDelegate((Paginable<DocumentModel>) list); 092 } else { 093 adapters = new ArrayList<BusinessAdapter>(); 094 } 095 096 for (DocumentModel docItem : list) { 097 adapters.add(getAdapter(adapterName, docItem)); 098 } 099 GenericEntity<List<BusinessAdapter>> entity = new GenericEntity<List<BusinessAdapter>>(adapters) { 100 }; 101 return entity; 102 } 103 104 @PUT 105 @Path("{adapterName}") 106 @Consumes(MediaType.APPLICATION_JSON) 107 public Object doPostAdapter(@PathParam("adapterName") String adapterName, BusinessAdapter input) { 108 ctx.getCoreSession().saveDocument(input.getDocument()); 109 110 ctx.getCoreSession().save(); 111 112 return new DefaultJsonAdapter(input); 113 } 114 115 @POST 116 @Path("{adapterName}/{docName}") 117 public Object doPutAdapter(@PathParam("adapterName") String adapterName, @PathParam("docName") String docName, 118 BusinessAdapter input) { 119 DocumentModel document = input.getDocument(); 120 121 DocumentObject dobj = (DocumentObject) getTarget(); 122 DocumentModel parentDoc = dobj.getDocument(); 123 124 document.setPathInfo(parentDoc.getPathAsString(), docName); 125 CoreSession session = ctx.getCoreSession(); 126 document = session.createDocument(document); 127 session.save(); 128 BusinessAdapter adapter = document.getAdapter(input.getClass()); 129 return new DefaultJsonAdapter(adapter); 130 } 131 132 private BusinessAdapter getAdapter(String adapterName, DocumentModel doc) { 133 ObjectCodecService cs = Framework.getService(ObjectCodecService.class); 134 ObjectCodec<?> codec = cs.getCodec(adapterName); 135 if (codec != null) { 136 return (BusinessAdapter) doc.getAdapter(codec.getJavaType()); 137 } else { 138 throw new WebResourceNotFoundException(String.format("Unable to find [%s] adapter", adapterName)); 139 } 140 } 141 142}