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.XmlElementWrapper;
021import javax.xml.bind.annotation.XmlRootElement;
022
023import org.eclipse.jetty.server.Handler;
024import org.eclipse.jetty.server.handler.ResourceHandler;
025
026/**
027 * Jetty resource handler.
028 * <p>
029 * Serves static content and handles If-Modified-Since headers.
030 * No caching is done. Requests for resources that do not exist
031 * are let pass (Eg no 404's).
032 * </p>
033 *
034 * @see ResourceHandler
035 */
036@XmlRootElement(name = "resource")
037public class ResourceHandlerConfig extends HandlerConfig {
038    @XmlElement
039    private boolean directoriesListed = false;
040
041    @XmlElementWrapper(name = "welcomeFiles")
042    @XmlElement(name = "welcomeFile")
043    private String[] welcomeFiles = {"index.html"};
044
045    @XmlElementWrapper(name = "mimeTypes")
046    @XmlElementRef
047    private ResourceHandlerMimeTypeConfig[] mimeTypes = {};
048
049    @XmlElement
050    private String resourceBase;
051
052    @Override
053    public Handler getHandler() {
054        ResourceHandler handler = new ResourceHandler();
055        handler.setDirectoriesListed(directoriesListed);
056        handler.setWelcomeFiles(welcomeFiles);
057
058        for (ResourceHandlerMimeTypeConfig mimeType : mimeTypes) {
059            handler.getMimeTypes().addMimeMapping(mimeType.getExtension(), mimeType.getName());
060        }
061
062        if (resourceBase != null) {
063            handler.setResourceBase(resourceBase);
064        }
065
066        return handler;
067    }
068}