Wordpress
Since Camel 2.21
Both producer and consumer are supported
Camel component for 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
The component level is the highest level which holds general and common configurations that are inherited by the endpoints. 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.
Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.
Configuring Endpoint Options
Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for 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, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.
The following two sections lists all the options, firstly for the component followed by the endpoint.
Component Options
The Wordpress component supports 14 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
The Wordpress REST API version. |
2 |
String |
|
Wordpress configuration. |
WordpressConfiguration |
||
The criteria to use with complex searches. |
Map |
||
Whether to bypass trash and force deletion. |
false |
boolean |
|
The entity ID. Should be passed when the operation performed requires a specific entity, e.g. deleting a post. |
Integer |
||
Password from authorized user. |
String |
||
Search criteria. |
SearchCriteria |
||
Required The Wordpress API URL from your site, e.g. http://myblog.com/wp-json/. |
String |
||
Authorized user to perform writing operations. |
String |
||
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 |
|
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 |
|
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 |
|
Used for enabling or disabling all consumer based health checks from this component. |
true |
boolean |
|
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 |
Endpoint Options
The Wordpress endpoint is configured using URI syntax:
wordpress:operation
with the following path and query parameters:
Path Parameters (2 parameters)
Name | Description | Default | Type |
---|---|---|---|
Required The endpoint operation. Enum values:
|
String |
||
The second part of an endpoint operation. Needed only when endpoint semantic is not enough, like wordpress:post:delete. Enum values:
|
String |
Query Parameters (12 parameters)
Name | Description | Default | Type |
---|---|---|---|
The Wordpress REST API version. |
2 |
String |
|
The criteria to use with complex searches. |
Map |
||
Whether to bypass trash and force deletion. |
false |
boolean |
|
The entity ID. Should be passed when the operation performed requires a specific entity, e.g. deleting a post. |
Integer |
||
Password from authorized user. |
String |
||
Search criteria. |
SearchCriteria |
||
Required The Wordpress API URL from your site, e.g. http://myblog.com/wp-json/. |
String |
||
Authorized user to perform writing operations. |
String |
||
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 |
|
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 |
||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
|
ExchangePattern |
||
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 |
Most of parameters needed when performing a read operation mirrors from the official API. When performing searches operations, the criteria.
suffix is needed. Take the following Consumer
as example:
wordpress:post?criteria.perPage=10&criteria.orderBy=author&criteria.categories=camel,dozer,json
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.
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");
}
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
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 theorg.apache.camel.component.wordpress.api.model.Post
class in the message body. -
wordpress:post?id=1
updates a post based on dataorg.apache.camel.component.wordpress.api.model.Post
from the message body. -
wordpress:post:delete?id=1
deletes a specific post
Authentication
Producers that perform write operations (e.g. create 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’s rely on nonce internal function.
There’s some alternatives to use the Wordpress API without nonces, but requires 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 recommend to use Basic Authentication in production without TLS!!