hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject httpcomponents-client git commit: Annotation for cacheable auth schemes
Date Sun, 05 Nov 2017 12:56:56 GMT
Repository: httpcomponents-client
Updated Branches:
  refs/heads/master 668e1d04c -> d88e32f95


Annotation for cacheable auth schemes


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/d88e32f9
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/d88e32f9
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/d88e32f9

Branch: refs/heads/master
Commit: d88e32f9525f36bbbb46dc212648fd484e70a072
Parents: 668e1d0
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Sat Nov 4 11:08:04 2017 +0100
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Sat Nov 4 11:10:22 2017 +0100

----------------------------------------------------------------------
 .../client5/http/auth/AuthStateCacheable.java   | 39 ++++++++++++++++++++
 .../hc/client5/http/impl/auth/BasicScheme.java  |  2 +
 .../http/impl/auth/HttpAuthenticator.java       | 10 ++---
 .../http/impl/auth/TestHttpAuthenticator.java   | 19 ++++++++--
 4 files changed, 60 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d88e32f9/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthStateCacheable.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthStateCacheable.java
b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthStateCacheable.java
new file mode 100644
index 0000000..60435cd
--- /dev/null
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthStateCacheable.java
@@ -0,0 +1,39 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.auth;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Documented
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AuthStateCacheable {
+}

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d88e32f9/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
index 1f95bc8..d700184 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java
@@ -43,6 +43,7 @@ import org.apache.commons.codec.binary.Base64;
 import org.apache.hc.client5.http.auth.AuthChallenge;
 import org.apache.hc.client5.http.auth.AuthScheme;
 import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.AuthStateCacheable;
 import org.apache.hc.client5.http.auth.AuthenticationException;
 import org.apache.hc.client5.http.auth.Credentials;
 import org.apache.hc.client5.http.auth.CredentialsProvider;
@@ -59,6 +60,7 @@ import org.apache.hc.core5.util.Args;
  *
  * @since 4.0
  */
+@AuthStateCacheable
 public class BasicScheme implements AuthScheme, Serializable {
 
     private static final long serialVersionUID = -1931571557597830536L;

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d88e32f9/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java
b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java
index 372f7e4..3f651c5 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java
@@ -39,11 +39,11 @@ import org.apache.hc.client5.http.auth.AuthCache;
 import org.apache.hc.client5.http.auth.AuthChallenge;
 import org.apache.hc.client5.http.auth.AuthExchange;
 import org.apache.hc.client5.http.auth.AuthScheme;
+import org.apache.hc.client5.http.auth.AuthStateCacheable;
 import org.apache.hc.client5.http.auth.AuthenticationException;
 import org.apache.hc.client5.http.auth.ChallengeType;
 import org.apache.hc.client5.http.auth.CredentialsProvider;
 import org.apache.hc.client5.http.auth.MalformedChallengeException;
-import org.apache.hc.client5.http.config.AuthSchemes;
 import org.apache.hc.client5.http.protocol.HttpClientContext;
 import org.apache.hc.core5.http.FormattedHeader;
 import org.apache.hc.core5.http.Header;
@@ -314,13 +314,9 @@ public class HttpAuthenticator {
         }
     }
 
-    private boolean isCachable(final AuthScheme authScheme) {
-        final String schemeName = authScheme.getName();
-        return schemeName.equalsIgnoreCase(AuthSchemes.BASIC);
-    }
-
     private void updateCache(final HttpHost host, final AuthScheme authScheme, final HttpClientContext
clientContext) {
-        if (isCachable(authScheme)) {
+        final boolean cachable = authScheme.getClass().getAnnotation(AuthStateCacheable.class)
!= null;
+        if (cachable) {
             AuthCache authCache = clientContext.getAuthCache();
             if (authCache == null) {
                 authCache = new BasicAuthCache();

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d88e32f9/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
index 778d5f1..110e6bb 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java
@@ -34,11 +34,13 @@ import org.apache.hc.client5.http.auth.AuthExchange;
 import org.apache.hc.client5.http.auth.AuthScheme;
 import org.apache.hc.client5.http.auth.AuthSchemeProvider;
 import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.AuthStateCacheable;
 import org.apache.hc.client5.http.auth.AuthenticationException;
 import org.apache.hc.client5.http.auth.ChallengeType;
 import org.apache.hc.client5.http.auth.Credentials;
 import org.apache.hc.client5.http.auth.CredentialsProvider;
 import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
+import org.apache.hc.client5.http.config.AuthSchemes;
 import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
 import org.apache.hc.client5.http.protocol.HttpClientContext;
 import org.apache.hc.core5.http.HttpHeaders;
@@ -56,13 +58,24 @@ import org.apache.hc.core5.http.protocol.HttpContext;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Answers;
 import org.mockito.Mockito;
 
 @SuppressWarnings({"boxing","static-access"})
 public class TestHttpAuthenticator {
 
+    @AuthStateCacheable
+    abstract class CacheableAuthState implements AuthScheme {
+
+        @Override
+        public String getName() {
+            return AuthSchemes.BASIC;
+        }
+
+    }
+
     private AuthExchange authExchange;
-    private AuthScheme authScheme;
+    private CacheableAuthState authScheme;
     private HttpContext context;
     private HttpHost defaultHost;
     private CredentialsProvider credentialsProvider;
@@ -73,8 +86,8 @@ public class TestHttpAuthenticator {
     @Before
     public void setUp() throws Exception {
         this.authExchange = new AuthExchange();
-        this.authScheme = Mockito.mock(AuthScheme.class);
-        Mockito.when(this.authScheme.getName()).thenReturn("Basic");
+        this.authScheme = Mockito.mock(CacheableAuthState.class, Mockito.withSettings()
+                .defaultAnswer(Answers.CALLS_REAL_METHODS));
         Mockito.when(this.authScheme.isChallengeComplete()).thenReturn(Boolean.TRUE);
         this.context = new BasicHttpContext();
         this.defaultHost = new HttpHost("localhost", 80);


Mime
View raw message