Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,16 @@
package org.lfenergy.shapeshifter.core.common.exception;

import lombok.Getter;
import org.lfenergy.shapeshifter.core.common.HttpStatusCode;

@Getter
public class UftpConnectorException extends RuntimeException {

private final HttpStatusCode httpStatusCode;

public UftpConnectorException(String message) {
this(message, HttpStatusCode.INTERNAL_SERVER_ERROR, null);
super(message);
}

public UftpConnectorException(String message, Throwable cause) {
this(message, HttpStatusCode.INTERNAL_SERVER_ERROR, cause);
}

public UftpConnectorException(String message, HttpStatusCode httpStatusCode) {
this(message, httpStatusCode, null);
}

public UftpConnectorException(String message, HttpStatusCode httpStatusCode, Throwable cause) {
super(message, cause);
this.httpStatusCode = httpStatusCode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 Contributors to the Shapeshifter project
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.shapeshifter.core.common.xsd;

import org.lfenergy.shapeshifter.core.common.exception.UftpConnectorException;

/**
* Thrown when validation against XSD failed.
*/
public class XsdValidationException extends UftpConnectorException {

public XsdValidationException(String message) {
super(message);
}

public XsdValidationException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Validator;
import lombok.RequiredArgsConstructor;
import org.lfenergy.shapeshifter.core.common.HttpStatusCode;
import org.lfenergy.shapeshifter.core.common.exception.UftpConnectorException;
import org.xml.sax.SAXException;

@RequiredArgsConstructor
Expand All @@ -26,7 +24,7 @@ public void validate(final String xml, URL xsd) {
final Validator validator = schemaProvider.getValidator(xsd);
validator.validate(xmlSource);
} catch (SAXException | IOException cause) {
throw new UftpConnectorException("XSD validation failed: " + cause.getMessage(), HttpStatusCode.BAD_REQUEST, cause);
throw new XsdValidationException("XSD validation failed: " + cause.getMessage(), cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2023 Contributors to the Shapeshifter project
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.shapeshifter.core.service.crypto;

import org.lfenergy.shapeshifter.core.common.exception.UftpConnectorException;

/**
* Thrown when message verification failed.
*/
@SuppressWarnings("java:S110") // More than 5 parents is useful and intended in this case
public class UftpCryptoException extends UftpConnectorException {

public UftpCryptoException(String message) {
super(message);
}

public UftpCryptoException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
package org.lfenergy.shapeshifter.core.service.crypto;

import com.goterl.lazysodium.LazySodiumJava;
import java.util.Base64;
import lombok.RequiredArgsConstructor;
import org.lfenergy.shapeshifter.api.SignedMessage;
import org.lfenergy.shapeshifter.core.common.HttpStatusCode;
import org.lfenergy.shapeshifter.core.common.exception.UftpConnectorException;
import org.lfenergy.shapeshifter.core.model.UftpParticipant;
import org.lfenergy.shapeshifter.core.service.participant.ParticipantResolutionService;

import java.util.Base64;

@RequiredArgsConstructor
public class UftpCryptoService {

Expand All @@ -33,7 +32,7 @@ public SignedMessage signMessage(String payloadXml, UftpParticipant sender, Stri

return signedMessage;
} catch (Exception cause) {
throw new UftpConnectorException("Failed to sign message.", cause);
throw new UftpSignException("Failed to sign message.", cause);
} finally {
lazySodiumInstancePool.release(lazySodium);
}
Expand All @@ -44,7 +43,7 @@ public String verifySignedMessage(SignedMessage signedMessage) {
String publicKey = participantService.getPublicKey(signedMessage.getSenderRole(), signedMessage.getSenderDomain());
return verifySignedMessage(signedMessage, publicKey);
} catch (Exception cause) {
throw new UftpConnectorException("Failed to verify message.", HttpStatusCode.UNAUTHORIZED, cause);
throw new UftpVerifyException("Failed to verify message.", cause);
}
}

Expand All @@ -57,7 +56,7 @@ public String verifySignedMessage(SignedMessage signedMessage, String publicKey)
var unsealed = lazySodium.cryptoSignOpen(base64Body, factory.keyFromBase64String(publicKey));

if (unsealed == null) {
throw new UftpConnectorException("Failed to verify message. Message is not validly signed for given public key.");
throw new UftpVerifyException("Failed to verify message. Message is not validly signed for given public key.");
}

return unsealed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 Contributors to the Shapeshifter project
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.shapeshifter.core.service.crypto;

/**
* Thrown when message signing failed.
*/
public class UftpSignException extends UftpCryptoException {

public UftpSignException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023 Contributors to the Shapeshifter project
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.shapeshifter.core.service.crypto;

/**
* Thrown when message verification failed.
*/
public class UftpVerifyException extends UftpCryptoException {

public UftpVerifyException(String message) {
super(message);
}

public UftpVerifyException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

package org.lfenergy.shapeshifter.core.service.sending;

import org.lfenergy.shapeshifter.core.common.HttpStatusCode;

/**
* Exception thrown when an HTTP 4xx is received while sending an UFTP message.
*/
@SuppressWarnings("java:S110") // More than 5 parents useful and intended in this case
public final class UftpClientErrorException extends UftpSendException {

public UftpClientErrorException(String message, HttpStatusCode httpStatusCode) {
public UftpClientErrorException(String message, int httpStatusCode) {
super(message, httpStatusCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@

package org.lfenergy.shapeshifter.core.service.sending;

import org.lfenergy.shapeshifter.core.common.HttpStatusCode;
import org.lfenergy.shapeshifter.core.common.exception.UftpConnectorException;

import java.util.Optional;

/**
* Base class for exceptions thrown when sending an UFTP message fails e.g. when a request fails because of an error response, or a low level I/O error.
*/
public class UftpSendException extends UftpConnectorException {

private final Integer httpStatusCode;

public UftpSendException(String message) {
super(message);
this.httpStatusCode = null;
}

public UftpSendException(String message, Throwable cause) {
super(message, cause);
this.httpStatusCode = null;
}

public UftpSendException(String message, HttpStatusCode httpStatusCode) {
super(message, httpStatusCode);
public UftpSendException(String message, int httpStatusCode) {
super(message);
this.httpStatusCode = httpStatusCode;
}

public UftpSendException(String message, HttpStatusCode httpStatusCode, Throwable cause) {
super(message, httpStatusCode, cause);
public Optional<Integer> getHttpStatusCode() {
return Optional.ofNullable(httpStatusCode);
}
}
Loading
Loading