Expand my Community achievements bar.

SOLVED

SlingHttpServletRequest object is null when we pass it to OSGI service from WCMUse class

Avatar

Level 3

Hi All,

SlingHttpServletRequest  object is null when i am passing it from WCMuse class to an OSGI service . why it will behave like that. Also when we do an getRequest in WCMUse class it returns an org.apache.sling.scripting.core.impl.helper.OnDemandReaderRequest object instead of org.apache.sling.api.SlingHttpServletRequest object. Sample code is as below

public class MyWrapperClass extends WCMUse {
private SlingHttpServletRequest request;
@Override
    public void activate() {
    
    request = getRequest(); //here the request object is returning org.apache.sling.scripting.core.impl.helper.OnDemandReaderRequest how we can get org.apache.sling.api.SlingHttpServletRequest
    String str = getSlingScriptHelper().getService(MyService.class).myMethod(request);
    }
}

 

@Component (immediate=true, enabled=true)
@Service (value = MyInterface.class)
public class MyService implements MyInterface {
    @Override
    public String myMethodyMethod(SlingHttpServletRequest request){
        request.setAttribute("example","example");// here the request object is coming as null
        return "true";
    }
}

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Why do you need to pass the request object to OSGI service? It is better not to pass the request object to the backend components and rather pass the information in the request to the backend component using POJO objects or standard data types. You should not couple your services code with the front end code by using the request object. 

You can get the SlingHttpServletRequest object by calling the following method-

request = getRequest().getSlingRequest();

View solution in original post

4 Replies

Avatar

Level 9

You are re-check on your project and make sure you are using below dependencies for Sightly.

<dependency>

          <groupId>com.adobe.granite</groupId>

            <artifactId>com.adobe.granite.sightly.engine</artifactId>

            <version>1.1.52</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>com.adobe.cq.sightly</groupId>

            <artifactId>cq-wcm-sightly-extension</artifactId>

            <version>1.1.18</version>

            <scope>provided</scope>

        </dependency>

And, it seems to know that you are referencing to sling.scripting.core jar.

Jitendra

Avatar

Correct answer by
Employee Advisor

Why do you need to pass the request object to OSGI service? It is better not to pass the request object to the backend components and rather pass the information in the request to the backend component using POJO objects or standard data types. You should not couple your services code with the front end code by using the request object. 

You can get the SlingHttpServletRequest object by calling the following method-

request = getRequest().getSlingRequest();

Avatar

Level 10

Agree with kunal23 - get the request object in the Sling Servlet using method in the servlet - do not pass it to a servlet. Also - most times - people use servlets to perform tasks in Java. For example - use the AEM Java AssetManager API. What you are trying to do in the servlet that you cannot perform in the Sightly Java class? 

Avatar

Level 10

Yes, I also agree with kunal's comment. Its better to pass the information using POJO classes from backend components. Do not pass the request objects to OSGI Service.

We have many community articles that contains use cases in this link: https://helpx.adobe.com/marketing-cloud/experience-manager.html

Thanks,
Ratna Kumar.