Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How resourceResolver getting current user session ?

Avatar

Level 4

How resourceResolver work when we say resourceResolver.getResource("/etc/geometrixx") based on current user permissions ?

1 Accepted Solution

Avatar

Correct answer by
Level 10

In AEM 6 - a better way is to create an dedicated account and use that. For example: 

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
           
resolver = resolverFactory.getServiceResourceResolver(param);
session = resolver.adaptTo(Session.class);

 

See https://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html

View solution in original post

16 Replies

Avatar

Level 10

its depends on how you have created the session before using resourceResolver.

Check this example [1]

 

[1] http://www.wemblog.com/2014/08/how-to-use-sessions-and-resource.html

Avatar

Correct answer by
Level 10

In AEM 6 - a better way is to create an dedicated account and use that. For example: 

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
           
resolver = resolverFactory.getServiceResourceResolver(param);
session = resolver.adaptTo(Session.class);

 

See https://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html

Avatar

Level 1

Hi Smac,

 

I have followed the link https://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html and tried to create the project for AEM 6.2

While creating the snapshot, m getting the following error :

SUBSERVICE cannot be resolved or is not a field    CustomerServiceImpl.java    /slingjcr-bundle/src/main/java/com/community/aem    line 74    Java Problem

The method getServiceResourceResolver(Map<String,Object>) is undefined for the type ResourceResolverFactory    CustomerServiceImpl.java    /slingjcr-bundle/src/main/java/com/community/aem    line 80    Java Problem

-----------------------------------------------------------------------------------------------------------------------------

param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;
  
try {
             
    //Invoke the adaptTo method to create a Session used to create a QueryManager
    resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);
      

Could you please suggeston this.

 

Thanks-

Hemant

Avatar

Level 10

Hi,

BTW, What do you mean by creating snapshot means -  when you create a bundle, you see the error in error.log?

I have tested this article many times, it works for me. However, I will again retest it and once again check the same.

~ Ratna.

Avatar

Level 1

Hi Ratna,

Thanks for your reply...

I followed exactly the same steps as shown in the video.. But while creating the osgi bundle, during mvn clean install, I m getting the below error :

_______________________________________________________________________________________________

C:\AEM4502\slingjcr>mvn clean install

[INFO] Scanning for projects...
[ERROR] [ERROR] Project 'com.community.aem:slingjcr-bundle:1.0-SNAPSHOT' is duplicated in the reactor @
[ERROR] Project 'com.community.aem:slingjcr-bundle:1.0-SNAPSHOT' is duplicated in the reactor -> [Help 1]
[ERROR][ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR][ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DuplicateProjectException

_________________________________________________________________________________

Screenshot of eclipse attached.

Avatar

Level 10

Hi,

From your screenshot you written incorrect class for implementing the interface.
It must be "CustomerServiceImp" and not "s" 

//It must be CustomerServiceImp Implements CutomerService

~ Ratna.

Avatar

Level 1

That was a typo while taking the screenshot.. but It was compiled with correct name..

Anyways, keeping the correct class name has even not solved the issue..

Avatar

Community Advisor

Hi,

is duplicated in the reactor 

looks like you have error on POM file.

please check your project artifact ID.

do you have same name of artifact ID and group ID ?

 

~ Prince

Avatar

Level 1

Hi Prince,

No, I have checked it.. Its different..

below is the error log :

______________________________________________________________________

 

[ERROR] [ERROR] Project 'com.community.aem:slingjcr-bundle:1.0-SNAPSHOT' is duplicated in the reactor @
[ERROR] Project 'com.community.aem:slingjcr-bundle:1.0-SNAPSHOT' is duplicated in the reactor -> [Help 1]
org.apache.maven.project.DuplicateProjectException: Project 'com.community.aem:slingjcr-bundle:1.0-SNAPSHOT' is duplicated in the reactor

        at org.apache.maven.project.ProjectSorter.<init>(ProjectSorter.java:93)
        at org.apache.maven.graph.DefaultProjectDependencyGraph.<init>(DefaultProjectDependencyGraph.java:56)
        at org.apache.maven.graph.DefaultGraphBuilder.dependencyGraph(DefaultGraphBuilder.java:109)
        at org.apache.maven.graph.DefaultGraphBuilder.build(DefaultGraphBuilder.java:92)
        at org.apache.maven.DefaultMaven.buildGraph(DefaultMaven.java:491)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:219)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DuplicateProjectException

____________________________________________________________________________________________________________________________

Avatar

Community Advisor

Hi,

if you will check the link: http://cwiki.apache.org/confluence/display/MAVEN/DuplicateProjectException

it is also saying the same thing: The coordinates for a project as given by its group id, artifact id and version must be unique within a reactor build.

Can you please post some more details or screenshot?

 

~ Prince

Avatar

Community Advisor

Hi,

If you will check the link:  http://cwiki.apache.org/confluence/display/MAVEN/DuplicateProjectException

It is also saying the same thing: The coordinates for a project as given by its group id, artifact id and version must be unique within a reactor build.

 

Can you please post some more details or screenshot related to POM file?

 

~ Prince

Avatar

Level 1

pom.xml..

____________________________________________________________________

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
    <modelVersion>4.0.0</modelVersion>
    <!-- ====================================================================== -->
    <!-- P A R E N T P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->
    <parent>
        <groupId>com.community.aem</groupId>
        <artifactId>slingjcr</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <!-- ====================================================================== -->
    <!-- P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->
 
    <artifactId>slingjcr-bundle</artifactId>
    <packaging>bundle</packaging>
    <name>My Project Bundle</name>
 
