Class HttpClient

  • All Implemented Interfaces:
    URLConnector, java.io.Closeable, java.lang.AutoCloseable

    public class HttpClient
    extends java.lang.Object
    implements java.io.Closeable, URLConnector
    A simple Http Client that inter-works with the bnd registry. It provides an easy way to construct a URL request. The request is then decorated with third parties that are in the bnd registry for proxies and authentication models.
    • Field Detail

      • logger

        private static final org.slf4j.Logger logger
      • sdf

        public static final java.text.SimpleDateFormat sdf
      • HTTP_DATE_FORMATTER

        private static final java.lang.ThreadLocal<java.text.DateFormat> HTTP_DATE_FORMATTER
      • HTTP_TEMPORARY_REDIRECT

        private static final int HTTP_TEMPORARY_REDIRECT
        See Also:
        Constant Field Values
      • HTTP_PERMANENT_REDIRECT

        private static final int HTTP_PERMANENT_REDIRECT
        See Also:
        Constant Field Values
      • proxyHandlers

        private final java.util.List<ProxyHandler> proxyHandlers
      • passwordAuthentication

        private java.lang.ThreadLocal<java.net.PasswordAuthentication> passwordAuthentication
      • inited

        private boolean inited
      • offline

        private volatile java.util.concurrent.atomic.AtomicBoolean offline
    • Constructor Detail

      • HttpClient

        public HttpClient()
    • Method Detail

      • init

        void init()
      • httpDateFormat

        private static java.text.DateFormat httpDateFormat()
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • connect

        public java.io.InputStream connect​(java.net.URL url)
                                    throws java.lang.Exception
        Description copied from interface: URLConnector
        Connect to the specified URL.
        Specified by:
        connect in interface URLConnector
        Throws:
        java.io.IOException
        java.lang.Exception
      • connectTagged

        public TaggedData connectTagged​(java.net.URL url)
                                 throws java.lang.Exception
        Description copied from interface: URLConnector
        Connect to the specified URL, also returning the ETag if available.
        Specified by:
        connectTagged in interface URLConnector
        Parameters:
        url - The remote URL.
        Returns:
        An instance of TaggedData; note that the TaggedData.getTag() method may return null if the resource has no tag.
        Throws:
        java.io.IOException - @since 1.1
        java.lang.Exception
      • connectTagged

        public TaggedData connectTagged​(java.net.URL url,
                                        java.lang.String tag)
                                 throws java.lang.Exception
        Description copied from interface: URLConnector
        Connect to the specified URL while providing the last known tag for the remote resource; the response will be null if the remote resource is unchanged.
        Specified by:
        connectTagged in interface URLConnector
        Parameters:
        url - The remote URL.
        tag - The last known tag value for the resource.
        Returns:
        An instance of TaggedData, or null if the resource has not modified (i.e., if it has the same tag value).
        Throws:
        java.io.IOException - @since 1.1
        java.lang.Exception
      • send

        public java.lang.Object send​(HttpRequest<?> request)
                              throws java.lang.Exception
        Throws:
        java.lang.Exception
      • doCached

        java.lang.Object doCached​(HttpRequest<?> request)
                           throws java.lang.Exception,
                                  java.io.IOException
        Throws:
        java.lang.Exception
        java.io.IOException
      • doCached0

        TaggedData doCached0​(HttpRequest<?> request)
                      throws java.lang.Exception,
                             java.io.IOException
        Throws:
        java.lang.Exception
        java.io.IOException
      • send0

        public TaggedData send0​(HttpRequest<?> request)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • entitytag

        private java.lang.String entitytag​(java.lang.String entity)
      • getProxySetup

        public ProxyHandler.ProxySetup getProxySetup​(java.net.URL url)
                                              throws java.lang.Exception
        Throws:
        java.lang.Exception
      • connectWithProxy

        public <T> T connectWithProxy​(ProxyHandler.ProxySetup proxySetup,
                                      java.util.concurrent.Callable<T> r)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getProxiedAndConfiguredConnection

        private java.net.URLConnection getProxiedAndConfiguredConnection​(java.net.URL url,
                                                                         ProxyHandler.ProxySetup proxy)
                                                                  throws java.io.IOException,
                                                                         java.lang.Exception
        Throws:
        java.io.IOException
        java.lang.Exception
      • findMatchingHandler

        public URLConnectionHandler findMatchingHandler​(java.net.URL url)
                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getURLConnectionHandlers

        private java.util.Collection<? extends URLConnectionHandler> getURLConnectionHandlers()
                                                                                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getProxyHandlers

        private java.util.Collection<? extends ProxyHandler> getProxyHandlers()
                                                                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createProgressWrappedStream

        private java.io.InputStream createProgressWrappedStream​(java.io.InputStream inputStream,
                                                                java.lang.String name,
                                                                int size,
                                                                ProgressPlugin.Task task,
                                                                long timeout)
      • doConnect

        private TaggedData doConnect​(java.lang.Object put,
                                     java.lang.reflect.Type ref,
                                     java.net.URLConnection con,
                                     java.net.HttpURLConnection hcon,
                                     HttpRequest<?> request,
                                     ProgressPlugin.Task task)
                              throws java.io.IOException,
                                     java.lang.Exception
        Throws:
        java.io.IOException
        java.lang.Exception
      • isUpdateInfo

        boolean isUpdateInfo​(java.net.URLConnection con,
                             HttpRequest<?> request,
                             int code)
      • convert

        private java.lang.Object convert​(java.lang.reflect.Type type,
                                         java.io.File in,
                                         TaggedData tag)
                                  throws java.io.IOException,
                                         java.lang.Exception
        Throws:
        java.io.IOException
        java.lang.Exception
      • convert

        private java.lang.Object convert​(java.lang.reflect.Type ref,
                                         java.io.InputStream in)
                                  throws java.io.IOException,
                                         java.lang.Exception
        Throws:
        java.io.IOException
        java.lang.Exception
      • handleContentEncoding

        private java.io.InputStream handleContentEncoding​(java.net.HttpURLConnection con,
                                                          java.io.InputStream in)
                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • doOutput

        private void doOutput​(java.lang.Object put,
                              java.net.URLConnection con,
                              HttpRequest<?> rq)
                       throws java.io.IOException,
                              java.lang.Exception
        Throws:
        java.io.IOException
        java.lang.Exception
      • configureHttpConnection

        private void configureHttpConnection​(java.lang.String verb,
                                             java.net.HttpURLConnection hcon)
                                      throws java.net.ProtocolException
        Throws:
        java.net.ProtocolException
      • setHeaders

        private void setHeaders​(java.util.Map<java.lang.String,​java.lang.String> headers,
                                java.net.URLConnection con)
      • setCache

        public void setCache​(java.io.File cache)
      • setReporter

        public void setReporter​(Reporter reporter)
      • setRegistry

        public void setRegistry​(Registry registry)
      • getReporter

        public Reporter getReporter()
      • addProxyHandler

        public void addProxyHandler​(ProxyHandler proxyHandler)
      • setLog

        public void setLog​(java.io.File log)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • getUserFor

        public java.lang.String getUserFor​(java.lang.String base)
                                    throws java.net.MalformedURLException,
                                           java.lang.Exception
        Throws:
        java.net.MalformedURLException
        java.lang.Exception
      • toName

        public java.lang.String toName​(java.net.URI url)
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getCacheFileFor

        public java.io.File getCacheFileFor​(java.net.URI url)
                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readSettings

        public void readSettings​(Processor processor)
                          throws java.io.IOException,
                                 java.lang.Exception
        Throws:
        java.io.IOException
        java.lang.Exception
      • makeDir

        public java.net.URI makeDir​(java.net.URI uri)
                             throws java.net.URISyntaxException
        Throws:
        java.net.URISyntaxException
      • isOffline

        public boolean isOffline()
      • setOffline

        public void setOffline​(java.util.concurrent.atomic.AtomicBoolean offline)