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

co.mewf.humpty.servlet.HumptyServletContextInitializer Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package co.mewf.humpty.servlet;

import java.util.Set;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import co.mewf.humpty.Pipeline;
import co.mewf.humpty.config.Configuration;
import co.mewf.humpty.config.Configuration.Options;
import co.mewf.humpty.config.HumptyBootstrap;
import co.mewf.humpty.servlet.html.Includes;
import co.mewf.humpty.spi.PipelineElement;

public class HumptyServletContextInitializer implements ServletContainerInitializer, PipelineElement {
  
  public static final String DEFAULT_URL_PATTERN = "/humpty";
  
  @Override
  public String getName() {
    return "servlet";
  }

  @Override
  public void onStartup(Set> c, ServletContext ctx) throws ServletException {
    Configuration configuration = Configuration.load("humpty.toml");
    HumptyBootstrap humptyBootstrap = new HumptyBootstrap(configuration, ctx);
    Options options = configuration.getOptionsFor(this);
    Pipeline pipeline = humptyBootstrap.createPipeline();
    String urlPattern = options.get("urlPattern", DEFAULT_URL_PATTERN);
    Includes includes = new Includes(configuration, ctx.getContextPath(), urlPattern);
    
    if (Thread.currentThread().getContextClassLoader().getResource(configuration.getGlobalOptions().getDigestFile().toString()) == null) {
      ServletRegistration.Dynamic registration = ctx.addServlet("humptyFilter", HumptyFilter.class);
      registration.addMapping(urlPattern + "/*");
    }

    ctx.setAttribute(Includes.class.getName(), includes);
    ctx.setAttribute(Pipeline.class.getName(), pipeline);
    ctx.setAttribute(HumptyServletContextInitializer.class.getName(), urlPattern);
    ctx.setAttribute(Configuration.class.getName(), configuration);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy