| 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 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 17 | import com.google.common.base.Enums; |
| 18 | import com.google.common.base.Optional; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 21 | |
| 22 | import org.eclipse.jgit.api.ArchiveCommand; |
| 23 | import org.eclipse.jgit.archive.TarFormat; |
| 24 | import org.eclipse.jgit.archive.Tbz2Format; |
| 25 | import org.eclipse.jgit.archive.TgzFormat; |
| 26 | import org.eclipse.jgit.archive.TxzFormat; |
| 27 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 28 | |
| Dave Borowitz | 29168c6 | 2014-04-16 12:08:23 -0700 | [diff] [blame] | 29 | public enum ArchiveFormat { |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 30 | TGZ("application/x-gzip", new TgzFormat()), |
| 31 | TAR("application/x-tar", new TarFormat()), |
| 32 | TBZ2("application/x-bzip2", new Tbz2Format()), |
| 33 | TXZ("application/x-xz", new TxzFormat()); |
| 34 | // Zip is not supported because it may be interpreted by a Java plugin as a |
| 35 | // valid JAR file, whose code would have access to cookies on the domain. |
| 36 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 37 | private static final ImmutableMap<String, ArchiveFormat> BY_EXT; |
| 38 | static { |
| 39 | ImmutableMap.Builder<String, ArchiveFormat> byExt = ImmutableMap.builder(); |
| 40 | for (ArchiveFormat format : ArchiveFormat.values()) { |
| 41 | for (String ext : format.getSuffixes()) { |
| 42 | byExt.put(ext.toLowerCase(), format); |
| 43 | } |
| 44 | } |
| 45 | BY_EXT = byExt.build(); |
| 46 | } |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 47 | |
| Dave Borowitz | 29168c6 | 2014-04-16 12:08:23 -0700 | [diff] [blame] | 48 | /** Unregister all JGit archive formats supported by Gitiles. */ |
| 49 | public static void unregisterAll() { |
| 50 | for (ArchiveFormat fmt : values()) { |
| Dave Borowitz | 36eb26d | 2014-04-19 16:42:32 -0700 | [diff] [blame] | 51 | ArchiveCommand.unregisterFormat(fmt.getRegisteredName()); |
| Dave Borowitz | 29168c6 | 2014-04-16 12:08:23 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 55 | private final ArchiveCommand.Format<?> format; |
| 56 | private final String mimeType; |
| 57 | |
| 58 | private ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) { |
| 59 | this.format = format; |
| 60 | this.mimeType = mimeType; |
| Dave Borowitz | 36eb26d | 2014-04-19 16:42:32 -0700 | [diff] [blame] | 61 | ArchiveCommand.registerFormat(getRegisteredName(), format); |
| 62 | } |
| 63 | |
| 64 | String getRegisteredName() { |
| 65 | return getShortName(); |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | String getShortName() { |
| 69 | return name().toLowerCase(); |
| 70 | } |
| 71 | |
| 72 | String getMimeType() { |
| 73 | return mimeType; |
| 74 | } |
| 75 | |
| 76 | String getDefaultSuffix() { |
| 77 | return getSuffixes().iterator().next(); |
| 78 | } |
| 79 | |
| 80 | Iterable<String> getSuffixes() { |
| 81 | return format.suffixes(); |
| 82 | } |
| 83 | |
| 84 | static ArchiveFormat getDefault(Config cfg) { |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 85 | for (String allowed : cfg.getStringList("archive", null, "format")) { |
| 86 | Optional<ArchiveFormat> result = |
| 87 | Enums.getIfPresent(ArchiveFormat.class, allowed.toUpperCase()); |
| 88 | if (result.isPresent()) { |
| 89 | return result.get(); |
| 90 | } |
| 91 | } |
| 92 | return TGZ; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 95 | static ImmutableSet<String> allExtensions() { |
| 96 | return BY_EXT.keySet(); |
| 97 | } |
| 98 | |
| 99 | static Optional<ArchiveFormat> byExtension(String ext, Config cfg) { |
| 100 | ArchiveFormat format = BY_EXT.get(ext); |
| 101 | if (format == null) { |
| 102 | return Optional.absent(); |
| 103 | } |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 104 | String[] formats = cfg.getStringList("archive", null, "format"); |
| 105 | if (formats.length == 0) { |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 106 | return Optional.of(format); |
| 107 | } |
| 108 | for (String allowed : formats) { |
| 109 | if (format.name().equals(allowed.toUpperCase())) { |
| 110 | return Optional.of(format); |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 113 | return Optional.absent(); |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 114 | } |
| 115 | } |