< prev index next >
1 /*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 */
24 package java.net.http;
25
26 import java.io.IOException;
27 import java.net.InetSocketAddress;
28 import java.nio.ByteBuffer;
29 import java.nio.channels.SocketChannel;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.function.Consumer;
32
33 /**
34 * Asynchronous version of SSLConnection.
35 */
36 class AsyncSSLConnection extends HttpConnection implements AsyncConnection {
37 final AsyncSSLDelegate sslDelegate;
38 final PlainHttpConnection delegate;
39
40 AsyncSSLConnection(InetSocketAddress addr, HttpClientImpl client, String[] ap) {
41 super(addr, client);
42 delegate = new PlainHttpConnection(addr, client);
43 sslDelegate = new AsyncSSLDelegate(delegate, client, ap);
44 }
45
46 @Override
47 public void connect() throws IOException, InterruptedException {
48 delegate.connect();
49 }
50
51 @Override
52 public CompletableFuture<Void> connectAsync() {
53 return delegate.connectAsync();
54 }
55
56 @Override
57 boolean connected() {
58 return delegate.connected();
59 }
60
61 @Override
62 boolean isSecure() {
63 return true;
64 }
65
66 @Override
67 boolean isProxied() {
68 return false;
69 }
70
71 @Override
72 SocketChannel channel() {
73 return delegate.channel();
74 }
75
76 @Override
77 ConnectionPool.CacheKey cacheKey() {
78 return ConnectionPool.cacheKey(address, null);
79 }
80
81 @Override
82 synchronized long write(ByteBuffer[] buffers, int start, int number) throws IOException {
83 ByteBuffer[] bufs = Utils.reduce(buffers, start, number);
84 long n = Utils.remaining(bufs);
85 sslDelegate.write(bufs);
86 return n;
87 }
88
89 @Override
90 long write(ByteBuffer buffer) throws IOException {
91 long n = buffer.remaining();
92 sslDelegate.write(buffer);
93 return n;
94 }
95
96 @Override
97 public void close() {
98 Utils.close(sslDelegate, delegate.channel());
99 }
100
101 @Override
102 public void setAsyncCallbacks(Consumer<ByteBuffer> asyncReceiver, Consumer<Throwable> errorReceiver) {
103 sslDelegate.setAsyncCallbacks(asyncReceiver, errorReceiver);
104 delegate.setAsyncCallbacks(sslDelegate::lowerRead, errorReceiver);
105 }
106
107 // Blocking read functions not used here
108
109 @Override
110 protected ByteBuffer readImpl(int length) throws IOException {
111 throw new UnsupportedOperationException("Not supported.");
112 }
113
114 @Override
115 protected int readImpl(ByteBuffer buffer) throws IOException {
116 throw new UnsupportedOperationException("Not supported.");
117 }
118
119 @Override
120 CompletableFuture<Void> whenReceivingResponse() {
121 throw new UnsupportedOperationException("Not supported.");
122 }
123
124 @Override
125 public void startReading() {
126 delegate.startReading();
127 sslDelegate.startReading();
128 }
129 }
< prev index next >