/** 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); }
/** Create using the specified {@link SaslServer}. */ public SaslSocketTransceiver(SocketChannel channel, SaslServer saslServer) throws IOException { this.sasl = new SaslParticipant(saslServer); this.channel = channel; LOG.debug("open from {}", getRemoteName()); open(false); }
@Override public synchronized List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { if (saslResponsePiggybacked) { // still need to read response saslResponsePiggybacked = false; Status status = readStatus(); ByteBuffer frame = readFrame(); switch (status) { case COMPLETE: break; case FAIL: throw new SaslException("Fail: "+toString(frame)); default: throw new IOException("Unexpected SASL status: "+status); } } return super.transceive(request); }
if (sasl.client.hasInitialResponse()) response = ByteBuffer.wrap(sasl.evaluate(response.array())); write(Status.START, sasl.getMechanismName(), response); if (sasl.isComplete()) saslResponsePiggybacked = true; Status status = readStatus(); ByteBuffer frame = readFrame(); switch (status) { case START: String mechanism = toString(frame); frame = readFrame(); if (!mechanism.equalsIgnoreCase(sasl.getMechanismName())) { write(Status.FAIL, "Wrong mechanism: "+mechanism); throw new SaslException("Wrong mechanism: "+mechanism); status = Status.FAIL; write(status, response!=null ? ByteBuffer.wrap(response) : EMPTY); break; case COMPLETE: break; case FAIL: throw new SaslException("Fail: "+toString(frame)); default: throw new IOException("Unexpected SASL status: "+status);
@Test public void test64kRequest() throws Exception { SaslSocketServer s = new SaslSocketServer (new ReflectResponder(ProtoInterface.class, new ProtoInterface() { public byte[] test(byte[] b) { return b; } }), new InetSocketAddress(0)); s.start(); SaslSocketTransceiver client = new SaslSocketTransceiver(new InetSocketAddress(s.getPort())); ProtoInterface proxy = (ProtoInterface)ReflectRequestor.getClient(ProtoInterface.class, client); byte[] result = proxy.test(new byte[64*1024]); client.close(); s.close(); }
@Override public synchronized List<ByteBuffer> readBuffers() throws IOException { List<ByteBuffer> buffers = new ArrayList<>(); while (true) { ByteBuffer buffer = readFrameAndUnwrap(); if (buffer.remaining() == 0) return buffers; buffers.add(buffer); } }
private ByteBuffer readFrameAndUnwrap() throws IOException { ByteBuffer frame = readFrame(); if (!dataIsWrapped) return frame; ByteBuffer unwrapped = ByteBuffer.wrap(sasl.unwrap(frame.array())); LOG.debug("unwrapped data of length: {}", unwrapped.remaining()); return unwrapped; }
@Override public void close() throws IOException { if (channel.isOpen()) { LOG.info("closing to "+getRemoteName()); channel.close(); } sasl.dispose(); }
if (sasl.client.hasInitialResponse()) response = ByteBuffer.wrap(sasl.evaluate(response.array())); write(Status.START, sasl.getMechanismName(), response); if (sasl.isComplete()) saslResponsePiggybacked = true; Status status = readStatus(); ByteBuffer frame = readFrame(); switch (status) { case START: String mechanism = toString(frame); frame = readFrame(); if (!mechanism.equalsIgnoreCase(sasl.getMechanismName())) { write(Status.FAIL, "Wrong mechanism: "+mechanism); throw new SaslException("Wrong mechanism: "+mechanism); status = Status.FAIL; write(status, response!=null ? ByteBuffer.wrap(response) : EMPTY); break; case COMPLETE: break; case FAIL: throw new SaslException("Fail: "+toString(frame)); default: throw new IOException("Unexpected SASL status: "+status);
@Override public synchronized List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { if (saslResponsePiggybacked) { // still need to read response saslResponsePiggybacked = false; Status status = readStatus(); ByteBuffer frame = readFrame(); switch (status) { case COMPLETE: break; case FAIL: throw new SaslException("Fail: "+toString(frame)); default: throw new IOException("Unexpected SASL status: "+status); } } return super.transceive(request); }
@Override public synchronized List<ByteBuffer> readBuffers() throws IOException { List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(); while (true) { ByteBuffer buffer = readFrameAndUnwrap(); if (buffer.remaining() == 0) return buffers; buffers.add(buffer); } }
private ByteBuffer readFrameAndUnwrap() throws IOException { ByteBuffer frame = readFrame(); if (!dataIsWrapped) return frame; ByteBuffer unwrapped = ByteBuffer.wrap(sasl.unwrap(frame.array())); LOG.debug("unwrapped data of length: {}", unwrapped.remaining()); return unwrapped; }
@Override public void close() throws IOException { if (channel.isOpen()) { LOG.info("closing to "+getRemoteName()); channel.close(); } sasl.dispose(); }
@Override protected Transceiver getTransceiver(SocketChannel channel) throws IOException { return new SaslSocketTransceiver(channel, factory.getServer()); }
/** Create using the specified {@link SaslClient}. */ public SaslSocketTransceiver(SocketAddress address, SaslClient saslClient) throws IOException { this.sasl = new SaslParticipant(saslClient); this.channel = SocketChannel.open(address); this.channel.socket().setTcpNoDelay(true); LOG.debug("open to {}", getRemoteName()); open(true); }
private Status readStatus() throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1); read(buffer); int status = buffer.get(); if (status > Status.values().length) throw new IOException("Unexpected SASL status byte: "+status); return Status.values()[status]; }
case SASL: this.clientTransceiver = new SaslSocketTransceiver(new InetSocketAddress(clientPort)); this.outputClient = SpecificRequestor.getClient(OutputProtocol.class, clientTransceiver); break;
/** Create using the specified {@link SaslServer}. */ public SaslSocketTransceiver(SocketChannel channel, SaslServer saslServer) throws IOException { this.sasl = new SaslParticipant(saslServer); this.channel = channel; LOG.debug("open from {}", getRemoteName()); open(false); }