Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsatisfied dependency expressed through constructor argument with index 0

I am getting the following error. I have also mentioned the java code related to the error.

Error

Following is the error I am getting

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.hp.ccue.serviceExchange.rest.OrderUtilsResource.orderUtils; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sawOrderUtils' defined in URL [jar:file:/opt/hp/propel/sx/WEB-INF/lib/sx-adapter-saw-2.20-SNAPSHOT.jar!/com/hp/ccue/serviceExchange/adapter/saw/util/SawOrderUtils.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.hp.ccue.serviceExchange.adapter.saw.SawOperationExecutor]: : No qualifying bean of type [com.hp.ccue.serviceExchange.adapter.saw.SawOperationExecutor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hp.ccue.serviceExchange.adapter.saw.SawOperationExecutor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 58 more

SawOrderUtils.java The file for which the error is related

package com.hp.ccue.serviceExchange.adapter.saw.util;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.hp.ccue.serviceExchange.SXConstants;
import com.hp.ccue.serviceExchange.adapter.saw.SawConstants;
import com.hp.ccue.serviceExchange.adapter.saw.SawOperationExecutor;
import com.hp.ccue.serviceExchange.aggregation.AggregationConstants;
import com.hp.ccue.serviceExchange.config.Configuration;
import com.hp.ccue.serviceExchange.message.MessageTransformer;
import com.hp.ccue.serviceExchange.message.builder.MessageConstants;
import com.hp.ccue.serviceExchange.utils.JsonUtils;
import com.hp.ccue.serviceExchange.utils.MessageUtils;
import com.hp.ccue.serviceExchange.utils.OrderUtilsBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.google.common.base.Objects.firstNonNull;
import static com.hp.ccue.serviceExchange.SXConstants.SawInstancesCfg;
import static com.hp.ccue.serviceExchange.utils.JsonUtils.getField;

@Component
public class SawOrderUtils extends OrderUtilsBase {
    @Autowired
    private Configuration configuration;

    @Autowired
    private MessageTransformer messageTransformer;

    @Autowired
    public SawOrderUtils(SawOperationExecutor operationExecutor) {
        super(SawConstants.SAW_TYPE, operationExecutor, SawInstancesCfg.CFG_NAME);
    }

    @Override
    public Map<String, String> getInstances() {
        Map<String, Object> allInstanceConfigs = configuration.getConfiguration(getInstancesConfigPath());
        Map<String, Object> instanceConfigs = Maps.filterValues(allInstanceConfigs, new Predicate<Object>() {
            @Override
            public boolean apply(Object input) {
                @SuppressWarnings("unchecked")
                Map<String, Object> instanceConfig = (Map<String, Object>) input;
                return firstNonNull(JsonUtils.getBooleanField(instanceConfig, SawInstancesCfg.R2F_ENABLED), false);
            }
        });
        return MessageUtils.instanceNames(instanceConfigs);
    }

    @Override
    protected Map<String, String> fetchCatalogItems(String targetInstance) {
        Map<String, Object> operationOutput = executeOperationForInstance(OPERATION_GET_CATALOG_ITEMS, targetInstance);
        return getField(operationOutput, MessageConstants.RESULT);
    }

    @Override
    protected List<String> fetchContacts(String targetInstance) {
        Map<String, Object> operationOutput = executeOperationForInstance(OPERATION_GET_CONTACTS, targetInstance);
        return getField(operationOutput, MessageConstants.RESULT);
    }

    @Override
    public String getCatalogItemForm(String targetInstance, String itemId, String authToken) throws Exception {
        Map<String, Object> offeringOperationOutput = executeOperationForInstance(AggregationConstants.OPERATION_GET_OFFERING, targetInstance,
                ImmutableMap.<String, Object>of("itemID", itemId));

        Map<String, Object> offering = getField(offeringOperationOutput, MessageConstants.RESULT);

        if (offering == null) {
            return "";
        }

        Map<String, Object> infrastructure = configuration.getConfiguration(SXConstants.INFRASTRUCTURE_CONFIG);

        if (Objects.equals(JsonUtils.getStrField(offering, "summary", "type"), "SUPPORT")) {
            List<Map<String, Object>> fields = JsonUtils.getField(offering, "form", "fields");
            fields.removeIf(field -> {
                final String id = JsonUtils.getStrField(field, "id");
                return Objects.equals(id, "name") || Objects.equals(id, "description");
            });
        }

        return messageTransformer.transformMessage(ImmutableMap.of(
                "offering", offering,
                "userAuthToken", authToken,
                "infrastructure", infrastructure
        ), "ui/optionsHtmlTransform.ftl");
    }
}

