Merge branch 'master' into BAEL-1174

This commit is contained in:
Loredana Crusoveanu 2017-12-01 15:31:57 +02:00
commit ae204db0af
5 changed files with 93 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package com.baeldung.cassecuredapp.controllers; package com.baeldung.cassecuredapp.controllers;
import org.apache.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.CookieClearingLogoutHandler; import org.springframework.security.web.authentication.logout.CookieClearingLogoutHandler;
@ -15,7 +16,7 @@ import javax.servlet.http.HttpServletResponse;
@Controller @Controller
public class AuthController { public class AuthController {
private Logger logger = Logger.getLogger(AuthController.class); private Logger logger = LogManager.getLogger(AuthController.class);
@GetMapping("/logout") @GetMapping("/logout")
public String logout( public String logout(

View File

@ -0,0 +1,3 @@
javac --module-path mods -d mods/com.baeldung.httpclient^
src/modules/com.baeldung.httpclient/module-info.java^
src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java

View File

@ -0,0 +1,83 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.baeldung.httpclient;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpRequest.BodyProcessor;
import jdk.incubator.http.HttpResponse;
import jdk.incubator.http.HttpResponse.BodyHandler;
/**
*
* @author pkaria
*/
public class HttpClientExample {
public static void main(String[] args) throws Exception {
httpGetRequest();
httpPosttRequest();
asynchronousRequest();
asynchronousMultipleRequests();
}
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
String responseBody = response.body();
int responseStatusCode = response.statusCode();
System.out.println(responseBody);
}
public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient
.newBuilder()
.build();
HttpRequest request = HttpRequest
.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
.POST(BodyProcessor.fromString("Sample Post Request"))
.build();
HttpResponse<String> response
= client.send(request, HttpResponse.BodyHandler.asString());
String responseBody = response.body();
System.out.println(responseBody);
}
public static void asynchronousRequest() throws URISyntaxException {
HttpClient client = HttpClient.newHttpClient();
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
CompletableFuture<HttpResponse<String>> futureResponse = client.sendAsync(request,
HttpResponse.BodyHandler.asString());
}
public static void asynchronousMultipleRequests() throws URISyntaxException {
List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
HttpClient client = HttpClient.newHttpClient();
List<CompletableFuture<File>> futures = targets
.stream()
.map(target -> client
.sendAsync(
HttpRequest.newBuilder(target)
.GET()
.build(),
BodyHandler.asFile(Paths.get("base", target.getPath())))
.thenApply(response -> response.body())
.thenApply(path -> path.toFile()))
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,3 @@
module com.baeldung.httpclient {
requires jdk.incubator.httpclient;
}