/** Create a client {@link Transceiver} connecting to the provided URI. */ public static Transceiver createTransceiver(URI uri) throws IOException { if ("http".equals(uri.getScheme())) return new HttpTransceiver(uri.toURL()); else if ("avro".equals(uri.getScheme())) return new SaslSocketTransceiver (new InetSocketAddress(uri.getHost(), uri.getPort())); else throw new IOException("unknown uri scheme: "+uri); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(HttpTransceiver.CONTENT_TYPE); List<ByteBuffer> requestBufs = HttpTransceiver.readBuffers(request.getInputStream()); try { List<ByteBuffer> responseBufs = responder.respond(requestBufs); response.setContentLength(HttpTransceiver.getLength(responseBufs)); HttpTransceiver.writeBuffers(responseBufs, response.getOutputStream()); } catch (AvroRuntimeException e) { throw new ServletException(e); } } }
static void writeBuffers(List<ByteBuffer> buffers, OutputStream out) throws IOException { for (ByteBuffer buffer : buffers) { writeLength(buffer.limit(), out); // length-prefix out.write(buffer.array(), buffer.position(), buffer.remaining()); buffer.position(buffer.limit()); } writeLength(0, out); // null-terminate }
public synchronized void writeBuffers(List<ByteBuffer> buffers) throws IOException { if (proxy == null) connection = (HttpURLConnection)url.openConnection(); else connection = (HttpURLConnection)url.openConnection(proxy); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", CONTENT_TYPE); connection.setRequestProperty("Content-Length", Integer.toString(getLength(buffers))); connection.setDoOutput(true); connection.setReadTimeout(timeout); connection.setConnectTimeout(timeout); OutputStream out = connection.getOutputStream(); try { writeBuffers(buffers, out); } finally { out.close(); } }
@Test(expected=SocketTimeoutException.class) public void testTimeout() throws Throwable { ServerSocket s = new ServerSocket(0); HttpTransceiver client = new HttpTransceiver(new URL("http://127.0.0.1:"+s.getLocalPort()+"/")); client.setTimeout(100); Simple proxy = SpecificRequestor.getClient(Simple.class, client); try { proxy.hello("foo"); } catch (AvroRemoteException e) { throw e.getCause(); } finally { s.close(); } }
public synchronized List<ByteBuffer> readBuffers() throws IOException { InputStream in = connection.getInputStream(); try { return readBuffers(in); } finally { in.close(); } }
public synchronized void writeBuffers(List<ByteBuffer> buffers) throws IOException { writeBuffers(buffers, connection.getOutputStream()); }
@Override public synchronized List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { this.connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", CONTENT_TYPE); connection.setRequestProperty("Content-Length", Integer.toString(getLength(request))); connection.setDoOutput(true); //LOG.info("Connecting to: "+url); return super.transceive(request); }
public synchronized void writeBuffers(List<ByteBuffer> buffers) throws IOException { connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", CONTENT_TYPE); connection.setRequestProperty("Content-Length", Integer.toString(getLength(buffers))); connection.setDoOutput(true); writeBuffers(buffers, connection.getOutputStream()); }
public synchronized List<ByteBuffer> readBuffers() throws IOException { return readBuffers(connection.getInputStream()); }
@Override public Transceiver createTransceiver() throws Exception{ return new HttpTransceiver(new URL("http://127.0.0.1:"+server.getPort()+"/")); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(HttpTransceiver.CONTENT_TYPE); List<ByteBuffer> requestBufs = HttpTransceiver.readBuffers(request.getInputStream()); try { List<ByteBuffer> responseBufs = responder.respond(requestBufs); response.setContentLength(HttpTransceiver.getLength(responseBufs)); HttpTransceiver.writeBuffers(responseBufs, response.getOutputStream()); } catch (AvroRuntimeException e) { throw new ServletException(e); } } }
public synchronized void writeBuffers(List<ByteBuffer> buffers) throws IOException { if (proxy == null) connection = (HttpURLConnection)url.openConnection(); else connection = (HttpURLConnection)url.openConnection(proxy); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", CONTENT_TYPE); connection.setRequestProperty("Content-Length", Integer.toString(getLength(buffers))); connection.setDoOutput(true); connection.setReadTimeout(timeout); connection.setConnectTimeout(timeout); OutputStream out = connection.getOutputStream(); try { writeBuffers(buffers, out); } finally { out.close(); } }
static void writeBuffers(List<ByteBuffer> buffers, OutputStream out) throws IOException { for (ByteBuffer buffer : buffers) { writeLength(buffer.limit(), out); // length-prefix out.write(buffer.array(), buffer.position(), buffer.remaining()); buffer.position(buffer.limit()); } writeLength(0, out); // null-terminate }
public synchronized List<ByteBuffer> readBuffers() throws IOException { InputStream in = connection.getInputStream(); try { return readBuffers(in); } finally { in.close(); } }
@Override public Transceiver createTransceiver() throws Exception{ return new HttpTransceiver(new URL("https://localhost:"+server.getPort()+"/")); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(HttpTransceiver.CONTENT_TYPE); List<ByteBuffer> requestBufs = HttpTransceiver.readBuffers(request.getInputStream()); try { List<ByteBuffer> responseBufs = responder.respond(requestBufs); response.setContentLength(HttpTransceiver.getLength(responseBufs)); HttpTransceiver.writeBuffers(responseBufs, response.getOutputStream()); } catch (AvroRuntimeException e) { throw new ServletException(e); } } }
static void writeBuffers(List<ByteBuffer> buffers, OutputStream out) throws IOException { for (ByteBuffer buffer : buffers) { writeLength(buffer.limit(), out); // length-prefix out.write(buffer.array(), buffer.position(), buffer.remaining()); buffer.position(buffer.limit()); } writeLength(0, out); // null-terminate }
public synchronized List<ByteBuffer> readBuffers() throws IOException { return readBuffers(connection.getInputStream()); }
@BeforeClass public static void initializeProxy() throws Exception { HttpTransceiver client = new HttpTransceiver(new URL("http://localhost")); SpecificRequestor requestor = new SpecificRequestor(SampleSpecificProtocol.class, client); proxy = SpecificRequestor.getClient(SampleSpecificProtocol.class, requestor); }