27 changed files with 1215 additions and 7 deletions
@ -0,0 +1,53 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
<parent> |
||||||
|
<!-- Your own application should inherit from spring-boot-starter-parent --> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-samples</artifactId> |
||||||
|
<version>1.2.0.BUILD-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<artifactId>spring-boot-sample-undertow-ssl</artifactId> |
||||||
|
<name>Spring Boot Undertow SSL Sample</name> |
||||||
|
<description>Spring Boot Undertow SSL Sample</description> |
||||||
|
<url>http://projects.spring.io/spring-boot/</url> |
||||||
|
<organization> |
||||||
|
<name>Pivotal Software, Inc.</name> |
||||||
|
<url>http://www.spring.io</url> |
||||||
|
</organization> |
||||||
|
<properties> |
||||||
|
<main.basedir>${basedir}/../..</main.basedir> |
||||||
|
</properties> |
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-undertow</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework</groupId> |
||||||
|
<artifactId>spring-webmvc</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.httpcomponents</groupId> |
||||||
|
<artifactId>httpclient</artifactId> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
<build> |
||||||
|
<plugins> |
||||||
|
<plugin> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||||
|
</plugin> |
||||||
|
</plugins> |
||||||
|
</build> |
||||||
|
</project> |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2013 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow; |
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication; |
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||||
|
|
||||||
|
@SpringBootApplication |
||||||
|
public class SampleUndertowSslApplication { |
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception { |
||||||
|
SpringApplication.run(SampleUndertowSslApplication.class, args); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2013 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow.service; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
@Component |
||||||
|
public class HelloWorldService { |
||||||
|
|
||||||
|
@Value("${name:World}") |
||||||
|
private String name; |
||||||
|
|
||||||
|
public String getHelloMessage() { |
||||||
|
return "Hello " + this.name; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2013 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow.web; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Controller; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||||
|
|
||||||
|
import sample.undertow.service.HelloWorldService; |
||||||
|
|
||||||
|
@Controller |
||||||
|
public class SampleController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private HelloWorldService helloWorldService; |
||||||
|
|
||||||
|
@RequestMapping("/") |
||||||
|
@ResponseBody |
||||||
|
public String helloWorld() { |
||||||
|
return this.helloWorldService.getHelloMessage(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
server.port = 8443 |
||||||
|
server.ssl.key-store = classpath:sample.jks |
||||||
|
server.ssl.key-store-password = secret |
||||||
|
server.ssl.key-password = password |
||||||
Binary file not shown.
@ -0,0 +1,72 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow; |
||||||
|
|
||||||
|
import org.apache.http.client.HttpClient; |
||||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||||
|
import org.apache.http.conn.ssl.SSLContextBuilder; |
||||||
|
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; |
||||||
|
import org.apache.http.impl.client.HttpClients; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.boot.test.IntegrationTest; |
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration; |
||||||
|
import org.springframework.boot.test.TestRestTemplate; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
import org.springframework.http.ResponseEntity; |
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
||||||
|
import org.springframework.test.annotation.DirtiesContext; |
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||||
|
import org.springframework.test.context.web.WebAppConfiguration; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
/** |
||||||
|
* Basic integration tests for demo application. |
||||||
|
* |
||||||
|
* @author Dave Syer |
||||||
|
*/ |
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@SpringApplicationConfiguration(classes = SampleUndertowSslApplication.class) |
||||||
|
@WebAppConfiguration |
||||||
|
@IntegrationTest("server.port:0") |
||||||
|
@DirtiesContext |
||||||
|
public class SampleUndertowSslApplicationTests { |
||||||
|
|
||||||
|
@Value("${local.server.port}") |
||||||
|
private int port; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testHome() throws Exception { |
||||||
|
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( |
||||||
|
new SSLContextBuilder().loadTrustMaterial(null, |
||||||
|
new TrustSelfSignedStrategy()).build()); |
||||||
|
|
||||||
|
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) |
||||||
|
.build(); |
||||||
|
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate(); |
||||||
|
((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()) |
||||||
|
.setHttpClient(httpClient); |
||||||
|
ResponseEntity<String> entity = testRestTemplate.getForEntity( |
||||||
|
"https://localhost:" + this.port, String.class); |
||||||
|
assertEquals(HttpStatus.OK, entity.getStatusCode()); |
||||||
|
assertEquals("Hello World", entity.getBody()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
<parent> |
||||||
|
<!-- Your own application should inherit from spring-boot-starter-parent --> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-samples</artifactId> |
||||||
|
<version>1.2.0.BUILD-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<artifactId>spring-boot-sample-undertow</artifactId> |
||||||
|
<name>Spring Boot Undertow Sample</name> |
||||||
|
<description>Spring Boot Undertow Sample</description> |
||||||
|
<url>http://projects.spring.io/spring-boot/</url> |
||||||
|
<organization> |
||||||
|
<name>Pivotal Software, Inc.</name> |
||||||
|
<url>http://www.spring.io</url> |
||||||
|
</organization> |
||||||
|
<properties> |
||||||
|
<main.basedir>${basedir}/../..</main.basedir> |
||||||
|
</properties> |
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-undertow</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework</groupId> |
||||||
|
<artifactId>spring-webmvc</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
<build> |
||||||
|
<plugins> |
||||||
|
<plugin> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||||
|
</plugin> |
||||||
|
</plugins> |
||||||
|
</build> |
||||||
|
</project> |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2013 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow; |
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication; |
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||||
|
|
||||||
|
@SpringBootApplication |
||||||
|
public class SampleUndertowApplication { |
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception { |
||||||
|
SpringApplication.run(SampleUndertowApplication.class, args); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2013 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow.service; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
@Component |
||||||
|
public class HelloWorldService { |
||||||
|
|
||||||
|
@Value("${name:World}") |
||||||
|
private String name; |
||||||
|
|
||||||
|
public String getHelloMessage() { |
||||||
|
return "Hello " + this.name; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2013 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow.web; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Controller; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||||
|
|
||||||
|
import sample.undertow.service.HelloWorldService; |
||||||
|
|
||||||
|
@Controller |
||||||
|
public class SampleController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private HelloWorldService helloWorldService; |
||||||
|
|
||||||
|
@RequestMapping("/") |
||||||
|
@ResponseBody |
||||||
|
public String helloWorld() { |
||||||
|
return this.helloWorldService.getHelloMessage(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,56 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 the original author or authors. |
||||||
|
* |
||||||
|
* 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 sample.undertow; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.boot.test.IntegrationTest; |
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration; |
||||||
|
import org.springframework.boot.test.TestRestTemplate; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
import org.springframework.http.ResponseEntity; |
||||||
|
import org.springframework.test.annotation.DirtiesContext; |
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||||
|
import org.springframework.test.context.web.WebAppConfiguration; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
/** |
||||||
|
* Basic integration tests for demo application. |
||||||
|
* |
||||||
|
* @author Ivan Sopov |
||||||
|
*/ |
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@SpringApplicationConfiguration(classes = SampleUndertowApplication.class) |
||||||
|
@WebAppConfiguration |
||||||
|
@IntegrationTest("server.port:0") |
||||||
|
@DirtiesContext |
||||||
|
public class SampleUndertowApplicationTests { |
||||||
|
|
||||||
|
@Value("${local.server.port}") |
||||||
|
private int port; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testHome() throws Exception { |
||||||
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( |
||||||
|
"http://localhost:" + this.port, String.class); |
||||||
|
assertEquals(HttpStatus.OK, entity.getStatusCode()); |
||||||
|
assertEquals("Hello World", entity.getBody()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
<parent> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starters</artifactId> |
||||||
|
<version>1.2.0.BUILD-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<artifactId>spring-boot-starter-undertow</artifactId> |
||||||
|
<name>Spring Boot Undertow Starter</name> |
||||||
|
<description>Spring Boot Undertow Starter</description> |
||||||
|
<url>http://projects.spring.io/spring-boot/</url> |
||||||
|
<organization> |
||||||
|
<name>Pivotal Software, Inc.</name> |
||||||
|
<url>http://www.spring.io</url> |
||||||
|
</organization> |
||||||
|
<properties> |
||||||
|
<main.basedir>${basedir}/../..</main.basedir> |
||||||
|
</properties> |
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>io.undertow</groupId> |
||||||
|
<artifactId>undertow-servlet</artifactId> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
</project> |
||||||
@ -0,0 +1,77 @@ |
|||||||
|
package org.springframework.boot.context.embedded.undertow; |
||||||
|
|
||||||
|
import io.undertow.Handlers; |
||||||
|
import io.undertow.Undertow; |
||||||
|
import io.undertow.Undertow.Builder; |
||||||
|
import io.undertow.server.HttpHandler; |
||||||
|
import io.undertow.server.handlers.PathHandler; |
||||||
|
import io.undertow.servlet.api.DeploymentManager; |
||||||
|
|
||||||
|
import javax.servlet.ServletException; |
||||||
|
|
||||||
|
import org.springframework.boot.context.embedded.EmbeddedServletContainer; |
||||||
|
import org.springframework.boot.context.embedded.EmbeddedServletContainerException; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Ivan Sopov |
||||||
|
*/ |
||||||
|
public class UndertowEmbeddedServletContainer implements EmbeddedServletContainer { |
||||||
|
|
||||||
|
private final DeploymentManager manager; |
||||||
|
private final Builder builder; |
||||||
|
private final String contextPath; |
||||||
|
private final int port; |
||||||
|
private final boolean autoStart; |
||||||
|
private Undertow undertow; |
||||||
|
private boolean started = false; |
||||||
|
|
||||||
|
public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, |
||||||
|
String contextPath, int port, boolean autoStart) { |
||||||
|
this.builder = builder; |
||||||
|
this.manager = manager; |
||||||
|
this.contextPath = contextPath; |
||||||
|
this.port = port; |
||||||
|
this.autoStart = autoStart; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public synchronized void start() throws EmbeddedServletContainerException { |
||||||
|
if (!this.autoStart) { |
||||||
|
return; |
||||||
|
} |
||||||
|
if (undertow == null) { |
||||||
|
try { |
||||||
|
HttpHandler servletHandler = manager.start(); |
||||||
|
if (StringUtils.isEmpty(contextPath)) { |
||||||
|
builder.setHandler(servletHandler); |
||||||
|
} |
||||||
|
else { |
||||||
|
PathHandler pathHandler = Handlers.path().addPrefixPath(contextPath, |
||||||
|
servletHandler); |
||||||
|
builder.setHandler(pathHandler); |
||||||
|
} |
||||||
|
undertow = builder.build(); |
||||||
|
} |
||||||
|
catch (ServletException ex) { |
||||||
|
throw new EmbeddedServletContainerException( |
||||||
|
"Unable to start embdedded Undertow", ex); |
||||||
|
} |
||||||
|
} |
||||||
|
undertow.start(); |
||||||
|
started = true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public synchronized void stop() throws EmbeddedServletContainerException { |
||||||
|
if (started) { |
||||||
|
started = false; |
||||||
|
undertow.stop(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getPort() { |
||||||
|
return port; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,396 @@ |
|||||||
|
package org.springframework.boot.context.embedded.undertow; |
||||||
|
|
||||||
|
import static io.undertow.servlet.Servlets.defaultContainer; |
||||||
|
import static io.undertow.servlet.Servlets.deployment; |
||||||
|
import static io.undertow.servlet.Servlets.servlet; |
||||||
|
import static org.xnio.Options.SSL_CLIENT_AUTH_MODE; |
||||||
|
import static org.xnio.SslClientAuthMode.NOT_REQUESTED; |
||||||
|
import static org.xnio.SslClientAuthMode.REQUESTED; |
||||||
|
import static org.xnio.SslClientAuthMode.REQUIRED; |
||||||
|
import io.undertow.Undertow; |
||||||
|
import io.undertow.Undertow.Builder; |
||||||
|
import io.undertow.UndertowMessages; |
||||||
|
import io.undertow.server.handlers.resource.ClassPathResourceManager; |
||||||
|
import io.undertow.server.handlers.resource.FileResourceManager; |
||||||
|
import io.undertow.server.handlers.resource.Resource; |
||||||
|
import io.undertow.server.handlers.resource.ResourceChangeListener; |
||||||
|
import io.undertow.server.handlers.resource.ResourceManager; |
||||||
|
import io.undertow.server.handlers.resource.URLResource; |
||||||
|
import io.undertow.servlet.api.DeploymentInfo; |
||||||
|
import io.undertow.servlet.api.DeploymentManager; |
||||||
|
import io.undertow.servlet.api.InstanceFactory; |
||||||
|
import io.undertow.servlet.api.InstanceHandle; |
||||||
|
import io.undertow.servlet.api.ListenerInfo; |
||||||
|
import io.undertow.servlet.api.MimeMapping; |
||||||
|
import io.undertow.servlet.api.ServletStackTraces; |
||||||
|
import io.undertow.servlet.handlers.DefaultServlet; |
||||||
|
import io.undertow.servlet.util.ImmediateInstanceHandle; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.net.URL; |
||||||
|
import java.security.KeyManagementException; |
||||||
|
import java.security.KeyStore; |
||||||
|
import java.security.NoSuchAlgorithmException; |
||||||
|
|
||||||
|
import javax.net.ssl.KeyManager; |
||||||
|
import javax.net.ssl.KeyManagerFactory; |
||||||
|
import javax.net.ssl.SSLContext; |
||||||
|
import javax.net.ssl.TrustManager; |
||||||
|
import javax.net.ssl.TrustManagerFactory; |
||||||
|
import javax.servlet.ServletContextEvent; |
||||||
|
import javax.servlet.ServletContextListener; |
||||||
|
import javax.servlet.ServletException; |
||||||
|
|
||||||
|
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory; |
||||||
|
import org.springframework.boot.context.embedded.EmbeddedServletContainer; |
||||||
|
import org.springframework.boot.context.embedded.ErrorPage; |
||||||
|
import org.springframework.boot.context.embedded.MimeMappings.Mapping; |
||||||
|
import org.springframework.boot.context.embedded.ServletContextInitializer; |
||||||
|
import org.springframework.boot.context.embedded.Ssl; |
||||||
|
import org.springframework.boot.context.embedded.Ssl.ClientAuth; |
||||||
|
import org.springframework.context.ResourceLoaderAware; |
||||||
|
import org.springframework.core.io.ResourceLoader; |
||||||
|
import org.springframework.util.ResourceUtils; |
||||||
|
import org.springframework.util.SocketUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Ivan Sopov |
||||||
|
*/ |
||||||
|
public class UndertowEmbeddedServletContainerFactory extends |
||||||
|
AbstractEmbeddedServletContainerFactory implements ResourceLoaderAware { |
||||||
|
|
||||||
|
private ResourceLoader resourceLoader; |
||||||
|
|
||||||
|
private Integer bufferSize; |
||||||
|
private Integer buffersPerRegion; |
||||||
|
private Integer ioThreads; |
||||||
|
private Integer workerThreads; |
||||||
|
private Boolean directBuffers; |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new |
||||||
|
* {@link org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory} |
||||||
|
* instance. |
||||||
|
*/ |
||||||
|
public UndertowEmbeddedServletContainerFactory() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new |
||||||
|
* {@link org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory} |
||||||
|
* that listens for requests using the specified port. |
||||||
|
* |
||||||
|
* @param port the port to listen on |
||||||
|
*/ |
||||||
|
public UndertowEmbeddedServletContainerFactory(int port) { |
||||||
|
super(port); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new |
||||||
|
* {@link org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory} |
||||||
|
* with the specified context path and port. |
||||||
|
* |
||||||
|
* @param contextPath root the context path |
||||||
|
* @param port the port to listen on |
||||||
|
*/ |
||||||
|
public UndertowEmbeddedServletContainerFactory(String contextPath, int port) { |
||||||
|
super(contextPath, port); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public EmbeddedServletContainer getEmbeddedServletContainer( |
||||||
|
ServletContextInitializer... initializers) { |
||||||
|
DeploymentInfo servletBuilder = deployment(); |
||||||
|
|
||||||
|
servletBuilder.addListener(new ListenerInfo( |
||||||
|
UndertowSpringServletContextListener.class, |
||||||
|
new UndertowSpringServletContextListenerFactory( |
||||||
|
new UndertowSpringServletContextListener( |
||||||
|
mergeInitializers(initializers))))); |
||||||
|
|
||||||
|
if (resourceLoader != null) { |
||||||
|
servletBuilder.setClassLoader(resourceLoader.getClassLoader()); |
||||||
|
} |
||||||
|
else { |
||||||
|
servletBuilder.setClassLoader(getClass().getClassLoader()); |
||||||
|
} |
||||||
|
servletBuilder.setContextPath(getContextPath()); |
||||||
|
servletBuilder.setDeploymentName("spring-boot"); |
||||||
|
if (isRegisterDefaultServlet()) { |
||||||
|
servletBuilder.addServlet(servlet("default", DefaultServlet.class)); |
||||||
|
} |
||||||
|
if (isRegisterJspServlet()) { |
||||||
|
logger.error("JSPs are not supported with Undertow"); |
||||||
|
} |
||||||
|
for (ErrorPage springErrorPage : getErrorPages()) { |
||||||
|
if (springErrorPage.getStatus() != null) { |
||||||
|
io.undertow.servlet.api.ErrorPage undertowErrorpage = new io.undertow.servlet.api.ErrorPage( |
||||||
|
springErrorPage.getPath(), springErrorPage.getStatusCode()); |
||||||
|
servletBuilder.addErrorPage(undertowErrorpage); |
||||||
|
} |
||||||
|
else if (springErrorPage.getException() != null) { |
||||||
|
io.undertow.servlet.api.ErrorPage undertowErrorpage = new io.undertow.servlet.api.ErrorPage( |
||||||
|
springErrorPage.getPath(), springErrorPage.getException()); |
||||||
|
servletBuilder.addErrorPage(undertowErrorpage); |
||||||
|
} |
||||||
|
else { |
||||||
|
io.undertow.servlet.api.ErrorPage undertowErrorpage = new io.undertow.servlet.api.ErrorPage( |
||||||
|
springErrorPage.getPath()); |
||||||
|
servletBuilder.addErrorPage(undertowErrorpage); |
||||||
|
} |
||||||
|
} |
||||||
|
servletBuilder.setServletStackTraces(ServletStackTraces.NONE); |
||||||
|
|
||||||
|
File root = getValidDocumentRoot(); |
||||||
|
if (root != null && root.isDirectory()) { |
||||||
|
servletBuilder.setResourceManager(new FileResourceManager(root, 0)); |
||||||
|
} |
||||||
|
else if (root != null && root.isFile()) { |
||||||
|
servletBuilder.setResourceManager(new JarResourcemanager(root)); |
||||||
|
} |
||||||
|
else if (resourceLoader != null) { |
||||||
|
servletBuilder.setResourceManager(new ClassPathResourceManager(resourceLoader |
||||||
|
.getClassLoader(), "")); |
||||||
|
} |
||||||
|
else { |
||||||
|
servletBuilder.setResourceManager(new ClassPathResourceManager(getClass() |
||||||
|
.getClassLoader(), "")); |
||||||
|
} |
||||||
|
for (Mapping mimeMapping : getMimeMappings()) { |
||||||
|
servletBuilder.addMimeMapping(new MimeMapping(mimeMapping.getExtension(), |
||||||
|
mimeMapping.getMimeType())); |
||||||
|
} |
||||||
|
|
||||||
|
DeploymentManager manager = defaultContainer().addDeployment(servletBuilder); |
||||||
|
|
||||||
|
manager.deploy(); |
||||||
|
|
||||||
|
manager.getDeployment().getSessionManager() |
||||||
|
.setDefaultSessionTimeout(getSessionTimeout()); |
||||||
|
|
||||||
|
Builder builder = Undertow.builder(); |
||||||
|
if (bufferSize != null) { |
||||||
|
builder.setBufferSize(bufferSize); |
||||||
|
} |
||||||
|
if (buffersPerRegion != null) { |
||||||
|
builder.setBuffersPerRegion(buffersPerRegion); |
||||||
|
} |
||||||
|
if (ioThreads != null) { |
||||||
|
builder.setIoThreads(ioThreads); |
||||||
|
} |
||||||
|
if (workerThreads != null) { |
||||||
|
builder.setWorkerThreads(workerThreads); |
||||||
|
} |
||||||
|
if (directBuffers != null) { |
||||||
|
builder.setDirectBuffers(directBuffers); |
||||||
|
} |
||||||
|
|
||||||
|
int realPort = getPort(); |
||||||
|
if (realPort == 0) { |
||||||
|
realPort = SocketUtils.findAvailableTcpPort(40000); |
||||||
|
} |
||||||
|
if (getSsl() == null) { |
||||||
|
builder.addHttpListener(realPort, "0.0.0.0"); |
||||||
|
} |
||||||
|
else { |
||||||
|
try { |
||||||
|
Ssl ssl = getSsl(); |
||||||
|
SSLContext sslContext = SSLContext.getInstance(ssl.getProtocol()); |
||||||
|
sslContext.init(getKeyManagers(), getTrustManagers(), null); |
||||||
|
builder.addHttpsListener(realPort, "0.0.0.0", sslContext); |
||||||
|
if (ssl.getClientAuth() == ClientAuth.NEED) { |
||||||
|
builder.setSocketOption(SSL_CLIENT_AUTH_MODE, REQUIRED); |
||||||
|
} |
||||||
|
else if (ssl.getClientAuth() == ClientAuth.WANT) { |
||||||
|
builder.setSocketOption(SSL_CLIENT_AUTH_MODE, REQUESTED); |
||||||
|
} |
||||||
|
else { |
||||||
|
builder.setSocketOption(SSL_CLIENT_AUTH_MODE, NOT_REQUESTED); |
||||||
|
} |
||||||
|
} |
||||||
|
catch (NoSuchAlgorithmException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
catch (KeyManagementException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
return new UndertowEmbeddedServletContainer(builder, manager, getContextPath(), |
||||||
|
realPort, realPort > 0); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private KeyManager[] getKeyManagers() { |
||||||
|
try { |
||||||
|
Ssl ssl = getSsl(); |
||||||
|
|
||||||
|
String keyStoreType = ssl.getKeyStoreType(); |
||||||
|
if (keyStoreType == null) { |
||||||
|
keyStoreType = "JKS"; |
||||||
|
} |
||||||
|
KeyStore keyStore = KeyStore.getInstance(keyStoreType); |
||||||
|
URL url = ResourceUtils.getURL(ssl.getKeyStore()); |
||||||
|
keyStore.load(url.openStream(), ssl.getKeyStorePassword().toCharArray()); |
||||||
|
|
||||||
|
// Get key manager to provide client credentials.
|
||||||
|
KeyManagerFactory keyManagerFactory = KeyManagerFactory |
||||||
|
.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
||||||
|
char[] keyPassword = ssl.getKeyPassword() != null ? ssl.getKeyPassword() |
||||||
|
.toCharArray() : ssl.getKeyStorePassword().toCharArray(); |
||||||
|
keyManagerFactory.init(keyStore, keyPassword); |
||||||
|
return keyManagerFactory.getKeyManagers(); |
||||||
|
} |
||||||
|
catch (Exception e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private TrustManager[] getTrustManagers() { |
||||||
|
try { |
||||||
|
Ssl ssl = getSsl(); |
||||||
|
|
||||||
|
String trustStoreType = ssl.getTrustStoreType(); |
||||||
|
if (trustStoreType == null) { |
||||||
|
trustStoreType = "JKS"; |
||||||
|
} |
||||||
|
String trustStore = ssl.getTrustStore(); |
||||||
|
if (trustStore == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
KeyStore trustedKeyStore = KeyStore.getInstance(trustStoreType); |
||||||
|
URL url = ResourceUtils.getURL(trustStore); |
||||||
|
trustedKeyStore.load(url.openStream(), ssl.getTrustStorePassword() |
||||||
|
.toCharArray()); |
||||||
|
|
||||||
|
TrustManagerFactory trustManagerFactory = TrustManagerFactory |
||||||
|
.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
||||||
|
trustManagerFactory.init(trustedKeyStore); |
||||||
|
return trustManagerFactory.getTrustManagers(); |
||||||
|
} |
||||||
|
catch (Exception e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setResourceLoader(ResourceLoader resourceLoader) { |
||||||
|
this.resourceLoader = resourceLoader; |
||||||
|
} |
||||||
|
|
||||||
|
public void setBufferSize(Integer bufferSize) { |
||||||
|
this.bufferSize = bufferSize; |
||||||
|
} |
||||||
|
|
||||||
|
public void setBuffersPerRegion(Integer buffersPerRegion) { |
||||||
|
this.buffersPerRegion = buffersPerRegion; |
||||||
|
} |
||||||
|
|
||||||
|
public void setIoThreads(Integer ioThreads) { |
||||||
|
this.ioThreads = ioThreads; |
||||||
|
} |
||||||
|
|
||||||
|
public void setWorkerThreads(Integer workerThreads) { |
||||||
|
this.workerThreads = workerThreads; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDirectBuffers(Boolean directBuffers) { |
||||||
|
this.directBuffers = directBuffers; |
||||||
|
} |
||||||
|
|
||||||
|
private static class JarResourcemanager implements ResourceManager { |
||||||
|
private final String jarPath; |
||||||
|
|
||||||
|
public JarResourcemanager(File jarFile) { |
||||||
|
this(jarFile.getAbsolutePath()); |
||||||
|
} |
||||||
|
|
||||||
|
public JarResourcemanager(String jarPath) { |
||||||
|
this.jarPath = jarPath; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
// no code
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Resource getResource(String path) throws IOException { |
||||||
|
URL url = new URL("jar:file:" + jarPath + "!" + path); |
||||||
|
URLResource resource = new URLResource(url, url.openConnection(), path); |
||||||
|
if (resource.getContentLength() < 0) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
return resource; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isResourceChangeListenerSupported() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void registerResourceChangeListener(ResourceChangeListener listener) { |
||||||
|
throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void removeResourceChangeListener(ResourceChangeListener listener) { |
||||||
|
throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private static class UndertowSpringServletContextListenerFactory implements |
||||||
|
InstanceFactory<UndertowSpringServletContextListener> { |
||||||
|
|
||||||
|
private final UndertowSpringServletContextListener listener; |
||||||
|
|
||||||
|
public UndertowSpringServletContextListenerFactory( |
||||||
|
UndertowSpringServletContextListener listener) { |
||||||
|
this.listener = listener; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public InstanceHandle<UndertowSpringServletContextListener> createInstance() |
||||||
|
throws InstantiationException { |
||||||
|
return new ImmediateInstanceHandle<UndertowSpringServletContextListener>( |
||||||
|
listener); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private static class UndertowSpringServletContextListener implements |
||||||
|
ServletContextListener { |
||||||
|
private final ServletContextInitializer[] initializers; |
||||||
|
|
||||||
|
public UndertowSpringServletContextListener( |
||||||
|
ServletContextInitializer... initializers) { |
||||||
|
this.initializers = initializers; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void contextInitialized(ServletContextEvent sce) { |
||||||
|
try { |
||||||
|
for (ServletContextInitializer initializer : initializers) { |
||||||
|
initializer.onStartup(sce.getServletContext()); |
||||||
|
} |
||||||
|
} |
||||||
|
catch (ServletException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void contextDestroyed(ServletContextEvent sce) { |
||||||
|
// no code
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,34 @@ |
|||||||
|
package org.springframework.boot.context.embedded.undertow; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.boot.context.embedded.*; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo; |
||||||
|
import static org.junit.Assert.assertThat; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory} and |
||||||
|
* {@link org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer}. |
||||||
|
* |
||||||
|
* @author Ivan Sopov |
||||||
|
*/ |
||||||
|
public class UndertowEmbeddedServletContainerFactoryTests extends AbstractEmbeddedServletContainerFactoryTests |
||||||
|
{ |
||||||
|
|
||||||
|
@Override |
||||||
|
protected UndertowEmbeddedServletContainerFactory getFactory() |
||||||
|
{ |
||||||
|
return new UndertowEmbeddedServletContainerFactory(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void errorPage404() throws Exception { |
||||||
|
AbstractEmbeddedServletContainerFactory factory = getFactory(); |
||||||
|
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/hello")); |
||||||
|
this.container = factory.getEmbeddedServletContainer(new ServletRegistrationBean(new ExampleServlet(), "/hello")); |
||||||
|
this.container.start(); |
||||||
|
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World")); |
||||||
|
assertThat(getResponse("http://localhost:8080/not-found"), equalTo("Hello World")); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue