com.autonomy.aci.client.services.impl.AciServiceImpl Maven / Gradle / Ivy
/*
* Copyright 2006-2018 Open Text.
*
* Licensed under the MIT License (the "License"); you may not use this file
* except in compliance with the License.
*
* The only warranties for products and services of Open Text and its affiliates
* and licensors ("Open Text") are as may be set forth in the express warranty
* statements accompanying such products and services. Nothing herein should be
* construed as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained herein. The
* information contained herein is subject to change without notice.
*/
package com.autonomy.aci.client.services.impl;
import com.autonomy.aci.client.services.*;
import com.autonomy.aci.client.transport.*;
import com.autonomy.aci.client.util.IOUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Set;
/**
* Implementation of the AciService
interface.
*
* This implementation of the AciService
interface does no configuration of the AciHttpClient
or
* AciServerDetails
objects that it uses. It expects all the configuration to have been done by the user before
* passing them to this object. This configuration can be done in normal code, or via an IoC container like Spring.
*/
public class AciServiceImpl implements AciService {
/**
* Class logger...
*/
private static final Logger LOGGER = LoggerFactory.getLogger(AciServiceImpl.class);
/**
* Used to confirm that a passed in set of parameters contains an action=xxx parameter...
*/
private static final AciParameter TEST_ACTION_PARAMETER = new AciParameter(AciConstants.PARAM_ACTION, null);
/**
* Holds value of property aciHttpClient.
*/
private AciHttpClient aciHttpClient;
/**
* Holds value of property aciServerDetails.
*/
private AciServerDetails aciServerDetails;
/**
* Creates a new instance of AciServiceImpl.
*/
public AciServiceImpl() {
super();
}
/**
* Creates a new instance of AciServiceImpl.
* @param aciHttpClient The configured AciHttpClient
to use for communication
*/
public AciServiceImpl(final AciHttpClient aciHttpClient) {
this.aciHttpClient = aciHttpClient;
}
/**
* Creates a new instance of AciServiceImpl.
* @param aciHttpClient The configured AciHttpClient
to use for communication
* @param aciServerDetails The details of the ACI Server to contact
*/
public AciServiceImpl(final AciHttpClient aciHttpClient, final AciServerDetails aciServerDetails) {
this.aciHttpClient = aciHttpClient;
this.aciServerDetails = aciServerDetails;
}
/**
* Executes an ACI action and processes the response with the supplied Processor
. This method relies on the
* ACI Server connection details being set via the {@link #setAciServerDetails(AciServerDetails)} method.
* @param parameters The parameters to use with the ACI command. This should include an {@code
* Action=} parameter.
* @param processor The Processor
to use for converting the response stream into an object
* @return The ACI response encoded as an object of type T
* @throws AciServiceException If an error occurred during the communication with the ACI Server, processing the
* response or if the response contained an error
* @throws IllegalArgumentException If connection details haven't bee set, or the parameters
is null
,
* empty or missing an action parameter. Will also be thrown in the processor
* is null.
*/
@Override
public T executeAction(final Set extends ActionParameter>> parameters, final Processor processor) {
LOGGER.trace("executeAction() called...");
// Execute and return the result...
return executeAction(aciServerDetails, parameters, processor);
}
/**
* Executes an ACI action and processes the response with the supplied Processor
.
* @param serverDetails The connection details of the ACI Server to execute the action on
* @param parameters The parameters to use with the ACI command. This should include an {@code
* Action=<command>} parameter
* @param processor The Processor
to use for converting the response stream into an object
* @return The ACI response encoded as an object of type T
* @throws AciServiceException If an error occurred during the communication with the ACI Server, processing the
* response or if the response contained an error
* @throws IllegalArgumentException If serverDetails
is null
, or the parameters
is
* null
, empty or missing an action parameter. Will also be thrown in the
* processor
is null.
*/
@Override
public T executeAction(final AciServerDetails serverDetails, final Set extends ActionParameter>> parameters, final Processor processor) {
LOGGER.trace("executeAction() called...");
// Sanity check the HttpClient...
Validate.notNull(aciHttpClient, "An AciHttpClient implementation must be set before calling this method.");
// Sanity check the method parameters...
Validate.notNull(serverDetails, "ACI Server connection details must be set before calling this method.");
Validate.notEmpty(parameters, "The parameter set must not be null or empty.");
Validate.isTrue(parameters.contains(TEST_ACTION_PARAMETER), "The parameter set must contain an action=xxx parameter.");
Validate.notNull(processor, "The processor must not be null.");
// This is so we can close the response and return the connection to the pool...
AciResponseInputStream response = null;
try {
LOGGER.debug("Sending the ACI parameters and server details to the AciHttpClient...");
// Execute the action and process the response...
response = aciHttpClient.executeAction(serverDetails, parameters);
return processor.process(response);
} catch (final AciHttpException ahe) {
LOGGER.trace("AciHttpException caught while executing the ACI action");
throw new AciServiceException(ahe);
} catch (final IOException ioe) {
LOGGER.trace("IOException caught while executing the ACI action");
throw new AciServiceException(ioe);
} catch (final ProcessorException pe) {
LOGGER.trace("ProcessorException caught while parsing ACI response");
throw new AciServiceException(pe);
} finally {
// Close the response as the processor should have dealt with it...
IOUtils.getInstance().closeQuietly(response);
}
}
/**
* Getter for property aciHttpClient.
* @return Value of property aciHttpClient.
*/
public AciHttpClient getAciHttpClient() {
return this.aciHttpClient;
}
/**
* Setter for property aciHttpClient.
* @param aciHttpClient New value of property aciHttpClient.
*/
public void setAciHttpClient(final AciHttpClient aciHttpClient) {
this.aciHttpClient = aciHttpClient;
}
/**
* Getter for property aciServerDetails.
* @return Value of property aciServerDetails.
*/
public AciServerDetails getAciServerDetails() {
return this.aciServerDetails;
}
/**
* Setter for property aciServerDetails.
* @param aciServerDetails New value of property aciServerDetails.
*/
public void setAciServerDetails(final AciServerDetails aciServerDetails) {
this.aciServerDetails = aciServerDetails;
}
}