Good afternoon
I am new to apache camel and I am working with a microservice using apache camel and quarkus, I am currently implementing validation of the input fields as automated as possible and in this case I want to validate the header and I want to return a 400 code and a custom response but no I manage to do it.
I am implementing the line clientRequestValidation(true)
in the DSL definition of the logic in addition to the line to validate my input parameter:
param().name("x-correlator").type(RestParamType.header).required(true).endParam()
With these two lines what I get is the following response:
And I want an answer like this:
My RestRoure code is the following:
@ApplicationScoped
public class RestRoute extends RouteBuilder {
@ConfigProperty(name = "client.url.userProfile")
String urlUserProfile;
@ConfigProperty(name = "client.utl.getActiveSessionByIp")
String urlGetActivateSessionByIp;
@ConfigProperty(name = "path.openapi")
String pathOpenapi;
@ConfigProperty(name = "descripcion.servicio")
String descriptionService;
private final IGetCurrentDateTime getCurrentDateTime;
private ConfigureSsl configureSsl;
private static final String SALIDA_BSS_EXCEPTION = "Salida Microservicio IdentifyOrigin ${body}";
private static final String MSG_EXCEPTION = "Descripcion de la Exception: ${exception.message}";
private static final String DATE_LOG = "[${bean:BeanDate.getCurrentDateTime()}] ";
public RestRoute() {
getCurrentDateTime = new GetCurrentDateTime();
TimeZone.setDefault(TimeZone.getTimeZone("GMT-4"));
configureSsl= new ConfigureSsl();
}
@Override
public void configure() throws Exception {
BeanDate beanDate= new BeanDate();
getContext().getRegistry().bind("BeanDate", beanDate);
restConfiguration().bindingMode(RestBindingMode.json).dataFormatProperty("json.in.disableFeatures","FAIL_ON_UNKNOWN_PROPERTIES")
.clientRequestValidation(true)
.apiContextPath(pathOpenapi)
.apiProperty("api.title","IdentifyOrigin")
.apiProperty("api.description",descriptionService)
.apiProperty("api-version","1.0.0")
.apiProperty("cors","true");
rest("/api/")
.produces("application/json")
.consumes("application/json")
.post("/identifyOrigin/v1/info")
.type(Request.class)
.param().name("x-correlator").type(RestParamType.header).required(true).endParam()
.outType(ResponseSuccess.class)
.param().name("Response").type(RestParamType.body).description("Parametros de Salidas")
.required(true)
.endParam().to("direct:pipeline");
from("direct:pipeline")
.doTry()
.to("bean-validator:validateRequest")
.log("["+"${bean:BeanDate.getCurrentDateTime()}"+"] "+ "Datos de Entrada del MS: ${body}")
.process(new GetActivateSessionByIpReqProcessor())
.log("\n[${bean:BeanDate.getCurrentDateTime()}] "+"Entrada Microservicio GetActivateSessionByIp: ${exchangeProperty[getActivateSessionByIpRequest]}")
.to(configureSsl.setupSSLContext(getCamelContext(), urlGetActivateSessionByIp))
.choice()
.when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(200))
.process(new GetActivateSessionByIpResProcessor())
.log("["+"${bean:BeanDate.getCurrentDateTime()}"+"] "+ "Salida MS GetActivateSessionByIp: ${body}")
.log("["+"${bean:BeanDate.getCurrentDateTime()}"+"] "+ "PhoneNumberByIp obtenido desde MS GetActivateSessionByIp: ${exchangeProperty[phoneNumberByIp]}")
.process(new UserProfileReqProcessor())
.to(configureSsl.setupSSLContext(getCamelContext(), urlUserProfile))
.log("["+"${bean:BeanDate.getCurrentDateTime()}"+"] "+ "Datos de Salida del MS UserProfile: ${body}")
.otherwise()
.process(new ApiResponseFaultProcessor())
.end()
.process(new UserProfileReqProcessor())
.to(configureSsl.setupSSLContext(getCamelContext(), urlUserProfile))
.log("["+"${bean:BeanDate.getCurrentDateTime()}"+"] "+ "Datos de Salida del MS UserProfile: ${body}")
.choice()
.when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(200))
.process(new UserProfileResProcessor())
.log("["+"${bean:BeanDate.getCurrentDateTime()}"+"] "+ "UserId obtenido desde UserProfile: ${exchangeProperty[userId]}")
.process(new IdentifyOriginResProcessor())
.log(DATE_LOG+"Salida MS IdentifyOrigin: ${exchangeProperty[identifyOriginResponse]}")
.otherwise()
.process(new ApiResponseFaultProcessor())
.end()
.endDoTry()
.doCatch(BeanValidationException.class)
.process(new InvalidFormatExcepctionProcessor())
.log(DATE_LOG+MSG_EXCEPTION)
.log(DATE_LOG+SALIDA_BSS_EXCEPTION)
.doCatch(NotFoundDataException.class)
.process(new NotFoundDataExceptionProcessor())
.log(DATE_LOG+MSG_EXCEPTION)
.log(DATE_LOG+SALIDA_BSS_EXCEPTION)
.doCatch(HttpHostConnectException.class)
.process(new HttpHostConnectExceptionProcessor())
.log(DATE_LOG+MSG_EXCEPTION)
.log(DATE_LOG+SALIDA_BSS_EXCEPTION)
.doCatch(Exception.class)
.process(new IdentifyOriginExceptionProcessor())
.log(DATE_LOG+MSG_EXCEPTION)
.log(DATE_LOG+SALIDA_BSS_EXCEPTION);
}
}
How can I do this?
from("direct:pipeline")
.process(exchange -> {
String xCorrelator = exchange.getIn().getHeader("x-correlator", String.class);
// Implement your validation logic here
if (xCorrelator == null || xCorrelator.isEmpty()) {
throw new ValidationException("x-correlator header is missing or invalid");
}
})
// Rest of your route...
.doCatch(ValidationException.class)
.process(exchange -> {
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
exchange.getIn().setBody("Invalid header information provided");
})
.log(DATE_LOG+MSG_EXCEPTION)
.log(DATE_LOG+SALIDA_BSS_EXCEPTION)
// Continue with other exception handling...
or
// Custom exception handling for validation errors
onException(PredicateValidationException.class)
.handled(true) // Stop the exception from propagating
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400)) // Set HTTP status 400
.setBody().constant("Invalid 'x-correlator' header value"); // Custom error message