SawOperationExecutor.java

package com.hp.ccue.serviceExchange.adapter.saw;

import com.google.common.net.HttpHeaders;
import com.hp.ccue.serviceExchange.SXConstants.SawInstancesCfg;
import com.hp.ccue.serviceExchange.adapter.caseex.CxAwareOperationExecutor;
import com.hp.ccue.serviceExchange.adapter.saw.sawBeans.BulkOperationResult;
import com.hp.ccue.serviceExchange.adapter.saw.sawBeans.BulkOperationResult.CompletionStatuses;
import com.hp.ccue.serviceExchange.adapter.saw.sawBeans.BulkOperationResult.EntityResult;
import com.hp.ccue.serviceExchange.adapter.saw.sawBeans.BulkOperationResult.ErrorDetails;
import com.hp.ccue.serviceExchange.adapter.saw.util.SXSAWImplProperties;
import com.hp.ccue.serviceExchange.config.Configuration;
import com.hp.ccue.serviceExchange.http.HttpRequest;
import com.hp.ccue.serviceExchange.http.HttpResponse;
import com.hp.ccue.serviceExchange.http.HttpResponseWrapper;
import com.hp.ccue.serviceExchange.operation.OperationConstants;
import com.hp.ccue.serviceExchange.utils.JsonUtils;
import com.hp.ccue.serviceExchange.utils.ValidationUtils;
import org.apache.http.StatusLine;
import org.apache.http.message.BasicStatusLine;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.google.common.base.Objects.firstNonNull;
import static com.hp.ccue.serviceExchange.SXConstants.SUPPORTED_LANGUAGE_TAGS;

public class SawOperationExecutor extends CxAwareOperationExecutor {

    private static final String LOGIN_REQUEST_URL = "/auth/authentication-endpoint/authenticate/login";
    private static final String DOWNLOAD_ATTACHMENT_URL = "/frs/file-list/";
    private static final String DOWNLOAD_IMAGES_URL = "/frs/image-list/";
    private static final String IMAGES_URL = "/js/modules/saw/resources/images/";

    @Autowired
    private SawNotificationSetupExecutor notificationSetupExecutor;

    private Set<String> operationsWithoutLogging;

    @Autowired
    private Configuration configuration;

    public void setOperationsWithoutLogging(Set<String> operationsWithoutLogging) {
        this.operationsWithoutLogging = operationsWithoutLogging;
    }

    public SawOperationExecutor() {
        super(SawConstants.SAW_TYPE, SawInstancesCfg.CFG_NAME, true);
        setDefaultHttpRequestContentType(MediaType.APPLICATION_JSON);
    }

    private Collection<String> getInstanceEndpoints(){
        Collection<String> instanceEndpoints = new ArrayList<>();
        Map<String, Object> allInstanceConfigs = configuration.getConfiguration(SawInstancesCfg.CFG_NAME);
        for (Object value : allInstanceConfigs.values()) {
            @SuppressWarnings("unchecked")
            Map<String,Object> instance = (Map<String, Object>) value;
            instanceEndpoints.add((String)instance.get("endpoint"));
        }
        return instanceEndpoints;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        Set<String> skippedOperations = firstNonNull(operationsWithoutLogging, Collections.<String>emptySet());
        for (String operation : skippedOperations) {
            skipLoggingForOperation(operation);
        }
    }

    @Override
    protected void beforeExecuteOperation(String operationName, Map<String, Object> message, Map<String, Object> context, boolean doLogging) {
        context.put(KEY_BUNDLE, SXSAWImplProperties.getMessagesBundle());
        context.put(KEY_BUNDLES, SUPPORTED_LANGUAGE_TAGS.stream()
                .collect(Collectors.toMap(Function.identity(), tag -> SXSAWImplProperties.getMessagesBundle(Locale.forLanguageTag(tag)))));
    }

