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.servlet.config;
020
021import java.io.IOException;
022import java.net.URL;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.osgi.framework.Bundle;
028import org.osgi.service.http.HttpContext;
029
030/**
031 * A HttpContext that delegates resource lookup to contributed {@link ResourcesDescriptor} in the inverse order of the
032 * contribution (preserving the ordering imposed by extension mechanism)
033 * <p>
034 * A BundleHttpContext is created for every declated servlet when it is registered against to HttpService. The context
035 * is removed when the servlet is unregistered.
036 *
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class BundleHttpContext implements HttpContext {
040
041    protected Bundle bundle;
042
043    protected ResourceResolver resolver;
044
045    protected volatile ResourcesDescriptor[] resources;
046
047    public BundleHttpContext(Bundle bundle, String resourcesPath) {
048        this.bundle = bundle;
049        if (resourcesPath != null) {
050            if (resourcesPath.startsWith("file:")) {
051                resolver = new FileResourceResolver(resourcesPath.substring("file:".length()));
052            } else {
053                resolver = new BundleResourceResolver(bundle, resourcesPath);
054            }
055        }
056    }
057
058    public void setResources(ResourcesDescriptor[] resources) {
059        this.resources = resources;
060    }
061
062    @Override
063    public String getMimeType(String name) {
064        return null;
065    }
066
067    @Override
068    public URL getResource(String name) {
069        ResourcesDescriptor[] _resources = resources;
070        if (_resources != null) {
071            for (int i = _resources.length - 1; i >= 0; i--) {
072                URL url = _resources[i].getResource(name);
073                if (url != null) {
074                    return url;
075                }
076            }
077        }
078        if (resolver != null) {
079            return resolver.getResource(name);
080        }
081        return null;
082    }
083
084    @Override
085    public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
086        // default behaviour assumes the container has already performed authentication
087        return true;
088    }
089}