001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.util;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.net.InetAddress;
025: import java.net.SocketAddress;
026: import java.net.SocketException;
027: import java.nio.channels.SocketChannel;
028:
029: import javax.net.ssl.HandshakeCompletedListener;
030: import javax.net.ssl.SSLSession;
031: import javax.net.ssl.SSLSocket;
032:
033: /**
034: * "Slow" SSLsocket implementation to emulate dial-up modems etc
035: *
036: * WARNING: the class relies on overriding all superclass methods in order to apply them to the input socket.
037: * Any missing methods will access the superclass socket, which will probably be in the wrong state.
038: *
039: */
040: public class SlowSSLSocket extends SSLSocket {
041:
042: private final int CPS; // Characters per second to emulate
043:
044: private final SSLSocket sslSock; // Save the actual socket
045:
046: // Ensure we can't be called without suitable parameters
047: private SlowSSLSocket() {
048: CPS = 0;
049: throw new IllegalArgumentException("No such constructor");
050: }
051:
052: /**
053: * Wrap an SSLSocket with slow input and output streams
054: * @param sock SSLSocket to be wrapped
055: * @param cps characters per second to emulate
056: */
057: public SlowSSLSocket(final SSLSocket sock, final int cps) {
058: if (cps <= 0) {
059: throw new IllegalArgumentException("Speed (cps) <= 0");
060: }
061: sslSock = sock;
062: CPS = cps;
063: }
064:
065: // Override so we can intercept the stream
066: public OutputStream getOutputStream() throws IOException {
067: return new SlowOutputStream(sslSock.getOutputStream(), CPS);
068: }
069:
070: // Override so we can intercept the stream
071: public InputStream getInputStream() throws IOException {
072: return new SlowInputStream(sslSock.getInputStream(), CPS);
073: }
074:
075: // Forward all the SSLSocket methods to the input socket
076:
077: public void addHandshakeCompletedListener(
078: HandshakeCompletedListener arg0) {
079: sslSock.addHandshakeCompletedListener(arg0);
080: }
081:
082: public boolean getEnableSessionCreation() {
083: return sslSock.getEnableSessionCreation();
084: }
085:
086: public String[] getEnabledCipherSuites() {
087: return sslSock.getEnabledCipherSuites();
088: }
089:
090: public String[] getEnabledProtocols() {
091: return sslSock.getEnabledProtocols();
092: }
093:
094: public boolean getNeedClientAuth() {
095: return sslSock.getNeedClientAuth();
096: }
097:
098: public SSLSession getSession() {
099: return sslSock.getSession();
100: }
101:
102: public String[] getSupportedCipherSuites() {
103: return sslSock.getSupportedCipherSuites();
104: }
105:
106: public String[] getSupportedProtocols() {
107: return sslSock.getSupportedProtocols();
108: }
109:
110: public boolean getUseClientMode() {
111: return sslSock.getUseClientMode();
112: }
113:
114: public boolean getWantClientAuth() {
115: return sslSock.getWantClientAuth();
116: }
117:
118: public void removeHandshakeCompletedListener(
119: HandshakeCompletedListener arg0) {
120: sslSock.removeHandshakeCompletedListener(arg0);
121: }
122:
123: public void setEnableSessionCreation(boolean arg0) {
124: sslSock.setEnableSessionCreation(arg0);
125: }
126:
127: public void setEnabledCipherSuites(String[] arg0) {
128: sslSock.setEnabledCipherSuites(arg0);
129: }
130:
131: public void setEnabledProtocols(String[] arg0) {
132: sslSock.setEnabledProtocols(arg0);
133: }
134:
135: public void setNeedClientAuth(boolean arg0) {
136: sslSock.setNeedClientAuth(arg0);
137: }
138:
139: public void setUseClientMode(boolean arg0) {
140: sslSock.setUseClientMode(arg0);
141: }
142:
143: public void setWantClientAuth(boolean arg0) {
144: sslSock.setWantClientAuth(arg0);
145: }
146:
147: public void startHandshake() throws IOException {
148: sslSock.startHandshake();
149: }
150:
151: // Also forward all the Socket methods.
152:
153: public void bind(SocketAddress bindpoint) throws IOException {
154: sslSock.bind(bindpoint);
155: }
156:
157: public synchronized void close() throws IOException {
158: sslSock.close();
159: }
160:
161: public void connect(SocketAddress endpoint, int timeout)
162: throws IOException {
163: sslSock.connect(endpoint, timeout);
164: }
165:
166: public void connect(SocketAddress endpoint) throws IOException {
167: sslSock.connect(endpoint);
168: }
169:
170: public SocketChannel getChannel() {
171: return sslSock.getChannel();
172: }
173:
174: public InetAddress getInetAddress() {
175: return sslSock.getInetAddress();
176: }
177:
178: public boolean getKeepAlive() throws SocketException {
179: return sslSock.getKeepAlive();
180: }
181:
182: public InetAddress getLocalAddress() {
183: return sslSock.getLocalAddress();
184: }
185:
186: public int getLocalPort() {
187: return sslSock.getLocalPort();
188: }
189:
190: public SocketAddress getLocalSocketAddress() {
191: return sslSock.getLocalSocketAddress();
192: }
193:
194: public boolean getOOBInline() throws SocketException {
195: return sslSock.getOOBInline();
196: }
197:
198: public int getPort() {
199: return sslSock.getPort();
200: }
201:
202: public synchronized int getReceiveBufferSize()
203: throws SocketException {
204: return sslSock.getReceiveBufferSize();
205: }
206:
207: public SocketAddress getRemoteSocketAddress() {
208: return sslSock.getRemoteSocketAddress();
209: }
210:
211: public boolean getReuseAddress() throws SocketException {
212: return sslSock.getReuseAddress();
213: }
214:
215: public synchronized int getSendBufferSize() throws SocketException {
216: return sslSock.getSendBufferSize();
217: }
218:
219: public int getSoLinger() throws SocketException {
220: return sslSock.getSoLinger();
221: }
222:
223: public synchronized int getSoTimeout() throws SocketException {
224: return sslSock.getSoTimeout();
225: }
226:
227: public boolean getTcpNoDelay() throws SocketException {
228: return sslSock.getTcpNoDelay();
229: }
230:
231: public int getTrafficClass() throws SocketException {
232: return sslSock.getTrafficClass();
233: }
234:
235: public boolean isBound() {
236: return sslSock.isBound();
237: }
238:
239: public boolean isClosed() {
240: return sslSock.isClosed();
241: }
242:
243: public boolean isConnected() {
244: return sslSock.isConnected();
245: }
246:
247: public boolean isInputShutdown() {
248: return sslSock.isInputShutdown();
249: }
250:
251: public boolean isOutputShutdown() {
252: return sslSock.isOutputShutdown();
253: }
254:
255: public void sendUrgentData(int data) throws IOException {
256: sslSock.sendUrgentData(data);
257: }
258:
259: public void setKeepAlive(boolean on) throws SocketException {
260: sslSock.setKeepAlive(on);
261: }
262:
263: public void setOOBInline(boolean on) throws SocketException {
264: sslSock.setOOBInline(on);
265: }
266:
267: public synchronized void setReceiveBufferSize(int size)
268: throws SocketException {
269: sslSock.setReceiveBufferSize(size);
270: }
271:
272: public void setReuseAddress(boolean on) throws SocketException {
273: sslSock.setReuseAddress(on);
274: }
275:
276: public synchronized void setSendBufferSize(int size)
277: throws SocketException {
278: sslSock.setSendBufferSize(size);
279: }
280:
281: public void setSoLinger(boolean on, int linger)
282: throws SocketException {
283: sslSock.setSoLinger(on, linger);
284: }
285:
286: public synchronized void setSoTimeout(int timeout)
287: throws SocketException {
288: sslSock.setSoTimeout(timeout);
289: }
290:
291: public void setTcpNoDelay(boolean on) throws SocketException {
292: sslSock.setTcpNoDelay(on);
293: }
294:
295: public void setTrafficClass(int tc) throws SocketException {
296: sslSock.setTrafficClass(tc);
297: }
298:
299: public void shutdownInput() throws IOException {
300: sslSock.shutdownInput();
301: }
302:
303: public void shutdownOutput() throws IOException {
304: sslSock.shutdownOutput();
305: }
306:
307: public String toString() {
308: return sslSock.toString();
309: }
310: }
|