}
Document document = getDocument(is);
String issuerEntityId;
RequestAbstractType samlRequestMessage = null;
StatusResponseType samlResponseMessage = null;
if (samlRequestOrResponse.isRequest()) {
samlRequestMessage = getSamlRequest(document);
issuerEntityId = samlRequestMessage.getIssuer().getValue();
} else {
samlResponseMessage = getSamlResponse(document);
issuerEntityId = samlResponseMessage.getIssuer().getValue();
}
log.debug("Received: " + SamlUtils.getDocumentAsString(document));
try {
if (samlRequestOrResponse.isRequest() || samlResponseMessage.getInResponseTo() == null) {
// Request or unsolicited response
String destination = samlRequestOrResponse.isRequest() ? samlRequestMessage.getDestination() : samlResponseMessage.getDestination();
if (!samlEntityBean.get().getServiceURL(service).equals(destination)) {
throw new InvalidRequestException("Destination (" + destination + ") is not valid.");
}
dialogueManager.beginDialogue();
samlDialogue.get().setExternalProviderMessageId(samlRequestOrResponse.isRequest() ? samlRequestMessage.getID() : samlResponseMessage.getID());
SamlExternalEntity externalProvider = samlEntityBean.get().getExternalSamlEntityByEntityId(issuerEntityId);
if (externalProvider == null) {
throw new InvalidRequestException("Received message from unknown entity id " + issuerEntityId);
}
samlDialogue.get().setExternalProvider(externalProvider);
} else {
String dialogueId = samlResponseMessage.getInResponseTo();
if (!dialogueManager.isExistingDialogue(dialogueId)) {
throw new InvalidRequestException("No request that corresponds with the received response");
}
dialogueManager.attachDialogue(dialogueId);