Repository: camel Updated Branches: refs/heads/master 30c735106 -> df556c4a5
CAMEL-8898: Added error handling for new LinkedIn OAuth behavior, also added w_share scope Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/df556c4a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/df556c4a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/df556c4a Branch: refs/heads/master Commit: df556c4a58b9d04aad64a8ce761bbfef8b75da3a Parents: 30c7351 Author: Dhiraj Bokde <dhira...@yahoo.com> Authored: Fri Sep 18 00:42:58 2015 -0700 Committer: Dhiraj Bokde <dhira...@yahoo.com> Committed: Fri Sep 18 00:42:58 2015 -0700 ---------------------------------------------------------------------- .../api/LinkedInOAuthRequestFilter.java | 27 +++++++++++++++----- .../component/linkedin/api/OAuthScope.java | 4 ++- 2 files changed, 24 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/df556c4a/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/LinkedInOAuthRequestFilter.java ---------------------------------------------------------------------- diff --git a/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/LinkedInOAuthRequestFilter.java b/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/LinkedInOAuthRequestFilter.java index d625bc5..7c5def9 100644 --- a/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/LinkedInOAuthRequestFilter.java +++ b/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/LinkedInOAuthRequestFilter.java @@ -19,6 +19,7 @@ package org.apache.camel.component.linkedin.api; import java.io.IOException; import java.net.URI; import java.net.URL; +import java.net.URLDecoder; import java.net.URLEncoder; import java.security.SecureRandom; import java.util.HashMap; @@ -131,8 +132,8 @@ public final class LinkedInOAuthRequestFilter implements ClientRequestFilter { @SuppressWarnings("deprecation") private String getRefreshToken() { - // authorize application on user's behalf - webClient.getOptions().setRedirectEnabled(true); + // disable redirect to avoid loading error redirect URL + webClient.getOptions().setRedirectEnabled(false); try { final String csrfId = String.valueOf(new SecureRandom().nextLong()); @@ -157,7 +158,24 @@ public final class LinkedInOAuthRequestFilter implements ClientRequestFilter { url = String.format(AUTHORIZATION_URL_WITH_SCOPE, oAuthParams.getClientId(), csrfId, builder.toString(), encodedRedirectUri); } - final HtmlPage authPage = webClient.getPage(url); + HtmlPage authPage; + try { + authPage = webClient.getPage(url); + } catch (FailingHttpStatusCodeException e) { + // only handle errors returned with redirects + if (e.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { + final URL location = new URL(e.getResponse().getResponseHeaderValue(HttpHeaders.LOCATION)); + final String locationQuery = location.getQuery(); + if (locationQuery != null && locationQuery.contains("error=")) { + throw new IOException(URLDecoder.decode(locationQuery).replaceAll("&", ", ")); + } else { + // follow the redirect to login form + authPage = webClient.getPage(location); + } + } else { + throw e; + } + } // look for <div role="alert"> final HtmlDivision div = authPage.getFirstByXPath("//div[@role='alert']"); @@ -173,9 +191,6 @@ public final class LinkedInOAuthRequestFilter implements ClientRequestFilter { password.setText(oAuthParams.getUserPassword()); final HtmlSubmitInput submitInput = loginForm.getInputByName("authorize"); - // disable redirect to avoid loading redirect URL - webClient.getOptions().setRedirectEnabled(false); - // validate CSRF and get authorization code String redirectQuery; try { http://git-wip-us.apache.org/repos/asf/camel/blob/df556c4a/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/OAuthScope.java ---------------------------------------------------------------------- diff --git a/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/OAuthScope.java b/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/OAuthScope.java index aadf353..3549061 100644 --- a/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/OAuthScope.java +++ b/components/camel-linkedin/camel-linkedin-api/src/main/java/org/apache/camel/component/linkedin/api/OAuthScope.java @@ -26,10 +26,12 @@ public enum OAuthScope { R_EMAILADDRESS("r_emailaddress"), R_NETWORK("r_network"), R_CONTACTINFO("r_contactinfo"), + @Deprecated // use W_SHARE instead RW_NUS("rw_nus"), RW_COMPANY_ADMIN("rw_company_admin"), RW_GROUPS("rw_groups"), - W_MESSAGES("w_messages"); + W_MESSAGES("w_messages"), + W_SHARE("w_share"); private final String value;