git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1360626 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2012-07-12 11:05:03 +00:00
parent f180b1d7db
commit d4742f597b
3 changed files with 51 additions and 4 deletions

View File

@ -83,11 +83,11 @@ public class JettyTestSupport extends TestCase {
}
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 {

View File

@ -178,4 +178,18 @@ public class RestTest extends JettyTestSupport {
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()));
}
}

View File

@ -89,6 +89,9 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
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 HttpSessionActivationListener, HttpSessionBind
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 HttpSessionActivationListener, HttpSessionBind
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 HttpSessionActivationListener, HttpSessionBind
}
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;
}
}