Package net.sourceforge.javautil.developer.web.unit.mockserver

Source Code of net.sourceforge.javautil.developer.web.unit.mockserver.MockServerHostURLStreamHandler$MockServerHostURLConnection

package net.sourceforge.javautil.developer.web.unit.mockserver;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

import net.sourceforge.javautil.developer.web.unit.mockbrowser.MockBrowser;
import net.sourceforge.javautil.developer.web.unit.mockbrowser.MockBrowserRequest;
import net.sourceforge.javautil.developer.web.unit.mockbrowser.MockBrowserResponse;

/**
* This allows {@link MockServerHost}'s to be accessed via normal {@link URL}'s.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class MockServerHostURLStreamHandler extends URLStreamHandler {
 
  public static final String PROTOCOL = "mockweb";
 
  protected final MockBrowser browser;

  public MockServerHostURLStreamHandler(MockBrowser browser) {
    this.browser = browser;
  }

  @Override protected URLConnection openConnection(URL u) throws IOException {
    return new MockServerHostURLConnection(browser, u);
  }
 
  public static class MockServerHostURLConnection extends URLConnection {

    protected final MockBrowser browser;
   
    public MockServerHostURLConnection(MockBrowser browser, URL url) {
      super(url);
      this.browser = browser;
    }

    @Override public void connect() throws IOException {
      this.connected = true;
    }

    @Override public InputStream getInputStream() throws IOException {
      MockBrowserResponse response = browser.process(new MockBrowserRequest(url, "GET"));
      return new ByteArrayInputStream( response.getServerResponse().getResponse().getData() );
    }

    @Override public OutputStream getOutputStream() throws IOException {
     
      return super.getOutputStream();
    }

  }

}
TOP

Related Classes of net.sourceforge.javautil.developer.web.unit.mockserver.MockServerHostURLStreamHandler$MockServerHostURLConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.