Initial import
[jpf-core.git] / src / classes / sun / net / www / protocol / http / Handler.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package sun.net.www.protocol.http;
19
20 import gov.nasa.jpf.CachedROHttpConnection;
21
22 import java.io.IOException;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.Proxy;
26 import java.net.URL;
27
28 /**
29  * replaced handler to support configured URLConnection classes
30  */
31 public class Handler extends java.net.URLStreamHandler {
32
33   protected String proxy;
34   protected int proxyPort;
35
36   public Handler() {
37     proxy = null;
38     proxyPort = -1;
39   }
40
41   public Handler(String proxy, int port) {
42     this.proxy = proxy;
43     this.proxyPort = port;
44   }
45
46   @Override
47   protected int getDefaultPort() {
48     return 80;
49   }
50
51
52   static Class<?>[] argTypes = { URL.class, Proxy.class };
53   private native Class<? extends java.net.URLConnection> getConnectionClass(String url);
54
55   
56   @Override
57   protected java.net.URLConnection openConnection (URL u, Proxy p) throws IOException {
58
59     Class<? extends java.net.URLConnection> clazz = getConnectionClass(u.toString());
60
61     if (clazz != null){
62       try {
63         Constructor<? extends java.net.URLConnection> ctor = clazz.getConstructor(argTypes);
64         return ctor.newInstance(u, p);
65
66       } catch (NoSuchMethodException nmx){
67         throw new IOException("connection class has no suitabe ctor: " + clazz.getName());
68       } catch (IllegalAccessException iax){
69         throw new IOException("connection class has no public ctor: " + clazz.getName());
70       } catch (InvocationTargetException itx){
71         throw new IOException("exception initializing URLConnection", itx);
72       } catch (InstantiationException ix){
73         throw new IOException("connection class cannot be instantiated: " + clazz.getName());
74       }
75
76     } else {
77       // we just go with the standard thing, hoping that we have a modeled Socket layer
78       return new CachedROHttpConnection(u, p, this);
79     }
80   }
81
82   @Override
83   protected java.net.URLConnection openConnection(URL u) throws IOException {
84     return openConnection(u, null);
85   }
86
87   //... and a lot of methods still missing
88 }