Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Pragmatically accessing jcr repository in sling servlet

ravi_shankerj14
Level 2
Level 2

Hi,

I was trying to access jcr repository of QA instance from the dev instance using a sling servlet. But i am facing an exception as mentioned below:

 

Please help me out with this. And also let me know if this scenario is possible or not.

 

Sling Servlet code:

package com.demo.core.servlets;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.demo.core.utils.Entity;
import com.demo.core.utils.EntityUtil;
import com.demo.core.utils.impl.EntityUtilImpl;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.*;
import javax.servlet.Servlet;
import java.io.IOException;
import java.io.PrintWriter;

@Component(service = Servlet.class, property = {"sling.servlet.methods=" + "GET",
"sling.servlet.paths=" + "/bin/validate"})
public class DataValidation extends SlingAllMethodsServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

@Reference
SlingRepository slingRepository;


@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws IOException {
//get request param
String parentPath = request.getParameter("parentPath");
String uri = request.getParameter("uri");
String origin = request.getParameter("origin");
ResourceResolver resourceResolver = null;
Session jcrSession = null;
if (uri.isEmpty() && !parentPath.isEmpty()) {
//resource resolver created from request
resourceResolver = request.getResourceResolver();
jcrSession = resourceResolver.adaptTo(Session.class);
} else if(!uri.isEmpty() && !parentPath.isEmpty()) {
try {
Repository repository = JcrUtils.getRepository(uri+"/crx/server");
jcrSession = repository.login(new SimpleCredentials("admin","admin".toCharArray()));
Node rootNode =jcrSession.getRootNode();
} catch (RepositoryException e) {
e.printStackTrace();
}
}else{
//return response that uri and path is not available
}

}


}

}

 

Exception Occurs:

javax.jcr.RepositoryException: Unable to access a repository with the following settings:
org.apache.jackrabbit.repository.uri: http://{server-ip:port}/crx/server
The following RepositoryFactory classes were consulted:
Perhaps the repository you are trying to access is not available at the moment.

1 Accepted Solution
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor
1 Reply
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor