You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/12/11 09:07:25 UTC

[sling-org-apache-sling-committer-cli] branch issue/SLING-8864 created (now 03b1495)

This is an automated email from the ASF dual-hosted git repository.

radu pushed a change to branch issue/SLING-8864
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git.


      at 03b1495  SLING-8864 - Report authentication errors immediately without looking at the response's body

This branch includes the following new commits:

     new 03b1495  SLING-8864 - Report authentication errors immediately without looking at the response's body

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-committer-cli] 01/01: SLING-8864 - Report authentication errors immediately without looking at the response's body

Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch issue/SLING-8864
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git

commit 03b14953db7629ebeaa3af3c3d8418ccc688225a
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Wed Dec 11 10:07:03 2019 +0100

    SLING-8864 - Report authentication errors immediately without looking at the response's body
    
    * added a response interceptor which trows an ISE when a server returns a 401 status code
---
 .../sling/cli/impl/http/HttpClientFactory.java     | 17 ++++-
 .../sling/cli/impl/http/HttpClientFactoryTest.java | 84 ++++++++++++++++++++++
 2 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/cli/impl/http/HttpClientFactory.java b/src/main/java/org/apache/sling/cli/impl/http/HttpClientFactory.java
index adc8d8e..5877398 100644
--- a/src/main/java/org/apache/sling/cli/impl/http/HttpClientFactory.java
+++ b/src/main/java/org/apache/sling/cli/impl/http/HttpClientFactory.java
@@ -16,10 +16,16 @@
  */
 package org.apache.sling.cli.impl.http;
 
+import java.util.concurrent.atomic.AtomicReference;
+
 import org.apache.http.HttpHost;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.HttpStatus;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.AuthCache;
+import org.apache.http.client.methods.HttpRequestWrapper;
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.impl.auth.BasicScheme;
 import org.apache.http.impl.client.BasicAuthCache;
@@ -61,9 +67,18 @@ public class HttpClientFactory {
     }
 
     public CloseableHttpClient newClient() {
-        
+        final AtomicReference<String> url = new AtomicReference<>();
         return HttpClients.custom()
                 .setDefaultCredentialsProvider(newCredentialsProvider())
+                .addInterceptorFirst(
+                        (HttpRequestInterceptor) (request, context) ->
+                        url.set(((HttpRequestWrapper) request).getOriginal().getRequestLine().getUri())
+                )
+                .addInterceptorFirst((HttpResponseInterceptor) (response, context) -> {
+                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
+                        throw new IllegalStateException("Please check your authentication details for " + url.get());
+                    }
+                })
                 .build();
     }
 
diff --git a/src/test/java/org/apache/sling/cli/impl/http/HttpClientFactoryTest.java b/src/test/java/org/apache/sling/cli/impl/http/HttpClientFactoryTest.java
new file mode 100644
index 0000000..b92d7fa
--- /dev/null
+++ b/src/test/java/org/apache/sling/cli/impl/http/HttpClientFactoryTest.java
@@ -0,0 +1,84 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.cli.impl.http;
+
+import java.net.InetSocketAddress;
+
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.sling.cli.impl.CredentialsService;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.sun.net.httpserver.HttpContext;
+import com.sun.net.httpserver.HttpServer;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class HttpClientFactoryTest {
+
+    private HttpServer server;
+
+    @Rule
+    public OsgiContext osgiContext = new OsgiContext();
+
+    @Before
+    public void before() throws Throwable {
+        server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
+        HttpContext rootContext = server.createContext("/");
+        rootContext.setHandler(ex -> {
+            ex.sendResponseHeaders(401, -1);
+            ex.close();
+        });
+        server.start();
+        System.setProperty("asf.username", "asf.username");
+        System.setProperty("asf.password", "asf.password");
+        System.setProperty("jira.username", "jira.username");
+        System.setProperty("jira.password", "jira.password");
+        osgiContext.registerInjectActivateService(new CredentialsService());
+        osgiContext.registerInjectActivateService(new HttpClientFactory());
+    }
+
+    @Test
+    public void test401() {
+        HttpClientFactory factory = osgiContext.getService(HttpClientFactory.class);
+        if (factory == null) {
+            throw new IllegalStateException("Unable to retrieve an HttpClientFactory.");
+        }
+        CloseableHttpClient client = factory.newClient();
+        HttpGet httpGet = new HttpGet("http://" + server.getAddress().getHostString() + ":" + server.getAddress().getPort());
+        Throwable t = null;
+        try {
+            client.execute(httpGet);
+        } catch (Exception e) {
+            t = e;
+        }
+        assertNotNull(t);
+        assertTrue(t.getMessage().contains("Please check your authentication details"));
+    }
+
+    @After
+    public void after() {
+        server.stop(0);
+    }
+}