Line data Source code
1 : // Copyright (C) 2016 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.restapi.change; 16 : 17 : import com.google.common.collect.ImmutableMap; 18 : import com.google.common.collect.ImmutableSet; 19 : import com.google.common.collect.Iterables; 20 : import com.google.common.collect.Sets; 21 : import com.google.gerrit.server.change.ArchiveFormatInternal; 22 : import com.google.gerrit.server.config.DownloadConfig; 23 : import com.google.inject.Inject; 24 : import com.google.inject.Singleton; 25 : import java.util.Set; 26 : 27 : @Singleton 28 : public class AllowedFormats { 29 : final ImmutableMap<String, ArchiveFormatInternal> extensions; 30 : final ImmutableSet<ArchiveFormatInternal> allowed; 31 : 32 : @Inject 33 149 : AllowedFormats(DownloadConfig cfg) { 34 149 : ImmutableMap.Builder<String, ArchiveFormatInternal> exts = ImmutableMap.builder(); 35 149 : for (ArchiveFormatInternal format : cfg.getArchiveFormats()) { 36 149 : for (String ext : format.getSuffixes()) { 37 149 : exts.put(ext, format); 38 149 : } 39 149 : exts.put(format.name().toLowerCase(), format); 40 149 : } 41 149 : extensions = exts.build(); 42 : 43 : // Zip is not supported because it may be interpreted by a Java plugin as a 44 : // valid JAR file, whose code would have access to cookies on the domain. 45 149 : allowed = 46 149 : Sets.immutableEnumSet( 47 149 : Iterables.filter(cfg.getArchiveFormats(), f -> f != ArchiveFormatInternal.ZIP)); 48 149 : } 49 : 50 : public Set<ArchiveFormatInternal> getAllowed() { 51 3 : return allowed; 52 : } 53 : 54 : public ImmutableMap<String, ArchiveFormatInternal> getExtensions() { 55 1 : return extensions; 56 : } 57 : }