001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.webengine.jaxrs.views; 013 014import java.net.URI; 015import java.security.Principal; 016 017import javax.servlet.http.HttpServletRequest; 018import javax.ws.rs.Path; 019import javax.ws.rs.PathParam; 020import javax.ws.rs.WebApplicationException; 021import javax.ws.rs.core.UriInfo; 022 023import org.nuxeo.ecm.core.api.CoreSession; 024import org.nuxeo.ecm.platform.rendering.api.RenderingEngine; 025import org.nuxeo.ecm.platform.rendering.api.View; 026import org.osgi.framework.Bundle; 027 028/** 029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 030 */ 031public class BundleResource { 032 033 public final static String VIEW_ROOT = "VROOT"; 034 035 protected ResourceContext context; 036 037 public BundleResource() { 038 } 039 040 public BundleResource(ResourceContext context) { 041 setContext(context); 042 } 043 044 public BundleResource setContext(ResourceContext context) { 045 this.context = context; 046 return this; 047 } 048 049 public ResourceContext getContext() { 050 return context; 051 } 052 053 public final Bundle getBundle() { 054 return context.getBundle(); 055 } 056 057 public final RenderingEngine getRenderingEngine() { 058 return context.getRenderingEngine(); 059 } 060 061 public final View getView(String path) { 062 String basePath = context.getUriInfo().getBaseUri().toString(); 063 if (basePath.endsWith("/")) { 064 basePath = basePath.substring(0, basePath.length() - 1); 065 } 066 // we need to prefix with bundle name to avoid template cache collisions (in freemarker for ex.) 067 // path=getBundle().getSymbolicName() + ":/" + path; 068 return context.getRenderingEngine().getView(path, this).arg("baseUrl", basePath).arg(VIEW_ROOT, 069 context.getViewRoot()); 070 } 071 072 public final HttpServletRequest getRequest() { 073 return context.getRequest(); 074 } 075 076 public CoreSession getSession() { 077 return context.getSession(); 078 } 079 080 public Principal getPrincipal() { 081 return context.getPrincipal(); 082 } 083 084 public UriInfo getUriInfo() { 085 return context.getUriInfo(); 086 } 087 088 public URI getBaseUri() { 089 return context.getBaseUri(); 090 } 091 092 public <T extends BundleResource> T getResource(Class<T> clazz) { 093 try { 094 T res = clazz.newInstance(); 095 res.setContext(context); 096 return res; 097 } catch (ReflectiveOperationException e) { 098 throw new WebApplicationException(e, 500); 099 } 100 } 101 102 @Path("{any}") 103 public Object dispatch(@PathParam("any") String segment) { 104 BundleResource res = context.getApplication().getExtension(this, segment); 105 if (res != null) { 106 return res; 107 } 108 throw new WebApplicationException(404); 109 } 110 111 /** 112 * This method is only for contributed sub-resources. It will be ignored for root resources. Extension resources may 113 * override this method to dynamically accept or reject to be installed as a sub-resource of the target resource 114 * 115 * @param target 116 * @return 117 */ 118 public boolean accept(BundleResource target) { 119 return true; 120 } 121 122}