Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.commons.net.pop3;
 
 
 
 import  org.apache.commons.net.util.SSLContextUtils;

POP3 over SSL processing. Copied from FTPSClient.java and modified to suit POP3. If implicit mode is selected (NOT the default), SSL/TLS negotiation starts right after the connection has been established. In explicit mode (the default), SSL/TLS negotiation starts when the user calls execTLS() and the server accepts the command. Implicit usage: POP3SClient c = new POP3SClient(true); c.connect("127.0.0.1", 995); Explicit usage: POP3SClient c = new POP3SClient(); c.connect("127.0.0.1", 110); if (c.execTLS()) { /rest of the commands here/ }

Since:
3.0
 
 public class POP3SClient extends POP3Client
 {
     // from http://www.iana.org/assignments/port-numbers
 
     //    pop3s           995/tcp    pop3 protocol over TLS/SSL (was spop3)
     //    pop3s           995/udp    pop3 protocol over TLS/SSL (was spop3)
 
     private static final int DEFAULT_POP3S_PORT = 995;

    
Default secure socket protocol name, like TLS
 
     private static final String DEFAULT_PROTOCOL = "TLS";

    
The security mode. True - Implicit Mode / False - Explicit Mode.
 
     private final boolean isImplicit;
    
The secure socket protocol to be used, like SSL/TLS.
 
     private final String protocol;
    
The context object.
 
     private SSLContext context = null;
    
The cipher suites. SSLSockets have a default set of these anyway, so no initialization required.
 
     private String[] suites = null;
    
The protocol versions.
 
     private String[] protocols = //null;
         null;//{"SSLv2", "SSLv3", "TLSv1", "TLSv1.1", "SSLv2Hello"};
 
    
The FTPS TrustManager implementation, default null.
 
     private TrustManager trustManager = null;

    
The KeyManager, default null.
 
     private KeyManager keyManager = null;

    
Constructor for POP3SClient, using DEFAULT_PROTOCOL i.e. TLS Sets security mode to explicit.
 
     public POP3SClient()
     {
         this(false);
     }

    
Constructor for POP3SClient, using DEFAULT_PROTOCOL i.e. TLS

Parameters:
implicit The security mode, true for implicit, false for explicit
 
     public POP3SClient(boolean implicit)
     {
         this(implicit);
     }

    
Constructor for POP3SClient. Sets security mode to explicit.

Parameters:
proto the protocol.
    public POP3SClient(String proto)
    {
        this(protofalse);
    }

    
Constructor for POP3SClient.

Parameters:
proto the protocol.
implicit The security mode, true for implicit, false for explicit
    public POP3SClient(String protoboolean implicit)
    {
        this(protoimplicitnull);
    }

    
Constructor for POP3SClient. Sets the default port to DEFAULT_POP3S_PORT - 995 - if using implicit mode

Parameters:
proto the protocol.
implicit The security mode, true for implicit, false for explicit
ctx the context to be used
    public POP3SClient(String protoboolean implicitSSLContext ctx)
    {
        super();
         = proto;
         = implicit;
         = ctx;
        if () {
            setDefaultPort();
        }
    }

    
Constructor for POP3SClient, using DEFAULT_PROTOCOL i.e. TLS

Parameters:
implicit The security mode, true for implicit, false for explicit
ctx A pre-configured SSL Context.
    public POP3SClient(boolean implicitSSLContext ctx)
    {
        this(implicitctx);
    }

    
Constructor for POP3SClient, using DEFAULT_PROTOCOL - TLS - and isImplicit = false

Parameters:
context A pre-configured SSL Context.
See also:
POP3SClient(boolean, SSLContext)
    public POP3SClient(SSLContext context)
    {
        this(falsecontext);
    }

    
Because there are so many connect() methods, the _connectAction_() method is provided as a means of performing some action immediately after establishing a connection, rather than reimplementing all of the connect() methods.

Throws:
IOException If it is thrown by _connectAction_().
See also:
org.apache.commons.net.SocketClient._connectAction_()
    @Override
    protected void _connectAction_() throws IOException
    {
        // Implicit mode.
        if () {
            performSSLNegotiation();
        }
        super._connectAction_();
        // Explicit mode - don't do anything. The user calls execTLS()
    }

    
Performs a lazy init of the SSL context.

Throws:
IOException When could not initialize the SSL context.
    private void initSSLContext() throws IOException
    {
        if ( == null)
        {
             = SSLContextUtils.createSSLContext(getKeyManager(), getTrustManager());
        }
    }

    
SSL/TLS negotiation. Acquires an SSL socket of a connection and carries out handshake processing.

Throws:
IOException If server negotiation fails.
    private void performSSLNegotiation() throws IOException
    {
        initSSLContext();
        SSLSocketFactory ssf = .getSocketFactory();
        String ip = getRemoteAddress().getHostAddress();
        int port = getRemotePort();
        SSLSocket socket =
            (SSLSocketssf.createSocket(ipporttrue);
        socket.setEnableSessionCreation(true);
        socket.setUseClientMode(true);
        if ( != null) {
            socket.setEnabledProtocols();
        }
        if ( != null) {
            socket.setEnabledCipherSuites();
        }
        socket.startHandshake();
         = socket;
         = socket.getInputStream();
         = socket.getOutputStream();
    }

    
Get the KeyManager instance.

Returns:
The current KeyManager instance.
    private KeyManager getKeyManager()
    {
        return ;
    }

    
Set a KeyManager to use.

Parameters:
newKeyManager The KeyManager implementation to set.
See also:
org.apache.commons.net.util.KeyManagerUtils
    public void setKeyManager(KeyManager newKeyManager)
    {
         = newKeyManager;
    }

    
Controls which particular cipher suites are enabled for use on this connection. Called before server negotiation.

Parameters:
cipherSuites The cipher suites.
    public void setEnabledCipherSuites(String[] cipherSuites)
    {
         = new String[cipherSuites.length];
        System.arraycopy(cipherSuites, 0, , 0, cipherSuites.length);
    }

    
Returns the names of the cipher suites which could be enabled for use on this connection. When the underlying Socket is not an SSLSocket instance, returns null.

Returns:
An array of cipher suite names, or null.
    public String[] getEnabledCipherSuites()
    {
        if ( instanceof SSLSocket)
        {
            return ((SSLSocket)).getEnabledCipherSuites();
        }
        return null;
    }

    
Controls which particular protocol versions are enabled for use on this connection. I perform setting before a server negotiation.

Parameters:
protocolVersions The protocol versions.
    public void setEnabledProtocols(String[] protocolVersions)
    {
         = new String[protocolVersions.length];
        System.arraycopy(protocolVersions, 0, , 0, protocolVersions.length);
    }

    
Returns the names of the protocol versions which are currently enabled for use on this connection. When the underlying Socket is not an SSLSocket instance, returns null.

Returns:
An array of protocols, or null.
    public String[] getEnabledProtocols()
    {
        if ( instanceof SSLSocket)
        {
            return ((SSLSocket)).getEnabledProtocols();
        }
        return null;
    }

    
The TLS command execution.

Returns:
TRUE if the command and negotiation succeeded.
Throws:
SSLException If the server reply code is not positive.
IOException If an I/O error occurs while sending the command or performing the negotiation.
    public boolean execTLS() throws SSLExceptionIOException
    {
        if (sendCommand("STLS") != .)
        {
            return false;
            //throw new SSLException(getReplyString());
        }
        performSSLNegotiation();
        return true;
    }

    
Get the currently configured TrustManager.

Returns:
A TrustManager instance.
    public TrustManager getTrustManager()
    {
        return ;
    }

    
Override the default TrustManager to use.

Parameters:
newTrustManager The TrustManager implementation to set.
See also:
org.apache.commons.net.util.TrustManagerUtils
    public void setTrustManager(TrustManager newTrustManager)
    {
         = newTrustManager;
    }
/* kate: indent-width 4; replace-tabs on; */
New to GrepCode? Check out our FAQ X