use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.
the class SaxSoapParserImpl method parseMessage.
private Soap parseMessage(InputStream is, String mimeType, String contentType, String charset) throws Exception {
log.trace("parseMessage({}, {})", mimeType, charset);
ByteArrayOutputStream rawXml = new ByteArrayOutputStream();
ByteArrayOutputStream processedXml = new ByteArrayOutputStream();
InputStream proxyStream = excludeUtf8Bom(contentType, new TeeInputStream(is, rawXml));
Writer outputWriter = new OutputStreamWriter(processedXml, charset);
XRoadSoapHandler handler = handleSoap(outputWriter, proxyStream);
CodedException fault = handler.getFault();
if (fault != null) {
return createSoapFault(charset, rawXml, fault);
}
byte[] xmlBytes = isProcessedXmlRequired() ? processedXml.toByteArray() : rawXml.toByteArray();
return createSoapMessage(contentType, charset, handler, xmlBytes);
}
use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.
the class Signature method getOcspResponses.
/**
* Return list of OCSP responses included in the signature.
*/
List<OCSPResp> getOcspResponses() {
List<OCSPResp> ocspResponses = new ArrayList<>();
NodeList ocspValueElements = getEncapsulatedOCSPValueElements(objectContainer.getElement());
if (ocspValueElements == null || ocspValueElements.getLength() == 0) {
throw new CodedException(X_MALFORMED_SIGNATURE, "Could not get any OCSP elements from signature");
}
for (int i = 0; i < ocspValueElements.getLength(); i++) {
Element ocspResponseElem = (Element) ocspValueElements.item(i);
// we have the ocsp response in base64 form, attempt to parse it
String base64 = ocspResponseElem.getTextContent();
try {
ocspResponses.add(new OCSPResp(decodeBase64(base64)));
} catch (IOException e) {
throw new CodedException(X_MALFORMED_SIGNATURE, e);
}
}
return ocspResponses;
}
use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.
the class Signature method getExtraCertificates.
/**
* Returns list of additional certificates that are included in the signature.
*/
List<X509Certificate> getExtraCertificates() {
List<X509Certificate> extraCertificates = new ArrayList<>();
NodeList certificateRefs = getCertificateRefElements(objectContainer.getElement());
if (certificateRefs == null || certificateRefs.getLength() == 0) {
// returning empty list, since there are no extra certificates
return extraCertificates;
}
for (int i = 0; i < certificateRefs.getLength(); i++) {
Element certRef = (Element) certificateRefs.item(i);
String certId = certRef.getAttribute(URI_ATTRIBUTE);
if (certId == null || certId.isEmpty()) {
throw new CodedException(X_MALFORMED_SIGNATURE, "Missing certificate id attribute");
}
// we have the ocsp response element id, let's find the response
Element certElem = XmlUtils.getElementById(document, certId);
if (certElem == null) {
throw new CodedException(X_MALFORMED_SIGNATURE, "Could not find certificate with id " + certId);
}
try {
X509Certificate x509 = CryptoUtils.readCertificate(certElem.getTextContent());
// we now have the certificate constructed, verify the digest
if (!verifyDigest((Element) certRef.getFirstChild(), x509.getEncoded())) {
throw new CodedException(X_MALFORMED_SIGNATURE, "Certificate (%s) digest does not match", x509.getSerialNumber());
}
extraCertificates.add(x509);
} catch (CertificateException | NoSuchAlgorithmException | IOException | OperatorCreationException e) {
throw new CodedException(X_MALFORMED_SIGNATURE, e);
}
}
return extraCertificates;
}
use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.
the class SoapParserImpl method unmarshalHeader.
@SuppressWarnings("unchecked")
static <T> T unmarshalHeader(Class<?> clazz, SOAPHeader soapHeader, boolean checkRequiredFields) throws Exception {
Unmarshaller unmarshaller = JaxbUtils.createUnmarshaller(clazz);
if (checkRequiredFields) {
unmarshaller.setListener(new RequiredHeaderFieldsChecker(clazz));
}
unmarshaller.setEventHandler(event -> {
switch(event.getSeverity()) {
case ValidationEvent.WARNING:
return true;
case ValidationEvent.ERROR:
Throwable t = event.getLinkedException();
return !(t instanceof AccessorException && t.getCause() instanceof CodedException);
case ValidationEvent.FATAL_ERROR:
return false;
default:
return true;
}
});
JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(soapHeader, clazz);
return jaxbElement.getValue();
}
use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.
the class SoapMessageDecoder method readMultipart.
private void readMultipart(InputStream is) throws Exception {
log.trace("readMultipart");
MimeConfig config = new MimeConfig.Builder().setHeadlessParsing(contentType).build();
MimeStreamParser mimeStreamParser = new MimeStreamParser(config);
mimeStreamParser.setContentHandler(new MultipartHandler());
// Parse the request.
try {
mimeStreamParser.parse(is);
} catch (MimeException ex) {
// invalid request from client and we want to report it as that.
throw new CodedException(X_MIME_PARSING_FAILED, ex);
}
}
Aggregations