title: Custom JavaFX WebView Protocol Handler
body: <p>I am trying to write my own protocol handler for a JavaFX application that uses webview to access a single website. What I have done so far</p>
  
  <p>My custom URLStreamHandlerFactory</p>
  
  <pre><code>public class MyURLStreamHandlerFactory implements URLStreamHandlerFactory {
  
      public URLStreamHandler createURLStreamHandler(String protocol) {
          System.out.println("Protocol: " + protocol);
          if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) {
             return new MyURLStreamHandler();
          } else {
              return new URLStreamHandler() {
                  @Override
                 protected URLConnection openConnection(URL u) throws IOException {
                      return new URLConnection(u) {
                          @Override
                          public void connect() throws IOException {
                          }
                      };
                  }
              };
          }
      }
   }
  </code></pre>
  
  <p>My custom URLStreamHandler</p>
  
  <pre><code>public class MyURLStreamHandler extends java.net.URLStreamHandler{
  
      protected HttpURLConnection openConnection(URL u){
          MyURLConnection q = new MyURLConnection(u);
          return q;
      }    
  }
  </code></pre>
  
  <p>My custom HttpURLConnection</p>
  
  <pre><code>public class MyURLConnection extends HttpURLConnection {
  
      static int defaultPort = 443;
      InputStream in;
      OutputStream out;
      Socket s;
  
      publicMyURLConnection(URL url) {
          super(url);
          try {
              setRequestMethod("POST");
          } catch (ProtocolException ex) {
              ex.printStackTrace();
          }
      }
  
      public void setRequestProperty(String name, String value){
          super.setRequestProperty(name, value);
          System.out.println("Namee: " + name);
          System.out.println("Value: " + value);
      }
  
      public String getRequestProperty(String name){
          System.out.println("GET REQUEST: ");
          return super.getRequestProperty(name);
      }
  
      public OutputStream getOutputStream() throws IOException {
          OutputStream os = super.getOutputStream();
          System.out.println("Output: " + os);
          return os;
      }
  
      public InputStream getInputStream() throws IOException {
          InputStream is = super.getInputStream();
          System.out.println("INout stream: " + is);
         return is;
      }
  
      @Override
      public void connect() throws IOException {
      }
  
      @Override
      public void disconnect() {
          throw new UnsupportedOperationException("Not supported yet."); 
      }
  
      @Override
      public boolean usingProxy() {
          throw new UnsupportedOperationException("Not supported yet."); 
      }
  </code></pre>
  
  <p>When I run the application I get the following error althouhg it seems to set some headers</p>
  
  <pre><code>Jul 08, 2013 11:09:04 AM com.sun.webpane.webkit.network.URLLoader doRun
  WARNING: Unexpected error
  java.net.UnknownServiceException: protocol doesn't support input
  at java.net.URLConnection.getInputStream(URLConnection.java:839)
  at qmed.QMedURLConnection.getInputStream(MyURLConnection.java:67)
  at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
  at com.sun.webpane.webkit.network.URLLoader.receiveResponse(URLLoader.java:383)
  at com.sun.webpane.webkit.network.URLLoader.doRun(URLLoader.java:142)
  at com.sun.webpane.webkit.network.URLLoader.access$000(URLLoader.java:44)
  at com.sun.webpane.webkit.network.URLLoader$1.run(URLLoader.java:106)
  at com.sun.webpane.webkit.network.URLLoader$1.run(URLLoader.java:103)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.webpane.webkit.network.URLLoader.run(URLLoader.java:103)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
  at java.util.concurrent.FutureTask.run(FutureTask.java:166)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:724)
  </code></pre>
  
  <p>All I want to do is get the response back for a given request and reads its binary data. I want the protocol to behave exactly the same way as the default one and only check the binary data of a given respone. What am I doing wrong? </p>
  
  <p>The application is doing all shorts of URLConnections. Is it correct to use a HTTPURLConnection as my custom URLConnection class when the protocol is http or https and start a default URLStreamHandler when other protocols are used like I am doing in MyURLStreamHandlerFactory? Should I just extend the default URLConnection class in MYURLConnection to handle all protocols the same?</p>
  
  <p>Any help would be much appreciated as this is a project threatening problem</p>
  
  <p>Thank you</p>