Search in sources :

Example 1 with HttpAuthenticator

use of com.adaptris.core.http.auth.HttpAuthenticator in project interlok by adaptris.

the class StandardHttpProducerTest method testProduce_WithUsernamePassword.

@Test
public void testProduce_WithUsernamePassword() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(HttpConsumerTest.JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("user");
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection jc = HttpHelper.createConnection();
    jc.setSecurityHandler(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(HttpHelper.URL_TO_POST_TO);
    Channel channel = JettyHelper.createChannel(jc, consumer, mockProducer);
    HttpAuthenticator auth = getAuthenticator(getName(), getName());
    StandardHttpProducer stdHttp = new StandardHttpProducer().withURL(HttpHelper.createURL(channel));
    stdHttp.setIgnoreServerResponseCode(false);
    stdHttp.registerConnection(new NullConnection());
    stdHttp.setAuthenticator(auth);
    try {
        start(channel);
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        start(stdHttp);
        AdaptrisMessage reply = stdHttp.request(msg);
        waitForMessages(mockProducer, 1);
        assertEquals(TEXT, mockProducer.getMessages().get(0).getContent());
    } finally {
        stop(stdHttp);
        HttpHelper.stopChannelAndRelease(channel);
        Thread.currentThread().setName(threadName);
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) HttpConnection(com.adaptris.core.http.jetty.HttpConnection) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JettyHelper.createChannel(com.adaptris.core.http.jetty.JettyHelper.createChannel) Channel(com.adaptris.core.Channel) ConfigurableSecurityHandler(com.adaptris.core.http.jetty.ConfigurableSecurityHandler) JettyMessageConsumer(com.adaptris.core.http.jetty.JettyMessageConsumer) HttpAuthenticator(com.adaptris.core.http.auth.HttpAuthenticator) NullConnection(com.adaptris.core.NullConnection) SecurityConstraint(com.adaptris.core.http.jetty.SecurityConstraint) HashLoginServiceFactory(com.adaptris.core.http.jetty.HashLoginServiceFactory) HttpConsumerTest(com.adaptris.core.http.jetty.HttpConsumerTest) Test(org.junit.Test)

Example 2 with HttpAuthenticator

use of com.adaptris.core.http.auth.HttpAuthenticator in project interlok by adaptris.

the class StandardHttpProducerTest method testProduce_WithUsernamePassword_BadCredentials.

@Test
public void testProduce_WithUsernamePassword_BadCredentials() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(HttpConsumerTest.JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("user");
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection jc = HttpHelper.createConnection();
    jc.setSecurityHandler(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(HttpHelper.URL_TO_POST_TO);
    Channel channel = JettyHelper.createChannel(jc, consumer, mockProducer);
    HttpAuthenticator auth = getAuthenticator(getName(), getName());
    StandardHttpProducer stdHttp = new StandardHttpProducer().withURL(HttpHelper.createURL(channel));
    stdHttp.setIgnoreServerResponseCode(false);
    stdHttp.registerConnection(new NullConnection());
    stdHttp.setAuthenticator(auth);
    try {
        start(channel);
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        start(stdHttp);
        AdaptrisMessage reply = stdHttp.request(msg);
        fail();
    } catch (ProduceException expected) {
    } finally {
        stop(stdHttp);
        HttpHelper.stopChannelAndRelease(channel);
        Thread.currentThread().setName(threadName);
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) HttpConnection(com.adaptris.core.http.jetty.HttpConnection) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JettyHelper.createChannel(com.adaptris.core.http.jetty.JettyHelper.createChannel) Channel(com.adaptris.core.Channel) ConfigurableSecurityHandler(com.adaptris.core.http.jetty.ConfigurableSecurityHandler) HttpAuthenticator(com.adaptris.core.http.auth.HttpAuthenticator) SecurityConstraint(com.adaptris.core.http.jetty.SecurityConstraint) ProduceException(com.adaptris.core.ProduceException) JettyMessageConsumer(com.adaptris.core.http.jetty.JettyMessageConsumer) NullConnection(com.adaptris.core.NullConnection) HashLoginServiceFactory(com.adaptris.core.http.jetty.HashLoginServiceFactory) HttpConsumerTest(com.adaptris.core.http.jetty.HttpConsumerTest) Test(org.junit.Test)

Example 3 with HttpAuthenticator

use of com.adaptris.core.http.auth.HttpAuthenticator in project interlok by adaptris.

the class StandardHttpProducerTest method testProduce_WithDynamicUsernamePassword.

@Test
public void testProduce_WithDynamicUsernamePassword() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(HttpConsumerTest.JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("user");
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection jc = HttpHelper.createConnection();
    jc.setSecurityHandler(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(HttpHelper.URL_TO_POST_TO);
    Channel channel = JettyHelper.createChannel(jc, consumer, mockProducer);
    String password = Password.encode(getName(), Password.PORTABLE_PASSWORD);
    HttpAuthenticator auth = new DynamicBasicAuthorizationHeader(getName(), password);
    StandardHttpProducer stdHttp = new StandardHttpProducer().withURL(HttpHelper.createURL(channel));
    stdHttp.setIgnoreServerResponseCode(false);
    stdHttp.registerConnection(new NullConnection());
    stdHttp.setAuthenticator(auth);
    try {
        start(channel);
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        start(stdHttp);
        AdaptrisMessage reply = stdHttp.request(msg);
        waitForMessages(mockProducer, 1);
        assertEquals(TEXT, mockProducer.getMessages().get(0).getContent());
    } finally {
        stop(stdHttp);
        HttpHelper.stopChannelAndRelease(channel);
        Thread.currentThread().setName(threadName);
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) HttpConnection(com.adaptris.core.http.jetty.HttpConnection) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JettyHelper.createChannel(com.adaptris.core.http.jetty.JettyHelper.createChannel) Channel(com.adaptris.core.Channel) ConfigurableSecurityHandler(com.adaptris.core.http.jetty.ConfigurableSecurityHandler) HttpAuthenticator(com.adaptris.core.http.auth.HttpAuthenticator) SecurityConstraint(com.adaptris.core.http.jetty.SecurityConstraint) JettyMessageConsumer(com.adaptris.core.http.jetty.JettyMessageConsumer) NullConnection(com.adaptris.core.NullConnection) HashLoginServiceFactory(com.adaptris.core.http.jetty.HashLoginServiceFactory) HttpConsumerTest(com.adaptris.core.http.jetty.HttpConsumerTest) Test(org.junit.Test)

Example 4 with HttpAuthenticator

use of com.adaptris.core.http.auth.HttpAuthenticator in project interlok by adaptris.

the class HttpRequestServiceTest method testProduce_WithUsernamePassword.

@Test
public void testProduce_WithUsernamePassword() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(HttpConsumerTest.JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("user");
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection jc = HttpHelper.createConnection();
    jc.setSecurityHandler(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(HttpHelper.URL_TO_POST_TO);
    Channel channel = JettyHelper.createChannel(jc, consumer, mockProducer);
    HttpAuthenticator auth = buildAuthenticator(getName(), getName());
    HttpRequestService service = new HttpRequestService(HttpHelper.createProduceDestination(channel)).withAuthenticator(auth).withMethod("POST");
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
    try {
        start(channel);
        execute(service, msg);
        waitForMessages(mockProducer, 1);
        assertEquals(TEXT, mockProducer.getMessages().get(0).getContent());
    } finally {
        HttpHelper.stopChannelAndRelease(channel);
        Thread.currentThread().setName(threadName);
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) HttpConnection(com.adaptris.core.http.jetty.HttpConnection) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JettyHelper.createChannel(com.adaptris.core.http.jetty.JettyHelper.createChannel) Channel(com.adaptris.core.Channel) ConfigurableSecurityHandler(com.adaptris.core.http.jetty.ConfigurableSecurityHandler) JettyMessageConsumer(com.adaptris.core.http.jetty.JettyMessageConsumer) HttpAuthenticator(com.adaptris.core.http.auth.HttpAuthenticator) SecurityConstraint(com.adaptris.core.http.jetty.SecurityConstraint) HashLoginServiceFactory(com.adaptris.core.http.jetty.HashLoginServiceFactory) Test(org.junit.Test) HttpConsumerTest(com.adaptris.core.http.jetty.HttpConsumerTest)

Aggregations

AdaptrisMessage (com.adaptris.core.AdaptrisMessage)4 Channel (com.adaptris.core.Channel)4 HttpAuthenticator (com.adaptris.core.http.auth.HttpAuthenticator)4 ConfigurableSecurityHandler (com.adaptris.core.http.jetty.ConfigurableSecurityHandler)4 HashLoginServiceFactory (com.adaptris.core.http.jetty.HashLoginServiceFactory)4 HttpConnection (com.adaptris.core.http.jetty.HttpConnection)4 HttpConsumerTest (com.adaptris.core.http.jetty.HttpConsumerTest)4 JettyHelper.createChannel (com.adaptris.core.http.jetty.JettyHelper.createChannel)4 JettyMessageConsumer (com.adaptris.core.http.jetty.JettyMessageConsumer)4 SecurityConstraint (com.adaptris.core.http.jetty.SecurityConstraint)4 MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)4 Test (org.junit.Test)4 NullConnection (com.adaptris.core.NullConnection)3 ProduceException (com.adaptris.core.ProduceException)1