Search in sources :

Example 1 with MetadataUsernamePassword

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

the class StandardHttpProducerTest method testProduce_WithMetadataUsernamePassword.

@Test
public void testProduce_WithMetadataUsernamePassword() 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);
    MetadataUsernamePassword auth = new MetadataUsernamePassword();
    auth.setUsernameMetadataKey("user-key");
    auth.setPasswordMetadataKey("pass-key");
    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);
        msg.addMetadata("user-key", getName());
        msg.addMetadata("pass-key", getName());
        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) MetadataUsernamePassword(com.adaptris.core.http.auth.MetadataUsernamePassword) 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) 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)

Aggregations

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