    @Override
    protected HttpResponse afterHttpResponseReceived(final HttpResponse httpResponse, final HttpRequest request) {
        // SAW HTTP responses are buggy!
        // we need to wrap them to determine proper Content-Type - the server does not send them
        // we must wrap all methods which have something to do with Content-Type handling
        HashMap<String, Object> context = new HashMap<>(); // unfortunately we don't have the context here; fixing this would required changing method signature
        SawHttpResponseWrapper result = new SawHttpResponseWrapper(httpResponse, request);
        getEffectiveStatusCode(httpResponse, context).ifPresent(result::setStatus);
        return result;
    }

    private Optional<Integer> getEffectiveStatusCode(HttpResponse httpResponse, Map<String, Object> context) {
        try {
            Optional<BulkOperationResult> bulkOperationResult = parseBulkOperationResult(httpResponse, context);
            if (bulkOperationResult.isPresent() && !Objects.equals(bulkOperationResult.get().getMeta().getCompletionStatus(), CompletionStatuses.FAILED)) {
                return Optional.empty();
            }
            return bulkOperationResult.flatMap(SawOperationExecutor::getErrorDetails).map(ErrorDetails::getHttpStatus);
        } catch (Exception e) {
            return Optional.empty();
        }
    }

    private class SawHttpResponseWrapper extends HttpResponseWrapper {

        private final HttpRequest request;
        private int statusCode;
        private StatusLine statusLine;

        public SawHttpResponseWrapper(HttpResponse httpResponse, HttpRequest request) {
            super(httpResponse);
            this.request = request;
            this.statusCode = httpResponse.getStatusCode();
            this.statusLine = httpResponse.getStatusLine();
        }

        @Override
        public String getHeader(String headerName) {
            if (headerName.equals(HttpHeaders.CONTENT_TYPE) && !isUrlKnownToHaveCorrectContentType(request.getUrl())) {
                if (request.getUrl().contains(LOGIN_REQUEST_URL)) {
                    // authentication response is plaintext
                    return MediaType.TEXT_PLAIN;
                } else {
                    // other responses are JSON
                    return MediaType.APPLICATION_JSON;
                }
            }
            // else:
            return super.getHeader(headerName);
        }

        @Override
        public boolean hasStringContent() {
            //noinspection SimplifiableIfStatement
            if (!isUrlKnownToHaveCorrectContentType(request.getUrl())) {
                return true;
            }
            return super.hasStringContent();
        }

        @Override
        public boolean isJson() {
            return getHeader(HttpHeaders.CONTENT_TYPE).equals(MediaType.APPLICATION_JSON);
        }

        private boolean isUrlKnownToHaveCorrectContentType(String url) {
            for (String endpoint : getInstanceEndpoints()) {
                if (url.startsWith(endpoint)) {
                    return url.contains(DOWNLOAD_ATTACHMENT_URL) || url.contains(IMAGES_URL) || url.contains(DOWNLOAD_IMAGES_URL);
                }
            }
            return true;
        }

        @Override
        public int getStatusCode() {
            return statusCode;
        }

        @Override
        public StatusLine getStatusLine() {
            return statusLine;
        }

        void setStatus(int statusCode) {
            this.statusCode = statusCode;
            Status status = Status.fromStatusCode(statusCode);
            String reasonPhrase = status != null ? status.getReasonPhrase() : String.format("HTTP %s", statusCode);
            this.statusLine = new BasicStatusLine(getDelegate().getStatusLine().getProtocolVersion(), statusCode, reasonPhrase);
        }
    }

    @Override
    protected void executeNotificationSetup(
            String entityId, Map<String,Object> entity, String checkOperation, Map<String, Object> checkOperationInputMessage, String catalogCallbackTemplate,
            EntityRegistrationMode mode, Map<String, Object> context, Map<String, Object> stepConfig) throws Exception {
        notificationSetupExecutor.executeNotificationSetup(
                entityId, entity, checkOperation, checkOperationInputMessage, catalogCallbackTemplate, mode, context, stepConfig);
    }

