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    // This is because JarURLConnection SUCKS
039    private static final String HACK_URL = "jar:http://somehost/somejar.jar!/";
040
041    private final FileContent content;
042    private final URL parentURL;
043    private final JarFileObject file;
044    private final String entryName;
045
046    public JarURLConnectionImpl(final JarFileObject file, final FileContent content)
047            throws MalformedURLException, FileSystemException {
048        // This is because JarURLConnection SUCKS!!
049        super(new URL(HACK_URL));
050
051        this.url = file.getURL();
052        this.content = content;
053        this.parentURL = file.getURL();
054        this.entryName = file.getName().getPath();
055        this.file = file;
056    }
057
058    @Override
059    public URL getJarFileURL() {
060        return parentURL;
061    }
062
063    @Override
064    public String getEntryName() {
065        return entryName;
066    }
067
068    @Override
069    public JarFile getJarFile() throws IOException {
070        throw new FileSystemException("vfs.provider.jar/jar-file-no-access.error");
071    }
072
073    @Override
074    public Manifest getManifest() throws IOException {
075        return file.getManifest();
076    }
077
078    @Override
079    public JarEntry getJarEntry() throws IOException {
080        throw new FileSystemException("vfs.provider.jar/jar-entry-no-access.error");
081    }
082
083    @Override
084    public Attributes getAttributes() throws IOException {
085        return file.getAttributes();
086    }
087
088    @Override
089    public Certificate[] getCertificates() {
090        return file.doGetCertificates();
091    }
092
093    @Override
094    public void connect() {
095        connected = true;
096    }
097
098    @Override
099    public InputStream getInputStream() throws IOException {
100        return content.getInputStream();
101    }
102
103    @Override
104    public OutputStream getOutputStream() throws IOException {
105        return content.getOutputStream();
106    }
107
108    @Override
109    public int getContentLength() {
110        try {
111            return (int) content.getSize();
112        } catch (final FileSystemException ignored) {
113            return -1;
114        }
115    }
116
117}