Camel to D pattern dosen't keep the encoded part of the url when invoking a http endpoint

212 Views Asked by At

I exposed a web service on Apache Camel.

Endpoint: https://company.aaaa.com/persons/{zone}/{personId}/equipments?equipmentId=value (named webservice A) The web service A takes this parameters:

-zone de type string : path param

-personId de type integer : path param

-equipmentId de type string : request param

The web service A do many things and call many other web services but to keep things simple, I will focus only on the web service that fails.

so if the equipmentId is not null, the web service A call a web service B with this endpoint https://company.bbbb.com/persons/{zone}/{personId}/equipments/{equipmentId}

It works when the equipmentId dosen't contain / and I got the right response.

But My problem is if equipmentId contains / caracter for example (equipmentId = AQCz/gIAJWsDACivB==) It will fails with 404 not found because he can't find the resource.

So I encoded the equipmentId, store it in an exchange property called equipmentEncoded and use the toD camel pattern to invoke the endpoint.

Then when I called the web service A :

https://company.aaaa.com/persons/zone/56809546/equipments?equipmentId=AQCz/gIAJWsDACivB==

I got an error 404 : Failed to invoke the endpoint https://company.bbbb.com/persons/zone/56809546/equipments/AQCz/gIAJWsDACivB==

When debugging : I do have an exchange property equipmentEncoded with right value encoded AQCz%2FgIAJWsDACivB%3D%3D But I don't know why toD don't keep the encoded value of the exchange property equipmentEncoded

This is the algorithme of the web service A : (Just the part that generates error)

  .process(setEquipmentIdPropertyProcessor)
                .choice()
                    .when(exchangeProperty(TestConstants.EQUIPMENT_ID_REQUEST_PARAM_EXIST).isEqualTo(1))
                        .process(exchange -> {
                            var t = true;
                        })
                        .toD(getHTTPEndpointURL(companyBEndpoint, "persons/${header." + TestConstants.HEADER_ZONE_PARAM + "}/"
                                + "${header." + TestConstants.HEADER_PERSON_ID_PARAM + "}/" + "equipments/"
                                +"${exchangeProperty." + TestConstants.EQUIPMENT_ENCODED + "}" ))
                .endChoice()
                .otherwise()
                    .process(exchange -> {
                        var t = true;
                    })

This the code of the processor setEquipmentIdPropertyProcessor

@Component
  public class SetEquipmentIdPropertyProcessor implements Processor {
  @Override
    public void process(Exchange exchange) throws Exception {
      String equipmentParamValue = 
       (String)exchange.getIn().getHeader(TestConstants.HEADER_EQUIPMENT_PARAM);

    if (StringUtils.isNotBlank(equipmentParamValue)){
        exchange.setProperty(TestConstants.EQUIPMENT_ID_REQUEST_PARAM_EXIST, 1);
        String equipmentEncoded = URLEncoder.encode(equipmentParamValue, StandardCharsets.UTF_8);
        exchange.setProperty(TestConstants.EQUIPMENT_ENCODED, equipmentEncoded);
    } else {
        exchange.setProperty(TestConstants.EQUIPMENT_ID_REQUEST_PARAM_EXIST, 0);
    }

}

}

so in the class SetEquipmentIdPropertyProcessor when the equipmentId request param is filled, I encode the equipmentId param and store the value in an exchange property.

Here is the code of the method getHttpEndpointURL to construct the url:

    public String getHTTPEndpointURL(final EndpointProperty endpointConf, final String 
             uriPath) {
    return endpointConf.getUrl() +
            endpointConf.getPath() +
            uriPath + "?bridgeEndpoint=true";

}

Here is the code of the classe TestConstants

public class TestConstants {
public static final String HEADER_ZONE_PARAM = "zone";
public static final String HEADER_PERSON_ID_PARAM = "personId";
public static final String HEADER_EQUIPMENT_PARAM = "equipment";
public static final String EQUIPMENT_ENCODED = "equipmentEncoded"; }

I would like that Camel invokes this url https://company.bbbb.com/persons/zone/56809546/equipments/AQCz%2FgIAJWsDACivB%3D%3D

instead of invoking this one : https://company.bbbb.com/persons/zone/56809546/equipments/AQCz/gIAJWsDACivB==

0

There are 0 best solutions below