org.subethamail.smtp.MessageContext Java Examples

The following examples show how to use org.subethamail.smtp.MessageContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmtpConsumer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public SmtpConsumer(
        final MessageContext context,
        final ProcessSessionFactory sessionFactory,
        final int port,
        final String host,
        final ComponentLog log,
        final int maxMessageSize
) {
    this.context = context;
    this.sessionFactory = sessionFactory;
    this.port = port;
    if (host == null || host.trim().isEmpty()) {
        this.host = context.getSMTPServer().getHostName();
    } else {
        this.host = host;
    }
    this.log = log;
    this.maxMessageSize = maxMessageSize;
}
 
Example #2
Source File: SmtpConsumer.java    From nifi with Apache License 2.0 6 votes vote down vote up
public SmtpConsumer(
        final MessageContext context,
        final ProcessSessionFactory sessionFactory,
        final int port,
        final String host,
        final ComponentLog log,
        final int maxMessageSize
) {
    this.context = context;
    this.sessionFactory = sessionFactory;
    this.port = port;
    if (host == null || host.trim().isEmpty()) {
        this.host = context.getSMTPServer().getHostName();
    } else {
        this.host = host;
    }
    this.log = log;
    this.maxMessageSize = maxMessageSize;
}
 
Example #3
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompletedMailTransaction() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			result = messageHandler;

			messageHandler.from(anyString);
			messageHandler.recipient(anyString);
			messageHandler.data((InputStream) any);
			messageHandler.done();
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");
	client.from("[email protected]");
	client.to("[email protected]");
	client.dataStart();
	client.dataWrite(TextUtils.getAsciiBytes("body"), 4);
	client.dataEnd();
	client.quit();
	smtpServer.stop(); // wait for the server to catch up
}
 
Example #4
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 6 votes vote down vote up
@Test
public void testAbortedMailTransaction() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			result = messageHandler;

			messageHandler.from(anyString);
			messageHandler.done();
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");
	client.from("[email protected]");
	client.quit();
	smtpServer.stop(); // wait for the server to catch up
}
 
Example #5
Source File: SMTPHandlerFactory.java    From holdmail with Apache License 2.0 5 votes vote down vote up
@Override
public SMTPHandler create(MessageContext ctx) {

    SMTPHandler smtpHandler = new SMTPHandler(ctx);
    beanFactory.autowireBean(smtpHandler);
    return smtpHandler;
}
 
Example #6
Source File: MessageHandlerFactoryImpl.java    From mireka with Apache License 2.0 5 votes vote down vote up
@Override
public MessageHandler create(MessageContext ctx) {
    MailTransactionImpl mailTransaction = new MailTransactionImpl(ctx);
    FilterInstances filterInstances =
            filters.createInstanceChain(mailTransaction);
    FilterChainMessageHandler filterChainMessageHandler =
            new FilterChainMessageHandler(filterInstances, mailTransaction);
    return new ErrorHandlerMessageHandler(filterChainMessageHandler);
}
 
Example #7
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisconnectImmediately() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			times = 0;
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");
	client.quit();
	smtpServer.stop(); // wait for the server to catch up
}
 
Example #8
Source File: MessageHandlerFactoryImpl.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public Handler(MessageContext ctx) {
    this.ctx = ctx;
}
 
Example #9
Source File: MessageHandlerFactoryImpl.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public MessageHandler create(MessageContext ctx) {
    return new Handler(ctx);
}
 
Example #10
Source File: MessageHandlerImpl.java    From keycloak with Apache License 2.0 4 votes vote down vote up
MessageHandlerImpl(MessageContext context) {
    this.context = context;
}
 
