From bfc192b22b1e41bdad76d928389a1e7bbc1f9b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDeljko=20Tomi=C4=87?= Date: Fri, 24 Jan 2020 16:03:19 +0100 Subject: [PATCH 1/3] Use Spring Boot parent and dependencies, implement MessageBirdServiceSpring which uses Spring RestTemplate --- api/pom.xml | 74 ++++------- .../java/com/messagebird/APIResponse.java | 6 +- .../messagebird/MessageBirdServiceImpl.java | 5 +- .../ConversationHsmLocalizableParameter.java | 2 +- .../spring/MessageBirdServiceSpring.java | 119 ++++++++++++++++++ 5 files changed, 147 insertions(+), 59 deletions(-) create mode 100644 api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java diff --git a/api/pom.xml b/api/pom.xml index 2389c9be..ae352080 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -2,9 +2,16 @@ 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.1 + + + com.messagebird messagebird-api - 3.2.5 @@ -81,24 +88,26 @@ - com.fasterxml.jackson.core - jackson-annotations - 2.13.2 + org.springframework.boot + spring-boot-starter-web - com.fasterxml.jackson.core - jackson-databind - 2.13.2.2 + org.springframework.boot + spring-boot-starter-json - com.auth0 - java-jwt - 3.17.0 + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + - junit - junit - 4.13.1 + org.apache.httpcomponents + httpclient + + + + org.springframework.boot + spring-boot-starter-test test @@ -107,18 +116,6 @@ 3.4.2 test - - org.mockito - mockito-core - 2.21.0 - test - - - org.jetbrains - annotations - 13.0 - provided - @@ -133,7 +130,6 @@ org.apache.maven.plugins maven-source-plugin - 3.0.1 attach-sources @@ -169,30 +165,6 @@ org.apache.maven.plugins - maven-gpg-plugin - 1.6 - - - sign-artifacts - verify - - sign - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.7.0 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - 3.1.0 maven-assembly-plugin @@ -212,7 +184,6 @@ org.apache.maven.plugins maven-surefire-plugin - 2.21.0 ${skipTests} @@ -224,7 +195,6 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.7 true ossrh diff --git a/api/src/main/java/com/messagebird/APIResponse.java b/api/src/main/java/com/messagebird/APIResponse.java index 0bd07626..dde931b1 100644 --- a/api/src/main/java/com/messagebird/APIResponse.java +++ b/api/src/main/java/com/messagebird/APIResponse.java @@ -18,7 +18,7 @@ public class APIResponse { private final String body; private final int status; - APIResponse(final String body, final int status) { + public APIResponse(final String body, final int status) { this.body = body; this.status = status; } @@ -27,7 +27,7 @@ public class APIResponse { * Initializes an APIResponse object and sets the HTTP status code to 200. * @param body response body */ - APIResponse(final String body) { + public APIResponse(final String body) { this(body, STATUS_OK); } @@ -38,7 +38,7 @@ public class APIResponse { * * @return True if the status indicates success. */ - static boolean isSuccessStatus(final int status) { + public static boolean isSuccessStatus(final int status) { return status >= STATUS_RANGE_SUCCESS_START && status <= STATUS_RANGE_SUCCESS_END; } diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index 717b40d9..9e64ccc1 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -88,7 +88,6 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) { this.accessKey = accessKey; this.serviceUrl = serviceUrl; this.userAgentString = determineUserAgentString(); - } private String determineUserAgentString() { @@ -403,7 +402,7 @@

APIResponse doRequest(final String method, final String url, final Map Type of the payload. + * @return APIResponse containing the response's body and status. + */ + @Override + protected

APIResponse doRequest(final String method, final String url, final P payload) throws GeneralException { + HttpMethod httpMethod = Objects.requireNonNull(HttpMethod.resolve(method), "method cannot be null."); + RequestEntity.BodyBuilder builder = RequestEntity.method(httpMethod, URI.create(url)); + if (httpMethod == HttpMethod.POST || httpMethod == HttpMethod.PUT || httpMethod == HttpMethod.PATCH) { + builder.contentType(MediaType.APPLICATION_JSON); + } else { + builder.contentType(MediaType.APPLICATION_FORM_URLENCODED); + } + ResponseEntity exchange = this.restTemplate.exchange(builder.body(payload), String.class); + return new APIResponse(exchange.getBody(), exchange.getStatusCodeValue()); + } + + /** + * + * Do get request for file from input url and stores the file in filepath. + * @param url Absolute URL. + * @param filePath the path where the downloaded file is going to be stored. + * @return if it succeed, it returns filepath otherwise null or exception. + */ + @Override + protected String doGetRequestForFileAndStore(final String url, final String filePath) throws GeneralException, UnauthorizedException, NotFoundException { + AtomicInteger status = new AtomicInteger(); + String body = restTemplate.execute(url, HttpMethod.GET, null, clientHttpResponse -> { + status.set(clientHttpResponse.getRawStatusCode()); + if (clientHttpResponse.getStatusCode().is2xxSuccessful()) { + File ret = new File(filePath); + StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret)); + return filePath; + } else { + return readToEnd(clientHttpResponse.getBody()); + } + }); + if (status.get() == HttpURLConnection.HTTP_OK) { + return body; + } + handleHttpFailStatuses(status.get(), body); + return null; + } + + @Override + public

HttpURLConnection getConnection(String serviceUrl, P body, String requestType) throws IOException { + throw new IOException("This method should not be called here."); + } +} From 3043f3a9cbb81d7d4a0164810dda3b738ddbdb7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDeljko=20Tomi=C4=87?= Date: Thu, 5 Mar 2020 09:30:06 +0100 Subject: [PATCH 2/3] Add ConversationMessageStatus.REJECTED --- api/pom.xml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index ae352080..e302c211 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -120,9 +120,14 @@ - ossrh - https://oss.sonatype.org/content/repositories/snapshots + false + deployment + http://dev1-git1.int.ch:8675/nexus/content/repositories/snapshot-policy + + deployment + http://dev1-git1.int.ch:8675/nexus/content/repositories/releases + From d99c70f609dc4901df5946f6c0fab02c68f2a40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDeljko=20Tomi=C4=87?= Date: Fri, 14 Jan 2022 15:47:34 +0100 Subject: [PATCH 3/3] Update --- api/pom.xml | 12 +++++++++--- .../java/com/messagebird/MessageBirdServiceImpl.java | 4 ++-- .../com/messagebird/objects/MessageResponse.java | 4 ++-- .../messagebird/spring/MessageBirdServiceSpring.java | 6 ++++-- examples/pom.xml | 4 ++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index e302c211..9243348b 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -105,6 +105,12 @@ httpclient + + com.auth0 + java-jwt + 3.17.0 + + org.springframework.boot spring-boot-starter-test @@ -113,7 +119,7 @@ org.unitils unitils-core - 3.4.2 + 3.4.6 test @@ -122,11 +128,11 @@ false deployment - http://dev1-git1.int.ch:8675/nexus/content/repositories/snapshot-policy + https://dev1-git1.int.ch:8676/nexus/content/repositories/snapshot-policy deployment - http://dev1-git1.int.ch:8675/nexus/content/repositories/releases + https://dev1-git1.int.ch:8676/nexus/content/repositories/releases diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index 9e64ccc1..e3481d38 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -335,7 +335,7 @@ private List readValueAsList(ObjectMapper mapper, String content, final C return mapper.readValue(content, mapper.getTypeFactory().constructCollectionType(List.class, elementClass)); } - private void handleHttpFailStatuses(final int status, String body) throws UnauthorizedException, NotFoundException, GeneralException { + protected void handleHttpFailStatuses(final int status, String body) throws UnauthorizedException, NotFoundException, GeneralException { if (status == HttpURLConnection.HTTP_UNAUTHORIZED) { final List errorReport = getErrorReportOrNull(body); throw new UnauthorizedException(NOT_AUTHORISED_MSG, errorReport); @@ -359,7 +359,7 @@ private void handleHttpFailStatuses(final int status, String body) throws Unauth * @param

Type of the payload. * @return APIResponse containing the response's body and status. */ -

APIResponse doRequest(final String method, final String url, final Map headers, final P payload) throws GeneralException { + protected

APIResponse doRequest(final String method, final String url, final Map headers, final P payload) throws GeneralException { HttpURLConnection connection = null; InputStream inputStream = null; diff --git a/api/src/main/java/com/messagebird/objects/MessageResponse.java b/api/src/main/java/com/messagebird/objects/MessageResponse.java index 91d4d6da..edaf1539 100644 --- a/api/src/main/java/com/messagebird/objects/MessageResponse.java +++ b/api/src/main/java/com/messagebird/objects/MessageResponse.java @@ -1,13 +1,13 @@ package com.messagebird.objects; -import org.jetbrains.annotations.Nullable; - import java.io.Serializable; import java.math.BigInteger; import java.util.Date; import java.util.List; import java.util.Map; +import org.springframework.lang.Nullable; + /** * This object represents a message response at MessageBird.com *

diff --git a/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java b/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java index 0928711f..4c54b025 100644 --- a/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java +++ b/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; +import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; @@ -68,12 +69,13 @@ public void handleError(ClientHttpResponse response) throws IOException { * * @param method HTTP method. * @param url Absolute URL. + * @param headers additional headers to set on the request. * @param payload Payload to JSON encode for the request body. May be null. * @param

Type of the payload. * @return APIResponse containing the response's body and status. */ @Override - protected

APIResponse doRequest(final String method, final String url, final P payload) throws GeneralException { + protected

APIResponse doRequest(final String method, final String url, final Map headers, final P payload) throws GeneralException { HttpMethod httpMethod = Objects.requireNonNull(HttpMethod.resolve(method), "method cannot be null."); RequestEntity.BodyBuilder builder = RequestEntity.method(httpMethod, URI.create(url)); if (httpMethod == HttpMethod.POST || httpMethod == HttpMethod.PUT || httpMethod == HttpMethod.PATCH) { @@ -113,7 +115,7 @@ protected String doGetRequestForFileAndStore(final String url, final String file } @Override - public

HttpURLConnection getConnection(String serviceUrl, P body, String requestType) throws IOException { + public

HttpURLConnection getConnection(String serviceUrl, P body, String requestType, Map headers) throws IOException { throw new IOException("This method should not be called here."); } } diff --git a/examples/pom.xml b/examples/pom.xml index ae536533..3e0d236c 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -6,7 +6,7 @@ com.messagebird examples - 3.2.5 + 3.2.1-SNAPSHOT @@ -20,7 +20,7 @@ com.messagebird messagebird-api - 3.2.5 + 3.2.1-SNAPSHOT compile