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