LCOV - code coverage report
Current view: top level - httpd - ProxyPropertiesProvider.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 6 18 33.3 %
Date: 2022-11-19 15:00:39 Functions: 4 6 66.7 %

          Line data    Source code
       1             : // Copyright (C) 2014 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.httpd;
      16             : 
      17             : import com.google.common.base.Strings;
      18             : import com.google.gerrit.server.config.GerritServerConfig;
      19             : import com.google.inject.Inject;
      20             : import com.google.inject.Provider;
      21             : import com.google.inject.Singleton;
      22             : import java.net.MalformedURLException;
      23             : import java.net.URL;
      24             : import org.eclipse.jgit.lib.Config;
      25             : 
      26             : @Singleton
      27             : class ProxyPropertiesProvider implements Provider<ProxyProperties> {
      28             : 
      29             :   private URL proxyUrl;
      30             :   private String proxyUser;
      31             :   private String proxyPassword;
      32             : 
      33             :   @Inject
      34          99 :   ProxyPropertiesProvider(@GerritServerConfig Config config) throws MalformedURLException {
      35          99 :     String proxyUrlStr = config.getString("http", null, "proxy");
      36          99 :     if (!Strings.isNullOrEmpty(proxyUrlStr)) {
      37           0 :       proxyUrl = new URL(proxyUrlStr);
      38           0 :       proxyUser = config.getString("http", null, "proxyUsername");
      39           0 :       proxyPassword = config.getString("http", null, "proxyPassword");
      40           0 :       String userInfo = proxyUrl.getUserInfo();
      41           0 :       if (userInfo != null) {
      42           0 :         int c = userInfo.indexOf(':');
      43           0 :         if (0 < c) {
      44           0 :           proxyUser = userInfo.substring(0, c);
      45           0 :           proxyPassword = userInfo.substring(c + 1);
      46             :         } else {
      47           0 :           proxyUser = userInfo;
      48             :         }
      49             :       }
      50             :     }
      51          99 :   }
      52             : 
      53             :   @Override
      54             :   public ProxyProperties get() {
      55          99 :     return new ProxyProperties() {
      56             :       @Override
      57             :       public URL getProxyUrl() {
      58          99 :         return proxyUrl;
      59             :       }
      60             : 
      61             :       @Override
      62             :       public String getUsername() {
      63           0 :         return proxyUser;
      64             :       }
      65             : 
      66             :       @Override
      67             :       public String getPassword() {
      68           0 :         return proxyPassword;
      69             :       }
      70             :     };
      71             :   }
      72             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750