Continuation cont = ContinuationSupport.getContinuation(request, null);
MessageExchange exchange;
// If the continuation is not a retry
if (!cont.isPending()) {
try {
SoapMessage message = soapHelper.getSoapMarshaler().createReader().read(
request.getInputStream(),
request.getHeader(Constants.HEADER_CONTENT_TYPE));
Context context = soapHelper.createContext(message);
if (request.getUserPrincipal() != null) {
if (request.getUserPrincipal() instanceof JaasJettyPrincipal) {
Subject subject = ((JaasJettyPrincipal) request.getUserPrincipal()).getSubject();
context.getInMessage().setSubject(subject);
} else {
context.getInMessage().addPrincipal(request.getUserPrincipal());
}
}
request.setAttribute(Context.class.getName(), context);
exchange = soapHelper.onReceive(context);
NormalizedMessage inMessage = exchange.getMessage("in");
inMessage.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(request));
locks.put(exchange.getExchangeId(), cont);
request.setAttribute(MessageExchange.class.getName(), exchange.getExchangeId());
synchronized (cont) {
((BaseLifeCycle) endpoint.getServiceUnit().getComponent().getLifeCycle()).sendConsumerExchange(exchange, endpoint);
if (exchanges.remove(exchange.getExchangeId()) == null) {
if (log.isDebugEnabled()) {
log.debug("Suspending continuation for exchange: " + exchange.getExchangeId());
}
// TODO: make this timeout configurable
boolean result = cont.suspend(1000 * 60); // 60 s
if (!result) {
throw new Exception("Error sending exchange: aborted");
}
}
request.removeAttribute(MessageExchange.class.getName());
}
} catch (SoapFault fault) {
sendFault(fault, request, response);
return;
}
} else {
String id = (String) request.getAttribute(MessageExchange.class.getName());
exchange = (MessageExchange) exchanges.remove(id);
request.removeAttribute(MessageExchange.class.getName());
boolean result = cont.suspend(0);
// Check if this is a timeout
if (exchange == null) {
throw new IllegalStateException("Exchange not found");
}
if (!result) {
throw new Exception("Timeout");
}
}
if (exchange.getStatus() == ExchangeStatus.ERROR) {
if (exchange.getError() != null) {
throw new Exception(exchange.getError());
} else {
throw new Exception("Unknown Error");
}
} else if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
try {
if (exchange.getFault() != null) {
SoapFault fault = new SoapFault(
(QName) exchange.getFault().getProperty(JBIMarshaler.SOAP_FAULT_CODE),
(QName) exchange.getFault().getProperty(JBIMarshaler.SOAP_FAULT_SUBCODE),
(String) exchange.getFault().getProperty(JBIMarshaler.SOAP_FAULT_REASON),
(URI) exchange.getFault().getProperty(JBIMarshaler.SOAP_FAULT_NODE),
(URI) exchange.getFault().getProperty(JBIMarshaler.SOAP_FAULT_ROLE),
exchange.getFault().getContent());
sendFault(fault, request, response);
} else {
NormalizedMessage outMsg = exchange.getMessage("out");
if (outMsg != null) {
Context context = (Context) request.getAttribute(Context.class.getName());
SoapMessage out = soapHelper.onReply(context, outMsg);
SoapWriter writer = soapHelper.getSoapMarshaler().createWriter(out);
response.setContentType(writer.getContentType());
writer.write(response.getOutputStream());
}
}