Search in sources :

Example 1 with SecurityContext

use of com.marklogic.client.DatabaseClientFactory.SecurityContext in project components by Talend.

the class MarkLogicConnection method connect.

/**
 * Creates new {@link DatabaseClient} connection or gets referenced one from container.
 *
 * @param container
 * @return connection to MarkLogic database.
 * @throws IOException thrown if referenced connection is not connected.
 */
public DatabaseClient connect(RuntimeContainer container) {
    MarkLogicConnectionProperties properties = getMarkLogicConnectionProperties();
    if (properties.getReferencedComponentId() != null && container != null) {
        DatabaseClient client = (DatabaseClient) container.getComponentData(properties.getReferencedComponentId(), CONNECTION);
        if (client != null) {
            return client;
        }
        throw new MarkLogicException(new MarkLogicErrorCode(MESSAGES.getMessage("error.invalid.referenceConnection", properties.getReferencedComponentId())));
    }
    SecurityContext context = "BASIC".equals(properties.authentication.getValue()) ? new DatabaseClientFactory.BasicAuthContext(properties.username.getValue(), properties.password.getValue()) : new DatabaseClientFactory.DigestAuthContext(properties.username.getValue(), properties.password.getValue());
    DatabaseClient client = DatabaseClientFactory.newClient(properties.host.getValue(), properties.port.getValue(), properties.database.getValue(), context);
    testConnection(client);
    LOGGER.info("Connected to MarkLogic server");
    if (container != null) {
        container.setComponentData(container.getCurrentComponentId(), CONNECTION, client);
        LOGGER.info("Connection stored in container");
    }
    return client;
}
Also used : MarkLogicException(org.talend.components.marklogic.exceptions.MarkLogicException) DatabaseClient(com.marklogic.client.DatabaseClient) DatabaseClientFactory(com.marklogic.client.DatabaseClientFactory) MarkLogicConnectionProperties(org.talend.components.marklogic.tmarklogicconnection.MarkLogicConnectionProperties) MarkLogicErrorCode(org.talend.components.marklogic.exceptions.MarkLogicErrorCode) SecurityContext(com.marklogic.client.DatabaseClientFactory.SecurityContext)

Aggregations

DatabaseClient (com.marklogic.client.DatabaseClient)1 DatabaseClientFactory (com.marklogic.client.DatabaseClientFactory)1 SecurityContext (com.marklogic.client.DatabaseClientFactory.SecurityContext)1 MarkLogicErrorCode (org.talend.components.marklogic.exceptions.MarkLogicErrorCode)1 MarkLogicException (org.talend.components.marklogic.exceptions.MarkLogicException)1 MarkLogicConnectionProperties (org.talend.components.marklogic.tmarklogicconnection.MarkLogicConnectionProperties)1