Example usage for java.net InetSocketAddress getAddress

List of usage examples for java.net InetSocketAddress getAddress

Introduction

In this page you can find the example usage for java.net InetSocketAddress getAddress.

Prototype

public final InetAddress getAddress() 

Source Link

Document

Gets the InetAddress .

Usage

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testNoop() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//w w w  . j  a v  a 2s .  c om
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.noop();
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();
        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testInvalidHelo() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;// ww  w .  j ava2s.c  o  m
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.helo("");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testHeloEnforcement() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;/*from   w  w  w  . ja  v  a 2 s.  c o  m*/
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.setSender(SENDER);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testMailWithoutBrackets() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;/*www  .jav  a2  s.  c om*/
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.helo("localhost");
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.mail("invalid");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.addRecipient(RCPT1);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testRcptWithoutBrackets() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//from   w  ww. ja v  a  2s.  c  om
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.helo("localhost");
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.setSender(SENDER);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.rcpt(RCPT1);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apparatus_templi.Coordinator.java

private static void restartWebServer() throws UnknownHostException {
    sysTray.setStatus(SysTray.Status.WAITING);

    assert webServer != null : "attempting to restart web server without one available";
    Log.w(TAG, "restarting web server");
    InetSocketAddress socket = webServer.getSocket();
    webServer.terminate();//from  w w  w.ja  va  2 s. com
    // give the server a bit of time to finish serving any ongoing connections
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e1) {
    }

    webServer = null;
    // find out if we can reuse the same socket as before. If either the host or port can not be
    // reused, then the socket will have to be discarded and a new one created by the web
    // server. Note that this could trigger a bug in Windows if the user changes the host but
    // keeps the port number the same.
    // TODO check that keeping port same and changing host will trigger the windows bug

    // can the host part be reused?
    boolean reuseSocket = true;

    if ("true".equals(prefs.getPreference(Prefs.Keys.serverBindLocalhost))
            && socket.getAddress().isLoopbackAddress()) {
        // server was loopback and we will now be using localhost
        reuseSocket = false;
    } else if (!"true".equals(prefs.getPreference(Prefs.Keys.serverBindLocalhost))
            && !socket.getAddress().isLoopbackAddress()) {
        // server was localhost and we will now be using loopback
        reuseSocket = false;
    }

    /*-
     * check that the port number should be reused. This provides five possibilities:
     * specified port   -> different specified port (don't reuse)
     * specified port   -> same specified port (reuse)
     * specified port   -> unspecified port (reuse)
     * unspecified port -> unspecified port (reuse)
     * unspecified port -> specified port (reuse if port numbers match)
     */
    if (prefs.getPreference(Prefs.Keys.portNum) != null) {
        // a port number has been set, is it the same port we are already using?
        try {
            int portNum = Integer.parseInt(prefs.getPreference(Prefs.Keys.portNum));
            if (portNum != socket.getPort()) {
                reuseSocket = false;
            }
        } catch (NumberFormatException e) {
            // if the new port is not a valid integer then it will be caught when starting the
            // web server
            reuseSocket = false;
        }

    } else {
        // if the port number is unspecified then we don't need to check for anything, all cases
        // lead to reuse

    }

    // if the socket should be reused pass it to the new web server, otherwise tell it to
    // generate a new one
    if (reuseSocket) {
        Log.d(TAG, "reusing socket in new web server");
        startWebServer(socket);
    } else {
        Log.d(TAG, "requesting web server generate new socket");
        startWebServer(null);
    }
    String newLocation = webServer.getProtocol() + webServer.getServerLocation() + ":" + webServer.getPort()
            + "/index.html";
    StringBuilder newAddress = new StringBuilder();
    newAddress.append(
            "Web server restarted, available at: <a href='" + newLocation + "'>" + newLocation + "</a>");
    sendNotificationEmail(newAddress.toString());
}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testHeloHookPermanentError() throws Exception {
    HeloHook hook = new HeloHook() {

        @Override/*www.j av a2 s .  c om*/
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public HookResult doHelo(SMTPSession session, String helo) {
            return new HookResult(HookReturnCode.DENY);
        }
    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.helo("localhost");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testHeloHookTempraryError() throws Exception {
    HeloHook hook = new HeloHook() {

        @Override/*  w  ww .  j  a  v  a 2s .  co  m*/
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public HookResult doHelo(SMTPSession session, String helo) {
            return new HookResult(HookReturnCode.DENYSOFT);
        }
    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.helo("localhost");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:com.datatorrent.stram.StreamingAppMasterService.java

private Token<StramDelegationTokenIdentifier> allocateDelegationToken(String username,
        InetSocketAddress address) {
    StramDelegationTokenIdentifier identifier = new StramDelegationTokenIdentifier(new Text(username),
            new Text(""), new Text(""));
    String service = address.getAddress().getHostAddress() + ":" + address.getPort();
    Token<StramDelegationTokenIdentifier> stramToken = new Token<StramDelegationTokenIdentifier>(identifier,
            delegationTokenManager);//from w w  w . j av  a 2  s .c  o m
    stramToken.setService(new Text(service));
    return stramToken;
}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testSimpleDelivery() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//w w w.  j av a 2 s .  c  o m
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.helo("localhost");
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.setSender(SENDER);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.addRecipient(RCPT1);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.addRecipient(RCPT2);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        assertTrue(client.sendShortMessageData(MSG1));
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertTrue(queued.hasNext());

        MailEnvelope env = queued.next();
        checkEnvelope(env, SENDER, Arrays.asList(RCPT1, RCPT2), MSG1);
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}