Coverage Report - com.sun.grizzly.utils.NonBlockingSSLIOClient
 
Classes in this File Line Coverage Branch Coverage Complexity
NonBlockingSSLIOClient
0 %
0/38
0 %
0/10
2
 
 1  
 /*
 2  
  * 
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 4  
  * 
 5  
  * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
 6  
  * 
 7  
  * The contents of this file are subject to the terms of either the GNU
 8  
  * General Public License Version 2 only ("GPL") or the Common Development
 9  
  * and Distribution License("CDDL") (collectively, the "License").  You
 10  
  * may not use this file except in compliance with the License. You can obtain
 11  
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 12  
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 13  
  * language governing permissions and limitations under the License.
 14  
  * 
 15  
  * When distributing the software, include this License Header Notice in each
 16  
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 17  
  * Sun designates this particular file as subject to the "Classpath" exception
 18  
  * as provided by Sun in the GPL Version 2 section of the License file that
 19  
  * accompanied this code.  If applicable, add the following below the License
 20  
  * Header, with the fields enclosed by brackets [] replaced by your own
 21  
  * identifying information: "Portions Copyrighted [year]
 22  
  * [name of copyright owner]"
 23  
  * 
 24  
  * Contributor(s):
 25  
  * 
 26  
  * If you wish your version of this file to be governed by only the CDDL or
 27  
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
 28  
  * elects to include this software in this distribution under the [CDDL or GPL
 29  
  * Version 2] license."  If you don't indicate a single choice of license, a
 30  
  * recipient has the option to distribute your version of this file under
 31  
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
 32  
  * its licensees as provided above.  However, if you add GPL Version 2 code
 33  
  * and therefore, elected the GPL Version 2 license, then the option applies
 34  
  * only if the new code is made subject to such option by the copyright
 35  
  * holder.
 36  
  *
 37  
  */
 38  
 
 39  
 package com.sun.grizzly.utils;
 40  
 
 41  
 import com.sun.grizzly.SSLConfig;
 42  
 import com.sun.grizzly.SSLConnectorHandler;
 43  
 import java.io.EOFException;
 44  
 import java.io.IOException;
 45  
 import java.net.InetSocketAddress;
 46  
 import java.nio.ByteBuffer;
 47  
 
 48  
 /**
 49  
  * SSL client that exercise the non blocking ConnectorHandler.
 50  
  *
 51  
  * @author Jeanfrancois Arcand
 52  
  */
 53  
 public class NonBlockingSSLIOClient implements NonBlockingIOClient {
 54  
     private String host;
 55  
     private int port;
 56  
     private SSLConfig sslConfig;
 57  
     
 58  
     private SSLConnectorHandler sslConnectorHandler;
 59  
     
 60  
     private ByteBuffer dataBuffer;
 61  
         
 62  0
     public NonBlockingSSLIOClient(String host, int port, SSLConfig sslConfig) {
 63  0
         this.host = host;
 64  0
         this.port = port;
 65  0
         this.sslConfig = sslConfig;
 66  0
         sslConnectorHandler = new SSLConnectorHandler(sslConfig);
 67  0
         dataBuffer = ByteBuffer.allocate(sslConnectorHandler.getApplicationBufferSize());
 68  0
         dataBuffer.limit(0);
 69  0
     }
 70  
     
 71  
     public void connect() throws IOException {
 72  0
         sslConnectorHandler.connect(new InetSocketAddress(host, port));
 73  0
         sslConnectorHandler.handshake(dataBuffer, true);
 74  0
     }
 75  
     
 76  
     public void send(byte[] msg) throws IOException {
 77  0
         send(msg, true);
 78  0
     }
 79  
 
 80  
     public void send(byte[] msg, boolean needFlush) throws IOException {
 81  0
         sslConnectorHandler.write(ByteBuffer.wrap(msg),true);
 82  0
     }
 83  
     
 84  
     public int receive(byte[] buf) throws IOException {
 85  0
         return receive(buf, buf.length);
 86  
     }
 87  
 
 88  
     public int receive(byte[] buf, int length) throws IOException {
 89  0
         int offset = 0;
 90  0
         if (dataBuffer.hasRemaining()) {
 91  0
             int transferred = copy(dataBuffer, buf, 0, length);
 92  0
             if (transferred == length) {
 93  0
                 return transferred;
 94  
             }
 95  
             
 96  0
             length -= transferred;
 97  0
             offset = transferred;
 98  
         }
 99  
         
 100  0
         dataBuffer.clear();
 101  
         
 102  0
         long bytesRead = sslConnectorHandler.read(dataBuffer, true);
 103  
         
 104  0
         dataBuffer.flip();
 105  
         
 106  0
         if (bytesRead == -1) {
 107  0
             throw new EOFException("Unexpected client EOF!");
 108  
         }
 109  
         
 110  0
         if (dataBuffer.hasRemaining()) {
 111  0
             int transferred = copy(dataBuffer, buf, offset, length);
 112  0
             offset += transferred;
 113  
         }
 114  
         
 115  0
         return offset;
 116  
     }
 117  
     
 118  
     public void close() throws IOException {
 119  0
         sslConnectorHandler.close();
 120  0
     }
 121  
     
 122  
     private static int copy(ByteBuffer src, byte[] dst, int offset, int length) {
 123  0
         int toTransfer = Math.min(src.remaining(), length);
 124  0
         if (toTransfer == 0) return 0;
 125  0
         src.get(dst, offset, toTransfer);
 126  
         
 127  0
         return toTransfer;
 128  
     }
 129  
 
 130  
 }