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.io.InputStream; 021import java.io.OutputStream; 022import java.net.JarURLConnection; 023import java.net.MalformedURLException; 024import java.net.URL; 025import java.security.cert.Certificate; 026import java.util.jar.Attributes; 027import java.util.jar.JarEntry; 028import java.util.jar.JarFile; 029import java.util.jar.Manifest; 030 031import org.apache.commons.vfs2.FileContent; 032import org.apache.commons.vfs2.FileSystemException; 033 034/** 035 * A default URL connection that will work for most file systems. 036 */ 037public class JarURLConnectionImpl extends JarURLConnection { 038 039 // This is because JarURLConnection SUCKS 040 private static final String HACK_URL = "jar:http://somehost/somejar.jar!/"; 041 042 private final FileContent fileContent; 043 private final URL parentURL; 044 private final JarFileObject jarFileObject; 045 private final String entryName; 046 047 /** 048 * Constructs a new instance. 049 * 050 * @param jarFileObject The JAR file. 051 * @param fileContent THe JAR file contents. 052 * @throws MalformedURLException Should not happen. 053 * @throws FileSystemException if an error occurs accessing the JAR file. 054 */ 055 public JarURLConnectionImpl(final JarFileObject jarFileObject, final FileContent fileContent) throws MalformedURLException, FileSystemException { 056 // This is because JarURLConnection SUCKS!! 057 super(new URL(HACK_URL)); 058 059 url = jarFileObject.getURL(); 060 this.fileContent = fileContent; 061 parentURL = jarFileObject.getURL(); 062 entryName = jarFileObject.getName().getPath(); 063 this.jarFileObject = jarFileObject; 064 } 065 066 @Override 067 public void connect() { 068 connected = true; 069 } 070 071 @Override 072 public Attributes getAttributes() throws IOException { 073 return jarFileObject.getAttributes(); 074 } 075 076 @Override 077 public Certificate[] getCertificates() { 078 return jarFileObject.doGetCertificates(); 079 } 080 081 @Override 082 public int getContentLength() { 083 try { 084 return (int) fileContent.getSize(); 085 } catch (final FileSystemException ignored) { 086 return -1; 087 } 088 } 089 090 @Override 091 public String getEntryName() { 092 return entryName; 093 } 094 095 @Override 096 public InputStream getInputStream() throws IOException { 097 return fileContent.getInputStream(); 098 } 099 100 @Override 101 public JarEntry getJarEntry() throws IOException { 102 throw new FileSystemException("vfs.provider.jar/jar-entry-no-access.error"); 103 } 104 105 @Override 106 public JarFile getJarFile() throws IOException { 107 throw new FileSystemException("vfs.provider.jar/jar-file-no-access.error"); 108 } 109 110 @Override 111 public URL getJarFileURL() { 112 return parentURL; 113 } 114 115 @Override 116 public Manifest getManifest() throws IOException { 117 return jarFileObject.getManifest(); 118 } 119 120 @Override 121 public OutputStream getOutputStream() throws IOException { 122 return fileContent.getOutputStream(); 123 } 124 125}