View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * The SUNW morons restrict the JDK handlers to GET/POST/etc for "security" reasons.
30   * <p>
31   * They do not understand HTTP. This is the hilarious comment in their source:
32   * </p>
33   * <p>
34   * "This restriction will prevent people from using this class to experiment w/ new
35   * HTTP methods using java.  But it should be placed for security - the request String
36   * could be arbitrarily long."
37   * </p>
38   *
39   * @author Christian Bauer
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              // see if the method supports output
82              if (method.equals("PUT") || method.equals("POST") || method.equals("NOTIFY")) {
83                  // fake the method so the superclass method sets its instance variables
84                  method = "PUT";
85              } else {
86                  // use any method that doesn't support output, an exception will be
87                  // raised by the superclass
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 }