1/* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java $ 3 * $Revision: 561083 $ 4 * $Date: 2007-07-30 11:31:17 -0700 (Mon, 30 Jul 2007) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32package org.apache.http.impl; 33 34import java.io.IOException; 35import java.net.InetAddress; 36import java.net.Socket; 37import java.net.SocketException; 38 39import org.apache.http.HttpInetConnection; 40import org.apache.http.impl.io.SocketInputBuffer; 41import org.apache.http.impl.io.SocketOutputBuffer; 42import org.apache.http.io.SessionInputBuffer; 43import org.apache.http.io.SessionOutputBuffer; 44import org.apache.http.params.HttpConnectionParams; 45import org.apache.http.params.HttpParams; 46 47/** 48 * Implementation of a client-side HTTP connection that can be bound to a 49 * network Socket in order to receive and transmit data. 50 * 51 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 52 * 53 * @version $Revision: 561083 $ 54 * 55 * @since 4.0 56 * 57 * @deprecated Please use {@link java.net.URL#openConnection} instead. 58 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 59 * for further details. 60 */ 61@Deprecated 62public class SocketHttpClientConnection 63 extends AbstractHttpClientConnection implements HttpInetConnection { 64 65 private volatile boolean open; 66 private Socket socket = null; 67 68 public SocketHttpClientConnection() { 69 super(); 70 } 71 72 protected void assertNotOpen() { 73 if (this.open) { 74 throw new IllegalStateException("Connection is already open"); 75 } 76 } 77 78 protected void assertOpen() { 79 if (!this.open) { 80 throw new IllegalStateException("Connection is not open"); 81 } 82 } 83 84 protected SessionInputBuffer createSessionInputBuffer( 85 final Socket socket, 86 int buffersize, 87 final HttpParams params) throws IOException { 88 return new SocketInputBuffer(socket, buffersize, params); 89 } 90 91 protected SessionOutputBuffer createSessionOutputBuffer( 92 final Socket socket, 93 int buffersize, 94 final HttpParams params) throws IOException { 95 return new SocketOutputBuffer(socket, buffersize, params); 96 } 97 98 protected void bind( 99 final Socket socket, 100 final HttpParams params) throws IOException { 101 if (socket == null) { 102 throw new IllegalArgumentException("Socket may not be null"); 103 } 104 if (params == null) { 105 throw new IllegalArgumentException("HTTP parameters may not be null"); 106 } 107 this.socket = socket; 108 109 int buffersize = HttpConnectionParams.getSocketBufferSize(params); 110 111 init( 112 createSessionInputBuffer(socket, buffersize, params), 113 createSessionOutputBuffer(socket, buffersize, params), 114 params); 115 116 this.open = true; 117 } 118 119 public boolean isOpen() { 120 return this.open; 121 } 122 123 protected Socket getSocket() { 124 return this.socket; 125 } 126 127 public InetAddress getLocalAddress() { 128 if (this.socket != null) { 129 return this.socket.getLocalAddress(); 130 } else { 131 return null; 132 } 133 } 134 135 public int getLocalPort() { 136 if (this.socket != null) { 137 return this.socket.getLocalPort(); 138 } else { 139 return -1; 140 } 141 } 142 143 public InetAddress getRemoteAddress() { 144 if (this.socket != null) { 145 return this.socket.getInetAddress(); 146 } else { 147 return null; 148 } 149 } 150 151 public int getRemotePort() { 152 if (this.socket != null) { 153 return this.socket.getPort(); 154 } else { 155 return -1; 156 } 157 } 158 159 public void setSocketTimeout(int timeout) { 160 assertOpen(); 161 if (this.socket != null) { 162 try { 163 this.socket.setSoTimeout(timeout); 164 } catch (SocketException ignore) { 165 // It is not quite clear from the Sun's documentation if there are any 166 // other legitimate cases for a socket exception to be thrown when setting 167 // SO_TIMEOUT besides the socket being already closed 168 } 169 } 170 } 171 172 public int getSocketTimeout() { 173 if (this.socket != null) { 174 try { 175 return this.socket.getSoTimeout(); 176 } catch (SocketException ignore) { 177 return -1; 178 } 179 } else { 180 return -1; 181 } 182 } 183 184 public void shutdown() throws IOException { 185 this.open = false; 186 Socket tmpsocket = this.socket; 187 if (tmpsocket != null) { 188 tmpsocket.close(); 189 } 190 } 191 192 public void close() throws IOException { 193 if (!this.open) { 194 return; 195 } 196 this.open = false; 197 doFlush(); 198 try { 199 try { 200 this.socket.shutdownOutput(); 201 } catch (IOException ignore) { 202 } 203 try { 204 this.socket.shutdownInput(); 205 } catch (IOException ignore) { 206 } 207 } catch (UnsupportedOperationException ignore) { 208 // if one isn't supported, the other one isn't either 209 } 210 this.socket.close(); 211 } 212 213} 214