Repository: incubator-streams Updated Branches: refs/heads/master a2e2cbef2 -> 6bb0ab1f9
upgrade instagram to work with latest juneau Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/6bb0ab1f Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/6bb0ab1f Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/6bb0ab1f Branch: refs/heads/master Commit: 6bb0ab1f9b1a194c90e744d60e7c6f443c2b00b2 Parents: a2e2cbe Author: Steve Blackmon @steveblackmon <sblack...@apache.org> Authored: Wed Jun 21 14:04:15 2017 -0500 Committer: Steve Blackmon @steveblackmon <sblack...@apache.org> Committed: Wed Jun 21 14:04:15 2017 -0500 ---------------------------------------------------------------------- .../streams-provider-instagram/pom.xml | 2 - .../apache/streams/instagram/api/Instagram.java | 46 +++++++++++--------- .../instagram/api/InstagramRetryHandler.java | 30 ++++++++++--- 3 files changed, 49 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/6bb0ab1f/streams-contrib/streams-provider-instagram/pom.xml ---------------------------------------------------------------------- diff --git a/streams-contrib/streams-provider-instagram/pom.xml b/streams-contrib/streams-provider-instagram/pom.xml index c58609d..0c9b599 100644 --- a/streams-contrib/streams-provider-instagram/pom.xml +++ b/streams-contrib/streams-provider-instagram/pom.xml @@ -87,12 +87,10 @@ <dependency> <groupId>org.apache.juneau</groupId> <artifactId>juneau-rest-client</artifactId> - <version>6.1.0-incubating</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> - <version>4.5.3</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/6bb0ab1f/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/Instagram.java ---------------------------------------------------------------------- diff --git a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/Instagram.java b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/Instagram.java index 122fd26..e153b5f 100644 --- a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/Instagram.java +++ b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/Instagram.java @@ -22,18 +22,20 @@ import org.apache.streams.instagram.config.InstagramConfiguration; import org.apache.streams.instagram.pojo.UserRecentMediaRequest; import org.apache.streams.instagram.provider.InstagramProviderUtil; import org.apache.streams.jackson.StreamsJacksonMapper; +import org.apache.streams.juneau.JodaDateSwap; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpRequestInterceptor; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.juneau.json.JsonParser; +import org.apache.juneau.json.JsonSerializer; import org.apache.juneau.rest.client.RestCall; import org.apache.juneau.rest.client.RestCallException; import org.apache.juneau.rest.client.RestClient; +import org.apache.juneau.rest.client.RestClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,6 +45,8 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; +import static org.apache.http.entity.ContentType.APPLICATION_JSON; + /** * Implementation of all instagram interfaces using juneau. */ @@ -56,12 +60,12 @@ public class Instagram implements Media, Users { private ObjectMapper mapper; - private String rootUrl; - private CloseableHttpClient httpclient; private InstagramOAuthRequestSigner oauthSigner; + private String rootUrl; + RestClient restClient; private Instagram(InstagramConfiguration configuration) throws InstantiationException { @@ -79,12 +83,25 @@ public class Instagram implements Media, Users { .setMaxConnPerRoute(20) .setMaxConnTotal(100) .build(); - - this.restClient = new RestClient() - .setHttpClient(httpclient) - .setParser(JsonParser.class) - .setRootUrl(rootUrl); - + this.restClient = new RestClientBuilder() + .rootUrl(rootUrl) + .accept(APPLICATION_JSON.getMimeType()) + .httpClient(httpclient, true) + .pooled() + .parser( + JsonParser.DEFAULT.builder() + .ignoreUnknownBeanProperties(true) + .pojoSwaps(JodaDateSwap.class) + .build()) + .serializer( + JsonSerializer.DEFAULT.builder() + .pojoSwaps(JodaDateSwap.class) + .build()) + .retryable( + configuration.getRetryMax().intValue(), + configuration.getRetrySleepMs(), + new InstagramRetryHandler()) + .build(); this.mapper = StreamsJacksonMapper.getInstance(); } @@ -120,7 +137,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); UserInfoResponse result = mapper.readValue(restResponseEntity, UserInfoResponse.class); return result; @@ -152,7 +168,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); UserInfoResponse result = mapper.readValue(restResponseEntity, UserInfoResponse.class); return result; @@ -193,7 +208,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); RecentMediaResponse result = mapper.readValue(restResponseEntity, RecentMediaResponse.class); return result; @@ -234,7 +248,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(1, 1000, new InstagramRetryHandler()) .getResponseAsString(); RecentMediaResponse result = mapper.readValue(restResponseEntity, RecentMediaResponse.class); return result; @@ -272,7 +285,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); RecentMediaResponse result = mapper.readValue(restResponseEntity, RecentMediaResponse.class); return result; @@ -310,7 +322,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); SearchUsersResponse result = mapper.readValue(restResponseEntity, SearchUsersResponse.class); return result; @@ -343,7 +354,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); CommentsResponse result = mapper.readValue(restResponseEntity, CommentsResponse.class); return result; @@ -375,7 +385,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); UsersInfoResponse result = mapper.readValue(restResponseEntity, UsersInfoResponse.class); return result; @@ -407,7 +416,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); MediaResponse result = mapper.readValue(restResponseEntity, MediaResponse.class); return result; @@ -439,7 +447,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); MediaResponse result = mapper.readValue(restResponseEntity, MediaResponse.class); return result; @@ -474,7 +481,6 @@ public class Instagram implements Media, Users { RestCall restCall = restClient.doGet(uriBuilder.build().toString()); try { String restResponseEntity = restCall - .setRetryable(configuration.getRetryMax().intValue(), configuration.getRetrySleepMs().intValue(), new InstagramRetryHandler()) .getResponseAsString(); SearchMediaResponse result = mapper.readValue(restResponseEntity, SearchMediaResponse.class); return result; http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/6bb0ab1f/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/InstagramRetryHandler.java ---------------------------------------------------------------------- diff --git a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/InstagramRetryHandler.java b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/InstagramRetryHandler.java index f92eabf..51cd395 100644 --- a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/InstagramRetryHandler.java +++ b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/api/InstagramRetryHandler.java @@ -20,6 +20,7 @@ package org.apache.streams.instagram.api; import org.apache.streams.util.api.requests.backoff.AbstractBackOffStrategy; +import org.apache.http.HttpResponse; import org.apache.juneau.rest.client.RetryOn; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,18 +28,33 @@ import org.slf4j.LoggerFactory; /** * Handle expected and unexpected exceptions. */ -public class InstagramRetryHandler implements RetryOn { +public class InstagramRetryHandler extends RetryOn { private static final Logger LOGGER = LoggerFactory.getLogger(InstagramRetryHandler.class); private static AbstractBackOffStrategy backoff_strategy; - @Override - public boolean onCode(int httpResponseCode) { - if ( httpResponseCode > 400 ) { - return true; - } else { - return false; + protected boolean onResponse(HttpResponse response) { + LOGGER.debug(response.toString()); + switch(response.getStatusLine().getStatusCode()) { + case 200: // Response.Status.OK + case 304: // Response.Status.NOT_MODIFIED + case 400: // Response.Status.BAD_REQUEST + return false; + case 401: // Response.Status.UNAUTHORIZED + return true; + case 403: // Response.Status.FORBIDDEN + case 404: // Response.Status.NOT_FOUND + case 406: // Response.Status.NOT_ACCEPTABLE + case 410: // Response.Status.GONE + return false; + case 500: // Response.Status.INTERNAL_SERVER_ERROR + case 502: // Bad Gateway + case 503: // Response.Status.SERVICE_UNAVAILABLE + case 504: // Gateway Timeout + return true; + default: + return false; } } }