Expand my Community achievements bar.

SOLVED

Trying to save nodes in crxde using java servlets

Avatar

Level 1

I am basicallly making a component in which a button will invoke a servlet and in the servlet i am accessing the repository and making nodes in that repository  .

But it is throwing exception.( javax.jcr.RepositoryException: Unable to access a repository with the following settings:
org.apache.jackrabbit.repository.uri: http://localhost:4509/crx/server
The following RepositoryFactory classes were consulted:
Perhaps the repository you are trying to access is not available at the moment.)  Below is the code.

 

 

package ph.com.globe.globeSap.core.servlets;


import java.io.IOException;
import java.rmi.ServerException;

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import javax.jcr.Repository;

import javax.jcr.Session;

import javax.jcr.SimpleCredentials;
import javax.jcr.Node;

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ph.com.globe.globeSap.core.models.Footer.FooterModel;

import java.util.UUID;



@SlingServlet(paths="/bin/mySearchServlet", methods = "POST", metatype=false)
public class HandleClaim extends org.apache.sling.api.servlets.SlingAllMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;


protected final Logger log = LoggerFactory.getLogger(getClass());


@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {


try
{
log.info("In the basic servlet");
// Create a connection to the C Q repository running on local host .
Repository repository = JcrUtils.getRepository("http://localhost:4509/crx/server");

System.out.println(repository);

//Create a Session
Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));

//Create a node that represents the root node
Node root = session.getRootNode();

// Store content
Node adobe = root.addNode("adobe");
Node day = adobe.addNode("cq");
day.setProperty("message", "Adobe Experience Manager is part of the Adobe Digital Marketing Suite!");


// Retrieve content
Node node = root.getNode("adobe/cq");
System.out.println(node.getPath());
System.out.println(node.getProperty("message").getString());

// Save the session changes and log out
session.save();
session.logout();

response.getWriter().write("data updated");

}
catch(Exception e)
{
e.printStackTrace();
}
}

I don't know why it is not able to access the repository.
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@saurkapo ,

When in servlets, the preferred way to write to the JCR is via using service users:

  1. Config: Create a system-user. instructions here.
  2. Config: Register the system-user in Apache Sling Service User Mapper Service. instructions here.
  3. Servlet: Use the system-user to obtain the session or resourceResolver.
  4. Servlet: Use the session to write to the JCR.
  5. Servlet: Don't forget to close the session or resourceResolver.

Example on how to get the session or resourceResolver:
origin: https://docs.adobe.com/content/help/en/experience-manager-learn/forms/adaptive-forms/service-user-tu...

 

 

 

public class GetResolverImpl implements GetResolver {
 @Reference
 ResourceResolverFactory resolverFactory;
 @Override
 public ResourceResolver getServiceResolver() {
  HashMap<String, Object> param = new HashMap<String, Object>();
  param.put(ResourceResolverFactory.SUBSERVICE, "getresourceresolver");
  ResourceResolver resolver = null;
  try {
   resolver = resolverFactory.getServiceResourceResolver(param);
  } catch (LoginException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return resolver;
 }

 

 

 

I hope this helps.

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

@saurkapo ,

When in servlets, the preferred way to write to the JCR is via using service users:

  1. Config: Create a system-user. instructions here.
  2. Config: Register the system-user in Apache Sling Service User Mapper Service. instructions here.
  3. Servlet: Use the system-user to obtain the session or resourceResolver.
  4. Servlet: Use the session to write to the JCR.
  5. Servlet: Don't forget to close the session or resourceResolver.

Example on how to get the session or resourceResolver:
origin: https://docs.adobe.com/content/help/en/experience-manager-learn/forms/adaptive-forms/service-user-tu...

 

 

 

public class GetResolverImpl implements GetResolver {
 @Reference
 ResourceResolverFactory resolverFactory;
 @Override
 public ResourceResolver getServiceResolver() {
  HashMap<String, Object> param = new HashMap<String, Object>();
  param.put(ResourceResolverFactory.SUBSERVICE, "getresourceresolver");
  ResourceResolver resolver = null;
  try {
   resolver = resolverFactory.getServiceResourceResolver(param);
  } catch (LoginException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return resolver;
 }

 

 

 

I hope this helps.