| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame^] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 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.gitiles; |
| 16 | |
| 17 | import com.google.common.collect.Maps; |
| 18 | |
| 19 | import org.eclipse.jgit.api.ArchiveCommand; |
| 20 | import org.eclipse.jgit.archive.TarFormat; |
| 21 | import org.eclipse.jgit.archive.Tbz2Format; |
| 22 | import org.eclipse.jgit.archive.TgzFormat; |
| 23 | import org.eclipse.jgit.archive.TxzFormat; |
| 24 | import org.eclipse.jgit.lib.Config; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Collections; |
| 29 | import java.util.Map; |
| 30 | |
| 31 | enum ArchiveFormat { |
| 32 | TGZ("application/x-gzip", new TgzFormat()), |
| 33 | TAR("application/x-tar", new TarFormat()), |
| 34 | TBZ2("application/x-bzip2", new Tbz2Format()), |
| 35 | TXZ("application/x-xz", new TxzFormat()); |
| 36 | // Zip is not supported because it may be interpreted by a Java plugin as a |
| 37 | // valid JAR file, whose code would have access to cookies on the domain. |
| 38 | |
| 39 | static final Logger log = LoggerFactory.getLogger(ArchiveFormat.class); |
| 40 | |
| 41 | private final ArchiveCommand.Format<?> format; |
| 42 | private final String mimeType; |
| 43 | |
| 44 | private ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) { |
| 45 | this.format = format; |
| 46 | this.mimeType = mimeType; |
| 47 | ArchiveCommand.registerFormat(name(), format); |
| 48 | } |
| 49 | |
| 50 | String getShortName() { |
| 51 | return name().toLowerCase(); |
| 52 | } |
| 53 | |
| 54 | String getMimeType() { |
| 55 | return mimeType; |
| 56 | } |
| 57 | |
| 58 | String getDefaultSuffix() { |
| 59 | return getSuffixes().iterator().next(); |
| 60 | } |
| 61 | |
| 62 | Iterable<String> getSuffixes() { |
| 63 | return format.suffixes(); |
| 64 | } |
| 65 | |
| 66 | static ArchiveFormat getDefault(Config cfg) { |
| 67 | return byExtension(cfg).values().iterator().next(); |
| 68 | } |
| 69 | |
| 70 | static Map<String, ArchiveFormat> byExtension(Config cfg) { |
| 71 | String[] formats = cfg.getStringList("archive", null, "format"); |
| 72 | if (formats.length == 0) { |
| 73 | formats = new String[values().length]; |
| 74 | for (int i = 0; i < values().length; i++) { |
| 75 | formats[i] = values()[i].name(); |
| 76 | } |
| 77 | } |
| 78 | Map<String, ArchiveFormat> exts = Maps.newLinkedHashMap(); |
| 79 | for (String name : formats) { |
| 80 | try { |
| 81 | ArchiveFormat format = valueOf(name.toUpperCase()); |
| 82 | for (String ext : format.getSuffixes()) { |
| 83 | exts.put(ext, format); |
| 84 | } |
| 85 | } catch (IllegalArgumentException e) { |
| 86 | log.warn("Invalid archive.format {}", name); |
| 87 | } |
| 88 | } |
| 89 | return Collections.unmodifiableMap(exts); |
| 90 | } |
| 91 | } |