Class HttpsURLConnectionImpl


  • public class HttpsURLConnectionImpl
    extends HttpsURLConnection
    A class to represent an HTTP connection to a remote object. Ideally, this class should subclass and inherit the http handler implementation, but it can't do so because that class have the wrong Java Type. Thus it uses the delegate (aka, the Adapter/Wrapper design pattern) to reuse code from the http handler. Since it would use a delegate to access sun.net.www.protocol.http.HttpURLConnection functionalities, it needs to implement all public methods in it's super class and all the way to Object.
    • Method Detail

      • setNewClient

        protected void setNewClient​(URL url)
                             throws IOException
        Create a new HttpClient object, bypassing the cache of HTTP client objects/connections.
        Parameters:
        url - the URL being accessed
        Throws:
        IOException
      • isConnected

        protected boolean isConnected()
        Used by subclass to access "connected" variable. Since we are delegating the actual implementation to "delegate", we need to delegate the access of "connected" as well.
      • setConnected

        protected void setConnected​(boolean conn)
        Used by subclass to access "connected" variable. Since we are delegating the actual implementation to "delegate", we need to delegate the access of "connected" as well.
      • getErrorStream

        @Nullable
        public InputStream getErrorStream()
        Description copied from class: HttpURLConnection
        Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.

        This method will not cause a connection to be initiated. If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null. This is the default.

        Overrides:
        getErrorStream in class HttpURLConnection
        Returns:
        an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data.
      • usingProxy

        public boolean usingProxy()
        Description copied from class: HttpURLConnection
        Indicates if the connection is going through a proxy.
        Specified by:
        usingProxy in class HttpURLConnection
        Returns:
        a boolean indicating if the connection is using a proxy.
      • getHeaderFields

        public Map<String,​List<String>> getHeaderFields()
        Returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values.
        Overrides:
        getHeaderFields in class URLConnection
        Returns:
        a Map of header fields
        Since:
        1.4
      • getHeaderField

        @Nullable
        public String getHeaderField​(String name)
        Gets a header field by name. Returns null if not known.
        Overrides:
        getHeaderField in class URLConnection
        Parameters:
        name - the name of the header field
        Returns:
        the value of the named header field, or null if there is no such field in the header.
      • getHeaderFieldKey

        @Nullable
        public String getHeaderFieldKey​(int n)
        Gets a header field by index. Returns null if not known.
        Overrides:
        getHeaderFieldKey in class HttpURLConnection
        Parameters:
        n - the index of the header field
        Returns:
        the key for the nth header field, or null if the key does not exist.
      • addRequestProperty

        public void addRequestProperty​(String key,
                                       String value)
        Adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.
        Overrides:
        addRequestProperty in class URLConnection
        Parameters:
        key - the keyword by which the request is known (e.g., "accept").
        value - the value associated with it.
        Since:
        1.4
        See Also:
        getRequestProperties()
      • getRequestProperties

        public Map<String,​List<String>> getRequestProperties()
        Returns an unmodifiable Map of general request properties for this connection. The Map keys are Strings that represent the request-header field names. Each Map value is a unmodifiable List of Strings that represents the corresponding field values.
        Overrides:
        getRequestProperties in class URLConnection
        Returns:
        a Map of the general request properties for this connection.
        Throws:
        IllegalStateException - if already connected
        Since:
        1.4
      • getResponseMessage

        @Nullable
        public String getResponseMessage()
                                  throws IOException
        Description copied from class: HttpURLConnection
        Gets the HTTP response message, if any, returned along with the response code from a server. From responses like:
         HTTP/1.0 200 OK
         HTTP/1.0 404 Not Found
         
        Extracts the Strings "OK" and "Not Found" respectively. Returns null if none could be discerned from the responses (the result was not valid HTTP).
        Overrides:
        getResponseMessage in class HttpURLConnection
        Returns:
        the HTTP response message, or null
        Throws:
        IOException - if an error occurred connecting to the server.
      • getPermission

        public Permission getPermission()
                                 throws IOException
        Description copied from class: HttpURLConnection
        Returns a SocketPermission object representing the permission necessary to connect to the destination host and port.
        Overrides:
        getPermission in class HttpURLConnection
        Returns:
        a SocketPermission object representing the permission necessary to connect to the destination host and port.
        Throws:
        IOException - if an error occurs while computing the permission.
      • getContentLength

        public int getContentLength()
        Description copied from class: URLConnection
        Returns the value of the content-length header field.

        Note: getContentLengthLong() should be preferred over this method, since it returns a long instead and is therefore more portable.

        Overrides:
        getContentLength in class URLConnection
        Returns:
        the content length of the resource that this connection's URL references, -1 if the content length is not known, or if the content length is greater than Integer.MAX_VALUE.
      • getContentLengthLong

        public long getContentLengthLong()
        Description copied from class: URLConnection
        Returns the value of the content-length header field as a long.
        Overrides:
        getContentLengthLong in class URLConnection
        Returns:
        the content length of the resource that this connection's URL references, or -1 if the content length is not known.
      • getHeaderFieldInt

        public int getHeaderFieldInt​(String name,
                                     int Default)
        Description copied from class: URLConnection
        Returns the value of the named field parsed as a number.

        This form of getHeaderField exists because some connection types (e.g., http-ng) have pre-parsed headers. Classes for that connection type can override this method and short-circuit the parsing.

        Overrides:
        getHeaderFieldInt in class URLConnection
        Parameters:
        name - the name of the header field.
        Default - the default value.
        Returns:
        the value of the named field, parsed as an integer. The Default value is returned if the field is missing or malformed.
      • getHeaderFieldLong

        public long getHeaderFieldLong​(String name,
                                       long Default)
        Description copied from class: URLConnection
        Returns the value of the named field parsed as a number.

        This form of getHeaderField exists because some connection types (e.g., http-ng) have pre-parsed headers. Classes for that connection type can override this method and short-circuit the parsing.

        Overrides:
        getHeaderFieldLong in class URLConnection
        Parameters:
        name - the name of the header field.
        Default - the default value.
        Returns:
        the value of the named field, parsed as a long. The Default value is returned if the field is missing or malformed.
      • getContent

        public Object getContent()
                          throws IOException
        Description copied from class: URLConnection
        Retrieves the contents of this URL connection.

        This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created:

        1. If the application has set up a content handler factory instance using the setContentHandlerFactory method, the createContentHandler method of that instance is called with the content type as an argument; the result is a content handler for that content type.
        2. If no content handler factory has yet been set up, or if the factory's createContentHandler method returns null, then the application loads the class named:
                   sun.net.www.content.<contentType>
           
          where <contentType> is formed by taking the content-type string, replacing all slash characters with a period ('.'), and all other non-alphanumeric characters with the underscore character ' _'. The alphanumeric characters are specifically the 26 uppercase ASCII letters 'A' through ' Z', the 26 lowercase ASCII letters 'a' through 'z', and the 10 ASCII digits '0' through '9'. If the specified class does not exist, or is not a subclass of ContentHandler, then an UnknownServiceException is thrown.
        Overrides:
        getContent in class URLConnection
        Returns:
        the object fetched. The instanceof operator should be used to determine the specific kind of object returned.
        Throws:
        IOException - if an I/O error occurs while getting the content.
        UnknownServiceException - if the protocol does not support the content type.
        See Also:
        ContentHandlerFactory.createContentHandler(java.lang.String), URLConnection.getContentType(), URLConnection.setContentHandlerFactory(java.net.ContentHandlerFactory)
      • toString

        public String toString()
        Description copied from class: URLConnection
        Returns a String representation of this URL connection.
        Overrides:
        toString in class URLConnection
        Returns:
        a string representation of this URLConnection.
      • setDoInput

        public void setDoInput​(boolean doinput)
        Description copied from class: URLConnection
        Sets the value of the doInput field for this URLConnection to the specified value.

        A URL connection can be used for input and/or output. Set the DoInput flag to true if you intend to use the URL connection for input, false if not. The default is true.

        Overrides:
        setDoInput in class URLConnection
        Parameters:
        doinput - the new value.
        See Also:
        URLConnection.doInput, URLConnection.getDoInput()
      • setDoOutput

        public void setDoOutput​(boolean dooutput)
        Description copied from class: URLConnection
        Sets the value of the doOutput field for this URLConnection to the specified value.

        A URL connection can be used for input and/or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.

        Overrides:
        setDoOutput in class URLConnection
        Parameters:
        dooutput - the new value.
        See Also:
        URLConnection.getDoOutput()
      • setUseCaches

        public void setUseCaches​(boolean usecaches)
        Description copied from class: URLConnection
        Sets the value of the useCaches field of this URLConnection to the specified value.

        Some protocols do caching of documents. Occasionally, it is important to be able to "tunnel through" and ignore the caches (e.g., the "reload" button in a browser). If the UseCaches flag on a connection is true, the connection is allowed to use whatever caches it can. If false, caches are to be ignored. The default value comes from DefaultUseCaches, which defaults to true.

        Overrides:
        setUseCaches in class URLConnection
        Parameters:
        usecaches - a boolean indicating whether or not to allow caching
        See Also:
        URLConnection.getUseCaches()
      • getDefaultUseCaches

        public boolean getDefaultUseCaches()
        Description copied from class: URLConnection
        Returns the default value of a URLConnection's useCaches flag.

        Ths default is "sticky", being a part of the static state of all URLConnections. This flag applies to the next, and all following URLConnections that are created.

        Overrides:
        getDefaultUseCaches in class URLConnection
        Returns:
        the default value of a URLConnection's useCaches flag.
        See Also:
        URLConnection.setDefaultUseCaches(boolean)
      • equals

        public boolean equals​(@Nullable
                              Object obj)
        Description copied from class: Object
        Indicates whether some other object is "equal to" this one.

        The equals method implements an equivalence relation on non-null object references:

        • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
        • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
        • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
        • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
        • For any non-null reference value x, x.equals(null) should return false.

        The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

        Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

        Overrides:
        equals in class Object
        Parameters:
        obj - the reference object with which to compare.
        Returns:
        true if this object is the same as the obj argument; false otherwise.
        See Also:
        Object.hashCode(), HashMap
      • hashCode

        public int hashCode()
        Description copied from class: Object
        Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

        The general contract of hashCode is:

        • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
        • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
        • It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

        As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

        Overrides:
        hashCode in class Object
        Returns:
        a hash code value for this object.
        See Also:
        Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)
      • setConnectTimeout

        public void setConnectTimeout​(int timeout)
        Description copied from class: URLConnection
        Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

        Some non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

        Overrides:
        setConnectTimeout in class URLConnection
        Parameters:
        timeout - an int that specifies the connect timeout value in milliseconds
        See Also:
        URLConnection.getConnectTimeout(), URLConnection.connect()
      • setReadTimeout

        public void setReadTimeout​(int timeout)
        Description copied from class: URLConnection
        Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

        Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().

        Overrides:
        setReadTimeout in class URLConnection
        Parameters:
        timeout - an int that specifies the timeout value to be used in milliseconds
        See Also:
        URLConnection.getReadTimeout(), InputStream.read()
      • setFixedLengthStreamingMode

        public void setFixedLengthStreamingMode​(int contentLength)
        Description copied from class: HttpURLConnection
        This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.

        An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount.

        When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error.

        This method must be called before the URLConnection is connected.

        NOTE: HttpURLConnection.setFixedLengthStreamingMode(long) is recommended instead of this method as it allows larger content lengths to be set.

        Overrides:
        setFixedLengthStreamingMode in class HttpURLConnection
        Parameters:
        contentLength - The number of bytes which will be written to the OutputStream.
        See Also:
        HttpURLConnection.setChunkedStreamingMode(int)
      • setFixedLengthStreamingMode

        public void setFixedLengthStreamingMode​(long contentLength)
        Description copied from class: HttpURLConnection
        This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.

        An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount.

        When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error.

        This method must be called before the URLConnection is connected.

        The content length set by invoking this method takes precedence over any value set by HttpURLConnection.setFixedLengthStreamingMode(int).

        Overrides:
        setFixedLengthStreamingMode in class HttpURLConnection
        Parameters:
        contentLength - The number of bytes which will be written to the OutputStream.
      • setChunkedStreamingMode

        public void setChunkedStreamingMode​(int chunklen)
        Description copied from class: HttpURLConnection
        This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance. In this mode, chunked transfer encoding is used to send the request body. Note, not all HTTP servers support this mode.

        When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error.

        This method must be called before the URLConnection is connected.

        Overrides:
        setChunkedStreamingMode in class HttpURLConnection
        Parameters:
        chunklen - The number of bytes to write in each chunk. If chunklen is less than or equal to zero, a default value will be used.
        See Also:
        HttpURLConnection.setFixedLengthStreamingMode(int)