use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.
the class TransferTest method durableTransferWithRejectedOutcome.
@Test
@SpecificationTest(section = "3.2.1", description = "Durable messages MUST NOT be lost even if an intermediary is unexpectedly terminated and " + "restarted. A target which is not capable of fulfilling this guarantee MUST NOT accept messages " + "where the durable header is set to true: if the source allows the rejected outcome then the " + "message SHOULD be rejected with the precondition-failed error, otherwise the link MUST be " + "detached by the receiver with the same error.")
@BrokerSpecific(kind = BrokerAdmin.KIND_BROKER_J)
public void durableTransferWithRejectedOutcome() throws Exception {
try (FrameTransport transport = new FrameTransport(getBrokerAdmin()).connect()) {
MessageEncoder messageEncoder = new MessageEncoder();
final Header header = new Header();
header.setDurable(true);
messageEncoder.setHeader(header);
messageEncoder.addData(getTestName());
final Disposition receivedDisposition = transport.newInteraction().negotiateOpen().begin().consumeResponse(Begin.class).attachRole(Role.SENDER).attachTargetAddress(BrokerAdmin.TEST_QUEUE_NAME).attachSourceOutcomes(Accepted.ACCEPTED_SYMBOL, Rejected.REJECTED_SYMBOL).attach().consumeResponse(Attach.class).consumeResponse(Flow.class).assertLatestResponse(Flow.class, this::assumeSufficientCredits).transferDeliveryId().transferPayload(messageEncoder.getPayload()).transferRcvSettleMode(ReceiverSettleMode.FIRST).transfer().consumeResponse().getLatestResponse(Disposition.class);
assertThat(receivedDisposition.getSettled(), is(true));
assertThat(receivedDisposition.getState(), is(instanceOf(Outcome.class)));
if (getBrokerAdmin().supportsRestart()) {
assertThat(((Outcome) receivedDisposition.getState()).getSymbol(), is(Accepted.ACCEPTED_SYMBOL));
} else {
assertThat(((Outcome) receivedDisposition.getState()).getSymbol(), is(Rejected.REJECTED_SYMBOL));
}
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.
the class DecodeErrorTest method buildInvalidMessage.
private QpidByteBuffer buildInvalidMessage() {
final List<QpidByteBuffer> payloads = new ArrayList<>();
try {
final Header header = new Header();
header.setTtl(UnsignedInteger.valueOf(10000L));
final HeaderSection headerSection = header.createEncodingRetainingSection();
try {
payloads.add(headerSection.getEncodedForm());
} finally {
headerSection.dispose();
}
final StringWriter stringWriter = new StringWriter("string in between message sections");
final QpidByteBuffer encodedString = QpidByteBuffer.allocate(stringWriter.getEncodedSize());
stringWriter.writeToBuffer(encodedString);
encodedString.flip();
payloads.add(encodedString);
final Map<Symbol, Object> annotationMap = Collections.singletonMap(Symbol.valueOf("foo"), "bar");
final DeliveryAnnotations annotations = new DeliveryAnnotations(annotationMap);
final DeliveryAnnotationsSection deliveryAnnotationsSection = annotations.createEncodingRetainingSection();
try {
payloads.add(deliveryAnnotationsSection.getEncodedForm());
} finally {
deliveryAnnotationsSection.dispose();
}
final AmqpValueSection payload = new AmqpValue(getTestName()).createEncodingRetainingSection();
try {
payloads.add(payload.getEncodedForm());
} finally {
payload.dispose();
}
return QpidByteBuffer.concatenate(payloads);
} finally {
payloads.forEach(QpidByteBuffer::dispose);
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.
the class DecodeErrorTest method illegalMessageFormatPayload.
@Test
@SpecificationTest(section = "3.2", description = "Altogether a message consists of the following sections: Zero or one header," + " Zero or one delivery-annotations, [...]")
public void illegalMessageFormatPayload() throws Exception {
try (FrameTransport transport = new FrameTransport(_brokerAddress).connect()) {
List<QpidByteBuffer> payloads = new ArrayList<>();
final HeaderSection headerSection = new Header().createEncodingRetainingSection();
payloads.add(headerSection.getEncodedForm());
headerSection.dispose();
final StringWriter stringWriter = new StringWriter("string in between annotation sections");
QpidByteBuffer encodedString = QpidByteBuffer.allocate(stringWriter.getEncodedSize());
stringWriter.writeToBuffer(encodedString);
encodedString.flip();
payloads.add(encodedString);
final DeliveryAnnotationsSection deliveryAnnotationsSection = new DeliveryAnnotations(Collections.emptyMap()).createEncodingRetainingSection();
payloads.add(deliveryAnnotationsSection.getEncodedForm());
deliveryAnnotationsSection.dispose();
final Detach detachResponse;
try (QpidByteBuffer combinedPayload = QpidByteBuffer.concatenate(payloads)) {
detachResponse = transport.newInteraction().negotiateProtocol().consumeResponse().open().consumeResponse(Open.class).begin().consumeResponse(Begin.class).attachRole(Role.SENDER).attachTargetAddress(BrokerAdmin.TEST_QUEUE_NAME).attach().consumeResponse(Attach.class).consumeResponse(Flow.class).transferMessageFormat(UnsignedInteger.ZERO).transferPayload(combinedPayload).transfer().consumeResponse().getLatestResponse(Detach.class);
}
payloads.forEach(QpidByteBuffer::dispose);
assertThat(detachResponse.getError(), is(notNullValue()));
assertThat(detachResponse.getError().getCondition(), is(equalTo(DECODE_ERROR)));
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.
the class AMQPConnection_1_0Impl method processProtocolHeader.
private void processProtocolHeader(final QpidByteBuffer msg) {
if (msg.remaining() >= 8) {
byte[] header = new byte[8];
msg.get(header);
final AuthenticationProvider<?> authenticationProvider = getPort().getAuthenticationProvider();
if (Arrays.equals(header, SASL_HEADER)) {
if (_saslComplete) {
throw new ConnectionScopedRuntimeException("SASL Layer header received after SASL already established");
}
try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(SASL_HEADER)) {
getSender().send(protocolHeader);
}
SaslMechanisms mechanisms = new SaslMechanisms();
ArrayList<Symbol> mechanismsList = new ArrayList<>();
for (String name : authenticationProvider.getAvailableMechanisms(getTransport().isSecure())) {
mechanismsList.add(Symbol.valueOf(name));
}
mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()]));
send(new SASLFrame(mechanisms));
_connectionState = ConnectionState.AWAIT_SASL_INIT;
_frameHandler = getFrameHandler(true);
} else if (Arrays.equals(header, AMQP_HEADER)) {
if (!_saslComplete) {
final List<String> mechanisms = authenticationProvider.getAvailableMechanisms(getTransport().isSecure());
if (mechanisms.contains(ExternalAuthenticationManagerImpl.MECHANISM_NAME) && getNetwork().getPeerPrincipal() != null) {
setUserPrincipal(new AuthenticatedPrincipal(getNetwork().getPeerPrincipal()));
} else if (mechanisms.contains(AnonymousAuthenticationManager.MECHANISM_NAME)) {
setUserPrincipal(new AuthenticatedPrincipal(((AnonymousAuthenticationManager) authenticationProvider).getAnonymousPrincipal()));
} else {
LOGGER.warn("{} : attempt to initiate AMQP connection without correctly authenticating", getLogSubject());
_connectionState = ConnectionState.CLOSED;
getNetwork().close();
}
}
try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(AMQP_HEADER)) {
getSender().send(protocolHeader);
}
_connectionState = ConnectionState.AWAIT_OPEN;
_frameHandler = getFrameHandler(false);
} else {
LOGGER.warn("{} : unknown AMQP header {}", getLogSubject(), Functions.str(header));
_connectionState = ConnectionState.CLOSED;
getNetwork().close();
}
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.
the class MessageConverter_0_8_to_1_0 method convertMetaData.
@Override
protected MessageMetaData_1_0 convertMetaData(final AMQMessage serverMessage, final EncodingRetainingSection<?> bodySection, SectionEncoder sectionEncoder) {
Header header = new Header();
Properties props = new Properties();
header.setDurable(serverMessage.isPersistent());
BasicContentHeaderProperties contentHeader = serverMessage.getContentHeaderBody().getProperties();
header.setPriority(UnsignedByte.valueOf(contentHeader.getPriority()));
if (contentHeader.hasExpiration()) {
final long expiration = serverMessage.getExpiration();
final long arrivalTime = serverMessage.getArrivalTime();
header.setTtl(UnsignedInteger.valueOf(Math.max(0, expiration - arrivalTime)));
}
if (!GZIPUtils.GZIP_CONTENT_ENCODING.equals(contentHeader.getEncodingAsString()) && bodySection instanceof DataSection) {
props.setContentEncoding(Symbol.valueOf(contentHeader.getEncodingAsString()));
}
Symbol contentType = getContentType(contentHeader.getContentTypeAsString());
props.setContentType(contentType);
final AMQShortString correlationId = contentHeader.getCorrelationId();
if (correlationId != null) {
final byte[] correlationIdAsBytes = correlationId.getBytes();
final String correlationIdAsString = contentHeader.getCorrelationIdAsString();
if (Arrays.equals(correlationIdAsBytes, correlationIdAsString.getBytes(StandardCharsets.UTF_8))) {
props.setCorrelationId(convertMessageId(correlationId));
} else {
props.setCorrelationId(correlationIdAsBytes);
}
}
final AMQShortString messageId = contentHeader.getMessageId();
if (messageId != null) {
props.setMessageId(convertMessageId(messageId));
}
if (contentHeader.getReplyTo() != null) {
props.setReplyTo(convertReplyTo(contentHeader.getReplyTo()));
}
if (contentHeader.getUserId() != null) {
props.setUserId(new Binary(contentHeader.getUserId().getBytes()));
}
if (contentHeader.hasTimestamp()) {
props.setCreationTime(new Date(contentHeader.getTimestamp()));
} else {
props.setCreationTime(new Date(serverMessage.getArrivalTime()));
}
if (contentHeader.getType() != null) {
props.setSubject(contentHeader.getType().toString());
}
Map<String, Object> applicationPropertiesMap = new LinkedHashMap<>(contentHeader.getHeadersAsMap());
if (applicationPropertiesMap.containsKey("qpid.subject")) {
props.setSubject(String.valueOf(applicationPropertiesMap.get("qpid.subject")));
applicationPropertiesMap.remove("qpid.subject");
}
if (applicationPropertiesMap.containsKey("JMSXGroupID")) {
props.setGroupId(String.valueOf(applicationPropertiesMap.get("JMSXGroupID")));
applicationPropertiesMap.remove("JMSXGroupID");
}
if (applicationPropertiesMap.containsKey("JMSXGroupSeq")) {
Object jmsxGroupSeq = applicationPropertiesMap.get("JMSXGroupSeq");
if (jmsxGroupSeq instanceof Integer) {
props.setGroupSequence(UnsignedInteger.valueOf((Integer) jmsxGroupSeq));
applicationPropertiesMap.remove("JMSXGroupSeq");
}
}
MessagePublishInfo messagePublishInfo = serverMessage.getMessagePublishInfo();
String to = AMQShortString.toString(messagePublishInfo.getExchange());
if (messagePublishInfo.getRoutingKey() != null) {
String routingKey = AMQShortString.toString(messagePublishInfo.getRoutingKey());
if (to != null && !"".equals(to)) {
to += "/" + routingKey;
} else {
to = routingKey;
}
}
props.setTo(to);
final ApplicationProperties applicationProperties;
try {
applicationProperties = new ApplicationProperties(applicationPropertiesMap);
} catch (IllegalArgumentException e) {
throw new MessageConversionException("Could not convert message from 0-8 to 1.0 because headers conversion failed.", e);
}
MessageAnnotations messageAnnotations = createMessageAnnotation(bodySection, contentHeader.getContentTypeAsString());
return new MessageMetaData_1_0(header.createEncodingRetainingSection(), null, messageAnnotations == null ? null : messageAnnotations.createEncodingRetainingSection(), props.createEncodingRetainingSection(), applicationProperties.createEncodingRetainingSection(), null, serverMessage.getArrivalTime(), bodySection.getEncodedSize());
}
Aggregations