Coverage Report - org.owasp.dependencycheck.utils.URLConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
URLConnectionFactory
0%
0/38
0%
0/10
4
URLConnectionFactory$1
0%
0/4
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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 21  
 import java.io.IOException;
 22  
 import java.net.Authenticator;
 23  
 import java.net.HttpURLConnection;
 24  
 import java.net.InetSocketAddress;
 25  
 import java.net.PasswordAuthentication;
 26  
 import java.net.Proxy;
 27  
 import java.net.SocketAddress;
 28  
 import java.net.URL;
 29  
 
 30  
 /**
 31  
  * A URLConnection Factory to create new connections. This encapsulates several configuration checks to ensure that the connection
 32  
  * uses the correct proxy settings.
 33  
  *
 34  
  * @author Jeremy Long
 35  
  */
 36  
 public final class URLConnectionFactory {
 37  
 
 38  
     /**
 39  
      * Private constructor for this factory.
 40  
      */
 41  0
     private URLConnectionFactory() {
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Utility method to create an HttpURLConnection. If the application is configured to use a proxy this method will retrieve
 46  
      * the proxy settings and use them when setting up the connection.
 47  
      *
 48  
      * @param url the url to connect to
 49  
      * @return an HttpURLConnection
 50  
      * @throws URLConnectionFailureException thrown if there is an exception
 51  
      */
 52  
     @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Just being extra safe")
 53  
     public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException {
 54  0
         HttpURLConnection conn = null;
 55  0
         final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER);
 56  
         try {
 57  0
             if (proxyUrl != null) {
 58  0
                 final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
 59  0
                 final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort);
 60  
 
 61  0
                 final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME);
 62  0
                 final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD);
 63  0
                 if (username != null && password != null) {
 64  0
                     final Authenticator auth = new Authenticator() {
 65  
                         @Override
 66  
                         public PasswordAuthentication getPasswordAuthentication() {
 67  0
                             if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) {
 68  0
                                 return new PasswordAuthentication(username, password.toCharArray());
 69  
                             }
 70  0
                             return super.getPasswordAuthentication();
 71  
                         }
 72  
                     };
 73  0
                     Authenticator.setDefault(auth);
 74  
                 }
 75  
 
 76  0
                 final Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
 77  0
                 conn = (HttpURLConnection) url.openConnection(proxy);
 78  0
             } else {
 79  0
                 conn = (HttpURLConnection) url.openConnection();
 80  
             }
 81  0
             final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000);
 82  0
             conn.setConnectTimeout(timeout);
 83  0
             conn.setInstanceFollowRedirects(true);
 84  0
         } catch (IOException ex) {
 85  0
             if (conn != null) {
 86  
                 try {
 87  0
                     conn.disconnect();
 88  
                 } finally {
 89  0
                     conn = null;
 90  0
                 }
 91  
             }
 92  0
             throw new URLConnectionFailureException("Error getting connection.", ex);
 93  0
         }
 94  0
         return conn;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Utility method to create an HttpURLConnection. The use of a proxy here is optional as there may be cases where a proxy is
 99  
      * configured but we don't want to use it (for example, if there's an internal repository configured)
 100  
      *
 101  
      * @param url the URL to connect to
 102  
      * @param proxy whether to use the proxy (if configured)
 103  
      * @return a newly constructed HttpURLConnection
 104  
      * @throws URLConnectionFailureException thrown if there is an exception
 105  
      */
 106  
     public static HttpURLConnection createHttpURLConnection(URL url, boolean proxy) throws URLConnectionFailureException {
 107  0
         if (proxy) {
 108  0
             return createHttpURLConnection(url);
 109  
         }
 110  0
         HttpURLConnection conn = null;
 111  
         try {
 112  0
             conn = (HttpURLConnection) url.openConnection();
 113  0
             final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000);
 114  0
             conn.setConnectTimeout(timeout);
 115  0
             conn.setInstanceFollowRedirects(true);
 116  0
         } catch (IOException ioe) {
 117  0
             throw new URLConnectionFailureException("Error getting connection.", ioe);
 118  0
         }
 119  0
         return conn;
 120  
     }
 121  
 }