SlingHttpServletRequest object is null when we pass it to OSGI service from WCMUse class | Community
Skip to main content
New Participant
February 3, 2016
Solved

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

  • February 3, 2016
  • 4 replies
  • 3785 views

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";
    }
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kunal_Gaba_

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();

4 replies

Ratna_Kumar
New Participant
February 3, 2016

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.

smacdonald2008
New Participant
February 3, 2016

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? 

Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
February 3, 2016

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();
Jitendra_S_Toma
New Participant
February 3, 2016

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