Add https publishing
Change-Id: I7611b7379c0f42d02342adc1eec56c0d79caa432
This commit is contained in:
parent
96a294828b
commit
0323749ff4
7 changed files with 129 additions and 7 deletions
|
@ -2,6 +2,7 @@ package pl.touk.mockserver.server
|
|||
|
||||
import com.sun.net.httpserver.HttpExchange
|
||||
import groovy.util.logging.Slf4j
|
||||
import pl.touk.mockserver.api.common.Https
|
||||
import pl.touk.mockserver.api.common.ImportAlias
|
||||
import pl.touk.mockserver.api.common.Method
|
||||
import pl.touk.mockserver.api.request.AddMock
|
||||
|
@ -41,7 +42,7 @@ class HttpMockServer {
|
|||
|
||||
HttpMockServer(int port = 9999, ConfigObject initialConfiguration = new ConfigObject(), int threads = 10) {
|
||||
executor = Executors.newFixedThreadPool(threads)
|
||||
httpServerWrapper = new HttpServerWrapper(port, executor)
|
||||
httpServerWrapper = new HttpServerWrapper(port, executor, null)
|
||||
|
||||
initialConfiguration.values()?.each { ConfigObject co ->
|
||||
addMock(co)
|
||||
|
@ -108,7 +109,7 @@ class HttpMockServer {
|
|||
throw new RuntimeException('mock already registered')
|
||||
}
|
||||
Mock mock = mockFromRequest(request)
|
||||
HttpServerWrapper child = getOrCreateChildServer(mock.port)
|
||||
HttpServerWrapper child = getOrCreateChildServer(mock.port, mock.https)
|
||||
child.addMock(mock)
|
||||
saveConfiguration(request)
|
||||
mockNames << name
|
||||
|
@ -121,7 +122,7 @@ class HttpMockServer {
|
|||
throw new RuntimeException('mock already registered')
|
||||
}
|
||||
Mock mock = mockFromConfig(co)
|
||||
HttpServerWrapper child = getOrCreateChildServer(mock.port)
|
||||
HttpServerWrapper child = getOrCreateChildServer(mock.port, null)
|
||||
child.addMock(mock)
|
||||
configuration.put(name, co)
|
||||
mockNames << name
|
||||
|
@ -156,6 +157,7 @@ class HttpMockServer {
|
|||
mock.responseHeaders = request.responseHeaders
|
||||
mock.schema = request.schema
|
||||
mock.preserveHistory = request.preserveHistory != false
|
||||
mock.https = request.https
|
||||
return mock
|
||||
}
|
||||
|
||||
|
@ -173,10 +175,10 @@ class HttpMockServer {
|
|||
return mock
|
||||
}
|
||||
|
||||
private HttpServerWrapper getOrCreateChildServer(int mockPort) {
|
||||
private HttpServerWrapper getOrCreateChildServer(int mockPort, Https https) {
|
||||
HttpServerWrapper child = childServers[mockPort]
|
||||
if (!child) {
|
||||
child = new HttpServerWrapper(mockPort, executor)
|
||||
child = new HttpServerWrapper(mockPort, executor, https)
|
||||
childServers.put(mockPort, child)
|
||||
}
|
||||
return child
|
||||
|
|
|
@ -2,9 +2,17 @@ package pl.touk.mockserver.server
|
|||
|
||||
import com.sun.net.httpserver.HttpHandler
|
||||
import com.sun.net.httpserver.HttpServer
|
||||
import com.sun.net.httpserver.HttpsConfigurator
|
||||
import com.sun.net.httpserver.HttpsServer
|
||||
import groovy.transform.PackageScope
|
||||
import groovy.util.logging.Slf4j
|
||||
import pl.touk.mockserver.api.common.Https
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory
|
||||
import javax.net.ssl.SSLContext
|
||||
import javax.net.ssl.TrustManager
|
||||
import java.security.KeyStore
|
||||
import java.security.SecureRandom
|
||||
import java.util.concurrent.Executor
|
||||
|
||||
@Slf4j
|
||||
|
@ -15,16 +23,37 @@ class HttpServerWrapper {
|
|||
|
||||
private List<ContextExecutor> executors = []
|
||||
|
||||
HttpServerWrapper(int port, Executor executor) {
|
||||
HttpServerWrapper(int port, Executor executor, Https https) {
|
||||
this.port = port
|
||||
InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port)
|
||||
httpServer = HttpServer.create(addr, 0)
|
||||
httpServer = buildServer(addr, https)
|
||||
httpServer.executor = executor
|
||||
log.info("Http server starting on port $port...")
|
||||
httpServer.start()
|
||||
log.info('Http server is started')
|
||||
}
|
||||
|
||||
private HttpServer buildServer(InetSocketAddress addr, Https https) {
|
||||
if (https) {
|
||||
HttpsServer httpsServer = HttpsServer.create(addr, 0)
|
||||
httpsServer.httpsConfigurator = new HttpsConfigurator(buildSslContext(https))
|
||||
return httpsServer
|
||||
} else {
|
||||
return HttpServer.create(addr, 0)
|
||||
}
|
||||
}
|
||||
|
||||
private SSLContext buildSslContext(Https https) {
|
||||
KeyStore keyStore = KeyStore.getInstance(KeyStore.defaultType)
|
||||
keyStore.load(new FileInputStream(https.keystorePath), https.keystorePassword.toCharArray())
|
||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.defaultAlgorithm)
|
||||
kmf.init(keyStore, https.keyPassword.toCharArray())
|
||||
|
||||
SSLContext ssl = SSLContext.getInstance('TLSv1')
|
||||
ssl.init(kmf.keyManagers, [] as TrustManager[], new SecureRandom())
|
||||
return ssl
|
||||
}
|
||||
|
||||
void createContext(String context, HttpHandler handler) {
|
||||
httpServer.createContext(context, handler)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import groovy.transform.PackageScope
|
|||
import groovy.util.logging.Slf4j
|
||||
import org.codehaus.groovy.control.CompilerConfiguration
|
||||
import org.codehaus.groovy.control.customizers.ImportCustomizer
|
||||
import pl.touk.mockserver.api.common.Https
|
||||
import pl.touk.mockserver.api.common.Method
|
||||
|
||||
import javax.xml.XMLConstants
|
||||
|
@ -35,6 +36,7 @@ class Mock implements Comparable<Mock> {
|
|||
private Validator validator
|
||||
Map<String, String> imports = [:]
|
||||
boolean preserveHistory = true
|
||||
Https https
|
||||
|
||||
Mock(String name, String path, int port) {
|
||||
if (!(name)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue