LCOV - code coverage report
Current view: top level - server/config - DownloadConfig.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 34 41 82.9 %
Date: 2022-11-19 15:00:39 Functions: 6 7 85.7 %

          Line data    Source code
       1             : // Copyright (C) 2010 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.server.config;
      16             : 
      17             : import com.google.common.collect.ImmutableSet;
      18             : import com.google.common.flogger.FluentLogger;
      19             : import com.google.gerrit.common.Nullable;
      20             : import com.google.gerrit.entities.CoreDownloadSchemes;
      21             : import com.google.gerrit.extensions.client.GeneralPreferencesInfo.DownloadCommand;
      22             : import com.google.gerrit.server.change.ArchiveFormatInternal;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.Singleton;
      25             : import java.lang.reflect.Field;
      26             : import java.lang.reflect.Modifier;
      27             : import java.util.EnumSet;
      28             : import java.util.List;
      29             : import org.eclipse.jgit.lib.Config;
      30             : 
      31             : /**
      32             :  * Download protocol from {@code gerrit.config}.
      33             :  *
      34             :  * <p>Only used to configure the built-in set of schemes and commands in the core download-commands
      35             :  * plugin; not used by other plugins.
      36             :  */
      37             : @Singleton
      38             : public class DownloadConfig {
      39         149 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      40             : 
      41             :   private final ImmutableSet<String> downloadSchemes;
      42             :   private final ImmutableSet<DownloadCommand> downloadCommands;
      43             :   private final ImmutableSet<ArchiveFormatInternal> archiveFormats;
      44             : 
      45             :   @Inject
      46         149 :   public DownloadConfig(@GerritServerConfig Config cfg) {
      47         149 :     String[] allSchemes = cfg.getStringList("download", null, "scheme");
      48         149 :     if (allSchemes.length == 0) {
      49         149 :       downloadSchemes =
      50         149 :           ImmutableSet.of(
      51             :               CoreDownloadSchemes.SSH, CoreDownloadSchemes.HTTP, CoreDownloadSchemes.ANON_HTTP);
      52             :     } else {
      53           1 :       ImmutableSet.Builder<String> normalized =
      54           1 :           ImmutableSet.builderWithExpectedSize(allSchemes.length);
      55           1 :       for (String s : allSchemes) {
      56           1 :         String core = toCoreScheme(s);
      57           1 :         if (core == null) {
      58           1 :           logger.atWarning().log("not a core download scheme: %s", s);
      59           1 :           continue;
      60             :         }
      61           0 :         normalized.add(core);
      62             :       }
      63           1 :       downloadSchemes = normalized.build();
      64             :     }
      65             : 
      66         149 :     DownloadCommand[] downloadCommandValues = DownloadCommand.values();
      67         149 :     List<DownloadCommand> allCommands =
      68         149 :         ConfigUtil.getEnumList(cfg, "download", null, "command", downloadCommandValues, null);
      69         149 :     if (isOnlyNull(allCommands)) {
      70         149 :       downloadCommands = ImmutableSet.copyOf(downloadCommandValues);
      71             :     } else {
      72           1 :       downloadCommands = ImmutableSet.copyOf(allCommands);
      73             :     }
      74             : 
      75         149 :     String v = cfg.getString("download", null, "archive");
      76         149 :     if (v == null) {
      77         149 :       archiveFormats = ImmutableSet.copyOf(EnumSet.allOf(ArchiveFormatInternal.class));
      78           2 :     } else if (v.isEmpty() || "off".equalsIgnoreCase(v)) {
      79           1 :       archiveFormats = ImmutableSet.of();
      80             :     } else {
      81           2 :       archiveFormats =
      82           2 :           ImmutableSet.copyOf(
      83           2 :               ConfigUtil.getEnumList(cfg, "download", null, "archive", ArchiveFormatInternal.TGZ));
      84             :     }
      85         149 :   }
      86             : 
      87             :   private static boolean isOnlyNull(List<?> list) {
      88         149 :     return list.size() == 1 && list.get(0) == null;
      89             :   }
      90             : 
      91             :   @Nullable
      92             :   private static String toCoreScheme(String s) {
      93             :     try {
      94           0 :       Field f = CoreDownloadSchemes.class.getField(s.toUpperCase());
      95           0 :       int m = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
      96           0 :       if ((f.getModifiers() & m) == m && f.getType() == String.class) {
      97           0 :         return (String) f.get(null);
      98             :       }
      99           0 :       return null;
     100           1 :     } catch (NoSuchFieldException
     101             :         | SecurityException
     102             :         | IllegalArgumentException
     103             :         | IllegalAccessException e) {
     104           1 :       return null;
     105             :     }
     106             :   }
     107             : 
     108             :   /** Scheme used to download. */
     109             :   public ImmutableSet<String> getDownloadSchemes() {
     110         103 :     return downloadSchemes;
     111             :   }
     112             : 
     113             :   /** Command used to download. */
     114             :   public ImmutableSet<DownloadCommand> getDownloadCommands() {
     115           0 :     return downloadCommands;
     116             :   }
     117             : 
     118             :   /** Archive formats for downloading. */
     119             :   public ImmutableSet<ArchiveFormatInternal> getArchiveFormats() {
     120         149 :     return archiveFormats;
     121             :   }
     122             : }

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