View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009 Intalio, Inc.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // Contributors:
13  //    Hugues Malphettes - initial API and implementation
14  // ========================================================================
15  package org.eclipse.jetty.osgi.boot.warurl;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.net.JarURLConnection;
20  import java.net.URL;
21  import java.net.URLConnection;
22  import java.util.jar.Manifest;
23  
24  import org.eclipse.jetty.osgi.boot.warurl.internal.WarBundleManifestGenerator;
25  import org.eclipse.jetty.osgi.boot.warurl.internal.WarURLConnection;
26  import org.eclipse.jetty.util.URIUtil;
27  import org.osgi.service.url.AbstractURLStreamHandlerService;
28  
29  /**
30   * RFC-66: support for the "war" protocol
31   * We are reusing the parsing of the query string from jetty.
32   * If we wanted to not depend on jetty at all we could duplicate that method here
33   */
34  public class WarUrlStreamHandler extends AbstractURLStreamHandlerService
35  {
36  
37      /**
38       * @param url The url with a war scheme
39       */
40      @Override
41      public URLConnection openConnection(URL url) throws IOException
42      {
43          //remove the war scheme.
44          URL actual = new URL(url.toString().substring("war:".length()));
45          
46          //let's do some basic tests: see if this is a folder or not.
47          //if it is a folder. we will try to support it.
48          if (actual.getProtocol().equals("file"))
49          {
50              File file = new File(URIUtil.encodePath(actual.getPath()));
51              if (file.exists())
52              {
53                  if (file.isDirectory())
54                  {
55                      //TODO (not mandatory for rfc66 though)
56                  }
57              }
58          }
59          
60        //  if (actual.toString().startsWith("file:/") && ! actual.to)
61          URLConnection ori = (URLConnection) actual.openConnection();
62          JarURLConnection jarOri = null;
63          try {
64              if (ori instanceof JarURLConnection)
65              {
66                  jarOri = (JarURLConnection)ori;
67              }
68              else
69              {
70                  jarOri = (JarURLConnection) new URL("jar:"+actual.toString() + "!/").openConnection();
71              }
72              Manifest mf = WarBundleManifestGenerator.createBundleManifest(
73                      jarOri.getManifest(), url, jarOri.getJarFile());
74              try { jarOri.getJarFile().close(); jarOri = null; } catch (Throwable t) {}
75              return new WarURLConnection(actual,mf);
76          }
77          finally
78          {
79              if (jarOri != null) try { jarOri.getJarFile().close(); } catch (Throwable t) {}
80          }
81          
82      }
83      
84  
85  }