<dependencies>
         
          
        
      <dependency>
        <groupId>com.adobe.aem</groupId>
        <artifactId>aem-api</artifactId>
        <version>6.0.0.1</version>
        <scope>provided</scope>
    </dependency>
         
         
   <dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.api</artifactId>
    <version>2.8.0</version>
</dependency>
  
         
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
           
            
        <dependency>
         <groupId>org.apache.felix</groupId>
     
         <artifactId>org.osgi.core</artifactId>
     
         <version>1.4.0</version>
      </dependency>
           
          
            
    <dependency>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>jackrabbit-core</artifactId>
    <version>2.4.3</version>
    </dependency>
         
    <dependency>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>jackrabbit-jcr-commons</artifactId>
    <version>2.4.3</version>
    </dependency>
   <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
             
     
           
      <dependency>
         <groupId>javax.jcr</groupId>
         <artifactId>jcr</artifactId>
         <version>2.0</version>
      </dependency>
         
       <dependency>
            <groupId>com.day.cq.wcm</groupId>
            <artifactId>cq-wcm-api</artifactId>
            <version>5.5.0</version>
            <scope>provided</scope>
        </dependency>
           
        <dependency>
            <groupId>com.day.cq</groupId>
            <artifactId>cq-commons</artifactId>
            <version>5.5.0</version>
            <scope>provided</scope>
        </dependency>
                 
    </dependencies> 
    <!-- ====================================================================== -->
    <!-- B U I L D D E F I N I T I O N -->
    <!-- ====================================================================== -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-scr-descriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>com.community.aem.slingjcr-bundle</Bundle-SymbolicName>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.sling</groupId>
                <artifactId>maven-sling-plugin</artifactId>
                <configuration>
                    <slingUrl>http://${crx.host}:${crx.port}/apps/myproject/install</slingUrl>
                    <usePut>true</usePut>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                 <configuration>
                    <excludePackageNames>
                        *.impl
                    </excludePackageNames>
                 </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Avatar

Level 1

pom.xml..

____________________________________________________________________

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
    <modelVersion>4.0.0</modelVersion>
    <!-- ====================================================================== -->
    <!-- P A R E N T P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->
    <parent>
        <groupId>com.community.aem</groupId>
        <artifactId>slingjcr</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <!-- ====================================================================== -->
    <!-- P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->
 
    <artifactId>slingjcr-bundle</artifactId>
    <packaging>bundle</packaging>
    <name>My Project Bundle</name>
 
<dependencies>
         
          
        
      <dependency>
        <groupId>com.adobe.aem</groupId>
        <artifactId>aem-api</artifactId>
        <version>6.0.0.1</version>
        <scope>provided</scope>
    </dependency>
         
         
   <dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.api</artifactId>
    <version>2.8.0</version>
</dependency>
  
         
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
           
            
        <dependency>
         <groupId>org.apache.felix</groupId>
     
         <artifactId>org.osgi.core</artifactId>
     
         <version>1.4.0</version>
      </dependency>
           
          
            
    <dependency>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>jackrabbit-core</artifactId>
    <version>2.4.3</version>
    </dependency>
         
    <dependency>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>jackrabbit-jcr-commons</artifactId>
    <version>2.4.3</version>
    </dependency>
   <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
             
     
           
      <dependency>
         <groupId>javax.jcr</groupId>
         <artifactId>jcr</artifactId>
         <version>2.0</version>
      </dependency>
         
       <dependency>
            <groupId>com.day.cq.wcm</groupId>
            <artifactId>cq-wcm-api</artifactId>
            <version>5.5.0</version>
            <scope>provided</scope>
        </dependency>
           
        <dependency>
            <groupId>com.day.cq</groupId>
            <artifactId>cq-commons</artifactId>
            <version>5.5.0</version>
            <scope>provided</scope>
        </dependency>
                 
    </dependencies> 
    <!-- ====================================================================== -->
    <!-- B U I L D D E F I N I T I O N -->
    <!-- ====================================================================== -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-scr-descriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>com.community.aem.slingjcr-bundle</Bundle-SymbolicName>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.sling</groupId>
                <artifactId>maven-sling-plugin</artifactId>
                <configuration>
                    <slingUrl>http://${crx.host}:${crx.port}/apps/myproject/install</slingUrl>
                    <usePut>true</usePut>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                 <configuration>
                    <excludePackageNames>
                        *.impl
                    </excludePackageNames>
                 </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Avatar

Community Advisor

Hi,

Please make sure project children POM file (Ex. Core , Apps)  artifact ID should not same.

Ex:

1) student\core\pom.xml

your artifact ID should be:

 <artifactId>student.core</artifactId>
    <packaging>bundle</packaging>
    <name>student - Core</name>
    <description>Core bundle for student</description>

2) student\ui.apps\pom.xml

your artifact ID should be:

<artifactId>student.ui.apps</artifactId>
    <packaging>content-package</packaging>
    <name>student - UI apps</name>
    <description>UI apps package for student</description>

I just did the same thing .. I kept the name of artifcat ID same for core and apps pom file. and got the same error.

please try this. 

 

~ Prince

Avatar

Community Advisor

This is when I am using maven archetype 10 project.