2
votes

I am working on Adobe AEM 6.0 and still new to Apache Felix and Sling and I would like to know how to get instance of SlingHttpServletRequest from an OSGI service annotated with @Service.

Is it possible to get the request from ResourceResolverFactory or SlingRepository?

@Service
@Component(metatype = false)
public class TestServiceImpl implements  TestService{

    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    @Reference
    private SlingRepository repository;

}

I am aware that SlingHttpServletRequest is readily available for classes extending SlingAllMethodsServlet however as for my requirement I need to write a service rather than a servlet.

The rationale behind why I need SlingHttpServletRequest is because I need to get the client's IP address for audit logging purposes.

Is there any better way to do this? Or at least someone can help point me to correct direction how I can achieve such requirement.

1
As the name implies SlingHttpServletReques` is a request. The only way to get this is a SlingServlet or JSP (which is actually a servlet as well). What I don't understand is, why you need a client IP in a Service? How does the service method get triggered, maybe you can pass the request as a method parameter.Thomas
A servlet in Sling is an OSGi service, actually.Markus
Thanks Thomas, I need to extract the IP address from the request object for auditing logging purpose. Passing the object as argument to call the service is my last choice. I was expecting there should be some way get it from the bundle context or get from injected object reference.Homer

1 Answers

1
votes

I think the Filter is what you need. Create a service that implements Filter. The doFilter method is to be called on every Sling request (if sling.filter.scope=REQUEST of course).
See also Sling Filter

package com.examples.test.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
    metatype = true,
    label = "Test Filter",
    immediate = true,
    enabled = true
)
@Service
@Properties({
    @Property(name = "sling.filter.scope", value = "REQUEST", propertyPrivate = true),
    @Property(name = "service.ranking", intValue = 100, propertyPrivate = true)
})
public class TestFilter implements Filter {

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

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        //If you'll need some SlingHttpServletRequest functionality
        //SlingHttpServletRequest httpRequest = (SlingHttpServletRequest) request;

        log.info(request.getRemoteAddr());

        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

}