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.eclipse.jetty.util.resource.Resource;
28  import org.osgi.service.url.AbstractURLStreamHandlerService;
29  
30  /**
31   * RFC-66: support for the "war" protocol We are reusing the parsing of the
32   * query string from jetty. If we wanted to not depend on jetty at all we could
33   * duplicate that method here
34   */
35  public class WarUrlStreamHandler extends AbstractURLStreamHandlerService
36  {
37  
38      /**
39       * @param url The url with a war scheme
40       */
41      @Override
42      public URLConnection openConnection(URL url) throws IOException
43      {
44          // remove the war scheme.
45          URL actual = new URL(url.toString().substring("war:".length()));
46  
47          // let's do some basic tests: see if this is a folder or not.
48          // if it is a folder. we will try to support it.
49          if (actual.getProtocol().equals("file"))
50          {
51              File file = new File(URIUtil.encodePath(actual.getPath()));
52              if (file.exists())
53              {
54                  if (file.isDirectory())
55                  {
56                      // TODO (not mandatory for rfc66 though)
57                  }
58              }
59          }
60  
61          // if (actual.toString().startsWith("file:/") && ! actual.to)
62          URLConnection ori = (URLConnection) actual.openConnection();
63          ori.setDefaultUseCaches(Resource.getDefaultUseCaches());
64          JarURLConnection jarOri = null;
65          try
66          {
67              if (ori instanceof JarURLConnection)
68              {
69                  jarOri = (JarURLConnection) ori;
70              }
71              else
72              {
73                  jarOri = (JarURLConnection) new URL("jar:" + actual.toString() + "!/").openConnection();
74                  jarOri.setDefaultUseCaches(Resource.getDefaultUseCaches());
75              }
76              Manifest mf = WarBundleManifestGenerator.createBundleManifest(jarOri.getManifest(), url, jarOri.getJarFile());
77              try
78              {
79                  jarOri.getJarFile().close();
80                  jarOri = null;
81              }
82              catch (Throwable t)
83              {
84              }
85              return new WarURLConnection(actual, mf);
86          }
87          finally
88          {
89              if (jarOri != null) try
90              {
91                  jarOri.getJarFile().close();
92              }
93              catch (Throwable t)
94              {
95              }
96          }
97  
98      }
99  
100 }