activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dej...@apache.org
Subject svn commit: r1360626 - in /activemq/trunk: activemq-web-demo/src/test/java/org/apache/activemq/web/ activemq-web/src/main/java/org/apache/activemq/web/
Date Thu, 12 Jul 2012 11:05:03 GMT
Author: dejanb
Date: Thu Jul 12 11:05:03 2012
New Revision: 1360626

URL: http://svn.apache.org/viewvc?rev=1360626&view=rev
Log:
https://issues.apache.org/jira/browse/AMQ-3924 - connect REST API to secure broker

Modified:
    activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/JettyTestSupport.java
    activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java
    activemq/trunk/activemq-web/src/main/java/org/apache/activemq/web/WebClient.java

Modified: activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/JettyTestSupport.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/JettyTestSupport.java?rev=1360626&r1=1360625&r2=1360626&view=diff
==============================================================================
--- activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/JettyTestSupport.java
(original)
+++ activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/JettyTestSupport.java
Thu Jul 12 11:05:03 2012
@@ -83,11 +83,11 @@ public class JettyTestSupport extends Te
     }
 
     protected void tearDown() throws Exception {
+        session.close();
+        connection.close();
         server.stop();
         broker.stop();
         broker.waitUntilStopped();
-        session.close();
-        connection.close();
     }
 
     public void waitForJettySocketToAccept(String bindLocation) throws Exception {

Modified: activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java?rev=1360626&r1=1360625&r2=1360626&view=diff
==============================================================================
--- activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java (original)
+++ activemq/trunk/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java Thu
Jul 12 11:05:03 2012
@@ -178,4 +178,18 @@ public class RestTest extends JettyTestS
         assertNotNull("Headers Exist", fields);
         assertEquals("header value", "value", fields.getStringField("property"));
     }
+
+    public void testAuth() throws Exception {
+        HttpClient httpClient = new HttpClient();
+        httpClient.start();
+        ContentExchange contentExchange = new ContentExchange();
+        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
+        contentExchange.setMethod("POST");
+        contentExchange.setURL("http://localhost:8080/message/testPost?type=queue");
+        contentExchange.setRequestHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
+        httpClient.send(contentExchange);
+
+        contentExchange.waitForDone();
+        assertTrue("success status", HttpStatus.isSuccess(contentExchange.getResponseStatus()));
+    }
 }

Modified: activemq/trunk/activemq-web/src/main/java/org/apache/activemq/web/WebClient.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-web/src/main/java/org/apache/activemq/web/WebClient.java?rev=1360626&r1=1360625&r2=1360626&view=diff
==============================================================================
--- activemq/trunk/activemq-web/src/main/java/org/apache/activemq/web/WebClient.java (original)
+++ activemq/trunk/activemq-web/src/main/java/org/apache/activemq/web/WebClient.java Thu Jul
12 11:05:03 2012
@@ -89,6 +89,9 @@ public class WebClient implements HttpSe
     private CamelContext camelContext;
     private ProducerTemplate producerTemplate;
 
+    private String username;
+    private String password;
+
     public WebClient() {
         if (factory == null) {
             throw new IllegalStateException("initContext(ServletContext) not called");
@@ -140,6 +143,22 @@ public class WebClient implements HttpSe
         this.deliveryMode = deliveryMode;
     }
 
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
     public synchronized void closeConsumers() {
         for (Iterator<MessageConsumer> it = consumers.values().iterator(); it.hasNext();)
{
             MessageConsumer consumer = it.next();
@@ -244,7 +263,7 @@ public class WebClient implements HttpSe
 
     public Connection getConnection() throws JMSException {
         if (connection == null) {
-            connection = factory.createConnection();
+            connection = factory.createConnection(username, password);
             connection.start();
         }
         return connection;
@@ -368,7 +387,21 @@ public class WebClient implements HttpSe
     }
 
     protected static WebClient createWebClient(HttpServletRequest request) {
-        return new WebClient();
+        WebClient client = new WebClient();
+        String auth = request.getHeader("Authorization");
+        if (auth != null) {
+            String[] tokens = auth.split(" ");
+            if (tokens.length == 2) {
+                String encoded = tokens[1].trim();
+                String credentials = new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(encoded));
+                String[] creds = credentials.split(":");
+                if (creds.length == 2) {
+                    client.setUsername(creds[0]);
+                    client.setPassword(creds[1]);
+                }
+            }
+        }
+        return client;
     }
 
 }



Mime
View raw message