
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