use of com.google.protobuf.ProtocolMessageEnum in project core-java by SpineEventEngine.
the class RequestValidator method validateMessage.
/**
* Checks whether the {@code Message} of the given request conforms the constraints
*
* @param request the request message to validate.
* @return an instance of exception,
* or {@code Optional.absent()} if the request message is valid.
*/
private Optional<InvalidRequestException> validateMessage(M request) {
final List<ConstraintViolation> violations = MessageValidator.newInstance().validate(request);
if (violations.isEmpty()) {
return Optional.absent();
}
final ValidationError validationError = ValidationError.newBuilder().addAllConstraintViolation(violations).build();
final ProtocolMessageEnum errorCode = getInvalidMessageErrorCode();
final String typeName = errorCode.getDescriptorForType().getFullName();
final String errorTextTemplate = getErrorText(request);
final String errorText = format("%s %s", errorTextTemplate, toText(violations));
final Error.Builder errorBuilder = Error.newBuilder().setType(typeName).setCode(errorCode.getNumber()).setValidationError(validationError).setMessage(errorText);
final Error error = errorBuilder.build();
return Optional.of(onInvalidMessage(formatExceptionMessage(request), request, error));
}
use of com.google.protobuf.ProtocolMessageEnum in project core-java by SpineEventEngine.
the class RequestValidator method checkSupported.
/**
* Checks whether this request is supported, forms the proper {@link Error error}
* and packs it into an exception.
*
* @param request the request to check for support
* @return an instance of exception or {@code Optional.absent()} if the request is supported.
*/
private Optional<InvalidRequestException> checkSupported(M request) {
final Optional<RequestNotSupported<M>> supported = isSupported(request);
if (!supported.isPresent()) {
return Optional.absent();
}
final RequestNotSupported<M> result = supported.get();
final ProtocolMessageEnum unsupportedErrorCode = result.getErrorCode();
final String errorMessage = result.getErrorMessage();
final String errorTypeName = unsupportedErrorCode.getDescriptorForType().getFullName();
final Error.Builder errorBuilder = Error.newBuilder().setType(errorTypeName).setCode(unsupportedErrorCode.getNumber()).setMessage(errorMessage);
final Error error = errorBuilder.build();
final InvalidRequestException exception = result.createException(errorMessage, request, error);
return Optional.of(exception);
}
use of com.google.protobuf.ProtocolMessageEnum in project j2objc by google.
the class CompatibilityTest method testProtocolMessageEnum.
public void testProtocolMessageEnum() throws Exception {
TypicalData data = TypicalData.newBuilder().setMyEnumType(TypicalData.EnumType.VALUE1).build();
ProtocolMessageEnum type = data.getMyEnumType();
assertEquals(1, type.getNumber());
// Test casting to ProtocolMessageEnum.
Object value = TypicalData.EnumType.VALUE2;
type = (ProtocolMessageEnum) value;
}
Aggregations