OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2010 Google Inc. |
| 3 *· |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 *· |
| 7 * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 *· |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. |
| 13 */ |
| 14 |
| 15 package com.google.api.client.http.apache; |
| 16 |
| 17 import org.apache.http.annotation.Immutable; |
| 18 import org.apache.http.conn.ConnectTimeoutException; |
| 19 import org.apache.http.conn.scheme.SchemeSocketFactory; |
| 20 import org.apache.http.params.HttpConnectionParams; |
| 21 import org.apache.http.params.HttpParams; |
| 22 |
| 23 import java.io.IOException; |
| 24 import java.net.InetSocketAddress; |
| 25 import java.net.Proxy; |
| 26 import java.net.Socket; |
| 27 import java.net.SocketTimeoutException; |
| 28 |
| 29 /** |
| 30 * The default class for creating plain (unencrypted) socks sockets. |
| 31 * <p> |
| 32 * The following parameters can be used to customize the behavior of this class: |
| 33 * <ul> |
| 34 * <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</l
i> |
| 35 * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li> |
| 36 * </ul> |
| 37 *· |
| 38 * @since 1.3 |
| 39 */ |
| 40 |
| 41 @Immutable |
| 42 public class PlainSocksSocketFactory implements SchemeSocketFactory { |
| 43 |
| 44 /** |
| 45 * Gets the default factory. |
| 46 *· |
| 47 * @return the default factory |
| 48 */ |
| 49 public static PlainSocksSocketFactory getSocketFactory() { |
| 50 return new PlainSocksSocketFactory(); |
| 51 } |
| 52 |
| 53 /** |
| 54 * @param params If the parameters contain values for socks.host and socks.por
t they are used as |
| 55 * the socket server. If they are not set (or set to <code>null</code>)
the function simply |
| 56 * returns a new instance of {@link Socket} class using default constru
ctor. |
| 57 *· |
| 58 * @return the new socket |
| 59 */ |
| 60 public Socket createSocket(final HttpParams params) { |
| 61 Socket sock; |
| 62 if (params == null) { |
| 63 sock = new Socket(); |
| 64 } else { |
| 65 String proxyHost = (String) params.getParameter("socks.host"); |
| 66 Integer proxyPort = (Integer) params.getParameter("socks.port"); |
| 67 if (proxyHost != null && proxyPort != null) { |
| 68 InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort
); |
| 69 Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); |
| 70 sock = new Socket(proxy); |
| 71 } else { |
| 72 sock = new Socket(); |
| 73 } |
| 74 } |
| 75 return sock; |
| 76 } |
| 77 |
| 78 public Socket connectSocket(final Socket socket, final InetSocketAddress remot
eAddress, |
| 79 final InetSocketAddress localAddress, final HttpParams params) throws IOEx
ception, |
| 80 ConnectTimeoutException { |
| 81 if (remoteAddress == null) { |
| 82 throw new IllegalArgumentException("Remote address may not be null"); |
| 83 } |
| 84 if (params == null) { |
| 85 throw new IllegalArgumentException("HTTP parameters may not be null"); |
| 86 } |
| 87 Socket sock = socket != null ? socket : createSocket(params); |
| 88 if (localAddress != null) { |
| 89 sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params)); |
| 90 sock.bind(localAddress); |
| 91 } |
| 92 int timeout = HttpConnectionParams.getConnectionTimeout(params); |
| 93 try { |
| 94 sock.connect(remoteAddress, timeout); |
| 95 } catch (SocketTimeoutException ex) { |
| 96 throw new ConnectTimeoutException("Connect to " + remoteAddress.getHostNam
e() + "/" |
| 97 + remoteAddress.getAddress() + " timed out"); |
| 98 } |
| 99 return sock; |
| 100 } |
| 101 |
| 102 /** |
| 103 * Checks whether a socket connection is secure. This factory creates plain so
cks socket |
| 104 * connections which are not considered secure. |
| 105 *· |
| 106 * @param sock the connected socket |
| 107 *· |
| 108 * @return <code>false</code> |
| 109 *· |
| 110 * @throws IllegalArgumentException if the argument is invalid |
| 111 */ |
| 112 public final boolean isSecure(Socket sock) throws IllegalArgumentException { |
| 113 if (sock == null) { |
| 114 throw new IllegalArgumentException("Socket may not be null."); |
| 115 } |
| 116 // This check is performed last since it calls a method implemented |
| 117 // by the argument object. getClass() is final in java.lang.Object. |
| 118 if (sock.isClosed()) { |
| 119 throw new IllegalArgumentException("Socket is closed."); |
| 120 } |
| 121 return false; |
| 122 } |
| 123 |
| 124 } |
OLD | NEW |