aboutsummaryrefslogtreecommitdiffstats
blob: fe9dbb44e2769dd297564d43c4c62dc5d9c080eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.util.resource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;


/* ------------------------------------------------------------ */
public class JarResource extends URLResource
{
    private static final Logger LOG = Log.getLogger(JarResource.class);
    protected JarURLConnection _jarConnection;
    
    /* -------------------------------------------------------- */
    protected JarResource(URL url)
    {
        super(url,null);
    }

    /* ------------------------------------------------------------ */
    protected JarResource(URL url, boolean useCaches)
    {
        super(url, null, useCaches);
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public synchronized void close()
    {
        _jarConnection=null;
        super.close();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    protected synchronized boolean checkConnection()
    {
        super.checkConnection();
        try
        {
            if (_jarConnection!=_connection)
                newConnection();
        }
        catch(IOException e)
        {
            LOG.ignore(e);
            _jarConnection=null;
        }
        
        return _jarConnection!=null;
    }

    /* ------------------------------------------------------------ */
    /**
     * @throws IOException Sub-classes of <code>JarResource</code> may throw an IOException (or subclass) 
     */
    protected void newConnection() throws IOException
    {
        _jarConnection=(JarURLConnection)_connection;
    }
    
    /* ------------------------------------------------------------ */
    /**
     * Returns true if the respresenetd resource exists.
     */
    @Override
    public boolean exists()
    {
        if (_urlString.endsWith("!/"))
            return checkConnection();
        else
            return super.exists();
    }    

    /* ------------------------------------------------------------ */
    @Override
    public File getFile()
        throws IOException
    {
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public InputStream getInputStream()
        throws java.io.IOException
    {     
        checkConnection();
        if (!_urlString.endsWith("!/"))
            return new FilterInputStream(super.getInputStream()) 
            {
                @Override
                public void close() throws IOException {this.in=IO.getClosedStream();}
            };

        URL url = new URL(_urlString.substring(4,_urlString.length()-2));      
        InputStream is = url.openStream();
        return is;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void copyTo(File directory)
        throws IOException
    {
        if (!exists())
            return;
        
        if(LOG.isDebugEnabled())
            LOG.debug("Extract "+this+" to "+directory);
        
        String urlString = this.getURL().toExternalForm().trim();
        int endOfJarUrl = urlString.indexOf("!/");
        int startOfJarUrl = (endOfJarUrl >= 0?4:0);
        
        if (endOfJarUrl < 0)
            throw new IOException("Not a valid jar url: "+urlString);
        
        URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
        String subEntryName = (endOfJarUrl+2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
        boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/")?true:false);
      
        if (LOG.isDebugEnabled()) 
            LOG.debug("Extracting entry = "+subEntryName+" from jar "+jarFileURL);
        
        InputStream is = jarFileURL.openConnection().getInputStream();
        JarInputStream jin = new JarInputStream(is);
        JarEntry entry;
        boolean shouldExtract;
        while((entry=jin.getNextJarEntry())!=null)
        {
            String entryName = entry.getName();
            if ((subEntryName != null) && (entryName.startsWith(subEntryName)))
            { 
                // is the subentry really a dir?
                if (!subEntryIsDir && subEntryName.length()+1==entryName.length() && entryName.endsWith("/"))
                        subEntryIsDir=true;
                
                //if there is a particular subEntry that we are looking for, only
                //extract it.
                if (subEntryIsDir)
                {
                    //if it is a subdirectory we are looking for, then we
                    //are looking to extract its contents into the target
                    //directory. Remove the name of the subdirectory so
                    //that we don't wind up creating it too.
                    entryName = entryName.substring(subEntryName.length());
                    if (!entryName.equals(""))
                    {
                        //the entry is 
                        shouldExtract = true;                   
                    }
                    else
                        shouldExtract = false;
                }
                else
                    shouldExtract = true;
            }
            else if ((subEntryName != null) && (!entryName.startsWith(subEntryName)))
            {
                //there is a particular entry we are looking for, and this one
                //isn't it
                shouldExtract = false;
            }
            else
            {
                //we are extracting everything
                shouldExtract =  true;
            }
                
            
            if (!shouldExtract)
            {
                if (LOG.isDebugEnabled()) 
                    LOG.debug("Skipping entry: "+entryName);
                continue;
            }
                
            String dotCheck = entryName.replace('\\', '/');   
            dotCheck = URIUtil.canonicalPath(dotCheck);
            if (dotCheck == null)
            {
                if (LOG.isDebugEnabled()) 
                    LOG.debug("Invalid entry: "+entryName);
                continue;
            }

            File file=new File(directory,entryName);
     
            if (entry.isDirectory())
            {
                // Make directory
                if (!file.exists())
                    file.mkdirs();
            }
            else
            {
                // make directory (some jars don't list dirs)
                File dir = new File(file.getParent());
                if (!dir.exists())
                    dir.mkdirs();

                // Make file
                FileOutputStream fout = null;
                try
                {
                    fout = new FileOutputStream(file);
                    IO.copy(jin,fout);
                }
                finally
                {
                    IO.close(fout);
                }

                // touch the file.
                if (entry.getTime()>=0)
                    file.setLastModified(entry.getTime());
            }
        }
        
        if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF")))
        {
            Manifest manifest = jin.getManifest();
            if (manifest != null)
            {
                File metaInf = new File (directory, "META-INF");
                metaInf.mkdir();
                File f = new File(metaInf, "MANIFEST.MF");
                FileOutputStream fout = new FileOutputStream(f);
                manifest.write(fout);
                fout.close();   
            }
        }
        IO.close(jin);
    }   
    
    public static Resource newJarResource(Resource resource) throws IOException
    {
        if (resource instanceof JarResource)
            return resource;
        return Resource.newResource("jar:" + resource + "!/");
    }
}