public synchronized int send(short channel, FrameBody body, ByteBuffer payload) { if (!_closedForOutput) { ValueWriter<FrameBody> writer = _describedTypeRegistry.getValueWriter(body); int size = writer.writeToBuffer(EMPTY_BYTE_BUFFER); ByteBuffer payloadDup = payload == null ? null : payload.duplicate(); int payloadSent = getMaxFrameSize() - (size + 9); if (payloadSent < (payload == null ? 0 : payload.remaining())) { if (body instanceof Transfer) { ((Transfer) body).setMore(Boolean.TRUE); } writer = _describedTypeRegistry.getValueWriter(body); size = writer.writeToBuffer(EMPTY_BYTE_BUFFER); payloadSent = getMaxFrameSize() - (size + 9); try { payloadDup.limit(payloadDup.position() + payloadSent); } catch (NullPointerException npe) { throw npe; } } else { payloadSent = payload == null ? 0 : payload.remaining(); } _frameOutputHandler.send(AMQFrame.createAMQFrame(channel, body, payloadDup)); return payloadSent; } else { return -1; } }
public void receiveSaslInit(final SaslInit saslInit) { Symbol mechanism = saslInit.getMechanism(); final Binary initialResponse = saslInit.getInitialResponse(); byte[] response = initialResponse == null ? new byte[0] : initialResponse.getArray(); try { _saslServer = _saslServerProvider.getSaslServer(mechanism.toString(), "localhost"); // Process response from the client byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]); if (_saslServer.isComplete()) { SaslOutcome outcome = new SaslOutcome(); outcome.setCode(SaslCode.OK); _saslFrameOutput.send(new SASLFrame(outcome), null); synchronized (getLock()) { _saslComplete = true; _authenticated = true; getLock().notifyAll(); } if (_onSaslCompleteTask != null) { _onSaslCompleteTask.run(); } } else { SaslChallenge challengeBody = new SaslChallenge(); challengeBody.setChallenge(new Binary(challenge)); _saslFrameOutput.send(new SASLFrame(challengeBody), null); } } catch (SaslException e) { SaslOutcome outcome = new SaslOutcome(); outcome.setCode(SaslCode.AUTH); _saslFrameOutput.send(new SASLFrame(outcome), null); synchronized (getLock()) { _saslComplete = true; _authenticated = false; getLock().notifyAll(); } if (_onSaslCompleteTask != null) { _onSaslCompleteTask.run(); } } }
public void receiveSaslResponse(final SaslResponse saslResponse) { final Binary responseBinary = saslResponse.getResponse(); byte[] response = responseBinary == null ? new byte[0] : responseBinary.getArray(); try { // Process response from the client byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]); if (_saslServer.isComplete()) { SaslOutcome outcome = new SaslOutcome(); outcome.setCode(SaslCode.OK); _saslFrameOutput.send(new SASLFrame(outcome), null); synchronized (getLock()) { _saslComplete = true; _authenticated = true; getLock().notifyAll(); } if (_onSaslCompleteTask != null) { _onSaslCompleteTask.run(); } } else { SaslChallenge challengeBody = new SaslChallenge(); challengeBody.setChallenge(new Binary(challenge)); _saslFrameOutput.send(new SASLFrame(challengeBody), null); } } catch (SaslException e) { SaslOutcome outcome = new SaslOutcome(); outcome.setCode(SaslCode.AUTH); _saslFrameOutput.send(new SASLFrame(outcome), null); synchronized (getLock()) { _saslComplete = true; _authenticated = false; getLock().notifyAll(); } if (_onSaslCompleteTask != null) { _onSaslCompleteTask.run(); } } }
public void receiveSaslOutcome(final SaslOutcome saslOutcome) { if (saslOutcome.getCode() == SaslCode.OK) { _saslFrameOutput.close(); synchronized (getLock()) { _saslComplete = true; _authenticated = true; getLock().notifyAll(); } if (_onSaslCompleteTask != null) { _onSaslCompleteTask.run(); } } else { synchronized (getLock()) { _saslComplete = true; _authenticated = false; getLock().notifyAll(); } setClosedForInput(true); _saslFrameOutput.close(); } }
public void receiveSaslMechanisms(final SaslMechanisms saslMechanisms) { if (Arrays.asList(saslMechanisms.getSaslServerMechanisms()).contains(Symbol.valueOf("PLAIN"))) { SaslInit init = new SaslInit(); init.setMechanism(Symbol.valueOf("PLAIN")); init.setHostname(_remoteHostname); byte[] usernameBytes = _user.getName().getBytes(Charset.forName("UTF-8")); byte[] passwordBytes = _password.getBytes(Charset.forName("UTF-8")); byte[] initResponse = new byte[usernameBytes.length + passwordBytes.length + 2]; System.arraycopy(usernameBytes, 0, initResponse, 1, usernameBytes.length); System.arraycopy( passwordBytes, 0, initResponse, usernameBytes.length + 2, passwordBytes.length); init.setInitialResponse(new Binary(initResponse)); _saslFrameOutput.send(new SASLFrame(init), null); } }
public void initiateSASL() { SaslMechanisms mechanisms = new SaslMechanisms(); final Enumeration<SaslServerFactory> saslServerFactories = Sasl.getSaslServerFactories(); SaslServerFactory f; ArrayList<Symbol> mechanismsList = new ArrayList<Symbol>(); while (saslServerFactories.hasMoreElements()) { f = saslServerFactories.nextElement(); final String[] mechanismNames = f.getMechanismNames(null); for (String name : mechanismNames) { mechanismsList.add(Symbol.valueOf(name)); } } mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()])); _saslFrameOutput.send(new SASLFrame(mechanisms), null); }
private void closeSender() { setClosedForOutput(true); _frameOutputHandler.close(); }