[[ldif-component]]
= LDIF Component

*Since Camel 2.20*

// HEADER START
*Only producer is supported*
// HEADER END

The LDIF component allows you to do updates on an LDAP server
from a LDIF body content.

This component uses a basic URL syntax to access the server. It uses
the Apache DS LDAP library to process the LDIF. After processing the LDIF,
the response body will be a list of statuses for success/failure of each
entry.

[NOTE]
The Apache LDAP API is very sensitive to LDIF syntax errors. If in doubt,
refer to the unit tests to see an example of each change type.

Maven users will need to add the following dependency to their `pom.xml`
for this component:

[source,xml]
----
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-ldif</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
----

== URI format

[source]
----
ldif:ldapServerBean[?options]
----

The _ldapServerBean_ portion of the URI refers to a
https://directory.apache.org/api/gen-docs/latest/apidocs/org/apache/directory/ldap/client/api/LdapConnection.html[LdapConnection].
This should be constructed from a factory at the point of use to avoid connection timeouts. The LDIF component only supports producer
endpoints, which means that an `ldif` URI cannot appear in the `from` at
the start of a route.

For SSL configuration, refer to the `camel-ldap` component where there is an example
of setting up a custom SocketFactory instance.

You can append query options to the URI in the following format,
`?option=value&option=value&...`

== Options

// component options: START
The LDIF component supports 2 options, which are listed below.



[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *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
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
|===
// component options: END

// endpoint options: START
The LDIF endpoint is configured using URI syntax:

----
ldif:ldapConnectionName
----

with the following path and query parameters:

=== Path Parameters (1 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *ldapConnectionName* | *Required* The name of the LdapConnection bean to pull from the registry. Note that this must be of scope prototype to avoid it being shared among threads or using a connection that has timed out. |  | String
|===


=== Query Parameters (3 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *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
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END


== Body types:

The body can be a URL to an LDIF file or an inline LDIF file. To signify the difference
in body types, an inline LDIF must start with:

[source]
----
version: 1
----

If not, the component will try to parse the body as a URL.

== Result

The result is returned in the Out body as a `ArrayList<java.lang.String>` object.
This contains either "success" or an Exception message for each LDIF entry.

== LdapConnection

The URI, `ldif:ldapConnectionName`, references a bean with the ID,
`ldapConnectionName`. The ldapConnection can be configured using a
`LdapConnectionConfig` bean. Note that the scope must have a scope of
`prototype` to avoid the connection being shared or picking up a
stale connection.

The `LdapConnection` bean may be defined as follows in Spring XML:

[source,xml]
----
<bean id="ldapConnectionOptions" class="org.apache.directory.ldap.client.api.LdapConnectionConfig">
  <property name="ldapHost" value="${ldap.host}"/>
  <property name="ldapPort" value="${ldap.port}"/>
  <property name="name" value="${ldap.username}"/>
  <property name="credentials" value="${ldap.password}"/>
  <property name="useSsl" value="false"/>
  <property name="useTls" value="false"/>
</bean>

<bean id="ldapConnectionFactory" class="org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory">
  <constructor-arg index="0" ref="ldapConnectionOptions"/>
</bean>

<bean id="ldapConnection" factory-bean="ldapConnectionFactory" factory-method="newLdapConnection" scope="prototype"/>
----

or in a OSGi blueprint.xml:
[source,xml]
----
<bean id="ldapConnectionOptions" class="org.apache.directory.ldap.client.api.LdapConnectionConfig">
  <property name="ldapHost" value="${ldap.host}"/>
  <property name="ldapPort" value="${ldap.port}"/>
  <property name="name" value="${ldap.username}"/>
  <property name="credentials" value="${ldap.password}"/>
  <property name="useSsl" value="false"/>
  <property name="useTls" value="false"/>
</bean>

<bean id="ldapConnectionFactory" class="org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory">
  <argument ref="ldapConnectionOptions"/>
</bean>

<bean id="ldapConnection" factory-ref="ldapConnectionFactory" factory-method="newLdapConnection" scope="prototype"/>
----


== Samples

Following on from the Spring configuration above, the code sample below
sends an LDAP request to filter search a group for a member. The Common
Name is then extracted from the response.

[source,java]
----
ProducerTemplate<Exchange> template = exchange.getContext().createProducerTemplate();

List<?> results = (Collection<?>) template.sendBody("ldif:ldapConnection, "LDiff goes here");

if (results.size() > 0) {
  // Check for no errors

  for (String result : results) {
    if ("success".equalTo(result)) {
      // LDIF entry success
    } else {
      // LDIF entry failure
    }
  }
}
----

include::camel-spring-boot::page$ldif-starter.adoc[]
