Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 699

How to add custom response when validating request header in Apache Camel

$
0
0

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:

enter image description here

And I want an answer like this:

enter image description here

My RestRoure code is the following:

@ApplicationScopedpublic 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?


Viewing all articles
Browse latest Browse all 699

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>