Skip to content

Commit

Permalink
Refactoring of the webserver package
Browse files Browse the repository at this point in the history
  • Loading branch information
perwendel committed Jan 28, 2016
1 parent 418f1f8 commit a1909a8
Show file tree
Hide file tree
Showing 40 changed files with 1,122 additions and 516 deletions.
1 change: 1 addition & 0 deletions NOTICE
Expand Up @@ -100,6 +100,7 @@ Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) and the
* AbstractResourceHandler.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd. * AbstractResourceHandler.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd.
* ClassPathResourceHandler.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd. * ClassPathResourceHandler.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd.
* ExternalResourceHandler.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd. * ExternalResourceHandler.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd.
* UriPath.java. Copyright 1995-2013 Mort Bay Consulting Pty. Ltd.


ARJEN POUTSMA ARJEN POUTSMA
This product includes the following software developed by Arjen Poutsma which is licensed under the terms of the This product includes the following software developed by Arjen Poutsma which is licensed under the terms of the
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/spark/Access.java
Expand Up @@ -18,13 +18,13 @@


import spark.routematch.RouteMatch; import spark.routematch.RouteMatch;


/**
* Provides access to package protected methods. JUST FOR INTERNAL USE. NOT PART OF PUBLIC SPARK API.
*/
public final class Access { public final class Access {


private Access() { private Access() {
} // hidden

public static String getBody(Response response) {
return response.body();
} }


public static void changeMatch(Request request, RouteMatch match) { public static void changeMatch(Request request, RouteMatch match) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/spark/HaltException.java
Expand Up @@ -48,17 +48,32 @@ public class HaltException extends RuntimeException {


/** /**
* @return the statusCode * @return the statusCode
* @deprecated replaced by {@link #statusCode()}
*/ */
public int getStatusCode() { public int getStatusCode() {
return statusCode; return statusCode;
} }


/**
* @return the statusCode
*/
public int statusCode() {
return statusCode;
}


/** /**
* @return the body * @return the body
* @deprecated replaced by {@link #body()}
*/ */
public String getBody() { public String getBody() {
return body; return body;
} }


/**
* @return the body
*/
public String body() {
return body;
}

} }
119 changes: 0 additions & 119 deletions src/main/java/spark/JettyLogger.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/spark/Spark.java
Expand Up @@ -849,7 +849,7 @@ public static void halt(int status, String body) {
* @deprecated replaced by {@link #ipAddress(String)} * @deprecated replaced by {@link #ipAddress(String)}
*/ */
public static void setIpAddress(String ipAddress) { public static void setIpAddress(String ipAddress) {
getInstance().setIpAddress(ipAddress); getInstance().ipAddress(ipAddress);
} }


/** /**
Expand All @@ -872,7 +872,7 @@ public static void ipAddress(String ipAddress) {
* @deprecated replaced by {@link #port(int)} * @deprecated replaced by {@link #port(int)}
*/ */
public static void setPort(int port) { public static void setPort(int port) {
getInstance().setPort(port); getInstance().port(port);
} }


/** /**
Expand Down Expand Up @@ -906,7 +906,7 @@ public static void setSecure(String keystoreFile,
String keystorePassword, String keystorePassword,
String truststoreFile, String truststoreFile,
String truststorePassword) { String truststorePassword) {
getInstance().setSecure(keystoreFile, keystorePassword, truststoreFile, truststorePassword); getInstance().secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
} }


/** /**
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/spark/SparkInstance.java
Expand Up @@ -29,7 +29,8 @@
import spark.route.SimpleRouteMatcher; import spark.route.SimpleRouteMatcher;
import spark.ssl.SslStores; import spark.ssl.SslStores;
import spark.staticfiles.StaticFiles; import spark.staticfiles.StaticFiles;
import spark.webserver.SparkServerFactory; import spark.webserver.EmbeddedServer;
import spark.webserver.EmbeddedServers;


import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;


Expand Down Expand Up @@ -59,14 +60,16 @@ final class SparkInstance extends Routable {
protected int threadIdleTimeoutMillis = -1; protected int threadIdleTimeoutMillis = -1;
protected Optional<Integer> webSocketIdleTimeoutMillis = Optional.empty(); protected Optional<Integer> webSocketIdleTimeoutMillis = Optional.empty();


protected SparkServer server; protected EmbeddedServer server;
protected SimpleRouteMatcher routeMatcher; protected SimpleRouteMatcher routeMatcher;


private boolean servletStaticLocationSet; private boolean servletStaticLocationSet;
private boolean servletExternalStaticLocationSet; private boolean servletExternalStaticLocationSet;


private CountDownLatch latch = new CountDownLatch(1); private CountDownLatch latch = new CountDownLatch(1);


private Object embeddedServerIdentifier = EmbeddedServers.defaultIdentifier();

/** /**
* Set the IP address that Spark should listen on. If not called the default * Set the IP address that Spark should listen on. If not called the default
* address is '0.0.0.0'. This has to be called before any route mapping is * address is '0.0.0.0'. This has to be called before any route mapping is
Expand Down Expand Up @@ -255,15 +258,19 @@ public synchronized void externalStaticFileLocation(String externalFolder) {
public synchronized void webSocket(String path, Class<?> handler) { public synchronized void webSocket(String path, Class<?> handler) {
requireNonNull(path, "WebSocket path cannot be null"); requireNonNull(path, "WebSocket path cannot be null");
requireNonNull(handler, "WebSocket handler class cannot be null"); requireNonNull(handler, "WebSocket handler class cannot be null");

if (initialized) { if (initialized) {
throwBeforeRouteMappingException(); throwBeforeRouteMappingException();
} }

if (ServletFlag.isRunningFromServlet()) { if (ServletFlag.isRunningFromServlet()) {
throw new IllegalStateException("WebSockets are only supported in the embedded server"); throw new IllegalStateException("WebSockets are only supported in the embedded server");
} }

if (webSocketHandlers == null) { if (webSocketHandlers == null) {
webSocketHandlers = new HashMap<>(); webSocketHandlers = new HashMap<>();
} }

webSocketHandlers.put(path, handler); webSocketHandlers.put(path, handler);
} }


Expand Down Expand Up @@ -310,7 +317,7 @@ private boolean hasMultipleHandlers() {
public synchronized void stop() { public synchronized void stop() {
if (server != null) { if (server != null) {
routeMatcher.clearRoutes(); routeMatcher.clearRoutes();
server.stop(); server.extinguish();
latch = new CountDownLatch(1); latch = new CountDownLatch(1);
} }
StaticFiles.clear(); StaticFiles.clear();
Expand All @@ -332,19 +339,21 @@ public void addFilter(String httpMethod, FilterImpl filter) {
public synchronized void init() { public synchronized void init() {
if (!initialized) { if (!initialized) {
routeMatcher = RouteMatcherFactory.get(); routeMatcher = RouteMatcherFactory.get();

if (!ServletFlag.isRunningFromServlet()) { if (!ServletFlag.isRunningFromServlet()) {
new Thread(() -> { new Thread(() -> {
server = SparkServerFactory.create(hasMultipleHandlers());
server = EmbeddedServers.create(embeddedServerIdentifier, hasMultipleHandlers());
server.configureWebSockets(webSocketHandlers, webSocketIdleTimeoutMillis);

server.ignite( server.ignite(
ipAddress, ipAddress,
port, port,
sslStores, sslStores,
latch, latch,
maxThreads, maxThreads,
minThreads, minThreads,
threadIdleTimeoutMillis, threadIdleTimeoutMillis);
webSocketHandlers,
webSocketIdleTimeoutMillis);
}).start(); }).start();
} }
initialized = true; initialized = true;
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/spark/SparkServer.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/spark/resource/ClassPathResourceHandler.java
Expand Up @@ -52,6 +52,7 @@ public ClassPathResourceHandler(String baseResource) {
*/ */
public ClassPathResourceHandler(String baseResource, String welcomeFile) { public ClassPathResourceHandler(String baseResource, String welcomeFile) {
Assert.notNull(baseResource); Assert.notNull(baseResource);

this.baseResource = baseResource; this.baseResource = baseResource;
this.welcomeFile = welcomeFile; this.welcomeFile = welcomeFile;
} }
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/spark/resource/ExternalResourceHandler.java
Expand Up @@ -19,7 +19,6 @@


import java.net.MalformedURLException; import java.net.MalformedURLException;


import org.eclipse.jetty.util.URIUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand Down Expand Up @@ -63,7 +62,7 @@ protected AbstractFileResolvingResource getResource(String path) throws Malforme
} }


try { try {
path = URIUtil.canonicalPath(path); path = UriPath.canonical(path);

This comment has been minimized.

Copy link
@cesarte789

cesarte789 Mar 18, 2016

Why did you decide to copy a fragment of the source code from Jetty to create a new class (UriPath) instead of continue using the URIUtil class from Jetty?



final String addedPath = addPaths(baseResource, path); final String addedPath = addPaths(baseResource, path);


Expand Down

1 comment on commit a1909a8

@jehrhardt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EmbbededServers class looks great. It will probably allow me to customize the embedded Jetty. When will this be available?

Please sign in to comment.