Merge pull request #5114 from neerajnyd/bael-1168
Bael 1168 Ratpack HTTP Client
This commit is contained in:
commit
5a09cee678
|
@ -1,35 +1,35 @@
|
||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath "io.ratpack:ratpack-gradle:1.4.5"
|
classpath "io.ratpack:ratpack-gradle:1.5.4"
|
||||||
classpath "com.h2database:h2:1.4.193"
|
classpath "com.h2database:h2:1.4.193"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!JavaVersion.current().java8Compatible) {
|
if (!JavaVersion.current().java8Compatible) {
|
||||||
throw new IllegalStateException("Must be built with Java 8 or higher")
|
throw new IllegalStateException("Must be built with Java 8 or higher")
|
||||||
}
|
}
|
||||||
|
|
||||||
apply plugin: "io.ratpack.ratpack-java"
|
apply plugin: "io.ratpack.ratpack-java"
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile ratpack.dependency('hikari')
|
compile ratpack.dependency('hikari')
|
||||||
compile 'com.h2database:h2:1.4.193'
|
compile 'com.h2database:h2:1.4.193'
|
||||||
testCompile 'junit:junit:4.11'
|
testCompile 'junit:junit:4.11'
|
||||||
runtime "org.slf4j:slf4j-simple:1.7.21"
|
runtime "org.slf4j:slf4j-simple:1.7.21"
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
testLogging {
|
testLogging {
|
||||||
events 'started', 'passed'
|
events 'started', 'passed'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mainClassName = "com.baeldung.Application"
|
mainClassName = "com.baeldung.Application"
|
||||||
|
|
|
@ -35,6 +35,17 @@
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>ratpack-hystrix</artifactId>
|
<artifactId>ratpack-hystrix</artifactId>
|
||||||
<version>${ratpack.version}</version>
|
<version>${ratpack.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.netflix.hystrix</groupId>
|
||||||
|
<artifactId>hystrix-core</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.netflix.hystrix</groupId>
|
||||||
|
<artifactId>hystrix-core</artifactId>
|
||||||
|
<version>${hystrix.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
|
@ -76,10 +87,11 @@
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<ratpack.version>1.4.6</ratpack.version>
|
<ratpack.version>1.5.4</ratpack.version>
|
||||||
<httpclient.version>4.5.3</httpclient.version>
|
<httpclient.version>4.5.3</httpclient.version>
|
||||||
<httpcore.version>4.4.6</httpcore.version>
|
<httpcore.version>4.4.6</httpcore.version>
|
||||||
<h2database.version>1.4.193</h2database.version>
|
<h2database.version>1.4.193</h2database.version>
|
||||||
|
<hystrix.version>1.5.12</hystrix.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,49 +1,81 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import com.baeldung.filter.RequestValidatorFilter;
|
||||||
import java.util.List;
|
import com.baeldung.handler.EmployeeHandler;
|
||||||
|
import com.baeldung.handler.RedirectHandler;
|
||||||
import com.baeldung.filter.RequestValidatorFilter;
|
import com.baeldung.model.Employee;
|
||||||
import com.baeldung.model.Employee;
|
import com.baeldung.repository.EmployeeRepository;
|
||||||
|
import com.baeldung.repository.EmployeeRepositoryImpl;
|
||||||
import ratpack.guice.Guice;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import ratpack.hikari.HikariModule;
|
import io.netty.buffer.PooledByteBufAllocator;
|
||||||
import ratpack.http.MutableHeaders;
|
import ratpack.func.Action;
|
||||||
import ratpack.jackson.Jackson;
|
import ratpack.func.Function;
|
||||||
import ratpack.http.MutableHeaders;
|
import ratpack.guice.BindingsSpec;
|
||||||
import ratpack.server.RatpackServer;
|
import ratpack.guice.Guice;
|
||||||
|
import ratpack.handling.Chain;
|
||||||
public class Application {
|
import ratpack.hikari.HikariModule;
|
||||||
|
import ratpack.http.client.HttpClient;
|
||||||
public static void main(String[] args) throws Exception {
|
import ratpack.jackson.Jackson;
|
||||||
|
import ratpack.registry.Registry;
|
||||||
List<Employee> employees = new ArrayList<Employee>();
|
import ratpack.server.RatpackServer;
|
||||||
employees.add(new Employee(1L, "Mr", "John Doe"));
|
import ratpack.server.RatpackServerSpec;
|
||||||
employees.add(new Employee(2L, "Mr", "White Snow"));
|
import ratpack.server.ServerConfig;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
RatpackServer.start(
|
import java.time.temporal.ChronoUnit;
|
||||||
server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> {
|
import java.util.ArrayList;
|
||||||
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
|
import java.util.List;
|
||||||
config.addDataSourceProperty("URL",
|
|
||||||
"jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
|
public class Application {
|
||||||
}))).handlers(chain -> chain
|
|
||||||
.all(
|
public static void main(String[] args) throws Exception {
|
||||||
// ctx -> {
|
|
||||||
// MutableHeaders headers =
|
final Action<HikariConfig> hikariConfigAction = hikariConfig -> {
|
||||||
// ctx.getResponse().getHeaders();
|
hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
|
||||||
// headers.set("Access-Control-Allow-Origin","*");
|
hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
|
||||||
// headers.set("Accept-Language", "en-us");
|
};
|
||||||
// headers.set("Accept-Charset", "UTF-8");
|
|
||||||
// ctx.next();
|
final Action<BindingsSpec> bindingsSpecAction = bindings -> bindings.module(HikariModule.class, hikariConfigAction);
|
||||||
// }
|
final HttpClient httpClient = HttpClient.of(httpClientSpec -> {
|
||||||
new RequestValidatorFilter())
|
httpClientSpec.poolSize(10)
|
||||||
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
.connectTimeout(Duration.of(60, ChronoUnit.SECONDS))
|
||||||
.get("data/employees", ctx -> ctx.render(Jackson.json(employees)))
|
.maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH)
|
||||||
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
|
.responseMaxChunkSize(16384)
|
||||||
.post(":amount", ctx -> ctx
|
.readTimeout(Duration.of(60, ChronoUnit.SECONDS))
|
||||||
.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))));
|
.byteBufAllocator(PooledByteBufAllocator.DEFAULT);
|
||||||
}
|
});
|
||||||
|
final Function<Registry, Registry> registryFunction = Guice.registry(bindingsSpecAction);
|
||||||
}
|
|
||||||
|
final Action<Chain> chainAction = chain -> chain.all(new RequestValidatorFilter())
|
||||||
|
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
||||||
|
.get("data/employees", ctx -> ctx.render(Jackson.json(createEmpList())))
|
||||||
|
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens()
|
||||||
|
.get("name") + "!!!"))
|
||||||
|
.post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens()
|
||||||
|
.get("amount") + " added successfully !!!"));
|
||||||
|
|
||||||
|
final Action<Chain> routerChainAction = routerChain -> {
|
||||||
|
routerChain.path("redirect", new RedirectHandler())
|
||||||
|
.prefix("employee", empChain -> {
|
||||||
|
empChain.get(":id", new EmployeeHandler());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
final Action<RatpackServerSpec> ratpackServerSpecAction = serverSpec -> serverSpec.registry(registryFunction)
|
||||||
|
.registryOf(registrySpec -> {
|
||||||
|
registrySpec.add(EmployeeRepository.class, new EmployeeRepositoryImpl());
|
||||||
|
registrySpec.add(HttpClient.class, httpClient);
|
||||||
|
})
|
||||||
|
.handlers(chain -> chain.insert(routerChainAction)
|
||||||
|
.insert(chainAction));
|
||||||
|
|
||||||
|
RatpackServer.start(ratpackServerSpecAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Employee> createEmpList() {
|
||||||
|
List<Employee> employees = new ArrayList<>();
|
||||||
|
employees.add(new Employee(1L, "Mr", "John Doe"));
|
||||||
|
employees.add(new Employee(2L, "Mr", "White Snow"));
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.handler;
|
||||||
|
|
||||||
|
import com.baeldung.repository.EmployeeRepository;
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import ratpack.exec.Promise;
|
||||||
|
import ratpack.handling.Context;
|
||||||
|
import ratpack.handling.Handler;
|
||||||
|
|
||||||
|
public class EmployeeHandler implements Handler {
|
||||||
|
@Override
|
||||||
|
public void handle(Context ctx) throws Exception {
|
||||||
|
EmployeeRepository repository = ctx.get(EmployeeRepository.class);
|
||||||
|
Long id = Long.valueOf(ctx.getPathTokens()
|
||||||
|
.get("id"));
|
||||||
|
Promise<Employee> employeePromise = repository.findEmployeeById(id);
|
||||||
|
employeePromise.map(employee -> employee.getName())
|
||||||
|
.then(name -> ctx.getResponse()
|
||||||
|
.send(name));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.handler;
|
||||||
|
|
||||||
|
import ratpack.handling.Context;
|
||||||
|
import ratpack.handling.Handler;
|
||||||
|
|
||||||
|
public class FooHandler implements Handler {
|
||||||
|
@Override
|
||||||
|
public void handle(Context ctx) throws Exception {
|
||||||
|
ctx.getResponse()
|
||||||
|
.send("Hello Foo!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.handler;
|
||||||
|
|
||||||
|
import ratpack.exec.Promise;
|
||||||
|
import ratpack.handling.Context;
|
||||||
|
import ratpack.handling.Handler;
|
||||||
|
import ratpack.http.client.HttpClient;
|
||||||
|
import ratpack.http.client.ReceivedResponse;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class RedirectHandler implements Handler {
|
||||||
|
@Override
|
||||||
|
public void handle(Context ctx) throws Exception {
|
||||||
|
HttpClient client = ctx.get(HttpClient.class);
|
||||||
|
URI uri = URI.create("http://localhost:5050/employee/1");
|
||||||
|
Promise<ReceivedResponse> responsePromise = client.get(uri);
|
||||||
|
responsePromise.map(response -> response.getBody()
|
||||||
|
.getText()
|
||||||
|
.toUpperCase())
|
||||||
|
.then(responseText -> ctx.getResponse()
|
||||||
|
.send(responseText));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import ratpack.exec.Promise;
|
||||||
|
|
||||||
|
public interface EmployeeRepository {
|
||||||
|
|
||||||
|
Promise<Employee> findEmployeeById(Long id) throws Exception;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import ratpack.exec.Promise;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class EmployeeRepositoryImpl implements EmployeeRepository {
|
||||||
|
|
||||||
|
private static final Map<Long, Employee> EMPLOYEE_MAP = new HashMap<>();
|
||||||
|
|
||||||
|
public EmployeeRepositoryImpl() {
|
||||||
|
EMPLOYEE_MAP.put(1L, new Employee(1L, "Ms", "Jane Doe"));
|
||||||
|
EMPLOYEE_MAP.put(2L, new Employee(2L, "Mr", "NY"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Promise<Employee> findEmployeeById(Long id) throws Exception {
|
||||||
|
return Promise.async(downstream -> {
|
||||||
|
Thread.sleep(500);
|
||||||
|
downstream.success(EMPLOYEE_MAP.get(id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import org.junit.Test;
|
||||||
|
import ratpack.exec.Promise;
|
||||||
|
import ratpack.func.Action;
|
||||||
|
import ratpack.handling.Chain;
|
||||||
|
import ratpack.handling.Handler;
|
||||||
|
import ratpack.registry.Registry;
|
||||||
|
import ratpack.test.embed.EmbeddedApp;
|
||||||
|
import ratpack.test.exec.ExecHarness;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class AppHttpUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnyUri_GetEmployeeFromSameRegistry() throws Exception {
|
||||||
|
Handler allHandler = ctx -> {
|
||||||
|
Long id = Long.valueOf(ctx.getPathTokens()
|
||||||
|
.get("id"));
|
||||||
|
Employee employee = new Employee(id, "Mr", "NY");
|
||||||
|
ctx.next(Registry.single(Employee.class, employee));
|
||||||
|
};
|
||||||
|
Handler empNameHandler = ctx -> {
|
||||||
|
Employee employee = ctx.get(Employee.class);
|
||||||
|
ctx.getResponse()
|
||||||
|
.send("Name of employee with ID " + employee.getId() + " is " + employee.getName());
|
||||||
|
};
|
||||||
|
Handler empTitleHandler = ctx -> {
|
||||||
|
Employee employee = ctx.get(Employee.class);
|
||||||
|
ctx.getResponse()
|
||||||
|
.send("Title of employee with ID " + employee.getId() + " is " + employee.getTitle());
|
||||||
|
};
|
||||||
|
|
||||||
|
Action<Chain> chainAction = chain -> chain.prefix("employee/:id", empChain -> {
|
||||||
|
empChain.all(allHandler)
|
||||||
|
.get("name", empNameHandler)
|
||||||
|
.get("title", empTitleHandler);
|
||||||
|
});
|
||||||
|
EmbeddedApp.fromHandlers(chainAction)
|
||||||
|
.test(testHttpClient -> {
|
||||||
|
assertEquals("Name of employee with ID 1 is NY", testHttpClient.get("employee/1/name")
|
||||||
|
.getBody()
|
||||||
|
.getText());
|
||||||
|
assertEquals("Title of employee with ID 1 is Mr", testHttpClient.get("employee/1/title")
|
||||||
|
.getBody()
|
||||||
|
.getText());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSyncDataSource_GetDataFromPromise() throws Exception {
|
||||||
|
String value = ExecHarness.yieldSingle(execution -> Promise.sync(() -> "Foo"))
|
||||||
|
.getValueOrThrow();
|
||||||
|
assertEquals("Foo", value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue