Permalink
Browse files

Refactoring of the webserver package

  • Loading branch information...
1 parent 418f1f8 commit a1909a83a1cf6b921d2ea07405a8ecb5ff7c8e59 @perwendel committed Jan 28, 2016
Showing with 1,122 additions and 516 deletions.
  1. +1 −0 NOTICE
  2. +4 −4 src/main/java/spark/Access.java
  3. +15 −0 src/main/java/spark/HaltException.java
  4. +0 −119 src/main/java/spark/JettyLogger.java
  5. +3 −3 src/main/java/spark/Spark.java
  6. +16 −7 src/main/java/spark/SparkInstance.java
  7. +0 −39 src/main/java/spark/SparkServer.java
  8. +1 −0 src/main/java/spark/resource/ClassPathResourceHandler.java
  9. +1 −2 src/main/java/spark/resource/ExternalResourceHandler.java
  10. +167 −0 src/main/java/spark/resource/UriPath.java
  11. +1 −1 src/main/java/spark/servlet/SparkFilter.java
  12. +67 −0 src/main/java/spark/webserver/EmbeddedServer.java
  13. +28 −0 src/main/java/spark/webserver/EmbeddedServerFactory.java
  14. +64 −0 src/main/java/spark/webserver/EmbeddedServers.java
  15. +0 −281 src/main/java/spark/webserver/MatcherFilter.java
  16. +11 −11 src/main/java/spark/webserver/{SparkServerFactory.java → jetty/EmbeddedJettyFactory.java}
  17. +19 −13 src/main/java/spark/webserver/{JettySparkServer.java → jetty/EmbeddedJettyServer.java}
  18. +3 −5 src/main/java/spark/webserver/{ → jetty}/JettyHandler.java
  19. +6 −4 src/main/java/spark/webserver/jetty/JettyServerFactory.java
  20. +1 −1 src/main/java/spark/webserver/jetty/SocketConnectorFactory.java
  21. +1 −1 src/main/java/spark/webserver/{ → jetty}/websocket/WebSocketCreatorFactory.java
  22. +1 −1 src/main/java/spark/webserver/{ → jetty}/websocket/WebSocketServletContextHandlerFactory.java
  23. +68 −0 src/main/java/spark/webserver/matching/AfterFilters.java
  24. +61 −0 src/main/java/spark/webserver/matching/BeforeFilters.java
  25. +80 −0 src/main/java/spark/webserver/matching/Body.java
  26. +55 −0 src/main/java/spark/webserver/matching/GeneralError.java
  27. +41 −0 src/main/java/spark/webserver/matching/Halt.java
  28. +173 −0 src/main/java/spark/webserver/matching/MatcherFilter.java
  29. +9 −1 src/main/java/spark/webserver/{ → matching}/RequestWrapper.java
  30. +9 −1 src/main/java/spark/webserver/{ → matching}/ResponseWrapper.java
  31. +132 −0 src/main/java/spark/webserver/matching/RouteContext.java
  32. +72 −0 src/main/java/spark/webserver/matching/Routes.java
  33. +1 −1 src/main/java/spark/webserver/serialization/Serializer.java
  34. +1 −1 src/main/java/spark/webserver/serialization/SerializerChain.java
  35. +2 −2 src/test/java/spark/GenericIntegrationTest.java
  36. +2 −3 src/test/java/spark/webserver/jetty/JettyServerFactoryTest.java
  37. +3 −10 src/test/java/spark/webserver/{ → jetty}/websocket/WebSocketCreatorFactoryTest.java
  38. +1 −3 src/test/java/spark/webserver/{ → jetty}/websocket/WebSocketServletContextHandlerFactoryTest.java
  39. +1 −1 src/test/java/spark/{ → webserver/jetty}/websocket/WebSocketTestClient.java
  40. +1 −1 src/test/java/spark/{ → webserver/jetty}/websocket/WebSocketTestHandler.java
