diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/BadRequestException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/BadRequestException.java new file mode 100644 index 00000000..2aebd285 --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/BadRequestException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Bad Request (status code 400) response from the server. + */ +public class BadRequestException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public BadRequestException(Response response) { + super("A Bad request status code was received. Please verify the input.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/ForbiddenException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/ForbiddenException.java new file mode 100644 index 00000000..fc3a2738 --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/ForbiddenException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Forbidden (status code 403) response from the server. + */ +public class ForbiddenException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public ForbiddenException(Response response) { + super("A Forbidden status code was received. Please verify the permissions of your user.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/ImATeapotException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/ImATeapotException.java new file mode 100644 index 00000000..b76a544e --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/ImATeapotException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's an I'm a Teapot (status code 418) response from the server. + */ +public class ImATeapotException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public ImATeapotException(Response response) { + super("I'm a teapot. I cannot process data.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/InternalServerErrorException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/InternalServerErrorException.java new file mode 100644 index 00000000..b50b4224 --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/InternalServerErrorException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's an Internal Server Error (status code 500) response from the server. + */ +public class InternalServerErrorException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public InternalServerErrorException(Response response) { + super("An internal error occurred on the server. Please contact an administrator.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/MethodNotAllowedException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/MethodNotAllowedException.java new file mode 100644 index 00000000..c8f55b6e --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/MethodNotAllowedException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Method Not Allowed (status code 405) response from the server. + */ +public class MethodNotAllowedException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public MethodNotAllowedException(Response response) { + super("Trying to access service with the wrong HTTP method.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/NotAcceptableException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/NotAcceptableException.java new file mode 100644 index 00000000..d975d67c --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/NotAcceptableException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Not Acceptable (status code 406) response from the server. + */ +public class NotAcceptableException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public NotAcceptableException(Response response) { + super("Request indicates that the accept header is not of the same type as the returned one from the server.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/NotFoundException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/NotFoundException.java new file mode 100644 index 00000000..811fa790 --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/NotFoundException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Not Found (status code 404) response from the server. + */ +public class NotFoundException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public NotFoundException(Response response) { + super("The sought entity was not found.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/RequestTimeoutException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/RequestTimeoutException.java new file mode 100644 index 00000000..4c79ae8d --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/RequestTimeoutException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Request Timeout (status code 408) response from the server. + */ +public class RequestTimeoutException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public RequestTimeoutException(Response response) { + super("Server is taking longer than the expected amount. Please retry later.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/ServiceUnavailableException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/ServiceUnavailableException.java new file mode 100644 index 00000000..8b4ee9ab --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/ServiceUnavailableException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's a Service Unavailable (status code 503) response from the server. + */ +public class ServiceUnavailableException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public ServiceUnavailableException(Response response) { + super("Service is currently unavailable. Please retry later.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/error/http/UnauthorizedException.java b/commons/src/main/java/io/polyapi/commons/api/error/http/UnauthorizedException.java new file mode 100644 index 00000000..ac09ae61 --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/api/error/http/UnauthorizedException.java @@ -0,0 +1,18 @@ +package io.polyapi.commons.api.error.http; + +import io.polyapi.commons.api.http.Response; + +/** + * Exception thrown when there's an Unauthorized (status code 401) response from the server. + */ +public class UnauthorizedException extends HttpResponseException { + + /** + * Constructor that takes a message and the response returned. + * + * @param response The response. + */ + public UnauthorizedException(Response response) { + super("An Unauthorized status code was received. Please verify the permissions of your user.", response); + } +} diff --git a/commons/src/main/java/io/polyapi/commons/api/service/PolyApiService.java b/commons/src/main/java/io/polyapi/commons/api/service/PolyApiService.java index bef4bfde..dae033cb 100644 --- a/commons/src/main/java/io/polyapi/commons/api/service/PolyApiService.java +++ b/commons/src/main/java/io/polyapi/commons/api/service/PolyApiService.java @@ -67,16 +67,6 @@ private O parsedCall(HttpMethod method, String relativePath, Map allHeaders.put(key, value.stream().toList())); Response response = callApi(method, relativePath, allHeaders, queryParams, jsonParser.toJsonInputStream(body)); - if (response.statusCode() < 200) { - throw new UnexpectedInformationalResponseException(response); - } - if (response.statusCode() >= 400) { - // TODO: Change this to more specific exceptions per code. As some may require actions rather than displaying an error (i.e. token refresh). - switch (response.statusCode()) { - default: - throw new UnexpectedHttpResponseException(response); - } - } log.debug("Response is successful. Status code is {}.", response.statusCode()); log.debug("Parsing response."); O result = Optional.of(expectedResponseType) diff --git a/commons/src/main/java/io/polyapi/commons/internal/http/DefaultHttpClient.java b/commons/src/main/java/io/polyapi/commons/internal/http/DefaultHttpClient.java index 2f88f346..b23786d7 100644 --- a/commons/src/main/java/io/polyapi/commons/internal/http/DefaultHttpClient.java +++ b/commons/src/main/java/io/polyapi/commons/internal/http/DefaultHttpClient.java @@ -1,7 +1,13 @@ package io.polyapi.commons.internal.http; import io.polyapi.commons.api.error.PolyApiException; -import io.polyapi.commons.api.http.*; +import io.polyapi.commons.api.http.HttpClient; +import io.polyapi.commons.api.http.HttpMethod; +import io.polyapi.commons.api.http.Request; +import io.polyapi.commons.api.http.RequestRecord; +import io.polyapi.commons.api.http.Response; +import io.polyapi.commons.api.http.ResponseRecord; +import io.polyapi.commons.api.http.TokenProvider; import lombok.extern.slf4j.Slf4j; import okhttp3.OkHttpClient; import okhttp3.RequestBody; @@ -21,27 +27,24 @@ public class DefaultHttpClient implements HttpClient { private final OkHttpClient client; - private final TokenProvider tokenProvider; + private final HttpClientConfiguration configuration; /** * Utility constructor that sets a default {@link OkHttpClient} and uses a {@link TokenProvider}. * - * @param tokenProvider The provided token provider. - * @param connectTimeoutMillis The amount of milliseconds that the client will wait on connection before timing out. - * @param readTimeoutMillis The amount of milliseconds that the client will wait on reading the response before timing out. - * @param writeTimeoutMillis The amount of milliseconds that the client will wait on writing before timing out. + * @param configuration The configuration for the HTTP client. */ - public DefaultHttpClient(TokenProvider tokenProvider, Long connectTimeoutMillis, Long readTimeoutMillis, Long writeTimeoutMillis) { + public DefaultHttpClient(HttpClientConfiguration configuration) { this(new OkHttpClient.Builder() - .connectTimeout(connectTimeoutMillis, MILLISECONDS) - .readTimeout(readTimeoutMillis, MILLISECONDS) - .writeTimeout(writeTimeoutMillis, MILLISECONDS) - .build(), tokenProvider); + .connectTimeout(configuration.getConnectTimeoutMillis(), MILLISECONDS) + .readTimeout(configuration.getReadTimeoutMillis(), MILLISECONDS) + .writeTimeout(configuration.getWriteTimeoutMillis(), MILLISECONDS) + .build(), configuration); } - public DefaultHttpClient(OkHttpClient client, TokenProvider tokenProvider) { + public DefaultHttpClient(OkHttpClient client, HttpClientConfiguration configuration) { this.client = client; - this.tokenProvider = tokenProvider; + this.configuration = configuration; } @Override @@ -53,7 +56,7 @@ public HttpRequestBuilder prepareRequest(String host, Integer port, HttpMethod m @Override public HttpRequestBuilder prepareAuthenticatedRequest(String host, Integer port, HttpMethod method, String relativePath) { return prepareRequest(host, port, method, relativePath) - .withHeader("Authorization", tokenProvider.getTokenAsHeader()); + .withHeader("Authorization", configuration.getTokenProvider().getTokenAsHeader()); } @Override @@ -97,7 +100,7 @@ public Response send(Request request) { ); result.body().reset(); } - return result; + return (response.code() < 200 || response.code() > 299) ? configuration.getErrorHandlingStrategy().apply(result) : result; } catch (IOException e) { // FIXME: Throw the appropriate exception. throw new RuntimeException(e); diff --git a/commons/src/main/java/io/polyapi/commons/internal/http/HttpClientConfiguration.java b/commons/src/main/java/io/polyapi/commons/internal/http/HttpClientConfiguration.java new file mode 100644 index 00000000..3805ef96 --- /dev/null +++ b/commons/src/main/java/io/polyapi/commons/internal/http/HttpClientConfiguration.java @@ -0,0 +1,152 @@ +package io.polyapi.commons.internal.http; + +import io.polyapi.commons.api.error.http.BadRequestException; +import io.polyapi.commons.api.error.http.ForbiddenException; +import io.polyapi.commons.api.error.http.HttpResponseException; +import io.polyapi.commons.api.error.http.ImATeapotException; +import io.polyapi.commons.api.error.http.InternalServerErrorException; +import io.polyapi.commons.api.error.http.MethodNotAllowedException; +import io.polyapi.commons.api.error.http.NotAcceptableException; +import io.polyapi.commons.api.error.http.NotFoundException; +import io.polyapi.commons.api.error.http.RequestTimeoutException; +import io.polyapi.commons.api.error.http.ServiceUnavailableException; +import io.polyapi.commons.api.error.http.UnauthorizedException; +import io.polyapi.commons.api.error.http.UnexpectedHttpResponseException; +import io.polyapi.commons.api.error.http.UnexpectedInformationalResponseException; +import io.polyapi.commons.api.http.Response; +import io.polyapi.commons.api.http.TokenProvider; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +@Getter +@AllArgsConstructor +public class HttpClientConfiguration { + public static final Long DEFAULT_TIMEOUT_MILLIS = 10000L; + private final TokenProvider tokenProvider; + private Long connectTimeoutMillis; + private Long readTimeoutMillis; + private Long writeTimeoutMillis; + private Function errorHandlingStrategy; + + public HttpClientConfiguration(String hardcodedApiKey) { + this(new HardcodedTokenProvider(hardcodedApiKey)); + } + + private HttpClientConfiguration(TokenProvider tokenProvider) { + this.tokenProvider = tokenProvider; + } + + public static HttpClientConfigurationBuilder builder(String apiKey) { + return builder(new HardcodedTokenProvider(apiKey)); + } + + public static HttpClientConfigurationBuilder builder(TokenProvider tokenProvider) { + return new HttpClientConfigurationBuilder(tokenProvider); + } + + public static class HttpClientConfigurationBuilder { + private HttpClientConfiguration configuration; + + private HttpClientConfigurationBuilder(TokenProvider tokenProvider) { + this.configuration = new HttpClientConfiguration(tokenProvider, DEFAULT_TIMEOUT_MILLIS, DEFAULT_TIMEOUT_MILLIS, DEFAULT_TIMEOUT_MILLIS, response -> { + if (response.statusCode() < 200) { + throw new UnexpectedInformationalResponseException(response); + } + Function exceptionConstructor = switch (response.statusCode()) { + case 400 -> BadRequestException::new; + case 401 -> UnauthorizedException::new; + case 403 -> ForbiddenException::new; + case 404 -> NotFoundException::new; + case 405 -> MethodNotAllowedException::new; + case 406 -> NotAcceptableException::new; + case 408 -> RequestTimeoutException::new; + case 418 -> ImATeapotException::new; + case 500 -> InternalServerErrorException::new; + case 503 -> ServiceUnavailableException::new; + default -> UnexpectedHttpResponseException::new; + }; + throw exceptionConstructor.apply(response); + }); + } + + /** + * Sets the connect timeout. + * + * @param millis The amount of milliseconds that the client will wait on connection before timing out. + */ + public HttpClientConfigurationBuilder withConnectTimeoutMillis(Long millis) { + this.configuration.connectTimeoutMillis = millis; + return this; + } + + /** + * Sets the connect timeout. + * + * @param amount The amount of time units that the client will wait on connection before timing out. + * @param timeUnit The {@link TimeUnit} of the timeout to set. + */ + public HttpClientConfigurationBuilder withConnectTimeout(int amount, TimeUnit timeUnit) { + this.configuration.connectTimeoutMillis = timeUnit.toMillis(amount); + return this; + } + + /** + * Sets the read timeout. + * + * @param millis The amount of milliseconds that the client will wait on reading the response before timing out. + */ + public HttpClientConfigurationBuilder withReadTimeoutMillis(Long millis) { + this.configuration.readTimeoutMillis = millis; + return this; + } + + /** + * Sets the read timeout. + * + * @param amount The amount of time units that the client will wait on reading the response before timing out. + * @param timeUnit The {@link TimeUnit} of the timeout to set. + */ + public HttpClientConfigurationBuilder withReadTimeout(int amount, TimeUnit timeUnit) { + this.configuration.readTimeoutMillis = timeUnit.toMillis(amount); + return this; + } + + /** + * Sets the write timeout. + * + * @param millis The amount of milliseconds that the client will wait on writing before timing out. + */ + public HttpClientConfigurationBuilder withWriteTimeoutMillis(Long millis) { + this.configuration.writeTimeoutMillis = millis; + return this; + } + + /** + * Sets the write timeout. + * + * @param amount The amount of time units that the client will wait on writing before timing out. + * @param timeUnit The {@link TimeUnit} of the timeout to set. + */ + public HttpClientConfigurationBuilder withWriteTimeout(int amount, TimeUnit timeUnit) { + this.configuration.writeTimeoutMillis = timeUnit.toMillis(amount); + return this; + } + + /** + * Sets the error handling strategy. + * + * @return strategy Strategy for handling status codes that are not 2XX. By default, an exception is thrown. + */ + public HttpClientConfigurationBuilder withErrorHandlingStrategy(Function strategy) { + this.configuration.errorHandlingStrategy = strategy; + return this; + } + + public HttpClientConfiguration build() { + return configuration; + } + } +} diff --git a/library/src/main/java/io/polyapi/client/internal/model/PolyContext.java b/library/src/main/java/io/polyapi/client/internal/model/PolyContext.java index 42eb7972..a159fd84 100644 --- a/library/src/main/java/io/polyapi/client/internal/model/PolyContext.java +++ b/library/src/main/java/io/polyapi/client/internal/model/PolyContext.java @@ -1,6 +1,11 @@ package io.polyapi.client.internal.model; -import io.polyapi.client.api.model.function.*; +import io.polyapi.client.api.model.function.AudienceTokenAuthFunction; +import io.polyapi.client.api.model.function.PolyApiFunction; +import io.polyapi.client.api.model.function.PolyCustomFunction; +import io.polyapi.client.api.model.function.PolyServerFunction; +import io.polyapi.client.api.model.function.SubresourceAuthFunction; +import io.polyapi.client.api.model.function.TokenAuthFunction; import io.polyapi.client.api.model.variable.ServerVariableHandler; import io.polyapi.client.api.model.websocket.PolyTrigger; import io.polyapi.client.internal.proxy.PolyProxyFactory; @@ -11,6 +16,7 @@ import io.polyapi.commons.api.json.JsonParser; import io.polyapi.commons.internal.http.DefaultHttpClient; import io.polyapi.commons.internal.http.HardcodedTokenProvider; +import io.polyapi.commons.internal.http.HttpClientConfiguration; import io.polyapi.commons.internal.json.JacksonJsonParser; import io.polyapi.commons.internal.websocket.SocketIOWebSocketClient; @@ -35,7 +41,11 @@ public PolyContext() { } private PolyContext(PolyContextConfiguration config, JsonParser jsonParser) { - this(config.getHost(), config.getPort(), config.getClientId(), new DefaultHttpClient(new HardcodedTokenProvider(config.getApiKey()), config.getConnectionTimeoutMillis(), config.getReadTimeoutMillis(), config.getWriteTimeoutMillis()), new SocketIOWebSocketClient(config.getUrl(), config.getClientId(), new HardcodedTokenProvider(config.getApiKey()), jsonParser, config.getConnectionTimeoutMillis()), jsonParser); + this(config.getHost(), config.getPort(), config.getClientId(), new DefaultHttpClient(HttpClientConfiguration.builder(config.getApiKey()) + .withConnectTimeoutMillis(config.getConnectionTimeoutMillis()) + .withReadTimeoutMillis(config.getReadTimeoutMillis()) + .withWriteTimeoutMillis(config.getWriteTimeoutMillis()) + .build()), new SocketIOWebSocketClient(config.getUrl(), config.getClientId(), new HardcodedTokenProvider(config.getApiKey()), jsonParser, config.getConnectionTimeoutMillis()), jsonParser); } private PolyContext(String host, Integer port, String clientId, HttpClient httpClient, SocketIOWebSocketClient webSocketClient, JsonParser jsonParser) { diff --git a/library/src/main/java/io/polyapi/client/internal/service/InvocationServiceImpl.java b/library/src/main/java/io/polyapi/client/internal/service/InvocationServiceImpl.java index 15733810..e4ee43ec 100644 --- a/library/src/main/java/io/polyapi/client/internal/service/InvocationServiceImpl.java +++ b/library/src/main/java/io/polyapi/client/internal/service/InvocationServiceImpl.java @@ -12,9 +12,7 @@ import io.polyapi.client.error.invocation.delegate.DelegateExecutionException; import io.polyapi.client.error.invocation.delegate.InvalidMethodDeclarationException; import io.polyapi.commons.api.error.PolyApiException; -import io.polyapi.commons.api.error.http.UnexpectedHttpResponseException; import io.polyapi.commons.api.http.HttpClient; -import io.polyapi.commons.api.http.ResponseRecord; import io.polyapi.commons.api.json.JsonParser; import io.polyapi.commons.api.model.PolyFunction; import io.polyapi.commons.api.service.PolyApiService; @@ -24,7 +22,11 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; -import java.util.*; +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; +import java.util.Timer; +import java.util.TimerTask; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,7 +43,7 @@ public class InvocationServiceImpl extends PolyApiService implements InvocationS private final VariableInjectionService variableInjectionService; public InvocationServiceImpl(String host, Integer port, String clientId, HttpClient client, JsonParser jsonParser, - WebSocketClient webSocketClient, VariableInjectionService variableInjectionService) { + WebSocketClient webSocketClient, VariableInjectionService variableInjectionService) { super(host, port, client, jsonParser); this.clientId = clientId; this.jsonParser = jsonParser; @@ -51,30 +53,21 @@ public InvocationServiceImpl(String host, Integer port, String clientId, HttpCli @Override public T invokeServerFunction(Class invokingClass, String id, Map body, - Type expectedResponseType) { + Type expectedResponseType) { return invokeFunction("server", id, body, expectedResponseType); } @Override public T invokeApiFunction(Class invokingClass, String id, Map body, - Type expectedResponseType) { - ApiFunctionResponse response = invokeFunction("API", id, body, defaultInstance().constructParametricType( - ApiFunctionResponse.class, defaultInstance().constructType(expectedResponseType))); - if (response.getStatus() < 200 || response.getStatus() >= 400) { - throw new UnexpectedHttpResponseException(new ResponseRecord( - response.getHeaders().entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> List.of(entry.getValue()))), - Optional.ofNullable(response.getData()).map(jsonParser::toJsonInputStream).orElse(null), - response.getStatus())); - } - - return response.getData(); + Type expectedResponseType) { + return this.>invokeFunction("API", id, body, defaultInstance().constructParametricType( + ApiFunctionResponse.class, defaultInstance().constructType(expectedResponseType))).getData(); } @Override @SuppressWarnings("unchecked") public T invokeCustomFunction(Class invokingClass, String id, Map body, - Type expectedResponseType) { + Type expectedResponseType) { try { var delegateClass = Class.forName(format("%s.delegate.%s", invokingClass.getPackageName(), Optional.ofNullable(invokingClass.getDeclaredAnnotation(PolyMetadata.class)) @@ -119,7 +112,7 @@ public T invokeCustomFunction(Class invokingClass, String id, Map invokingClass, String id, Map body, - Type expectedResponseType) { + Type expectedResponseType) { try { AuthTokenEventConsumer callback = AuthTokenEventConsumer.class.cast(body.remove("callback")); AuthTokenOptions options = AuthTokenOptions.class.cast(body.remove("options")); @@ -203,7 +196,7 @@ public void updateVariable(String id, T entity) { @Override public Void invokeSubresourceAuthFunction(Class invokingClass, String id, Map body, - Type expectedResponseType) { + Type expectedResponseType) { body.put("clientID", clientId); post(format("auth-providers/%s/%s", id, invokingClass.getDeclaredAnnotation(PolyAuthSubresource.class).value()), replace(body), expectedResponseType); diff --git a/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/mojo/PolyApiMojo.java b/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/mojo/PolyApiMojo.java index 5d56dec8..06407ab3 100644 --- a/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/mojo/PolyApiMojo.java +++ b/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/mojo/PolyApiMojo.java @@ -6,12 +6,12 @@ import io.polyapi.commons.api.json.JsonParser; import io.polyapi.commons.internal.http.DefaultHttpClient; import io.polyapi.commons.internal.http.HardcodedTokenProvider; +import io.polyapi.commons.internal.http.HttpClientConfiguration; import io.polyapi.commons.internal.json.JacksonJsonParser; import io.polyapi.plugin.error.PolyApiMavenPluginException; import io.polyapi.plugin.service.MavenService; import lombok.Setter; import lombok.extern.slf4j.Slf4j; -import okhttp3.OkHttpClient; import org.apache.commons.io.IOUtils; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -24,7 +24,6 @@ import static io.polyapi.plugin.mojo.validation.Validator.validateNotEmpty; import static io.polyapi.plugin.mojo.validation.Validator.validatePortFormat; import static java.nio.charset.Charset.defaultCharset; -import static java.util.concurrent.TimeUnit.MINUTES; @Setter @Slf4j @@ -57,12 +56,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { mavenService.getPropertyFromPlugin("apiKey", apiKey, this::setApiKey); validateNotEmpty("apiKey", apiKey); tokenProvider = new HardcodedTokenProvider(apiKey); - httpClient = new DefaultHttpClient(new OkHttpClient.Builder() - .connectTimeout(10, MINUTES) - .readTimeout(10, MINUTES) - .writeTimeout(10, MINUTES) - .build(), - tokenProvider); + httpClient = new DefaultHttpClient(HttpClientConfiguration.builder(tokenProvider).build()); jsonParser = new JacksonJsonParser(); execute(host, Integer.valueOf(port)); } catch (PolyApiMavenPluginException e) { diff --git a/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java b/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java index 72a5bc7c..74099491 100644 --- a/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java +++ b/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java @@ -1,16 +1,5 @@ package io.polyapi.plugin.service; -import static com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance; -import static java.lang.String.format; -import static java.util.function.Predicate.isEqual; -import static java.util.stream.Collectors.joining; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; - import io.polyapi.commons.api.http.HttpClient; import io.polyapi.commons.api.json.JsonParser; import io.polyapi.commons.api.service.PolyApiService; @@ -18,6 +7,15 @@ import io.polyapi.plugin.model.specification.function.ServerFunctionSpecification; import lombok.extern.slf4j.Slf4j; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance; +import static java.lang.String.format; +import static java.util.stream.Collectors.joining; + @Slf4j public class SpecificationServiceImpl extends PolyApiService implements SpecificationService {