Camel Components

WordPress

Since Camel 2.21

Both producer and consumer are supported

Camel component for the WordPress API.

Currently only the Posts and Users operations are supported.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The WordPress component supports the following options which are listed below.

Name Description Default Type

apiVersion (common)

The version of the WordPress REST API, used in the request paths (wp/v2/posts for version 2).

2

String

configuration (common)

The configuration whose values are the defaults of every endpoint of this component.

WordpressConfiguration

criteria (common)

The search parameters of the consumer, with the property names of PostSearchCriteria or UserSearchCriteria, for example criteria.perPage=10. Not used when searchCriteria or id is set. This is a multi-value option with prefix: criteria.

Map

force (common)

Whether the delete of a post removes it permanently instead of moving it to the trash. A user is always deleted permanently.

false

boolean

id (common)

The id of the post or user to work with. With it, the consumer polls only this entity, and the producer updates it, or deletes it with the delete operation detail. Without it, the consumer polls the search criteria and the producer creates a new entity.

Integer

searchCriteria (common)

The search criteria of the consumer: a PostSearchCriteria for post or a UserSearchCriteria for user, of the org.apache.camel.component.wordpress.api.model package. When set, the criteria options are not used.

SearchCriteria

url (common)

Required The URL of the WordPress REST API of your site, for example http://myblog.com/wp-json/.

String

user (common)

The name of an authorized user, needed to perform write operations.

String

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

healthCheckConsumerEnabled (health)

Used for enabling or disabling all consumer based health checks from this component.

true

boolean

healthCheckProducerEnabled (health)

Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true.

true

boolean

password (security)

The password of the authorized user.

String

Endpoint Options

The WordPress endpoint is configured using URI syntax:

wordpress:operation

With the following path and query parameters:

Path Parameters

Name Description Default Type

operation (common)

Required The kind of WordPress entity to work with: post or user.

Enum values:

  • post

  • user

String

operationDetail (common)

Set to delete to delete the post or user of the id option, as in wordpress:post:delete. Without it, the producer creates a new entity, or updates the one of the id option.

Enum values:

  • delete

String

Query Parameters

Name Description Default Type

apiVersion (common)

The version of the WordPress REST API, used in the request paths (wp/v2/posts for version 2).

2

String

criteria (common)

The search parameters of the consumer, with the property names of PostSearchCriteria or UserSearchCriteria, for example criteria.perPage=10. Not used when searchCriteria or id is set. This is a multi-value option with prefix: criteria.

Map

force (common)

Whether the delete of a post removes it permanently instead of moving it to the trash. A user is always deleted permanently.

false

boolean

id (common)

The id of the post or user to work with. With it, the consumer polls only this entity, and the producer updates it, or deletes it with the delete operation detail. Without it, the consumer polls the search criteria and the producer creates a new entity.

Integer

searchCriteria (common)

The search criteria of the consumer: a PostSearchCriteria for post or a UserSearchCriteria for user, of the org.apache.camel.component.wordpress.api.model package. When set, the criteria options are not used.

SearchCriteria

url (common)

Required The URL of the WordPress REST API of your site, for example http://myblog.com/wp-json/.

String

user (common)

The name of an authorized user, needed to perform write operations.

String

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

password (security)

The password of the authorized user.

String

Message Body

The message body at each side of the WordPress component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Consumer

pojo

One message per post or user of the search at every poll, the same ones again on the next poll, or the one of the id option: a Post or a User of org.apache.camel.component.wordpress.api.model.

  • With operation=post: pojo (org.apache.camel.component.wordpress.api.model.Post)

  • With operation=user: pojo (org.apache.camel.component.wordpress.api.model.User)

Producer input

pojo, text, bytes, stream or map

The Post or User (org.apache.camel.component.wordpress.api.model) to create, or to update with the id option. JSON text or a Map is converted to the class with Jackson; another body fails. A delete does not read the body.

  • With operationDetail=delete: any

Producer output

pojo

The Post or User that WordPress answers: the one created, updated or deleted.

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Most of the parameters needed when performing a read operation mirrors from the official API. When performing search operations, the criteria. prefix is needed. Take the following Consumer as example:

wordpress:post?criteria.perPage=10&criteria.orderBy=author&criteria.categories=camel,dozer,json

Usage

Configuring WordPress component

The WordpressConfiguration class can be used to set initial properties configuration to the component instead of passing it as query parameter. The following listing shows how to set the component to be used in your routes.

Java-only: configuring the WordPress component programmatically
public void configure() {
    final WordpressConfiguration configuration = new WordpressConfiguration();
    final WordpressComponent component = new WordpressComponent();
    configuration.setApiVersion("2");
    configuration.setUrl("http://yoursite.com/wp-json/");
    component.setConfiguration(configuration);
    getContext().addComponent("wordpress", component);

    from("wordpress:post?id=1")
      .to("mock:result");
}

Without Java code, set the same component options in application.properties:

camel.component.wordpress.url = http://yoursite.com/wp-json/
camel.component.wordpress.api-version = 2

Examples

Consumer Example

Consumer polls from the API from time to time domain objects from WordPress. Following, an example using the Post operation:

  • wordpress:post retrieves posts (defaults to 10 posts)

  • wordpress:post?id=1 search for a specific post

The following route polls the post with the id 1:

- route:
    from:
      uri: wordpress:post
      parameters:
        id: 1
      steps:
        - log:
            message: "${body}"

Producer Example

Producer performs write operations on WordPress like adding a new user or update a post. To be able to write, you must have an authorized user credentials (see Authentication).

  • wordpress:post creates a new post from the org.apache.camel.component.wordpress.api.model.Post class in the message body.

  • wordpress:post?id=1 updates a post based on data org.apache.camel.component.wordpress.api.model.Post from the message body.

  • wordpress:post:delete?id=1 deletes a specific post

The message body can also be JSON text (a String, byte[] or InputStream) or a Map, with the same field names as the Post or User that WordPress answers (for example first_name for a user). It is converted to the Post or User class with Jackson. Any other body fails with an InvalidPayloadException.

Authentication

Producers that perform write operations, e.g., creating a new post, must have an authenticated user to do so. The standard authentication mechanism used by WordPress is cookie. Unfortunately, this method is not supported outside WordPress environment because it relies on nonce internal function.

There are some alternatives to using the WordPress API without nonces, but require specific plugin installations.

At this time, camel-wordpress only supports Basic Authentication (more to come). To configure it, you must install the Basic-Auth WordPress plugin and pass the credentials to the endpoint:

from("direct:deletePost").to("wordpress:post:delete?id=9&user=ben&password=password123").to("mock:resultDelete");

It’s not recommended to use Basic Authentication in production without TLS!!