001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.conn.scheme;
029
030import java.io.IOException;
031import java.net.InetAddress;
032import java.net.Socket;
033import java.net.UnknownHostException;
034
035import org.apache.http.conn.ConnectTimeoutException;
036import org.apache.http.params.HttpParams;
037
038/**
039 * A factory for creating, initializing and connecting sockets.
040 * The factory encapsulates the logic for establishing a socket connection.
041 *
042 * @since 4.0
043 *
044 * @deprecated (4.1)  use {@link SchemeSocketFactory}
045 */
046@Deprecated
047public interface SocketFactory {
048
049    /**
050     * Creates a new, unconnected socket.
051     * The socket should subsequently be passed to
052     * {@link #connectSocket connectSocket}.
053     *
054     * @return  a new socket
055     *
056     * @throws IOException if an I/O error occurs while creating the socket
057     */
058    Socket createSocket()
059        throws IOException;
060
061    /**
062     * Connects a socket to the given host.
063     *
064     * @param sock      the socket to connect, as obtained from
065     *                  {@link #createSocket createSocket}.
066     *                  {@code null} indicates that a new socket
067     *                  should be created and connected.
068     * @param host      the host to connect to
069     * @param port      the port to connect to on the host
070     * @param localAddress the local address to bind the socket to, or
071     *                  {@code null} for any
072     * @param localPort the port on the local machine,
073     *                  0 or a negative number for any
074     * @param params    additional {@link HttpParams parameters} for connecting
075     *
076     * @return  the connected socket. The returned object may be different
077     *          from the {@code sock} argument if this factory supports
078     *          a layered protocol.
079     *
080     * @throws IOException if an I/O error occurs
081     * @throws UnknownHostException if the IP address of the target host
082     *          can not be determined
083     * @throws ConnectTimeoutException if the socket cannot be connected
084     *          within the time limit defined in the {@code params}
085     */
086    Socket connectSocket(
087        Socket sock,
088        String host,
089        int port,
090        InetAddress localAddress,
091        int localPort,
092        HttpParams params
093    ) throws IOException, UnknownHostException, ConnectTimeoutException;
094
095    /**
096     * Checks whether a socket provides a secure connection.
097     * The socket must be {@link #connectSocket connected}
098     * by this factory.
099     * The factory will <i>not</i> perform I/O operations
100     * in this method.
101     * <p>
102     * As a rule of thumb, plain sockets are not secure and
103     * TLS/SSL sockets are secure. However, there may be
104     * application specific deviations. For example, a plain
105     * socket to a host in the same intranet ("trusted zone")
106     * could be considered secure. On the other hand, a
107     * TLS/SSL socket could be considered insecure based on
108     * the cipher suite chosen for the connection.
109     * </p>
110     *
111     * @param sock      the connected socket to check
112     *
113     * @return  {@code true} if the connection of the socket
114     *          should be considered secure, or
115     *          {@code false} if it should not
116     *
117     * @throws IllegalArgumentException
118     *  if the argument is invalid, for example because it is
119     *  not a connected socket or was created by a different
120     *  socket factory.
121     *  Note that socket factories are <i>not</i> required to
122     *  check these conditions, they may simply return a default
123     *  value when called with an invalid socket argument.
124     */
125    boolean isSecure(Socket sock)
126        throws IllegalArgumentException;
127
128}