    @Override
    protected boolean isResponseSuccess(String operationName, Map<String, Object> stepConfig, HttpResponse httpResponse, Map<String, Object> message, Map<String, Object> context) {
        final int statusCode = httpResponse.getStatusCode();
        List<Integer> whiteListedStatuses = firstNonNull(JsonUtils.<List<Integer>>getField(stepConfig, OperationConstants.REPORT_SUCCESS_FOR_HTTP_STATUSES),
                Collections.<Integer>emptyList());
        //noinspection SimplifiableIfStatement
        if (whiteListedStatuses.contains(statusCode)) {
            return true;
        }
        return statusCode < 400;
    }

    private Optional<BulkOperationResult> parseBulkOperationResult(HttpResponse httpResponse, Map<String, Object> context) {
        if (!httpResponse.isJson()) {
            return Optional.empty();
        }
        Object jsonPayload;
        try {
            jsonPayload = parseJsonPayload(httpResponse, context);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (!(jsonPayload instanceof Map)) {
            return Optional.empty();
        }
        BulkOperationResult bulkOperationResult;
        try {
            //noinspection unchecked
            bulkOperationResult = JsonUtils.readBean((Map<String, Object>) jsonPayload, BulkOperationResult.class);
            ValidationUtils.validateBean(bulkOperationResult);
        } catch (Exception e) {
            return Optional.empty();
        }
        return Optional.of(bulkOperationResult);
    }

    @Override
    protected String getDetailErrorMessage(String operationName, Map<String, Object> stepConfig, HttpResponse httpResponse, Map<String, Object> message, Map<String, Object> context) {
        if (!httpResponse.isJson()) {
            return null;
        }
        try {
            ErrorDetails errorDetails = parseBulkOperationResult(httpResponse, context).flatMap(SawOperationExecutor::getErrorDetails).orElse(null);
            if (errorDetails == null) {
                return null;
            }
            final Integer httpStatus = errorDetails.getHttpStatus();
            final String detailMessage = errorDetails.getMessage();
            if (httpStatus == null || detailMessage == null) {
                return null;
            }
            return String.format("%s (effective HTTP status: %s)", detailMessage, httpStatus);
        } catch (RuntimeException e) {
            log.debug("Failed to extract the detail message.", e);
            return null;
        }
    }

    private static Optional<ErrorDetails> getErrorDetails(BulkOperationResult bulkOperationResult) {
        ErrorDetails errorDetails;
        final List<EntityResult> entityResultList = bulkOperationResult.getEntityResultList();
        if (entityResultList.size() != 1) { // let's cover only the case of single entity
            errorDetails = bulkOperationResult.getMeta().getErrorDetails();
        } else {
            errorDetails = entityResultList.get(0).getErrorDetails();
        }
        return Optional.ofNullable(errorDetails);
    }
}

SAWAdapterContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.hp.ccue.serviceExchange.adapter.saw"/>
    <context:component-scan base-package="com.hp.ccue.serviceExchange.adapter.saw.util"/>
    <context:component-scan base-package="com.hp.ccue.serviceExchange.adapter.saw.sawBeans"/>
    <context:component-scan base-package="com.hp.ccue.serviceExchange.adapter.saw.caseex"/>
    <context:component-scan base-package="com.hp.ccue.serviceExchange.adapter.saw.freemarker"/>
    <context:component-scan base-package="com.hp.ccue.serviceExchange.adapter.saw.healthcheck"/>

   <!--  <bean class="com.hp.ccue.serviceExchange.adapter.saw.SawOperationExecutor">
        <property name="operationsWithoutLogging">
            <set>
                <value>getChangedIncidents</value>
                <value>getChangedRequests</value>
                <value>getChangedOfferings</value>
                <value>getAggregationFeatures</value>
            </set>
        </property>
    </bean> -->

    <!--
        To avoid IllegalArgumentException when autowiring lastUpdatedEntityDao in SawChangeListener:
        http://stackoverflow.com/questions/18369258/spring-aop-at-service-layer
    -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>
like image 524
Harsha Aithal Avatar asked May 09 '16 09:05

Harsha Aithal


1 Answers

I think you're missing an @Component annotation on the SawOperationExecutor class

like image 88
bernie Avatar answered Oct 13 '22 01:10

bernie