1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.transport.impl;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.net.ProtocolException;
21 import java.net.Proxy;
22 import java.net.URL;
23 import java.net.URLStreamHandler;
24 import java.net.URLStreamHandlerFactory;
25 import java.util.logging.Logger;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class FixedSunURLStreamHandler implements URLStreamHandlerFactory {
42
43 final private static Logger log = Logger.getLogger(FixedSunURLStreamHandler.class.getName());
44
45 public URLStreamHandler createURLStreamHandler(String protocol) {
46 log.fine("Creating new URLStreamHandler for protocol: " + protocol);
47 if ("http".equals(protocol)) {
48 return new sun.net.www.protocol.http.Handler() {
49
50 protected java.net.URLConnection openConnection(URL u) throws IOException {
51 return openConnection(u, null);
52 }
53
54 protected java.net.URLConnection openConnection(URL u, Proxy p) throws IOException {
55 return new UpnpURLConnection(u, this);
56 }
57 };
58 } else {
59 return null;
60 }
61 }
62
63 static class UpnpURLConnection extends sun.net.www.protocol.http.HttpURLConnection {
64
65 private static final String[] methods = {
66 "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE",
67 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY"
68 };
69
70 protected UpnpURLConnection(URL u, sun.net.www.protocol.http.Handler handler) throws IOException {
71 super(u, handler);
72 }
73
74 public UpnpURLConnection(URL u, String host, int port) throws IOException {
75 super(u, host, port);
76 }
77
78 public synchronized OutputStream getOutputStream() throws IOException {
79 OutputStream os;
80 String savedMethod = method;
81
82 if (method.equals("PUT") || method.equals("POST") || method.equals("NOTIFY")) {
83
84 method = "PUT";
85 } else {
86
87
88 method = "GET";
89 }
90 os = super.getOutputStream();
91 method = savedMethod;
92 return os;
93 }
94
95 public void setRequestMethod(String method) throws ProtocolException {
96 if (connected) {
97 throw new ProtocolException("Cannot reset method once connected");
98 }
99 for (String m : methods) {
100 if (m.equals(method)) {
101 this.method = method;
102 return;
103 }
104 }
105 throw new ProtocolException("Invalid UPnP HTTP method: " + method);
106 }
107 }
108 }