001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.provider.jar; 018 019import java.io.IOException; 020import java.security.cert.Certificate; 021import java.util.HashMap; 022import java.util.Map; 023import java.util.jar.Attributes; 024import java.util.jar.JarEntry; 025import java.util.jar.JarFile; 026import java.util.jar.Manifest; 027import java.util.zip.ZipEntry; 028 029import org.apache.commons.vfs2.FileSystemException; 030import org.apache.commons.vfs2.provider.AbstractFileName; 031import org.apache.commons.vfs2.provider.zip.ZipFileObject; 032 033/** 034 * A file in a Jar file system. 035 */ 036public class JarFileObject extends ZipFileObject { 037 038 private final JarFileSystem fs; 039 040 private Attributes attributes; 041 042 /** 043 * Constructs a new instance. 044 * 045 * @param fileName the file name. 046 * @param entry the zip entry. 047 * @param fileSystem the file system. 048 * @param zipExists Whether the zip file exists. 049 * @throws FileSystemException if a file system error occurs. 050 */ 051 protected JarFileObject(final AbstractFileName fileName, final ZipEntry entry, final JarFileSystem fileSystem, 052 final boolean zipExists) throws FileSystemException { 053 super(fileName, entry, fileSystem, zipExists); 054 if (entry != null) { 055 // For Java 9 and up: Force the certificates to be read and cached now. This avoids an 056 // IllegalStateException in java.util.jar.JarFile.isMultiRelease() when it tries 057 // to read the certificates and the file is closed. 058 ((JarEntry) entry).getCertificates(); 059 } 060 this.fs = fileSystem; 061 062 try { 063 getAttributes(); // early get the attributes as the zip file might be closed 064 } catch (final IOException e) { 065 throw new FileSystemException(e); 066 } 067 } 068 069 /** 070 * Adds the source attributes to the destination map. 071 */ 072 private void addAll(final Attributes src, final Map<String, Object> dest) { 073 src.forEach((k, v) -> dest.put(String.valueOf(k), v)); 074 } 075 076 /** 077 * Returns the value of an attribute. 078 */ 079 @Override 080 protected Map<String, Object> doGetAttributes() throws Exception { 081 final Map<String, Object> attrs = new HashMap<>(); 082 083 // Add the file system's attributes first 084 final JarFileSystem fs = (JarFileSystem) getFileSystem(); 085 addAll(fs.getAttributes(), attrs); 086 087 // Add this file's attributes 088 addAll(getAttributes(), attrs); 089 090 return attrs; 091 } 092 093 /** 094 * Gets the certificates of this JarEntry. 095 */ 096 @Override 097 protected Certificate[] doGetCertificates() { 098 if (entry == null) { 099 return null; 100 } 101 102 return ((JarEntry) entry).getCertificates(); 103 } 104 105 /** 106 * Returns the attributes of this file. 107 */ 108 Attributes getAttributes() throws IOException { 109 if (attributes == null) { 110 if (entry == null) { 111 attributes = new Attributes(1); 112 } else { 113 attributes = ((JarEntry) entry).getAttributes(); 114 if (attributes == null) { 115 attributes = new Attributes(1); 116 } 117 } 118 } 119 120 return attributes; 121 } 122 123 /** 124 * Returns the Jar manifest. 125 */ 126 Manifest getManifest() throws IOException { 127 if (fs.getZipFile() == null) { 128 return null; 129 } 130 131 return ((JarFile) fs.getZipFile()).getManifest(); 132 } 133}