Browse files

rename and use Servlet Filter

  • Loading branch information...
1 parent f9f031f commit 08a237a7459a7dcc613660fe7f2cf9083300f62b @jamesward committed Aug 10, 2012
View
2 Procfile
@@ -1 +1 @@
-web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war
+web: java $JAVA_OPTS -cp target/classes:target/dependency/* com.jamesward.Webapp
View
49 pom.xml
@@ -3,73 +3,42 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springmvc-https-enforcer</artifactId>
- <packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
- <properties>
- <org.springframework.version>3.1.1.RELEASE</org.springframework.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
<dependencies>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.0.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>taglibs</groupId>
- <artifactId>standard</artifactId>
- <version>1.1.2</version>
+ <version>3.1.2.RELEASE</version>
</dependency>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
+ <groupId>cglib</groupId>
+ <artifactId>cglib</artifactId>
+ <version>2.2.2</version>
</dependency>
<dependency>
- <groupId>com.github.jsimone</groupId>
- <artifactId>webapp-runner</artifactId>
- <version>7.0.22.3</version>
- <scope>provided</scope>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-webapp</artifactId>
+ <version>8.1.3.v20120416</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.2</version>
- </plugin>
- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
- <goal>copy</goal>
+ <goal>copy-dependencies</goal>
</goals>
- <configuration>
- <artifactItems>
- <artifactItem>
- <groupId>com.github.jsimone</groupId>
- <artifactId>webapp-runner</artifactId>
- <version>7.0.22.3</version>
- <destFileName>webapp-runner.jar</destFileName>
- </artifactItem>
- </artifactItems>
- </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
+
</project>
View
34 src/main/java/com/example/HttpsEnforcer.java
@@ -1,34 +0,0 @@
-package com.example;
-
-import org.springframework.stereotype.Component;
-import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-@Component
-public class HttpsEnforcer extends HandlerInterceptorAdapter {
-
- public static final String X_FORWARDED_PROTO = "x-forwarded-proto";
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
-
- if (request.getHeader(X_FORWARDED_PROTO) != null) {
-
- if (request.getHeader(X_FORWARDED_PROTO).indexOf("https") != 0) {
-
- System.out.println("getRequestURI: " + request.getRequestURI());
- System.out.println("getRemoteHost: " + request.getRemoteHost());
- System.out.println("getServerName: " + request.getServerName());
-
- response.sendRedirect("https://" + request.getRequestURI());
- return false;
- }
-
- }
-
- return true;
- }
-
-}
View
47 src/main/java/com/jamesward/HttpsEnforcer.java
@@ -0,0 +1,47 @@
+package com.jamesward;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class HttpsEnforcer implements Filter {
+
+ private FilterConfig filterConfig;
+
+ public static final String X_FORWARDED_PROTO = "x-forwarded-proto";
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ this.filterConfig = filterConfig;
+ }
+
+ @Override
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+
+ HttpServletRequest request = (HttpServletRequest) servletRequest;
+ HttpServletResponse response = (HttpServletResponse) servletResponse;
+
+ System.out.println("doFilter");
+
+ if (request.getHeader(X_FORWARDED_PROTO) != null) {
+
+ if (request.getHeader(X_FORWARDED_PROTO).indexOf("https") != 0) {
+
+ System.out.println("getRequestURI: " + request.getRequestURI());
+ System.out.println("getRemoteHost: " + request.getRemoteHost());
+ System.out.println("getServerName: " + request.getServerName());
+
+ response.sendRedirect("https://" + request.getRequestURI());
+ }
+
+ }
+
+ filterChain.doFilter(request, response);
+ }
+
+ @Override
+ public void destroy() {
+ // nothing
+ }
+}
View
21 src/main/java/com/jamesward/WebConfig.java
@@ -0,0 +1,21 @@
+package com.jamesward;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.*;
+
+@Configuration
+@EnableWebMvc
+public class WebConfig extends WebMvcConfigurerAdapter {
+
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("/**").addResourceLocations("classpath:/assets/");
+ }
+
+ @Override
+ public void addViewControllers(ViewControllerRegistry registry) {
+ registry.addViewController("/").setViewName("redirect:index.html");
+ }
+
+}
View
39 src/main/java/com/jamesward/Webapp.java
@@ -0,0 +1,39 @@
+package com.jamesward;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
+import org.springframework.web.servlet.DispatcherServlet;
+
+import javax.servlet.DispatcherType;
+import java.util.EnumSet;
+
+
+public class Webapp {
+
+ public static void main(String[] args) throws Exception {
+
+ final AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
+ applicationContext.register(WebConfig.class);
+
+ final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(applicationContext));
+ final ServletContextHandler context = new ServletContextHandler();
+ context.setContextPath("/");
+ context.addServlet(servletHolder, "/*");
+ context.addFilter(HttpsEnforcer.class, "/*", EnumSet.of(DispatcherType.REQUEST));
+
+ String webPort = System.getenv("PORT");
+ if (webPort == null || webPort.isEmpty()) {
+ webPort = "8080";
+ }
+
+ final Server server = new Server(Integer.valueOf(webPort));
+
+ server.setHandler(context);
+
+ server.start();
+ server.join();
+ }
+
+}
View
21 src/main/resources/applicationContext.xml
@@ -1,21 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
- <context:annotation-config />
- <context:component-scan base-package="com.example" />
-
- <mvc:annotation-driven/>
-
- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
-
-</beans>
View
6 src/main/resources/assets/index.html
@@ -0,0 +1,6 @@
+<!doctype html>
+<html>
+<body>
+hello, HTTPS
+</body>
+</html>
View
31 src/main/webapp/WEB-INF/web.xml
@@ -1,31 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
-
- <display-name>Spring-Hibernate-Template</display-name>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
-
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/people/*</url-pattern>
- </servlet-mapping>
-
-</web-app>
View
10 src/main/webapp/index.jsp
@@ -1,10 +0,0 @@
-<!doctype html>
-
-<html>
- <head>
- <title>hello, HTTPS</title>
- </head>
- <body>
- hello, HTTPS
- </body>
-</html>

0 comments on commit 08a237a

Please sign in to comment.