View
@@ -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.
* ClassPathResourceHandler.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
This product includes the following software developed by Arjen Poutsma which is licensed under the terms of the
@@ -18,13 +18,13 @@
import spark.routematch.RouteMatch;
+/**
+ * Provides access to package protected methods. JUST FOR INTERNAL USE. NOT PART OF PUBLIC SPARK API.
+ */
public final class Access {
private Access() {
- }
-
- public static String getBody(Response response) {
- return response.body();
+ // hidden
}
public static void changeMatch(Request request, RouteMatch match) {
@@ -48,17 +48,32 @@
/**
* @return the statusCode
+ * @deprecated replaced by {@link #statusCode()}
*/
public int getStatusCode() {
return statusCode;
}
+ /**
+ * @return the statusCode
+ */
+ public int statusCode() {
+ return statusCode;
+ }
/**
* @return the body
+ * @deprecated replaced by {@link #body()}
*/
public String getBody() {
return body;
}
+ /**
+ * @return the body
+ */
+ public String body() {
+ return body;
+ }
+
}
@@ -1,119 +0,0 @@
-/*
- * Copyright 2011- Per Wendel
- *
- * 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 spark;
-
-import org.eclipse.jetty.util.log.Logger;
-
-/**
- * Jetty Logger
- *
- * @author Per Wendel
- */
-public class JettyLogger implements Logger {
-
- private org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
-
- public void debug(String msg, Throwable th) {
- logger.debug(msg, th);
- }
-
- public Logger getLogger(String arg) {
- return this;
- }
-
- @Override
- public boolean isDebugEnabled() {
- return logger.isDebugEnabled();
- }
-
- @Override
- public void warn(String msg, Throwable th) {
- logger.warn(msg, th);
- }
-
- @Override
- public void debug(Throwable thrown) {
- logger.debug("", thrown);
-
- }
-
- @Override
- public void debug(String msg, Object... args) {
- StringBuffer log = new StringBuffer(msg);
- for (Object arg : args) {
- log.append(", ");
- log.append(arg);
- }
- logger.debug(log.toString());
- }
-
- @Override
- public String getName() {
- return "Spark Jetty Logger";
- }
-
- @Override
- public void ignore(Throwable ignored) {
- //
- }
-
- @Override
- public void info(Throwable thrown) {
- logger.info("", thrown);
- }
-
- @Override
- public void info(String msg, Object... args) {
- StringBuffer log = new StringBuffer(msg);
- for (Object arg : args) {
- log.append(", ");
- log.append(arg);
- }
- logger.info(log.toString());
- }
-
- @Override
- public void info(String msg, Throwable thrown) {
- logger.info(msg, thrown);
- }
-
- @Override
- public void setDebugEnabled(boolean enabled) {
- //
- }
-
- @Override
- public void warn(Throwable thrown) {
- logger.warn("", thrown);
- }
-
- @Override
- public void warn(String msg, Object... args) {
- StringBuffer log = new StringBuffer(msg);
- for (Object arg : args) {
- log.append(", ");
- log.append(arg);
- }
- logger.warn(log.toString());
- }
-
- @Override
- public void debug(String arg0, long arg1) {
- logger.debug(arg0, arg1);
- }
-
-}
@@ -849,7 +849,7 @@ public static void halt(int status, String body) {
* @deprecated replaced by {@link #ipAddress(String)}
*/
public static void setIpAddress(String ipAddress) {
- getInstance().setIpAddress(ipAddress);
+ getInstance().ipAddress(ipAddress);
}
/**
@@ -872,7 +872,7 @@ public static void ipAddress(String ipAddress) {
* @deprecated replaced by {@link #port(int)}
*/
public static void setPort(int port) {
- getInstance().setPort(port);
+ getInstance().port(port);
}
/**
@@ -906,7 +906,7 @@ public static void setSecure(String keystoreFile,
String keystorePassword,
String truststoreFile,
String truststorePassword) {
- getInstance().setSecure(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
+ getInstance().secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
}
/**
@@ -29,7 +29,8 @@
import spark.route.SimpleRouteMatcher;
import spark.ssl.SslStores;
import spark.staticfiles.StaticFiles;
-import spark.webserver.SparkServerFactory;
+import spark.webserver.EmbeddedServer;
+import spark.webserver.EmbeddedServers;
import static java.util.Objects.requireNonNull;
@@ -59,14 +60,16 @@
protected int threadIdleTimeoutMillis = -1;
protected Optional<Integer> webSocketIdleTimeoutMillis = Optional.empty();
- protected SparkServer server;
+ protected EmbeddedServer server;
protected SimpleRouteMatcher routeMatcher;
private boolean servletStaticLocationSet;
private boolean servletExternalStaticLocationSet;
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
* address is '0.0.0.0'. This has to be called before any route mapping is
@@ -255,15 +258,19 @@ public synchronized void externalStaticFileLocation(String externalFolder) {
public synchronized void webSocket(String path, Class<?> handler) {
requireNonNull(path, "WebSocket path cannot be null");
requireNonNull(handler, "WebSocket handler class cannot be null");
+
if (initialized) {
throwBeforeRouteMappingException();
}
+
if (ServletFlag.isRunningFromServlet()) {
throw new IllegalStateException("WebSockets are only supported in the embedded server");
}
+
if (webSocketHandlers == null) {
webSocketHandlers = new HashMap<>();
}
+
webSocketHandlers.put(path, handler);
}
@@ -310,7 +317,7 @@ private boolean hasMultipleHandlers() {
public synchronized void stop() {
if (server != null) {
routeMatcher.clearRoutes();
- server.stop();
+ server.extinguish();
latch = new CountDownLatch(1);
}
StaticFiles.clear();
@@ -332,19 +339,21 @@ public void addFilter(String httpMethod, FilterImpl filter) {
public synchronized void init() {
if (!initialized) {
routeMatcher = RouteMatcherFactory.get();
+
if (!ServletFlag.isRunningFromServlet()) {
new Thread(() -> {
- server = SparkServerFactory.create(hasMultipleHandlers());
+
+ server = EmbeddedServers.create(embeddedServerIdentifier, hasMultipleHandlers());
+ server.configureWebSockets(webSocketHandlers, webSocketIdleTimeoutMillis);
+
server.ignite(
ipAddress,
port,
sslStores,
latch,
maxThreads,
minThreads,
- threadIdleTimeoutMillis,
- webSocketHandlers,
- webSocketIdleTimeoutMillis);
+ threadIdleTimeoutMillis);
}).start();
}
initialized = true;
@@ -1,39 +0,0 @@
-package spark;
-
-import java.util.Map;
-import java.util.Optional;
-import java.util.concurrent.CountDownLatch;
-
-import spark.ssl.SslStores;
-
-/**
- * Created by Per Wendel on 2015-09-01.
- */
-public interface SparkServer {
-
- /**
- * Ignites the spark server, listening on the specified port, running SSL secured with the specified keystore
- * and truststore. If truststore is null, keystore is reused.
- *
- * @param host The address to listen on
- * @param port - the port
- * @param sslStores - The SSL sslStores.
- * @param latch - the countdown latch
- * @param maxThreads - max nbr of threads.
- * @param minThreads - min nbr of threads.
- * @param threadIdleTimeoutMillis - idle timeout (ms).
- * @param webSocketHandlers - web socket handlers.
- * @param webSocketIdleTimeoutMillis - Optional WebSocket idle timeout (ms).
- */
- void ignite(String host,
- int port,
- SslStores sslStores,
- CountDownLatch latch,
- int maxThreads,
- int minThreads,
- int threadIdleTimeoutMillis,
- Map<String, Class<?>> webSocketHandlers,
- Optional<Integer> webSocketIdleTimeoutMillis);
-
- void stop();
-}
@@ -52,6 +52,7 @@ public ClassPathResourceHandler(String baseResource) {
*/
public ClassPathResourceHandler(String baseResource, String welcomeFile) {
Assert.notNull(baseResource);
+
this.baseResource = baseResource;
this.welcomeFile = welcomeFile;
}
@@ -19,7 +19,6 @@
import java.net.MalformedURLException;
-import org.eclipse.jetty.util.URIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -63,7 +62,7 @@ protected AbstractFileResolvingResource getResource(String path) throws Malforme
}
try {
- path = URIUtil.canonicalPath(path);
+ path = UriPath.canonical(path);
@cesartg
cesartg 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);
Oops, something went wrong.

1 comment on commit a1909a8

@jehrhardt

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.