SSLSocketWrapper.java revision 7329fa972d9c20777444e5e1b13169d700de6567
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.xnet.provider.jsse;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.Socket;
23import java.net.SocketAddress;
24import java.net.SocketException;
25
26/**
27 * This class wraps the SSL fuctionality over existing conneted socket.
28 */
29public class SSLSocketWrapper extends SSLSocketImpl {
30
31    private final Socket socket;
32    private final boolean autoClose;
33
34    protected SSLSocketWrapper(Socket socket, boolean autoClose, SSLParameters sslParameters) throws IOException {
35        super(sslParameters);
36        if (!socket.isConnected()) {
37            throw new SocketException("Socket is not connected.");
38        }
39        this.socket = socket;
40        this.autoClose = autoClose;
41        init();
42    }
43
44    @Override
45    protected void initTransportLayer() throws IOException {
46        input = socket.getInputStream();
47        output = socket.getOutputStream();
48    }
49
50    @Override
51    protected void closeTransportLayer() throws IOException {
52        if (autoClose && (input != null)) {
53            socket.close();
54            input.close();
55            output.close();
56        }
57    }
58
59    // ------------------- Wrapping method implementations ---------------
60
61    @Override
62    public void connect(SocketAddress sockaddr, int timeout)
63        throws IOException {
64        throw new IOException("Underlying socket is already connected.");
65    }
66
67    @Override
68    public void connect(SocketAddress sockaddr) throws IOException {
69        throw new IOException("Underlying socket is already connected.");
70    }
71
72    @Override
73    public void bind(SocketAddress sockaddr) throws IOException {
74        throw new IOException("Underlying socket is already connected.");
75    }
76
77    @Override
78    public SocketAddress getRemoteSocketAddress() {
79        return socket.getRemoteSocketAddress();
80    }
81
82    @Override
83    public SocketAddress getLocalSocketAddress() {
84        return socket.getLocalSocketAddress();
85    }
86
87    @Override
88    public InetAddress getLocalAddress() {
89        return socket.getLocalAddress();
90    }
91
92    @Override
93    public InetAddress getInetAddress() {
94        return socket.getInetAddress();
95    }
96
97    @Override
98    public String toString() {
99        return "SSL socket over " + socket.toString();
100    }
101
102    @Override
103    public void setSoLinger(boolean on, int linger) throws SocketException {
104        socket.setSoLinger(on, linger);
105    }
106
107    @Override
108    public void setTcpNoDelay(boolean on) throws SocketException {
109        socket.setTcpNoDelay(on);
110    }
111
112    @Override
113    public void setReuseAddress(boolean on) throws SocketException {
114        socket.setReuseAddress(on);
115    }
116
117    @Override
118    public void setKeepAlive(boolean on) throws SocketException {
119        socket.setKeepAlive(on);
120    }
121
122    @Override
123    public void setTrafficClass(int tos) throws SocketException {
124        socket.setTrafficClass(tos);
125    }
126
127    @Override
128    public void setSoTimeout(int to) throws SocketException {
129        socket.setSoTimeout(to);
130    }
131
132    @Override
133    public void setSendBufferSize(int size) throws SocketException {
134        socket.setSendBufferSize(size);
135    }
136
137    @Override
138    public void setReceiveBufferSize(int size) throws SocketException {
139        socket.setReceiveBufferSize(size);
140    }
141
142    @Override
143    public boolean getTcpNoDelay() throws SocketException {
144        return socket.getTcpNoDelay();
145    }
146
147    @Override
148    public boolean getReuseAddress() throws SocketException {
149        return socket.getReuseAddress();
150    }
151
152    @Override
153    public boolean getOOBInline() throws SocketException {
154        return socket.getOOBInline();
155    }
156
157    @Override
158    public boolean getKeepAlive() throws SocketException {
159        return socket.getKeepAlive();
160    }
161
162    @Override
163    public int getTrafficClass() throws SocketException {
164        return socket.getTrafficClass();
165    }
166
167    @Override
168    public int getSoTimeout() throws SocketException {
169        return socket.getSoTimeout();
170    }
171
172    @Override
173    public int getSoLinger() throws SocketException {
174        return socket.getSoLinger();
175    }
176
177    @Override
178    public int getSendBufferSize() throws SocketException {
179        return socket.getSendBufferSize();
180    }
181
182    @Override
183    public int getReceiveBufferSize() throws SocketException {
184        return socket.getReceiveBufferSize();
185    }
186
187    @Override
188    public boolean isConnected() {
189        return socket.isConnected();
190    }
191
192    @Override
193    public boolean isClosed() {
194        return socket.isClosed();
195    }
196
197    @Override
198    public boolean isBound() {
199        return socket.isBound();
200    }
201
202    @Override
203    public boolean isOutputShutdown() {
204        return socket.isOutputShutdown();
205    }
206
207    @Override
208    public boolean isInputShutdown() {
209        return socket.isInputShutdown();
210    }
211
212    @Override
213    public int getPort() {
214        return socket.getPort();
215    }
216
217    @Override
218    public int getLocalPort() {
219        return socket.getLocalPort();
220    }
221
222    // -------------------------------------------------------------------
223
224}
225
226