Expand my Community achievements bar.

SOLVED

AEM Hybris Connector issue

Avatar

Level 4

Hi Team,

We are using OOB aem hybris connector package to connect hybris server from AEM 6.2.

Here we haven't customized the auth handler we are usign the OOB authhandler.

Issue here is whenever we are trying to get the device details from Hybris we are getting below error-

com.adobe.cq.commerce.hybris.common.DefaultHybrisConnection execute: hybris response status: [400], body: [{
  "class": "AmbiguousIdentifierException",
  "message": "Product code 'Device020' is not unique, 7 products found!"
}]
 

Here we have set the parameter in command as -

public boolean needsServerAuth() {
        return false;
    }

 

Please let me know if i am missing anything here or do we need to customize the authhandler.

Regards

Ankur

1 Accepted Solution

Avatar

Correct answer by
Employee

Looking at the code, Are you getting this message in your log?

Sending {} to {} returned with status {} in {} ms", if not then probably it should be logging the error below this as well. The status code is not correct, the status == 200 || status == 201 || status == 202) then it would return commandResult. 

View solution in original post

5 Replies

Avatar

Administrator

Hi, 

I have asked internal team to have a look at this.

~kautuk



Kautuk Sahni

Avatar

Correct answer by
Employee

Looking at the code, Are you getting this message in your log?

Sending {} to {} returned with status {} in {} ms", if not then probably it should be logging the error below this as well. The status code is not correct, the status == 200 || status == 201 || status == 202) then it would return commandResult. 

Avatar

Level 4

Thanks Kanika for the reply.

Here as i wrote above i am getting status as 400 with below error message-

com.adobe.cq.commerce.hybris.common.DefaultHybrisConnection execute: hybris response status: [400], body: [{
  "class": "AmbiguousIdentifierException",
  "message": "Product code 'Device020' is not unique, 7 products found!"
}]

While debugging was able to see it's going to default hybris connection where in its going to 400 condition and and not to 200.

Also this issue is coming sometimes and not always here i would like to mention we are not maintaining the session as this is for anonymous users only.

We are extending the hybris connector as mentioned  in the below post with 6.2  aem hybris connector(AEM version 6.2 and Hybris version 5.4)-

https://github.com/Adobe-Marketing-Cloud/cq-java-hybris-extensions

Please let me know if i am doing something wrong here.

I haven't written our own auth handler we are using OOB auth handler.

Regards

Ankur

Avatar

Employee

Ankur, can you share the code?

Avatar

Level 4

Kanika Gera wrote...

Ankur, can you share the code?

 


Hi Kanika,

Below is my code-

For import we need authenticated request so as to get all the products from hybris for which we cant use below OOB code oAuthHandler.java -

 

 public void executeAuthenticated(CloseableHttpClient client, HybrisRequest hybrisRequest, String username, String password) throws IOException, CommerceException
    {
        try {
            HttpUriRequest request = hybrisRequest.getRequest();
            URI uri = request.getURI();
            if (uri.getPath().startsWith("/ws410/rest")) {
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));

                AuthCache authCache = new BasicAuthCache();
                BasicScheme basicAuth = new BasicScheme();
                authCache.put(targetHost, basicAuth);
                HttpClientContext context = HttpClientContext.create();
                context.setAuthCache(authCache);
                context.setCredentialsProvider(credsProvider);
                CloseableHttpResponse response = client.execute(request, context);
                HybrisConnection connection = (HybrisConnection) getService(HybrisConnection.class.getName());
                String responseEncoding = connection.getResponseEncoding();
                ungetService(HybrisConnection.class.getName());
                hybrisRequest.setResult(new CommandResult(response, responseEncoding));
            } else {
                executeAuthenticated(client, hybrisRequest, username);
            }
        } catch (IllegalStateException x) {
            if (x.getCause() instanceof ConnectException) {
                throw new CommerceException("", x.getCause());
            }
            throw x;
        }
    }

As it is checking for-

uri.getPath().startsWith("/ws410/rest")

Due to this issue we have write our own Authentication hanler which we wrote like below-

 

CustomAuthHandler.java-

 @Override
    public void executeAuthenticated(CloseableHttpClient client, HybrisRequest hybrisRequest, String username, String password) throws IOException, CommerceException {

        HttpUriRequest request = hybrisRequest.getRequest();
        URI uri = request.getURI();
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        HttpClientContext context = HttpClientContext.create();
        context.setAuthCache(authCache);
        context.setCredentialsProvider(credsProvider);
        CloseableHttpResponse response = client.execute(request, context);
        final HybrisConnection connection = (HybrisConnection) getService(HybrisConnection.class.getName());
        final String responseEncoding = connection.getResponseEncoding();
         ungetService(HybrisConnection.class.getName());
        hybrisRequest.setResult(new CommandResult(response, responseEncoding));
    }

    protected Object getService(String className) {
        if (services.containsKey(className)) {
            log.debug("getService: Getting service from cache: " + className);
            return context.getBundleContext().getService(services.get(className));
        }
        final ServiceReference serviceReference = context.getBundleContext().getServiceReference(className);
        if (serviceReference != null) {
            services.put(className, serviceReference);
            final Object service = context.getBundleContext().getService(serviceReference);
            log.debug("getService: Service not in cache: " + className + ", got instance: " + service);
            return service;
        }
        return null;
    }

    protected void ungetService(String className) {
        log.debug("ungetService: " + className + ", in cache: " + services.containsKey(className));
        if (services.containsKey(className)) {
            final ServiceReference serviceReference = services.get(className);
            if (serviceReference != null) {
                context.getBundleContext().ungetService(serviceReference);
            }
            services.remove(className);
        }
    }

 

    @Override
    public Map<String, String> authenticateUser(String paramString1, String paramString2) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void executeAuthenticated(CloseableHttpClient paramCloseableHttpClient, HybrisRequest paramHybrisRequest, String paramString) throws IOException, CommerceException {
        // TODO Auto-generated method stub

    }

 

So in this case import works fine as always our condition goes to in DefaultHybrisConnection.java-

if (command.needsServerAuth()) {
                log.debug("Using server username/password to authenticate request.");
                HybrisRequest hybrisRequest = new HybrisRequest(command, request);
                this.authHandler.executeAuthenticated(httpClient, hybrisRequest, this.serverUsername, this.serverPassword);
                result = hybrisRequest.getResult();
            }

Aand our customauth handler executeAuthenticated method is called and import works fine.

 

2.But for our product details page we need to get unique products there we are facing issue and we are getting below error-

com.adobe.cq.commerce.hybris.common.DefaultHybrisConnection execute: hybris response status: [400], body: [{
  "class": "AmbiguousIdentifierException",
  "message": "Product code 'Device020' is not unique, 7 products found!"
}]
 So i am not sure how to make both import and other functionlaities work.

 

We are extending the cq-commerce-hybris-impl-6.1.2.jar and created our own factory and import classes.

Please let me know if i am doing something wrong.

Regards

Ankur