001package org.nuxeo.template.jaxrs; 002 003import javax.ws.rs.GET; 004import javax.ws.rs.Path; 005import javax.ws.rs.PathParam; 006import javax.ws.rs.Produces; 007 008import org.nuxeo.ecm.core.api.Blob; 009import org.nuxeo.ecm.core.api.DocumentModel; 010import org.nuxeo.ecm.core.api.DocumentRef; 011import org.nuxeo.ecm.core.api.IdRef; 012import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 013import org.nuxeo.ecm.webengine.model.WebObject; 014import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 015import org.nuxeo.template.api.adapters.TemplateBasedDocument; 016import org.nuxeo.template.api.adapters.TemplateSourceDocument; 017 018@WebObject(type = "templateBasedResource") 019public class TemplateBasedResource extends DefaultObject { 020 021 protected String uuid; 022 023 public TemplateBasedResource() { 024 uuid = null; 025 } 026 027 public TemplateBasedResource(String uuid) { 028 this.uuid = uuid; 029 } 030 031 protected void initialize(Object... args) { 032 if (args != null && args.length > 0) { 033 this.uuid = (String) args[0]; 034 } 035 } 036 037 protected TemplateBasedDocument resolve() { 038 if (uuid != null) { 039 IdRef idRef = new IdRef(uuid); 040 DocumentModel doc = getContext().getCoreSession().getDocument(idRef); 041 return doc.getAdapter(TemplateBasedDocument.class); 042 } else { 043 return null; 044 } 045 } 046 047 @GET 048 public Object get() { 049 TemplateBasedDocument tmpl = resolve(); 050 if (tmpl == null) { 051 return ""; 052 } else { 053 StringBuffer sb = new StringBuffer(); 054 sb.append(tmpl.getAdaptedDoc().getId() + " - " + tmpl.getAdaptedDoc().getTitle()); 055 return sb.toString(); 056 } 057 } 058 059 @GET 060 @Path("templates") 061 public String getAssociatedTemplates() { 062 063 IdRef idRef = new IdRef(uuid); 064 DocumentModel doc = getContext().getCoreSession().getDocument(idRef); 065 TemplateBasedDocument tmpl = doc.getAdapter(TemplateBasedDocument.class); 066 if (tmpl == null) { 067 return "This document is not template based"; 068 } 069 return tmpl.getTemplateNames().toString(); 070 } 071 072 @Path("template/{name}") 073 public Object getAssociatedTemplate(@PathParam(value = "name") String name) { 074 075 IdRef idRef = new IdRef(uuid); 076 DocumentModel doc = getContext().getCoreSession().getDocument(idRef); 077 TemplateBasedDocument tmpl = doc.getAdapter(TemplateBasedDocument.class); 078 if (tmpl == null) { 079 return "This document is not template based"; 080 } 081 082 DocumentRef sourceRef = tmpl.getSourceTemplateDocRef(name); 083 if (sourceRef != null) { 084 return getContext().newObject("templateResource", sourceRef.toString()); 085 } 086 return null; 087 } 088 089 @GET 090 @Path("resource/{templateName}/{resourceName}") 091 @Produces("*/*") 092 public Blob getResource(@PathParam(value = "templateName") String templateName, 093 @PathParam(value = "resourceName") String resourceName) { 094 095 TemplateBasedDocument tmpl = resolve(); 096 097 BlobHolder bh = tmpl.getAdaptedDoc().getAdapter(BlobHolder.class); 098 if (bh != null) { 099 for (Blob blob : bh.getBlobs()) { 100 if (resourceName.equalsIgnoreCase(blob.getFilename())) { 101 return blob; 102 } 103 } 104 } 105 106 TemplateSourceDocument template = tmpl.getSourceTemplate(templateName); 107 if (template != null) { 108 return TemplateResource.getResource(template, resourceName); 109 } 110 return null; 111 } 112}