Line data Source code
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.gerrit.server.change; 16 : 17 : import java.io.Closeable; 18 : import java.io.IOException; 19 : import java.io.OutputStream; 20 : import org.apache.commons.compress.archivers.ArchiveOutputStream; 21 : import org.eclipse.jgit.api.ArchiveCommand; 22 : import org.eclipse.jgit.api.ArchiveCommand.Format; 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.archive.ZipFormat; 28 : import org.eclipse.jgit.lib.FileMode; 29 : import org.eclipse.jgit.lib.ObjectLoader; 30 : 31 149 : public enum ArchiveFormatInternal { 32 149 : TGZ("application/x-gzip", new TgzFormat()), 33 149 : TAR("application/x-tar", new TarFormat()), 34 149 : TBZ2("application/x-bzip2", new Tbz2Format()), 35 149 : TXZ("application/x-xz", new TxzFormat()), 36 149 : ZIP("application/x-zip", new ZipFormat()); 37 : 38 : @SuppressWarnings("ImmutableEnumChecker") // ArchiveCommand.Format is effectively immutable. 39 : private final ArchiveCommand.Format<?> format; 40 : 41 : private final String mimeType; 42 : 43 149 : ArchiveFormatInternal(String mimeType, ArchiveCommand.Format<?> format) { 44 149 : this.format = format; 45 149 : this.mimeType = mimeType; 46 149 : ArchiveCommand.registerFormat(name(), format); 47 149 : } 48 : 49 : public String getShortName() { 50 3 : return name().toLowerCase(); 51 : } 52 : 53 : public String getMimeType() { 54 1 : return mimeType; 55 : } 56 : 57 : public String getDefaultSuffix() { 58 1 : return getSuffixes().iterator().next(); 59 : } 60 : 61 : public Iterable<String> getSuffixes() { 62 149 : return format.suffixes(); 63 : } 64 : 65 : public ArchiveOutputStream createArchiveOutputStream(OutputStream o) throws IOException { 66 0 : return (ArchiveOutputStream) this.format.createArchiveOutputStream(o); 67 : } 68 : 69 : public <T extends Closeable> void putEntry(T out, String path, byte[] data) throws IOException { 70 : @SuppressWarnings("unchecked") 71 0 : ArchiveCommand.Format<T> fmt = (Format<T>) format; 72 0 : fmt.putEntry( 73 : out, 74 : null, 75 : path, 76 : FileMode.REGULAR_FILE, 77 : new ObjectLoader.SmallObject(FileMode.TYPE_FILE, data)); 78 0 : } 79 : }