DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Trending

  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • PWA on iOS, Part 1: PWAs on the iPhone Are Older Than They Look
  • How to Build Your Own AI SRE From Scratch
  • Frame Buffer Hashing for Visual Regression on Embedded Devices

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Camel CXF Service With Multiple Query Parameters

Camel CXF Service With Multiple Query Parameters

By 
Arun Manivannan
·
Nov. 17, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

While the awesome Apache Camel team is busy fixing the handling of the multiple parameters in the query, here’s a workaround. Hopefully, this post will become obsolete with the next versions of Camel. (Currently, I use 2.7.5)

Problem

Query parameters more than 1 is passed as a null value into a Camel-CXF service. Say, if the URL has four query parameters as in

name=arun&[email protected]&age=10&phone=123456


only the first one gets populated when you do a

Multi Query Params

@GET
@Path("search")
@Produces(MediaType.APPLICATION_JSON)
public String sourceResultsFromTwoSources(@QueryParam("name") String name, @QueryParam("age") String age,
                                          @QueryParam("phone") String phone,@QueryParam("email") String email
);


All other parameters are null.

Final Output

For url

http://localhost:8181/cxf/karafcxfcamel/search?name=arun&[email protected]&age=31&phone=232323

the result expected is :

Workaround

Interestingly, we could get the entire query string in the header.

QueryStringHeader
1.String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);

We could then do a

ExtractingParams
MultivaluedMap<String, String> queryMap = JAXRSUtils.getStructuredParams(queryString, "&", false, false);

to get the query parameters as a multi valued Map.

The query parameters could then be set as a property to the Exchange and used across the exchange.

Code

The entire code could be downloaded from github

Please note that I am running Camel as part of OSGi inside the Karaf container. While the workaround does not differ because of the environment in which you are using Camel-CXF, please be wary of this fact when you download the code from github. Watch out for the blueprint xmls for Camel configuration.

The most important piece here is the Router

Router

RestToBeanRouter
package me.rerun.karafcxfcamel.camel.beans;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.core.MultivaluedMap;
import java.util.List;
import java.util.Map;
public class RestToBeanRouter extends RouteBuilder {
    private static Logger logger= LoggerFactory.getLogger(RouteBuilder.class);
    @Override
    public void configure() throws Exception {
        from ("cxfrs://bean://rsServer")
                .process(new ParamProcessor())
                .multicast()
                .parallelProcessing()
                .aggregationStrategy(new ResultAggregator())
                .beanRef("restServiceImpl", "getNameEmailResult")
                .beanRef("restServiceImpl", "getAgePhoneResult")
                .end()
                .marshal().json(JsonLibrary.Jackson)
                .to("log://camelLogger?level=DEBUG");
    }
    private class ParamProcessor implements Processor {
        @Override
        public void process(Exchange exchange) throws Exception {
            String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
            MultivaluedMap<String, String> queryMap = JAXRSUtils.getStructuredParams(queryString, "&", false, false);
            for (Map.Entry<String, List<String>> eachQueryParam : queryMap.entrySet()) {
                exchange.setProperty(eachQueryParam.getKey(), eachQueryParam.getValue());
            }
        }
    }
}

Interface

RestService
package me.rerun.karafcxfcamel.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public interface RestService {
    @GET
    @Path("search")
    @Produces(MediaType.APPLICATION_JSON)
    public String sourceResultsFromTwoSources();
}

Implementation

RestServiceImpl
package me.rerun.karafcxfcamel.rest;
import me.rerun.karafcxfcamel.model.AgePhoneResult;
import me.rerun.karafcxfcamel.model.NameEmailResult;
import me.rerun.karafcxfcamel.service.base.AgePhoneService;
import me.rerun.karafcxfcamel.service.base.NameEmailService;
import me.rerun.karafcxfcamel.service.impl.AgePhoneServiceImpl;
import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class RestServiceImpl implements RestService {
    private static Logger logger= LoggerFactory.getLogger(AgePhoneServiceImpl.class);
    private NameEmailService nameEmailService;
    private AgePhoneService agePhoneService;
    public RestServiceImpl(){
    }
    //Do nothing. Camel intercepts and routes the requests
    public String sourceResultsFromTwoSources() {
        return null;
    }
    public NameEmailResult getNameEmailResult(Exchange exchange){
        logger.info("Invoking getNameEmailResult from RestServiceImpl");
        String name=getFirstEntrySafelyFromList(exchange.getProperty("name", List.class));
        String email=getFirstEntrySafelyFromList(exchange.getProperty("email", List.class));
        return nameEmailService.getNameAndEmail(name, email);
    }
    public AgePhoneResult getAgePhoneResult(Exchange exchange){
        logger.info("Invoking getAgePhoneResult from RestServiceImpl");
        String age=getFirstEntrySafelyFromList(exchange.getProperty("age", List.class));
        String phone=getFirstEntrySafelyFromList(exchange.getProperty("phone", List.class));
        return agePhoneService.getAgePhoneResult(age, phone);
    }
    public NameEmailService getNameEmailService() {
        return nameEmailService;
    }
    public AgePhoneService getAgePhoneService() {
        return agePhoneService;
    }
    public void setNameEmailService(NameEmailService nameEmailService) {
        this.nameEmailService = nameEmailService;
    }
    public void setAgePhoneService(AgePhoneService agePhoneService) {
        this.agePhoneService = agePhoneService;
    }
    private String getFirstEntrySafelyFromList(List<String> list){
        if (list!=null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
}

Reference

Camel Mailing List Question

Database

Published at DZone with permission of Arun Manivannan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook