From 1ae67b2d93c2fa57302997bf4cdd84909819a673 Mon Sep 17 00:00:00 2001 From: Cesar Andres Date: Fri, 13 Aug 2021 12:37:42 -0500 Subject: [PATCH] 302-valid auth --- .gitignore | 3 ++- .../connector/service/DrupalHttpClient.java | 22 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ef08f31..cac1c46 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build .gradle *.iml .idea/* -out \ No newline at end of file +out +.DS_Store diff --git a/src/main/java/com/lucidworks/fusion/connector/service/DrupalHttpClient.java b/src/main/java/com/lucidworks/fusion/connector/service/DrupalHttpClient.java index a0aa77a..56f138c 100644 --- a/src/main/java/com/lucidworks/fusion/connector/service/DrupalHttpClient.java +++ b/src/main/java/com/lucidworks/fusion/connector/service/DrupalHttpClient.java @@ -92,8 +92,10 @@ public String doLogin(String url, String username, String password) { String requestBody = objectMapper.writeValueAsString(prepareLoginBody(username, password)); byte[] loginBody = requestBody.getBytes(StandardCharsets.UTF_8); - //TODO ?_format=json - added if needed - URL obj = new URL(url); + URL obj = new URL(url + "?_format=json"); + //URL obj = new URL(url); + log.info("URL={}", obj); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); @@ -108,6 +110,18 @@ public String doLogin(String url, String username, String password) { } int responseCode = con.getResponseCode(); + log.error("responseCode={}", responseCode); + + if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { // 302 + // Save cookie for future GET Requests + cookie = con.getHeaderField("Set-Cookie") != null ? + con.getHeaderField("Set-Cookie").split(";")[0] : ""; + // with 302 the getInputStream fails + log.info("Successful login for user " + username); + log.info("cookie={}", cookie); + return "login for user" + username + " with 302"; + } + if (responseCode == HttpURLConnection.HTTP_OK) { // Success // Read from the response StringBuilder stringBuilder = new StringBuilder(); @@ -123,9 +137,11 @@ public String doLogin(String url, String username, String password) { con.getHeaderField("Set-Cookie").split(";")[0] : ""; log.info("Successful login for user " + username); + log.info("cookie={}", cookie); + return stringBuilder.toString(); } else { - log.error("Unsuccessful login request, Status-Code: " + responseCode); + log.error("Unsuccessful login request, Status-Code: {}", responseCode); return null; } } catch (IOException e) {