001/*
002 * Copyright 2012 Atteo.
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 */
016package org.atteo.moonshine.jetty.handlers;
017
018import javax.xml.bind.annotation.XmlElement;
019import javax.xml.bind.annotation.XmlElementRef;
020import javax.xml.bind.annotation.XmlRootElement;
021
022import org.atteo.evo.config.Configurable;
023import org.eclipse.jetty.server.handler.ContextHandler;
024
025
026/**
027 * Jetty context handler.
028 * <p>
029 * This handler wraps a call to handle
030 * by setting the context and servlet path, plus setting the context classloader.
031 * </p>
032 */
033@XmlRootElement(name = "context")
034public class ContextHandlerConfig extends Configurable {
035    /**
036     * Set the context path. Only requests that have a matching path
037     * will be passed to the wrapped handler.
038     */
039    @XmlElement
040    private String contextPath = "/";
041
042    /**
043     * Set the virtual hosts for the context. Only requests that have a matching
044     * host header or fully qualified URL will be passed to that context
045     * with a virtual host name. A context with no virtual host names or a null
046     * virtual host name is available to all requests that are not served
047     * by a context with a matching virtual host name.
048     */
049    @XmlElement
050    private String[] virtualHosts;
051
052    @XmlElementRef(required = true)
053    private HandlerConfig wrappedHandler;
054
055    public ContextHandler getHandler() {
056        ContextHandler contextHandler = new ContextHandler();
057        contextHandler.setContextPath(contextPath);
058        contextHandler.setHandler(wrappedHandler.getHandler());
059        contextHandler.setVirtualHosts(virtualHosts);
060
061        return contextHandler;
062    }
063
064    public HandlerConfig getWrappedHandlerConfig() {
065        return wrappedHandler;
066    }
067}