diff --git a/api/pom.xml b/api/pom.xml index 2389c9be..9243348b 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,51 +88,52 @@ - com.fasterxml.jackson.core - jackson-annotations - 2.13.2 + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-json - com.fasterxml.jackson.core - jackson-databind - 2.13.2.2 + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + + + org.apache.httpcomponents + httpclient + + com.auth0 java-jwt 3.17.0 + - junit - junit - 4.13.1 + org.springframework.boot + spring-boot-starter-test test org.unitils unitils-core - 3.4.2 - test - - - org.mockito - mockito-core - 2.21.0 + 3.4.6 test - - org.jetbrains - annotations - 13.0 - provided - - ossrh - https://oss.sonatype.org/content/repositories/snapshots + false + deployment + https://dev1-git1.int.ch:8676/nexus/content/repositories/snapshot-policy + + deployment + https://dev1-git1.int.ch:8676/nexus/content/repositories/releases + @@ -133,7 +141,6 @@ org.apache.maven.plugins maven-source-plugin - 3.0.1 attach-sources @@ -169,30 +176,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 +195,6 @@ org.apache.maven.plugins maven-surefire-plugin - 2.21.0 ${skipTests} @@ -224,7 +206,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..e3481d38 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() { @@ -336,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); @@ -360,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; @@ -403,7 +402,7 @@

APIResponse doRequest(final String method, final String url, final Map diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationHsmLocalizableParameter.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationHsmLocalizableParameter.java index cbf2e324..439f76b7 100644 --- a/api/src/main/java/com/messagebird/objects/conversations/ConversationHsmLocalizableParameter.java +++ b/api/src/main/java/com/messagebird/objects/conversations/ConversationHsmLocalizableParameter.java @@ -17,7 +17,7 @@ public class ConversationHsmLocalizableParameter { private ConversationHsmLocalizableParameterCurrency currency; private Date dateTime; - private ConversationHsmLocalizableParameter() { + public ConversationHsmLocalizableParameter() { // Jackson requires an empty constructor for instantiation. } diff --git a/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java b/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java new file mode 100644 index 00000000..4c54b025 --- /dev/null +++ b/api/src/main/java/com/messagebird/spring/MessageBirdServiceSpring.java @@ -0,0 +1,121 @@ +package com.messagebird.spring; + +import java.io.File; +import java.io.FileOutputStream; +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; + +import com.messagebird.APIResponse; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import org.apache.http.impl.client.HttpClientBuilder; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.util.StreamUtils; +import org.springframework.web.client.DefaultResponseErrorHandler; +import org.springframework.web.client.RestTemplate; + +public class MessageBirdServiceSpring extends MessageBirdServiceImpl { + + private final RestTemplate restTemplate; + + public MessageBirdServiceSpring(RestTemplateBuilder restTemplateBuilder, String accessKey, String serviceUrl) { + super(accessKey, serviceUrl); + this.restTemplate = createRestTemplate(restTemplateBuilder); + } + + public MessageBirdServiceSpring(RestTemplateBuilder restTemplateBuilder, String accessKey) { + super(accessKey); + this.restTemplate = createRestTemplate(restTemplateBuilder); + } + + protected RestTemplate createRestTemplate(RestTemplateBuilder restTemplateBuilder) { + return restTemplateBuilder + .requestFactory(this::createClientHttpRequestFactory) + .errorHandler(new NoOpResponseErrorHandler()) + .defaultHeader("Authorization", "AccessKey " + getAccessKey()) + .build(); + } + + protected HttpComponentsClientHttpRequestFactory createClientHttpRequestFactory() { + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() + .useSystemProperties() + .setUserAgent(getUserAgentString()) + .disableDefaultUserAgent() + .disableContentCompression(); + return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()); + } + + static class NoOpResponseErrorHandler extends DefaultResponseErrorHandler { + public void handleError(ClientHttpResponse response) throws IOException { + + } + } + + /** + * Actually sends a HTTP request and returns its body and HTTP status code. + * + * @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 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) { + 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, 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