Example #11
Source File: ListenSMTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
private SMTPServer prepareServer(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    final int port = context.getProperty(SMTP_PORT).asInteger();
    final String host = context.getProperty(SMTP_HOSTNAME).getValue();
    final ComponentLog log = getLogger();
    final int maxMessageSize = context.getProperty(SMTP_MAXIMUM_MSG_SIZE).asDataSize(DataUnit.B).intValue();
    //create message handler factory
    final MessageHandlerFactory messageHandlerFactory = (final MessageContext mc) -> {
        return new SmtpConsumer(mc, sessionFactory, port, host, log, maxMessageSize);
    };
    //create smtp server
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final SMTPServer smtpServer = sslContextService == null ? new SMTPServer(messageHandlerFactory) : new SMTPServer(messageHandlerFactory) {
        @Override
        public SSLSocket createSSLSocket(Socket socket) throws IOException {
            InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
            String clientAuth = context.getProperty(CLIENT_AUTH).getValue();
            SSLContext sslContext = sslContextService.createSSLContext(SslContextFactory.ClientAuth.valueOf(clientAuth));
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            SSLSocket sslSocket = (SSLSocket) (socketFactory.createSocket(socket, remoteAddress.getHostName(), socket.getPort(), true));
            sslSocket.setUseClientMode(false);

            if (SslContextFactory.ClientAuth.REQUIRED.toString().equals(clientAuth)) {
                this.setRequireTLS(true);
                sslSocket.setNeedClientAuth(true);
            }
            return sslSocket;
        }
    };
    if (sslContextService != null) {
        smtpServer.setEnableTLS(true);
    } else {
        smtpServer.setHideTLS(true);
    }
    smtpServer.setSoftwareName("Apache NiFi SMTP");
    smtpServer.setPort(port);
    smtpServer.setMaxConnections(context.getProperty(SMTP_MAXIMUM_CONNECTIONS).asInteger());
    smtpServer.setMaxMessageSize(maxMessageSize);
    smtpServer.setConnectionTimeout(context.getProperty(SMTP_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    if (context.getProperty(SMTP_HOSTNAME).isSet()) {
        smtpServer.setHostName(context.getProperty(SMTP_HOSTNAME).getValue());
    }
    return smtpServer;
}
 
Example #12
Source File: MockMessageHandler.java    From entando-components with GNU Lesser General Public License v3.0 4 votes vote down vote up
public MockMessageHandler(MessageContext ctx) {
	this._ctx = ctx;
	if (null == _messages) {
		_messages = new ArrayList<MockMailMessage>();
	}
}
 
Example #13
Source File: MockMessageHandlerFactory.java    From entando-components with GNU Lesser General Public License v3.0 4 votes vote down vote up
public MessageHandler create(MessageContext ctx) {
	if (this._messageHandler == null) {
		this._messageHandler = new MockMessageHandler(ctx);
	}
    return _messageHandler;
}
 
Example #14
Source File: MailTransactionImpl.java    From mireka with Apache License 2.0 4 votes vote down vote up
@Override
public MessageContext getMessageContext() {
    return messageContext;
}
 
Example #15
Source File: MailTransactionImpl.java    From mireka with Apache License 2.0 4 votes vote down vote up
public MailTransactionImpl(MessageContext messageContext) {
    super();
    this.messageContext = messageContext;
}
 
Example #16
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 4 votes vote down vote up
@Test
public void testTwoMailsInOneSession() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			result = messageHandler;

			onInstance(messageHandler).from(anyString);
			onInstance(messageHandler).recipient(anyString);
			onInstance(messageHandler).data((InputStream) any);
			onInstance(messageHandler).done();

			messageHandlerFactory.create((MessageContext) any);
			result = messageHandler2;

			onInstance(messageHandler2).from(anyString);
			onInstance(messageHandler2).recipient(anyString);
			onInstance(messageHandler2).data((InputStream) any);
			onInstance(messageHandler2).done();
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");

	client.from("[email protected]");
	client.to("[email protected]");
	client.dataStart();
	client.dataWrite(TextUtils.getAsciiBytes("body1"), 5);
	client.dataEnd();

	client.from("[email protected]");
	client.to("[email protected]");
	client.dataStart();
	client.dataWrite(TextUtils.getAsciiBytes("body2"), 5);
	client.dataEnd();

	client.quit();

	smtpServer.stop(); // wait for the server to catch up
}
 
Example #17
Source File: SimpleMessageListenerAdapter.java    From subethasmtp with Apache License 2.0 4 votes vote down vote up
/** */
public Handler(MessageContext ctx)
{
	this.ctx = ctx;
}
 
Example #18
Source File: SimpleMessageListenerAdapter.java    From subethasmtp with Apache License 2.0 4 votes vote down vote up
public MessageHandler create(MessageContext ctx)
{
	return new Handler(ctx);
}
 
Example #19
Source File: SmarterMessageListenerAdapter.java    From subethasmtp with Apache License 2.0 4 votes vote down vote up
/** */
public Handler(MessageContext ctx)
{
	this.ctx = ctx;
}
 
Example #20
Source File: SmarterMessageListenerAdapter.java    From subethasmtp with Apache License 2.0 4 votes vote down vote up
public MessageHandler create(MessageContext ctx)
{
	return new Handler(ctx);
}
 
Example #21
Source File: SubethaEmailServer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public MessageContext getMessageContext()
{
    return messageContext;
}
 
Example #22
Source File: SubethaEmailServer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Handler(MessageContext messageContext)
{
    this.messageContext = messageContext;
}
 
Example #23
Source File: SubethaEmailServer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public MessageHandler create(MessageContext messageContext)
{
    return new Handler(messageContext);
}
 
Example #24
Source File: ListenSMTP.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private SMTPServer prepareServer(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    final int port = context.getProperty(SMTP_PORT).asInteger();
    final String host = context.getProperty(SMTP_HOSTNAME).getValue();
    final ComponentLog log = getLogger();
    final int maxMessageSize = context.getProperty(SMTP_MAXIMUM_MSG_SIZE).asDataSize(DataUnit.B).intValue();
    //create message handler factory
    final MessageHandlerFactory messageHandlerFactory = (final MessageContext mc) -> {
        return new SmtpConsumer(mc, sessionFactory, port, host, log, maxMessageSize);
    };
    //create smtp server
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final SMTPServer smtpServer = sslContextService == null ? new SMTPServer(messageHandlerFactory) : new SMTPServer(messageHandlerFactory) {
        @Override
        public SSLSocket createSSLSocket(Socket socket) throws IOException {
            InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
            String clientAuth = context.getProperty(CLIENT_AUTH).getValue();
            SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.valueOf(clientAuth));
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            SSLSocket sslSocket = (SSLSocket) (socketFactory.createSocket(socket, remoteAddress.getHostName(), socket.getPort(), true));
            sslSocket.setUseClientMode(false);

            if (SSLContextService.ClientAuth.REQUIRED.toString().equals(clientAuth)) {
                this.setRequireTLS(true);
                sslSocket.setNeedClientAuth(true);
            }
            return sslSocket;
        }
    };
    if (sslContextService != null) {
        smtpServer.setEnableTLS(true);
    } else {
        smtpServer.setHideTLS(true);
    }
    smtpServer.setSoftwareName("Apache NiFi SMTP");
    smtpServer.setPort(port);
    smtpServer.setMaxConnections(context.getProperty(SMTP_MAXIMUM_CONNECTIONS).asInteger());
    smtpServer.setMaxMessageSize(maxMessageSize);
    smtpServer.setConnectionTimeout(context.getProperty(SMTP_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    if (context.getProperty(SMTP_HOSTNAME).isSet()) {
        smtpServer.setHostName(context.getProperty(SMTP_HOSTNAME).getValue());
    }
    return smtpServer;
}
 
Example #25
Source File: SMTPHandlerFactoryTest.java    From holdmail with Apache License 2.0 3 votes vote down vote up
@Test
public void shouldCreateHandler() throws Exception{

    MessageContext ctxMock = mock(MessageContext.class);
    SMTPHandler handlerMock = mock(SMTPHandler.class);
    whenNew(SMTPHandler.class).withArguments(ctxMock).thenReturn(handlerMock);

    SMTPHandler actual = smtpHandlerFactory.create(ctxMock);

    assertThat(actual).isEqualTo(handlerMock);
    verify(beanFactoryMock).autowireBean(handlerMock);



}
 
Example #26
Source File: MailTransaction.java    From mireka with Apache License 2.0 2 votes vote down vote up
/**
 * @x.category GETSET
 */
MessageContext getMessageContext();
 
Example #27
Source File: SMTPHandler.java    From holdmail with Apache License 2.0 2 votes vote down vote up
public SMTPHandler(MessageContext ctx) {

        InetSocketAddress hostAddr = (InetSocketAddress) ctx.getRemoteAddress();
        this.senderHost = hostAddr.getAddress().getHostAddress();

    }