=ht* #17279 rename akka-http-* modules where agreed

This commit is contained in:
Mathias 2015-04-24 11:49:53 +02:00
parent 20530be054
commit 5859c39f8b
140 changed files with 330 additions and 11018 deletions

View file

@ -0,0 +1,96 @@
/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.server.japi.examples.simple;
import akka.actor.ActorSystem;
import akka.http.server.japi.*;
import java.io.IOException;
public class SimpleServerApp8 extends HttpApp {
static Parameter<Integer> x = Parameters.integer("x");
static Parameter<Integer> y = Parameters.integer("y");
static PathMatcher<Integer> xSegment = PathMatchers.integerNumber();
static PathMatcher<Integer> ySegment = PathMatchers.integerNumber();
public static RouteResult multiply(RequestContext ctx, int x, int y) {
int result = x * y;
return ctx.complete(String.format("%d * %d = %d", x, y, result));
}
static class Test {
int constant;
Test(int constant) {
this.constant = constant;
}
RouteResult constantPlusMultiply(RequestContext ctx, int x, int y) {
int result = x * y + constant;
return ctx.complete(String.format("%d * %d + %d = %d", x, y, constant, result));
}
}
public void test() {
handleWith(xSegment, ySegment, SimpleServerApp8::multiply);
}
@Override
public Route createRoute() {
Handler addHandler = new Handler() {
@Override
public RouteResult handle(RequestContext ctx) {
int xVal = x.get(ctx);
int yVal = y.get(ctx);
int result = xVal + yVal;
return ctx.complete(String.format("%d + %d = %d", xVal, yVal, result));
}
};
Handler2<Integer, Integer> subtractHandler = new Handler2<Integer, Integer>() {
public RouteResult handle(RequestContext ctx, Integer xVal, Integer yVal) {
int result = xVal - yVal;
return ctx.complete(String.format("%d - %d = %d", xVal, yVal, result));
}
};
return
route(
// matches the empty path
pathSingleSlash().route(
getFromResource("web/calculator.html")
),
// matches paths like this: /add?x=42&y=23
path("add").route(
handleWith(addHandler, x, y)
),
path("subtract").route(
handleWith(x, y, subtractHandler)
),
path("divide").route(
handleWith(x, y,
(ctx, x, y) ->
ctx.complete(String.format("%d / %d = %d", x, y, x / y))
)
),
// matches paths like this: /multiply/{x}/{y}
path("multiply", xSegment, ySegment).route(
// bind handler by reflection
handleWith(xSegment, ySegment, SimpleServerApp8::multiply)
),
path("multiply-methodref", xSegment, ySegment).route(
// bind handler by reflection
handleWith(xSegment, ySegment, new Test(123)::constantPlusMultiply)
)
);
}
public static void main(String[] args) throws IOException {
ActorSystem system = ActorSystem.create();
new SimpleServerApp8().bindRoute("localhost", 8080, system);
System.out.println("Type RETURN to exit");
System.in.read();
system.shutdown();
}
}

View file

@ -0,0 +1,23 @@
<html>
<body>
<h1>Calculator</h1>
<h2>Add</h2>
<form action="add">
<label for="x">x:</label><input id="x" name="x" type="text" value="42"/>
<label for="y">y:</label><input id="y" name="y" type="text" value="23"/>
<input type="submit" />
</form>
<h2>Subtract</h2>
<form action="subtract">
<label for="x">x:</label><input id="x" name="x" type="text" value="42"/>
<label for="y">y:</label><input id="y" name="y" type="text" value="23"/>
<input type="submit" />
</form>
<h3>Multiply</h3>
<a href="/multiply/42/23">/multiply/42/23</a>
</body>
</html>

View file

@ -0,0 +1,12 @@
/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
import akka.http.server.japi.HandlerBindingTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses(HandlerBindingTest.class)
public class AllJavaTests {
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.server.japi;
import akka.http.model.HttpRequest;
import org.junit.Test;
import static akka.http.server.japi.Directives.*;
public class HandlerBindingTest extends JUnitRouteTest {
@Test
public void testHandlerWithoutExtractions() {
Route route = handleWith(ctx -> ctx.complete("Ok"));
TestResponse response = runRoute(route, HttpRequest.GET("/"));
response.assertEntity("Ok");
}
@Test
public void testHandler1() {
Route route = handleWith(Parameters.integer("a"), (ctx, a) -> ctx.complete("Ok " + a));
TestResponse response = runRoute(route, HttpRequest.GET("?a=23"));
response.assertStatusCode(200);
response.assertEntity("Ok 23");
}
@Test
public void testHandler2() {
Route route =
handleWith(
Parameters.integer("a"),
Parameters.integer("b"),
(ctx, a, b) -> ctx.complete("Sum: " + (a + b)));
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42"));
response.assertStatusCode(200);
response.assertEntity("Sum: 65");
}
@Test
public void testHandler3() {
Route route =
handleWith(
Parameters.integer("a"),
Parameters.integer("b"),
Parameters.integer("c"),
(ctx, a, b, c) -> ctx.complete("Sum: " + (a + b + c)));
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30"));
response.assertStatusCode(200);
response.assertEntity("Sum: 95");
}
@Test
public void testHandler4() {
Route route =
handleWith(
Parameters.integer("a"),
Parameters.integer("b"),
Parameters.integer("c"),
Parameters.integer("d"),
(ctx, a, b, c, d) -> ctx.complete("Sum: " + (a + b + c + d)));
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30&d=45"));
response.assertStatusCode(200);
response.assertEntity("Sum: 140");
}
public RouteResult sum(RequestContext ctx, int a, int b, int c, int d) {
return ctx.complete("Sum: "+(a + b + c + d));
}
@Test
public void testHandler4MethodRef() {
Route route =
handleWith(
Parameters.integer("a"),
Parameters.integer("b"),
Parameters.integer("c"),
Parameters.integer("d"),
this::sum);
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30&d=45"));
response.assertStatusCode(200);
response.assertEntity("Sum: 140");
}
}