Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

SQL2 Error on publish server

Avatar

Level 5

I'm trying to run the following on both our author and publish servers.

~package ewcm.global.core;


import java.io.IOException;

import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(
        paths = {"/services/documentListNewWindow"},
        metatype = false
)
public class ContentUpdateDocumentListNewWindow extends SlingSafeMethodsServlet {
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private static final Logger LOG = LoggerFactory.getLogger(ContentUpdateDocumentListNewWindow.class);
 
    @Override
    protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws
            ServletException, IOException {
  Resource resource = request.getResource();
  Session session = resource.getResourceResolver().adaptTo(Session.class);
  boolean exceptionsFound = false;
  try {
         // Obtain the query manager for the session ...
         javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager();
                 
         //Setup the query based on user input     
         String sqlStatement="";
                
         //Setup the query to get all customer records
         sqlStatement = "SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/content') AND [sling:resourceType] like '%documentList'";
                    
         javax.jcr.query.Query query = queryManager.createQuery(sqlStatement,"JCR-SQL2");
         
         //Execute the query and get the results ...
         javax.jcr.query.QueryResult results = query.execute();

         //Iterate over the nodes in the results ...
         javax.jcr.NodeIterator nodeIter = results.getNodes();
      
         modifyProperties(nodeIter, response);

   session.save();
   
  } catch (Exception e) {
   LOG.error("Error", e);
   exceptionsFound = true;
  }

  if (exceptionsFound) {
            response.getWriter().print("Exceptions Found ..."  + "\n");
   response.getWriter().print("No content was updated."  + "\n");
  }
  else {
   response.getWriter().print("\n");
   response.getWriter().print("Content Updated for all components."  + "\n");
  }
    }

 private void modifyProperties(NodeIterator nodeIter, SlingHttpServletResponse response) throws IOException {
  response.getWriter().println("Number of instances found: " + nodeIter.getSize() + "\n");
  response.getWriter().println("Pages Updated ... " + "\n");

  // Iterate over the Hits
        while ( nodeIter.hasNext() ) {
   try {
    javax.jcr.Node node = nodeIter.nextNode();
    node.setProperty("newWindow", "true");
             response.getWriter().print("PATH: " + node.getPath() + "\n");
   } catch (Exception e) {
             LOG.error("Error",e);
   }
  }
 }

}

Everything runs find on our Author instance.       

However, on the publish server I get the following error:

11.07.2016 14:01:04.747 *ERROR* [10.1.209.140 [1468267261689] GET /services/documentListNewWindow HTTP/1.1] ewcm.global.core.ContentUpdateDocumentListNewWindow Error
javax.jcr.version.VersionException: Cannot set property. Node is checked in.
        at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.checkPreconditions(NodeImpl.java:1330)
        at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.prePerform(SessionDelegate.java:606)
        at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:197)
        at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112)
        at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1325)
        at org.apache.jackrabbit.oak.jcr.session.NodeImpl.setProperty(NodeImpl.java:428)
        at ewcm.global.core.ContentUpdateDocumentListNewWindow.modifyProperties(ContentUpdateDocumentListNewWindow.java:80)
        at ewcm.global.core.ContentUpdateDocumentListNewWindow.doGet(ContentUpdateDocumentListNewWindow.java:53)
        at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
        at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
        at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
        at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:533)
        at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
        at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:96)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
        at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:133)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
        at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:357)
        at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:166)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
        at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:282)
        at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
        at com.adobe.granite.resourceresolverhelper.impl.ResourceResolverHelperImpl.doFilter(ResourceResolverHelperImpl.java:81)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
        at com.adobe.cq.dam.s7imaging.impl.auth.MemoryTokenAuthHandler.doFilter(MemoryTokenAuthHandler.java:156)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
        at org.apache.sling.security.impl.ContentDispositionFilter.doFilter(ContentDispositionFilter.java:152)
        at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
        at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:128)
 

Any help is greatly appreciated.

Thanks,

-Dean

1 Accepted Solution

Avatar

Correct answer by
Level 10

This looks like a permission issue for me.

On Author, you will would have logged in as Admin and you will get the admin session from the resolver where as in publish instance you wouldnt have logged in and hence you are not able to write to JCR. 

Instead of using from the request, create a system user with the defined sub service. Use resolverFactory.getServiceResourceResolver(param); and then adaptTo session. This should work both in Author and Publish instance

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

This looks like a permission issue for me.

On Author, you will would have logged in as Admin and you will get the admin session from the resolver where as in publish instance you wouldnt have logged in and hence you are not able to write to JCR. 

Instead of using from the request, create a system user with the defined sub service. Use resolverFactory.getServiceResourceResolver(param); and then adaptTo session. This should work both in Author and Publish instance

Avatar

Employee Advisor

It seems that there is a node in your publish AEM instance which is in checked in state. Log the path of the node before you update any properties in the code. And then you can checkout the node manually using crx/explorer and try again. See the attached screenshot. 

Avatar

Level 5

Thank you so much BSLOKI for your help.

The support on this forum never ceases to amaze me.  Once again, you have and others have gone above and beyond my expectations.

Thanks again!!!!

-Dean Anderson

Avatar

Level 2

Just a minor point.  You're changing the data via a GET request.  A GET is supposed to be idempotent and safe.