##### Coverage Report - com.sun.grizzly.util.SSLOutputWriter

Classes in this File | Line Coverage | Branch Coverage | Complexity
SSLOutputWriter |
82 % | 14/17
62 % | 5/8
| 3.5;3,5
SSLOutputWriter$1 |
100 % | 1/1
N/A | N/A
| 3.5;3,5

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.util;
40 |  |
41 |  |
     import java.io.IOException;
42 |  |
     import java.nio.ByteBuffer;
43 |  |
     import java.nio.channels.SelectableChannel;
44 |  |
     import javax.net.ssl.SSLEngine;
45 |  |
     import javax.net.ssl.SSLEngineResult;
46 |  |
     import javax.net.ssl.SSLEngineResult.HandshakeStatus;
47 |  |
48 |  |
     /**
49 |  |
      * SSL over NIO utility to encrypt {@link ByteBuffer} and flush them.
50 |  |
      * All the SSLEngine operations are delegated to class {@link SSLUtils}
51 |  |
      *
52 |  |
      * @author Jeanfrancois Arcand
53 |  |
      */
54 | 0 |
     public final class SSLOutputWriter{
55 |  |
56 |  |
         /**
57 |  |
          * Encrypt the response and flush it using {@link OutputWriter}
58 |  |
          * @param channel  {@link SelectableChannel} to flush
59 |  |
          * @param bb  {@link ByteBuffer}
60 |  |
          * @return  number of bytes written
61 |  |
          * @throws java.io.IOException
62 |  |
          */
63 |  |
         public static long flushChannel(SelectableChannel channel, ByteBuffer bb)
64 |  |
                 throws IOException{
65 |  |
66 | 101 |
             WorkerThread workerThread = (WorkerThread)Thread.currentThread();
67 | 101 |
             SSLEngine sslEngine = workerThread.getSSLEngine();
68 | 101 |
             ByteBuffer outputBB = workerThread.getOutputBB();
69 | 101 |
             return flushChannel(channel,bb,outputBB,sslEngine);
70 |  |
         }
71 |  |
72 |  |
73 |  |
         /**
74 |  |
          * Encrypt the response and flush it using {@link OutputWriter}
75 |  |
          * @param channel  {@link SelectableChannel} to flush
76 |  |
          * @param bb  input {@link ByteBuffer}
77 |  |
          * @param outputBB  output {@link ByteBuffer}
78 |  |
          * @param sslEngine {@link SSLEngine}
79 |  |
          * @return  number of bytes written
80 |  |
          * @throws java.io.IOException
81 |  |
          */
82 |  |
         public static long flushChannel(SelectableChannel channel, ByteBuffer bb,
83 |  |
                 ByteBuffer outputBB, SSLEngine sslEngine) throws IOException{
84 |  |
85 | 201 |
             long nWrite = 0;
86 | 402 |
             while (bb.hasRemaining()) {
87 | 201 |
                 SSLEngineResult result = SSLUtils.wrap(bb,outputBB,sslEngine);
88 | 201 |
                 switch (result.getStatus()) {
89 |  |
                     case OK:
90 | 201 |
                         if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
91 | 0 |
                             SSLUtils.executeDelegatedTask(sslEngine);
92 |  |
                         }
93 |  |
                         break;
94 |  |
95 |  |
                     default:
96 | 0 |
                         throw new IOException("SSLOutputWriter: " + result.getStatus());
97 |  |
                 }
98 |  |
99 | 201 |
                 if (outputBB.hasRemaining()) {
100 | 201 |
                     nWrite += OutputWriter.flushChannel(channel,outputBB);
101 |  |
                 }
102 | 201 |
             }
103 | 201 |
             outputBB.clear();
104 | 201 |
             return nWrite;
105 |  |
         }
106 |  |
     }