/*
 * Copyright 2015 herd contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.finra.herd.tools.retention.destroyer;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import javax.xml.bind.JAXBException;

import org.apache.commons.collections.CollectionUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import org.finra.herd.model.api.xml.BusinessObjectData;
import org.finra.herd.model.api.xml.BusinessObjectDataKey;
import org.finra.herd.tools.common.databridge.DataBridgeWebClient;

/**
 * This class encapsulates web client functionality required to communicate with the registration server.
 */
@Component
public class RetentionExpirationDestroyerWebClient extends DataBridgeWebClient
{
    private static final Logger LOGGER = LoggerFactory.getLogger(RetentionExpirationDestroyerWebClient.class);

    /**
     * Retrieves business object definition from the herd registration server.
     *
     * @param businessObjectDataKey the name of the business object data key
     *
     * @return the business object definition
     * @throws JAXBException if a JAXB error was encountered
     * @throws IOException if an I/O error was encountered
     * @throws URISyntaxException if a URI syntax error was encountered
     */
    public BusinessObjectData destroyBusinessObjectData(BusinessObjectDataKey businessObjectDataKey) throws IOException, JAXBException, URISyntaxException
    {
        StringBuilder uriPathBuilder = new StringBuilder(HERD_APP_REST_URI_PREFIX);
        uriPathBuilder.append("/businessObjectData/destroy");
        uriPathBuilder.append("/namespaces/").append(businessObjectDataKey.getNamespace());
        uriPathBuilder.append("/businessObjectDefinitionNames/").append(businessObjectDataKey.getBusinessObjectDefinitionName());
        uriPathBuilder.append("/businessObjectFormatUsages/").append(businessObjectDataKey.getBusinessObjectFormatUsage());
        uriPathBuilder.append("/businessObjectFormatFileType/").append(businessObjectDataKey.getBusinessObjectFormatFileType());
        uriPathBuilder.append("/businessObjectFormatVersion/").append(businessObjectDataKey.getBusinessObjectFormatVersion());
        uriPathBuilder.append("/partitionValue/").append(businessObjectDataKey.getPartitionValue());
        uriPathBuilder.append("/businessObjectDataVersion/").append(businessObjectDataKey.getBusinessObjectDataVersion());

        URIBuilder uriBuilder =
            new URIBuilder().setScheme(getUriScheme()).setHost(regServerAccessParamsDto.getRegServerHost()).setPort(regServerAccessParamsDto.getRegServerPort())
                .setPath(uriPathBuilder.toString());

        if (CollectionUtils.isNotEmpty(businessObjectDataKey.getSubPartitionValues()))
        {
            uriBuilder.setParameter("subPartitionValues", herdStringHelper.join(businessObjectDataKey.getSubPartitionValues(), "|", "\\"));
        }

        URI uri = uriBuilder.build();

        try (CloseableHttpClient client = httpClientOperations.createHttpClient())
        {
            HttpPost request = new HttpPost(uri);
            request.addHeader("Content-Type", DEFAULT_CONTENT_TYPE);
            request.addHeader("Accepts", DEFAULT_ACCEPT);

            // If SSL is enabled, set the client authentication header.
            if (regServerAccessParamsDto.isUseSsl())
            {
                request.addHeader(getAuthorizationHeader());
            }

            LOGGER.info(String.format("    HTTP POST URI: %s", request.getURI().toString()));
            LOGGER.info(String.format("    HTTP POST Headers: %s", Arrays.toString(request.getAllHeaders())));

            BusinessObjectData businessObjectData = destroyBusinessObjectData(httpClientOperations.execute(client, request));

            LOGGER.info("Successfully destroyed business object data from the registration server.");

            return businessObjectData;
        }
    }

    /**
     * Call business object Destroy to mark for destruction.
     *
     * @param httpResponse the response received from the supported options
     *
     * @return the BusinessObjectDefinition object extracted from the registration server response
     */
    private BusinessObjectData destroyBusinessObjectData(CloseableHttpResponse httpResponse)
    {
        return (BusinessObjectData) processXmlHttpResponse(httpResponse, "destroy business object data", BusinessObjectData.class);
    }
}

@@ -2807,16 +2807,17 @@
 FileType
+s
 /%22).appe
@@ -2932,24 +2932,25 @@
 ormatVersion
+s
 /%22).append(b
@@ -3051,16 +3051,17 @@
 ionValue
+s
 /%22).appe
@@ -3164,16 +3164,17 @@
 aVersion
+s
 /%22).appe

Mark BData for destruction based on approved BData file from FINRA RDL