Skip to content

Commit

Permalink
Merge branch 'richarddd-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
perwendel committed Jun 11, 2018
2 parents 61507f1 + 3a9dfc6 commit c697d98
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
75 changes: 57 additions & 18 deletions src/main/java/spark/Routable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
abstract class Routable {

private ResponseTransformer defaultResponseTransformer;

/**
* Adds a route
*
Expand Down Expand Up @@ -56,7 +58,7 @@ abstract class Routable {
* @param route The route
*/
public void get(String path, Route route) {
addRoute(HttpMethod.get, RouteImpl.create(path, route));
addRoute(HttpMethod.get, createRouteImpl(path, route));
}

/**
Expand All @@ -66,7 +68,7 @@ public void get(String path, Route route) {
* @param route The route
*/
public void post(String path, Route route) {
addRoute(HttpMethod.post, RouteImpl.create(path, route));
addRoute(HttpMethod.post, createRouteImpl(path, route));
}

/**
Expand All @@ -76,7 +78,7 @@ public void post(String path, Route route) {
* @param route The route
*/
public void put(String path, Route route) {
addRoute(HttpMethod.put, RouteImpl.create(path, route));
addRoute(HttpMethod.put, createRouteImpl(path, route));
}

/**
Expand All @@ -86,7 +88,7 @@ public void put(String path, Route route) {
* @param route The route
*/
public void patch(String path, Route route) {
addRoute(HttpMethod.patch, RouteImpl.create(path, route));
addRoute(HttpMethod.patch, createRouteImpl(path, route));
}

/**
Expand All @@ -96,7 +98,7 @@ public void patch(String path, Route route) {
* @param route The route
*/
public void delete(String path, Route route) {
addRoute(HttpMethod.delete, RouteImpl.create(path, route));
addRoute(HttpMethod.delete, createRouteImpl(path, route));
}

/**
Expand All @@ -106,7 +108,7 @@ public void delete(String path, Route route) {
* @param route The route
*/
public void head(String path, Route route) {
addRoute(HttpMethod.head, RouteImpl.create(path, route));
addRoute(HttpMethod.head, createRouteImpl(path, route));
}

/**
Expand All @@ -116,7 +118,7 @@ public void head(String path, Route route) {
* @param route The route
*/
public void trace(String path, Route route) {
addRoute(HttpMethod.trace, RouteImpl.create(path, route));
addRoute(HttpMethod.trace, createRouteImpl(path, route));
}

/**
Expand All @@ -126,7 +128,7 @@ public void trace(String path, Route route) {
* @param route The route
*/
public void connect(String path, Route route) {
addRoute(HttpMethod.connect, RouteImpl.create(path, route));
addRoute(HttpMethod.connect, createRouteImpl(path, route));
}

/**
Expand All @@ -136,7 +138,7 @@ public void connect(String path, Route route) {
* @param route The route
*/
public void options(String path, Route route) {
addRoute(HttpMethod.options, RouteImpl.create(path, route));
addRoute(HttpMethod.options, createRouteImpl(path, route));
}

/**
Expand Down Expand Up @@ -171,7 +173,7 @@ public void after(String path, Filter filter) {
* @param route The route
*/
public void get(String path, String acceptType, Route route) {
addRoute(HttpMethod.get, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.get, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -182,7 +184,7 @@ public void get(String path, String acceptType, Route route) {
* @param route The route
*/
public void post(String path, String acceptType, Route route) {
addRoute(HttpMethod.post, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.post, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -193,7 +195,7 @@ public void post(String path, String acceptType, Route route) {
* @param route The route
*/
public void put(String path, String acceptType, Route route) {
addRoute(HttpMethod.put, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.put, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -204,7 +206,7 @@ public void put(String path, String acceptType, Route route) {
* @param route The route
*/
public void patch(String path, String acceptType, Route route) {
addRoute(HttpMethod.patch, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.patch, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -215,7 +217,7 @@ public void patch(String path, String acceptType, Route route) {
* @param route The route
*/
public void delete(String path, String acceptType, Route route) {
addRoute(HttpMethod.delete, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.delete, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -226,7 +228,7 @@ public void delete(String path, String acceptType, Route route) {
* @param route The route
*/
public void head(String path, String acceptType, Route route) {
addRoute(HttpMethod.head, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.head, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -237,7 +239,7 @@ public void head(String path, String acceptType, Route route) {
* @param route The route
*/
public void trace(String path, String acceptType, Route route) {
addRoute(HttpMethod.trace, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.trace, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -248,7 +250,7 @@ public void trace(String path, String acceptType, Route route) {
* @param route The route
*/
public void connect(String path, String acceptType, Route route) {
addRoute(HttpMethod.connect, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.connect, createRouteImpl(path, acceptType, route));
}

/**
Expand All @@ -259,7 +261,7 @@ public void connect(String path, String acceptType, Route route) {
* @param route The route
*/
public void options(String path, String acceptType, Route route) {
addRoute(HttpMethod.options, RouteImpl.create(path, acceptType, route));
addRoute(HttpMethod.options, createRouteImpl(path, acceptType, route));
}


Expand Down Expand Up @@ -793,4 +795,41 @@ public void patch(String path,
addRoute(HttpMethod.patch, ResponseTransformerRouteImpl.create(path, acceptType, route, transformer));
}

/**
* Create route implementation or use default response transformer
*
* @param path the path
* @param acceptType the accept type
* @param route the route
* @return ResponseTransformerRouteImpl or RouteImpl
*/
private RouteImpl createRouteImpl(String path, String acceptType, Route route) {
if (defaultResponseTransformer != null) {
return ResponseTransformerRouteImpl.create(path, acceptType, route, defaultResponseTransformer);
}
return RouteImpl.create(path, acceptType, route);
}

/**
* Create route implementation or use default response transformer
*
* @param path the path
* @param route the route
* @return ResponseTransformerRouteImpl or RouteImpl
*/
private RouteImpl createRouteImpl(String path, Route route) {
if (defaultResponseTransformer != null) {
return ResponseTransformerRouteImpl.create(path, route, defaultResponseTransformer);
}
return RouteImpl.create(path, route);
}

/**
* Sets default response transformer
*
* @param transformer
*/
public void defaultResponseTransformer(ResponseTransformer transformer) {
defaultResponseTransformer = transformer;
}
}
15 changes: 12 additions & 3 deletions src/main/java/spark/Spark.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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
*
Expand Down Expand Up @@ -952,6 +952,15 @@ public static void ipAddress(String ipAddress) {
getInstance().ipAddress(ipAddress);
}

/**
* Set the default response transformer. All requests not using a custom transformer will use this one
*
* @param transformer
*/
public static void defaultResponseTransformer(ResponseTransformer transformer) {
getInstance().defaultResponseTransformer(transformer);
}

/**
* Set the port that Spark should listen on. If not called the default port
* is 4567. This has to be called before any route mapping is done.
Expand Down Expand Up @@ -1062,8 +1071,8 @@ public static void secure(String keystoreFile,
public static void initExceptionHandler(Consumer<Exception> initExceptionHandler) {
getInstance().initExceptionHandler(initExceptionHandler);
}
/**

/**
* Set the connection to be secure, using the specified keystore and
* truststore. This has to be called before any route mapping is done. You
* have to supply a keystore file, truststore file is optional (keystore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package spark.examples.transformer;

import spark.ResponseTransformer;

import static spark.Spark.get;
import static spark.Spark.defaultResponseTransformer;

public class DefaultTransformerExample {

public static void main(String args[]) {

defaultResponseTransformer(json);

get("/hello", "application/json", (request, response) -> {
return new MyMessage("Hello World");
});

get("/hello2", "application/json", (request, response) -> {
return new MyMessage("Hello World");
}, model -> "custom transformer");
}

private static final ResponseTransformer json = new JsonTransformer();

}

0 comments on commit c697d98

Please sign in to comment.