All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opentable.server.StaticResourceConfiguration Maven / Gradle / Ivy

Go to download

Wiring for basic Jetty core servlet regardless of MVC or JaxRS choices. Also wires logging, metrics, etc

There is a newer version: 6.0.4
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.opentable.server;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.util.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

// TODO Don't serve directory indexes.

@Configuration
public class StaticResourceConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(StaticResourceConfiguration.class);

    public static final String DEFAULT_PATH_NAME = "static";
    private static final String PATH_CONFIG_VALUE = "${ot.httpserver.static-path:" + DEFAULT_PATH_NAME + "}";

    private final String staticPathName;

    @Inject
    StaticResourceConfiguration(@Value(PATH_CONFIG_VALUE) final String staticPathName) {
        this.staticPathName = staticPathName;
    }

    public String staticPath() {
        return staticPath("");
    }

    public String staticPath(final String rest) {
        return String.format("/%s/%s", staticPathName, rest);
    }

    @Bean
    @SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
    public ServletRegistrationBean staticResourceServlet() {
        try(Resource rsrc = Resource.newClassPathResource(staticPath())){
            LOG.debug("Found static resources at {}", rsrc);

            if (rsrc == null) {
                LOG.info("Didn't find '/static' on classpath, not serving static files");
                ServletRegistrationBean servletRegistrationBean =  new ServletRegistrationBean<>(new DefaultServlet());
                servletRegistrationBean.setName("static-inactive");
                servletRegistrationBean.setEnabled(false);
                return servletRegistrationBean;
            }

            DefaultServlet servlet = new DefaultServlet();
            ServletRegistrationBean bean = new ServletRegistrationBean<>(servlet, staticPath() + "*");
            bean.addInitParameter("gzip", "true");
            bean.addInitParameter("etags", "true");
            bean.addInitParameter("resourceBase", StringUtils.substringBeforeLast(rsrc.toString(), staticPathName));
            LOG.debug("Configuring static resources: {}", bean.getInitParameters());
            return bean;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy