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: */
017: package org.apache.commons.vfs.provider.jar;
018:
019: import org.apache.commons.vfs.FileContent;
020: import org.apache.commons.vfs.FileSystemException;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.net.JarURLConnection;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.security.cert.Certificate;
029: import java.util.jar.Attributes;
030: import java.util.jar.JarEntry;
031: import java.util.jar.JarFile;
032: import java.util.jar.Manifest;
033:
034: /**
035: * A default URL connection that will work for most file systems.
036: *
037: * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
038: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
039: */
040: public class JarURLConnectionImpl extends JarURLConnection {
041: // This is because JarURLConnection SUCKS
042: private static final String HACK_URL = "jar:http://somehost/somejar.jar!/";
043:
044: private FileContent content;
045: private URL parentURL;
046: private JarFileObject file;
047: private String entryName;
048:
049: public JarURLConnectionImpl(JarFileObject file, FileContent content)
050: throws MalformedURLException, FileSystemException {
051: //This is because JarURLConnection SUCKS!!
052: super (new URL(HACK_URL));
053:
054: this .url = file.getURL();
055: this .content = content;
056: this .parentURL = file.getURL();
057: this .entryName = file.getName().getPath();
058: this .file = file;
059: }
060:
061: public URL getJarFileURL() {
062: return parentURL;
063: }
064:
065: public String getEntryName() {
066: return entryName;
067: }
068:
069: public JarFile getJarFile() throws IOException {
070: throw new FileSystemException(
071: "vfs.provider.jar/jar-file-no-access.error");
072: }
073:
074: public Manifest getManifest() throws IOException {
075: return file.getManifest();
076: }
077:
078: public JarEntry getJarEntry() throws IOException {
079: throw new FileSystemException(
080: "vfs.provider.jar/jar-entry-no-access.error");
081: }
082:
083: public Attributes getAttributes() throws IOException {
084: return file.getAttributes();
085: }
086:
087: public Certificate[] getCertificates() {
088: return file.doGetCertificates();
089: }
090:
091: public void connect() {
092: connected = true;
093: }
094:
095: public InputStream getInputStream() throws IOException {
096: return content.getInputStream();
097: }
098:
099: public OutputStream getOutputStream() throws IOException {
100: return content.getOutputStream();
101: }
102:
103: public int getContentLength() {
104: try {
105: return (int) content.getSize();
106: } catch (FileSystemException fse) {
107: }
108:
109: return -1;
110: }
111:
112: }
|