Coverage Report - org.owasp.dependencycheck.utils.URLConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
URLConnectionFactory
0%
0/34
0%
0/10
4
URLConnectionFactory$1
0%
0/3
0%
0/2
4
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.net.Authenticator;
 22  
 import java.net.HttpURLConnection;
 23  
 import java.net.InetSocketAddress;
 24  
 import java.net.PasswordAuthentication;
 25  
 import java.net.Proxy;
 26  
 import java.net.SocketAddress;
 27  
 import java.net.URL;
 28  
 
 29  
 /**
 30  
  * A URLConnection Factory to create new connections. This encapsulates several configuration checks to ensure that the
 31  
  * connection uses the correct proxy settings.
 32  
  *
 33  
  * @author Jeremy Long <jeremy.long@owasp.org>
 34  
  */
 35  
 public final class URLConnectionFactory {
 36  
 
 37  
     /**
 38  
      * Private constructor for this factory.
 39  
      */
 40  
     private URLConnectionFactory() {
 41  
     }
 42  
 
 43  
     /**
 44  
      * Utility method to create an HttpURLConnection. If the application is configured to use a proxy this method will
 45  
      * retrieve the proxy settings and use them when setting up the connection.
 46  
      *
 47  
      * @param url the url to connect to
 48  
      * @return an HttpURLConnection
 49  
      * @throws URLConnectionFailureException thrown if there is an exception
 50  
      */
 51  
     public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException {
 52  0
         HttpURLConnection conn = null;
 53  
         Proxy proxy;
 54  0
         final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER);
 55  
         try {
 56  0
             if (proxyUrl != null) {
 57  0
                 final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
 58  0
                 final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort);
 59  
 
 60  0
                 final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME);
 61  0
                 final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD);
 62  0
                 if (username != null && password != null) {
 63  0
                     final Authenticator auth = new Authenticator() {
 64  
                         @Override
 65  
                         public PasswordAuthentication getPasswordAuthentication() {
 66  0
                             if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) {
 67  0
                                 return new PasswordAuthentication(username, password.toCharArray());
 68  
                             }
 69  0
                             return super.getPasswordAuthentication();
 70  
                         }
 71  
                     };
 72  0
                     Authenticator.setDefault(auth);
 73  
                 }
 74  
 
 75  0
                 proxy = new Proxy(Proxy.Type.HTTP, address);
 76  0
                 conn = (HttpURLConnection) url.openConnection(proxy);
 77  0
             } else {
 78  0
                 conn = (HttpURLConnection) url.openConnection();
 79  
             }
 80  0
             final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 60000);
 81  0
             conn.setConnectTimeout(timeout);
 82  0
         } catch (IOException ex) {
 83  0
             if (conn != null) {
 84  
                 try {
 85  0
                     conn.disconnect();
 86  
                 } finally {
 87  0
                     conn = null;
 88  0
                 }
 89  
             }
 90  0
             throw new URLConnectionFailureException("Error getting connection.", ex);
 91  0
         }
 92  0
         return conn;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Utility method to create an HttpURLConnection. The use of a proxy here is optional as there may be cases where a
 97  
      * proxy is configured but we don't want to use it (for example, if there's an internal repository configured)
 98  
      *
 99  
      * @param url the URL to connect to
 100  
      * @param proxy whether to use the proxy (if configured)
 101  
      * @return a newly constructed HttpURLConnection
 102  
      * @throws URLConnectionFailureException thrown if there is an exception
 103  
      */
 104  
     public static HttpURLConnection createHttpURLConnection(URL url, boolean proxy) throws URLConnectionFailureException {
 105  0
         if (proxy) {
 106  0
             return createHttpURLConnection(url);
 107  
         }
 108  0
         HttpURLConnection conn = null;
 109  
         try {
 110  0
             conn = (HttpURLConnection) url.openConnection();
 111  0
             final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 60000);
 112  0
             conn.setConnectTimeout(timeout);
 113  0
         } catch (IOException ioe) {
 114  0
             throw new URLConnectionFailureException("Error getting connection.", ioe);
 115  0
         }
 116  0
         return conn;
 117  
     }
 118  
 }