Search in sources :

Example 1 with ProduceException

use of com.adaptris.core.ProduceException 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 2 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class HttpConsumerTest method testLoopbackWithIncorrectRole.

@Test
public void testLoopbackWithIncorrectRole() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("admin");
    securityConstraint.setPaths(Arrays.asList("/"));
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection connection = createConnection(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(URL_TO_POST_TO);
    Channel adapter = JettyHelper.createChannel(connection, consumer, mockProducer);
    try {
        adapter.requestStart();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
        msg.addMetadata("content.type", "text/xml");
        httpProducer.setUserName("user");
        httpProducer.setPassword("password");
        httpProducer.setUrl(createProduceDestinationUrl(connection.getPort()));
        start(httpProducer);
        AdaptrisMessage reply = httpProducer.request(msg);
        assertEquals("0 message consumed", 0, mockProducer.getMessages().size());
        assertTrue("Reply Response Code present", reply.containsKey(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE));
        int rc = Integer.valueOf(reply.getMetadataValue(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE)).intValue();
        assertTrue("Reply Response Value 401 || 403", rc == HttpURLConnection.HTTP_UNAUTHORIZED || rc == HttpURLConnection.HTTP_FORBIDDEN);
    } catch (ProduceException e) {
        // This is expected actually,.
        if (e.getCause() instanceof IOException) {
            String s = ((IOException) e.getCause()).getMessage();
            String httpString = "Server returned HTTP response code: ";
            if (s.startsWith(httpString)) {
                int rc = Integer.parseInt(s.substring(httpString.length(), httpString.length() + 3));
                assertTrue(rc == HttpURLConnection.HTTP_UNAUTHORIZED || rc == HttpURLConnection.HTTP_FORBIDDEN);
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    } finally {
        stop(httpProducer);
        adapter.requestClose();
        PortManager.release(connection.getPort());
        Thread.currentThread().setName(threadName);
        assertEquals(0, AdapterResourceAuthenticator.getInstance().currentAuthenticators().size());
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) StaticMockMessageProducer(com.adaptris.core.stubs.StaticMockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Channel(com.adaptris.core.Channel) IOException(java.io.IOException) Constraint(org.eclipse.jetty.util.security.Constraint) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 3 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class JndiPasProducerCase method testProduceJndiOnlyObjectNotFound.

@Test
public void testProduceJndiOnlyObjectNotFound() throws Exception {
    String queueName = testName.getMethodName() + "_queue";
    String topicName = testName.getMethodName() + "_topic";
    StandardJndiImplementation sendVendorImp = createVendorImplementation();
    StandaloneProducer standaloneProducer = new StandaloneProducer(activeMqBroker.getJndiPasConnection(sendVendorImp, true, queueName, topicName), new PasProducer().withTopic(this.getClass().getSimpleName()));
    try {
        start(standaloneProducer);
        standaloneProducer.produce(createMessage(null));
        fail("Expected ProduceException");
    } catch (ProduceException expected) {
    } finally {
        stop(standaloneProducer);
    }
}
Also used : StandardJndiImplementation(com.adaptris.core.jms.jndi.StandardJndiImplementation) PasProducer(com.adaptris.core.jms.PasProducer) StandaloneProducer(com.adaptris.core.StandaloneProducer) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 4 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class JndiPtpProducerCase method testProduceJndiOnlyObjectNotFound.

@Test
public void testProduceJndiOnlyObjectNotFound() throws Exception {
    String queueName = testName.getMethodName() + "_queue";
    String topicName = testName.getMethodName() + "_topic";
    StandardJndiImplementation sendVendorImp = createVendorImplementation();
    StandaloneProducer standaloneProducer = new StandaloneProducer(activeMqBroker.getJndiPtpConnection(sendVendorImp, true, queueName, topicName), new PtpProducer().withQueue((this.getClass().getSimpleName())));
    try {
        start(standaloneProducer);
        standaloneProducer.produce(createMessage(null));
        fail("Expected ProduceException");
    } catch (ProduceException expected) {
    } finally {
        stop(standaloneProducer);
    }
}
Also used : StandardJndiImplementation(com.adaptris.core.jms.jndi.StandardJndiImplementation) PtpProducer(com.adaptris.core.jms.PtpProducer) StandaloneProducer(com.adaptris.core.StandaloneProducer) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 5 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class LargeMessageWorkflowTest method testProduceException.

@Override
@Test
public void testProduceException() throws Exception {
    MockMessageProducer producer = new MockMessageProducer() {

        @Override
        protected void doProduce(AdaptrisMessage msg, String endpoint) throws ProduceException {
            throw new ProduceException();
        }
    };
    ;
    MockMessageProducer meh = new MockMessageProducer();
    MockChannel channel = createChannel(producer, Arrays.asList(new Service[] { new AddMetadataService(Arrays.asList(new MetadataElement[] { new MetadataElement(METADATA_KEY, METADATA_VALUE) })), new PayloadFromTemplateService().withTemplate(PAYLOAD_2) }));
    try {
        LargeMessageWorkflow workflow = (LargeMessageWorkflow) channel.getWorkflowList().get(0);
        channel.setMessageErrorHandler(new StandardProcessingExceptionHandler(new ServiceList(new ArrayList<Service>(Arrays.asList(new Service[] { new StandaloneProducer(meh) })))));
        channel.prepare();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(PAYLOAD_1);
        start(channel);
        workflow.onAdaptrisMessage(msg);
        assertEquals("Make none produced", 0, producer.getMessages().size());
        assertEquals(1, meh.getMessages().size());
        for (AdaptrisMessage m : meh.getMessages()) {
            assertEquals(PAYLOAD_2, m.getContent());
            assertTrue("Contains correct metadata key", m.containsKey(METADATA_KEY));
            assertEquals(METADATA_VALUE, m.getMetadataValue(METADATA_KEY));
        }
    } finally {
        stop(channel);
    }
}
Also used : MockChannel(com.adaptris.core.stubs.MockChannel) MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ServiceList(com.adaptris.core.ServiceList) PayloadFromTemplateService(com.adaptris.core.services.metadata.PayloadFromTemplateService) Service(com.adaptris.core.Service) ThrowExceptionService(com.adaptris.core.services.exception.ThrowExceptionService) AddMetadataService(com.adaptris.core.services.metadata.AddMetadataService) MetadataElement(com.adaptris.core.MetadataElement) AddMetadataService(com.adaptris.core.services.metadata.AddMetadataService) ProduceException(com.adaptris.core.ProduceException) StandardProcessingExceptionHandler(com.adaptris.core.StandardProcessingExceptionHandler) PayloadFromTemplateService(com.adaptris.core.services.metadata.PayloadFromTemplateService) StandaloneProducer(com.adaptris.core.StandaloneProducer) Test(org.junit.Test) StandardWorkflowTest(com.adaptris.core.StandardWorkflowTest)

Aggregations

ProduceException (com.adaptris.core.ProduceException)29 Test (org.junit.Test)17 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)13 CoreException (com.adaptris.core.CoreException)9 MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)7 Channel (com.adaptris.core.Channel)6 StandaloneProducer (com.adaptris.core.StandaloneProducer)6 IOException (java.io.IOException)6 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)5 File (java.io.File)5 GuidGenerator (com.adaptris.util.GuidGenerator)4 JMSException (javax.jms.JMSException)4 PtpProducer (com.adaptris.core.jms.PtpProducer)3 MockChannel (com.adaptris.core.stubs.MockChannel)3 InputStream (java.io.InputStream)3 Destination (javax.jms.Destination)3 FileNameCreator (com.adaptris.core.FileNameCreator)2 MetadataElement (com.adaptris.core.MetadataElement)2 MetadataFileNameCreator (com.adaptris.core.MetadataFileNameCreator)2 ServiceException (com.adaptris.core.ServiceException)2