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.FileDescriptor;
21import java.io.IOException;
22import java.net.InetAddress;
23import java.net.Socket;
24import java.net.SocketAddress;
25import java.net.SocketException;
26
27/**
28 * This class wraps the SSL fuctionality over existing conneted socket.
29 */
30public class SSLSocketWrapper extends SSLSocketImpl {
31
32    private final Socket socket;
33    private final boolean autoClose;
34
35    protected SSLSocketWrapper(Socket socket, String host, int port, boolean autoClose,
36                               SSLParametersImpl sslParameters) throws IOException {
37        super(host, port, sslParameters);
38        if (!socket.isConnected()) {
39            throw new SocketException("Socket is not connected.");
40        }
41        this.socket = socket;
42        this.autoClose = autoClose;
43        init();
44    }
45
46    @Override
47    protected void initTransportLayer() throws IOException {
48        input = socket.getInputStream();
49        output = socket.getOutputStream();
50    }
51
52    @Override
53    protected void closeTransportLayer() throws IOException {
54        if (autoClose && (input != null)) {
55            socket.close();
56            input.close();
57            output.close();
58        }
59    }
60
61    // ------------------- Wrapping method implementations ---------------
62
63    @Override
64    public void connect(SocketAddress sockaddr, int timeout)
65        throws IOException {
66        throw new IOException("Underlying socket is already connected.");
67    }
68
69    @Override
70    public void connect(SocketAddress sockaddr) throws IOException {
71        throw new IOException("Underlying socket is already connected.");
72    }
73
74    @Override
75    public void bind(SocketAddress sockaddr) throws IOException {
76        throw new IOException("Underlying socket is already connected.");
77    }
78
79    @Override
80    public SocketAddress getRemoteSocketAddress() {
81        return socket.getRemoteSocketAddress();
82    }
83
84    @Override
85    public SocketAddress getLocalSocketAddress() {
86        return socket.getLocalSocketAddress();
87    }
88
89    @Override
90    public InetAddress getLocalAddress() {
91        return socket.getLocalAddress();
92    }
93
94    @Override
95    public InetAddress getInetAddress() {
96        return socket.getInetAddress();
97    }
98
99    @Override
100    public String toString() {
101        return "SSL socket over " + socket.toString();
102    }
103
104    @Override
105    public void setSoLinger(boolean on, int linger) throws SocketException {
106        socket.setSoLinger(on, linger);
107    }
108
109    @Override
110    public void setTcpNoDelay(boolean on) throws SocketException {
111        socket.setTcpNoDelay(on);
112    }
113
114    @Override
115    public void setReuseAddress(boolean on) throws SocketException {
116        socket.setReuseAddress(on);
117    }
118
119    @Override
120    public void setKeepAlive(boolean on) throws SocketException {
121        socket.setKeepAlive(on);
122    }
123
124    @Override
125    public void setTrafficClass(int tos) throws SocketException {
126        socket.setTrafficClass(tos);
127    }
128
129    @Override
130    public void setSoTimeout(int to) throws SocketException {
131        socket.setSoTimeout(to);
132    }
133
134    @Override
135    public void setSendBufferSize(int size) throws SocketException {
136        socket.setSendBufferSize(size);
137    }
138
139    @Override
140    public void setReceiveBufferSize(int size) throws SocketException {
141        socket.setReceiveBufferSize(size);
142    }
143
144    @Override
145    public boolean getTcpNoDelay() throws SocketException {
146        return socket.getTcpNoDelay();
147    }
148
149    @Override
150    public boolean getReuseAddress() throws SocketException {
151        return socket.getReuseAddress();
152    }
153
154    @Override
155    public boolean getOOBInline() throws SocketException {
156        return socket.getOOBInline();
157    }
158
159    @Override
160    public boolean getKeepAlive() throws SocketException {
161        return socket.getKeepAlive();
162    }
163
164    @Override
165    public int getTrafficClass() throws SocketException {
166        return socket.getTrafficClass();
167    }
168
169    @Override
170    public int getSoTimeout() throws SocketException {
171        return socket.getSoTimeout();
172    }
173
174    @Override
175    public int getSoLinger() throws SocketException {
176        return socket.getSoLinger();
177    }
178
179    @Override
180    public int getSendBufferSize() throws SocketException {
181        return socket.getSendBufferSize();
182    }
183
184    @Override
185    public int getReceiveBufferSize() throws SocketException {
186        return socket.getReceiveBufferSize();
187    }
188
189    @Override
190    public boolean isConnected() {
191        return socket.isConnected();
192    }
193
194    @Override
195    public boolean isClosed() {
196        return socket.isClosed();
197    }
198
199    @Override
200    public boolean isBound() {
201        return socket.isBound();
202    }
203
204    @Override
205    public boolean isOutputShutdown() {
206        return socket.isOutputShutdown();
207    }
208
209    @Override
210    public boolean isInputShutdown() {
211        return socket.isInputShutdown();
212    }
213
214    @Override
215    public int getPort() {
216        return socket.getPort();
217    }
218
219    @Override
220    public int getLocalPort() {
221        return socket.getLocalPort();
222    }
223
224    @Override
225    public FileDescriptor getFileDescriptor$() {
226        return socket.getFileDescriptor$();
227    }
228
229    // -------------------------------------------------------------------
230
231}
232
233