response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, request.getMethod() + " not supported");
return;
}
// Not giving a specific mutex will synchronize on the contination itself
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(HEADER_CONTENT_TYPE));
Context ctx = soapHelper.createContext(message);
if (request.getUserPrincipal() != null) {
if (request.getUserPrincipal() instanceof JaasJettyPrincipal) {
Subject subject = ((JaasJettyPrincipal) request.getUserPrincipal()).getSubject();
ctx.getInMessage().setSubject(subject);
} else {
ctx.getInMessage().addPrincipal(request.getUserPrincipal());
}
}
request.setAttribute(Context.class.getName(), ctx);
exchange = soapHelper.onReceive(ctx);
NormalizedMessage inMessage = exchange.getMessage("in");
if (getConfiguration().isWantHeadersFromHttpIntoExchange()) {
inMessage.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(request));
}
locks.put(exchange.getExchangeId(), cont);
request.setAttribute(MessageExchange.class.getName(), exchange.getExchangeId());
synchronized (cont) {
channel.send(exchange);
if (log.isDebugEnabled()) {
log.debug("Suspending continuation for exchange: " + exchange.getExchangeId());
}
boolean result = cont.suspend(suspentionTime);
exchanges.remove(exchange.getExchangeId());
if (!result) {
throw new Exception("Error sending exchange: aborted");
}
request.removeAttribute(MessageExchange.class.getName());
}
} catch (RetryRequest retry) {
throw retry;
} catch (SoapFault fault) {
sendFault(fault, request, response);
return;
} catch (Exception e) {
SoapFault fault = new SoapFault(e);
sendFault(fault, request, response);
return;
}
} else {
String id = (String) request.getAttribute(MessageExchange.class.getName());
exchange = 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) {
processFault(exchange, request, response);
} else {
processResponse(exchange, request, response);
}
} finally {
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
}
} else if (exchange.getStatus() == ExchangeStatus.DONE) {
// This happens when there is no response to send back
response.setStatus(HttpServletResponse.SC_ACCEPTED);
}
}