michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this michael@0: * class: michael@0: *
michael@0: * This method can be overridden in a super class in order to provide michael@0: * a custom implementation of {@link SessionInputBuffer} interface. michael@0: * michael@0: * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams) michael@0: * michael@0: * @param socket the socket. michael@0: * @param buffersize the buffer size. michael@0: * @param params HTTP parameters. michael@0: * @return session input buffer. michael@0: * @throws IOException in case of an I/O error. michael@0: */ michael@0: protected SessionInputBuffer createSessionInputBuffer( michael@0: final Socket socket, michael@0: int buffersize, michael@0: final HttpParams params) throws IOException { michael@0: return new SocketInputBuffer(socket, buffersize, params); michael@0: } michael@0: michael@0: /** michael@0: * Creates an instance of {@link SessionOutputBuffer} to be used for michael@0: * sending data to the given {@link Socket}. michael@0: *
michael@0: * This method can be overridden in a super class in order to provide michael@0: * a custom implementation of {@link SocketOutputBuffer} interface. michael@0: * michael@0: * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams) michael@0: * michael@0: * @param socket the socket. michael@0: * @param buffersize the buffer size. michael@0: * @param params HTTP parameters. michael@0: * @return session output buffer. michael@0: * @throws IOException in case of an I/O error. michael@0: */ michael@0: protected SessionOutputBuffer createSessionOutputBuffer( michael@0: final Socket socket, michael@0: int buffersize, michael@0: final HttpParams params) throws IOException { michael@0: return new SocketOutputBuffer(socket, buffersize, params); michael@0: } michael@0: michael@0: /** michael@0: * Binds this connection to the given {@link Socket}. This socket will be michael@0: * used by the connection to send and receive data. michael@0: *
michael@0: * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)} michael@0: * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods michael@0: * to create session input / output buffers bound to this socket and then michael@0: * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)} michael@0: * method to pass references to those buffers to the underlying HTTP message michael@0: * parser and formatter. michael@0: *
michael@0: * After this method's execution the connection status will be reported
michael@0: * as open and the {@link #isOpen()} will return true
.
michael@0: *
michael@0: * @param socket the socket.
michael@0: * @param params HTTP parameters.
michael@0: * @throws IOException in case of an I/O error.
michael@0: */
michael@0: protected void bind(final Socket socket, final HttpParams params) throws IOException {
michael@0: if (socket == null) {
michael@0: throw new IllegalArgumentException("Socket may not be null");
michael@0: }
michael@0: if (params == null) {
michael@0: throw new IllegalArgumentException("HTTP parameters may not be null");
michael@0: }
michael@0: this.socket = socket;
michael@0:
michael@0: int buffersize = HttpConnectionParams.getSocketBufferSize(params);
michael@0:
michael@0: init(
michael@0: createHttpDataReceiver(socket, buffersize, params),
michael@0: createHttpDataTransmitter(socket, buffersize, params),
michael@0: params);
michael@0:
michael@0: this.open = true;
michael@0: }
michael@0:
michael@0: protected Socket getSocket() {
michael@0: return this.socket;
michael@0: }
michael@0:
michael@0: public boolean isOpen() {
michael@0: return this.open;
michael@0: }
michael@0:
michael@0: public InetAddress getLocalAddress() {
michael@0: if (this.socket != null) {
michael@0: return this.socket.getLocalAddress();
michael@0: } else {
michael@0: return null;
michael@0: }
michael@0: }
michael@0:
michael@0: public int getLocalPort() {
michael@0: if (this.socket != null) {
michael@0: return this.socket.getLocalPort();
michael@0: } else {
michael@0: return -1;
michael@0: }
michael@0: }
michael@0:
michael@0: public InetAddress getRemoteAddress() {
michael@0: if (this.socket != null) {
michael@0: return this.socket.getInetAddress();
michael@0: } else {
michael@0: return null;
michael@0: }
michael@0: }
michael@0:
michael@0: public int getRemotePort() {
michael@0: if (this.socket != null) {
michael@0: return this.socket.getPort();
michael@0: } else {
michael@0: return -1;
michael@0: }
michael@0: }
michael@0:
michael@0: public void setSocketTimeout(int timeout) {
michael@0: assertOpen();
michael@0: if (this.socket != null) {
michael@0: try {
michael@0: this.socket.setSoTimeout(timeout);
michael@0: } catch (SocketException ignore) {
michael@0: // It is not quite clear from the Sun's documentation if there are any
michael@0: // other legitimate cases for a socket exception to be thrown when setting
michael@0: // SO_TIMEOUT besides the socket being already closed
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: public int getSocketTimeout() {
michael@0: if (this.socket != null) {
michael@0: try {
michael@0: return this.socket.getSoTimeout();
michael@0: } catch (SocketException ignore) {
michael@0: return -1;
michael@0: }
michael@0: } else {
michael@0: return -1;
michael@0: }
michael@0: }
michael@0:
michael@0: public void shutdown() throws IOException {
michael@0: this.open = false;
michael@0: Socket tmpsocket = this.socket;
michael@0: if (tmpsocket != null) {
michael@0: tmpsocket.close();
michael@0: }
michael@0: }
michael@0:
michael@0: public void close() throws IOException {
michael@0: if (!this.open) {
michael@0: return;
michael@0: }
michael@0: this.open = false;
michael@0: this.open = false;
michael@0: Socket sock = this.socket;
michael@0: try {
michael@0: doFlush();
michael@0: try {
michael@0: try {
michael@0: sock.shutdownOutput();
michael@0: } catch (IOException ignore) {
michael@0: }
michael@0: try {
michael@0: sock.shutdownInput();
michael@0: } catch (IOException ignore) {
michael@0: }
michael@0: } catch (UnsupportedOperationException ignore) {
michael@0: // if one isn't supported, the other one isn't either
michael@0: }
michael@0: } finally {
michael@0: sock.close();
michael@0: }
michael@0: }
michael@0:
michael@0: }