/* * This file is part of the OWASP Proxy, a free intercepting proxy library. * Copyright (C) 2008-2010 Rogan Dawes <rogan@dawes.za.net> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to: * The Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ package org.owasp.proxy.io; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PushbackInputStream; import java.net.InetAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketException; import java.nio.channels.SocketChannel; /** * This class allows substitution of the {@link Socket}'s {@link InputStream} and {@link OutputStream}. It can be useful * for e.g. allowing identification of data from the socket, using a {@link PushbackInputStream}, and passing the data * to an appropriate handler based on the first few bytes read. * * @author rogan * */ public class SocketWrapper extends Socket { private Socket socket; private InputStream is; private OutputStream os; public SocketWrapper(Socket socket, InputStream is, OutputStream os) { this.socket = socket; this.is = is; this.os = os; } @Override public void bind(SocketAddress bindpoint) throws IOException { socket.bind(bindpoint); } @Override public synchronized void close() throws IOException { socket.close(); } @Override public void connect(SocketAddress endpoint, int timeout) throws IOException { socket.connect(endpoint, timeout); } @Override public void connect(SocketAddress endpoint) throws IOException { socket.connect(endpoint); } @Override public SocketChannel getChannel() { return socket.getChannel(); } @Override public InetAddress getInetAddress() { return socket.getInetAddress(); } @Override public InputStream getInputStream() { return is; } @Override public boolean getKeepAlive() throws SocketException { return socket.getKeepAlive(); } @Override public InetAddress getLocalAddress() { return socket.getLocalAddress(); } @Override public int getLocalPort() { return socket.getLocalPort(); } @Override public SocketAddress getLocalSocketAddress() { return socket.getLocalSocketAddress(); } @Override public boolean getOOBInline() throws SocketException { return socket.getOOBInline(); } @Override public OutputStream getOutputStream() { return os; } @Override public int getPort() { return socket.getPort(); } @Override public synchronized int getReceiveBufferSize() throws SocketException { return socket.getReceiveBufferSize(); } @Override public SocketAddress getRemoteSocketAddress() { return socket.getRemoteSocketAddress(); } @Override public boolean getReuseAddress() throws SocketException { return socket.getReuseAddress(); } @Override public synchronized int getSendBufferSize() throws SocketException { return socket.getSendBufferSize(); } @Override public int getSoLinger() throws SocketException { return socket.getSoLinger(); } @Override public synchronized int getSoTimeout() throws SocketException { return socket.getSoTimeout(); } @Override public boolean getTcpNoDelay() throws SocketException { return socket.getTcpNoDelay(); } @Override public int getTrafficClass() throws SocketException { return socket.getTrafficClass(); } @Override public boolean isBound() { return socket.isBound(); } @Override public boolean isClosed() { return socket.isClosed(); } @Override public boolean isConnected() { return socket.isConnected(); } @Override public boolean isInputShutdown() { return socket.isInputShutdown(); } @Override public boolean isOutputShutdown() { return socket.isOutputShutdown(); } @Override public void sendUrgentData(int data) throws IOException { socket.sendUrgentData(data); } @Override public void setKeepAlive(boolean on) throws SocketException { socket.setKeepAlive(on); } @Override public void setOOBInline(boolean on) throws SocketException { socket.setOOBInline(on); } @Override public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { socket.setPerformancePreferences(connectionTime, latency, bandwidth); } @Override public synchronized void setReceiveBufferSize(int size) throws SocketException { socket.setReceiveBufferSize(size); } @Override public void setReuseAddress(boolean on) throws SocketException { socket.setReuseAddress(on); } @Override public synchronized void setSendBufferSize(int size) throws SocketException { socket.setSendBufferSize(size); } @Override public void setSoLinger(boolean on, int linger) throws SocketException { socket.setSoLinger(on, linger); } @Override public synchronized void setSoTimeout(int timeout) throws SocketException { socket.setSoTimeout(timeout); } @Override public void setTcpNoDelay(boolean on) throws SocketException { socket.setTcpNoDelay(on); } @Override public void setTrafficClass(int tc) throws SocketException { socket.setTrafficClass(tc); } @Override public void shutdownInput() throws IOException { socket.shutdownInput(); } @Override public void shutdownOutput() throws IOException { socket.shutdownOutput(); } @Override public String toString() { return socket.toString(); } }