Is it possible to put variables in a resource path in a sling servlet?

We are trying to provide a clean URI structure for external endpoints in order to pull json information from CQ5.

For example, if you want to receive information about a specific user history (provided that you have permissions, etc.), ideally we would like the endpoint to be able to do the following:

/bin/api/user/abc123/phone/555-klondike-5/history.json

In the URI, we will specify / bin / api / user / {username} / phone / {phoneNumber} /history.json, so it’s very easy to use a dispatcher to discard caching changes, etc. without invalidating a wide range of cached information.

We would like to use the sling servlet to handle the request, however I do not know how to put the variables in the path.

It would be nice if something like @PathParam from JaxRS to add a sling path to the variable, but I suspect it is not available.

Another approach we had in mind was to use a selector for recognition when we access the api, and thus we can return whatever we want out of the way, but that would require a single surf servlet to process all requests, and therefore I am not happy with this approach, since it glues a lot of unrelated code together.

Any help with this would be appreciated.


UPDATE:

If we used an OptingServlet, then we add some logic inside the accepts function, we can collect a number of sling servlets and make decisions about accepting from a regular expression path.

Then, at run time, the path itself can be parsed for variables.

+4
5

jersy (JAX-RS) CQ. , "" .

https://github.com/hstaudacher/osgi-jax-rs-connector

@PathParam

, -

+2

. /bin/api/user.json :

/bin/api/user.json/abc123/phone/555-klondike-5/history
^                 ^
|                 |
servlet path      suffix starts here

:

@SlingServlet(paths = "/bin/api/user", extensions = "json")
public class UserServlet extends SlingSafeMethodsServlet {
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        String suffix = request.getRequestPathInfo().getSuffix();
        String[] split = StringUtils.split(suffix, '/');
        // parse split path and check if the path is valid
        // if path is not valid, send 404:
        // response.sendError(HttpURLConnection.HTTP_NOT_FOUND);
    }
}
+1

RESTful , , . /content/user/abc123/phone/555-klondike-5/history/ .

. json,

/content/user/abc123/phone/555-klondike-5/history.json

, - json , sling, json.

0

, ! ~ , , , .

:

osgi-jax-rs, kallada, , Sling 8. , , , , , .

: ResourceProvider

Bertrand Sling 9, . , Sling 8 !

:

  • ResourceProvider
  • Servlet

ResourceProvider

/service, "" , JCR.

@Component
@Service(value=ResourceProvider.class)
@Properties({
        @Property(name = ResourceProvider.ROOTS, value = "service/image"),
        @Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
})
public class ImageResourceProvider implements ResourceProvider  {

@Override
public Resource getResource(ResourceResolver resourceResolver, String path) {

    AbstractResource abstractResource;
    abstractResource = new AbstractResource() {
        @Override
        public String getResourceType() {
            return TypeServlet.RESOURCE_TYPE;
        }

        @Override
        public String getResourceSuperType() {
            return null;
        }

        @Override
        public String getPath() {
            return path;
        }

        @Override
        public ResourceResolver getResourceResolver() {
            return resourceResolver;
        }

        @Override
        public ResourceMetadata getResourceMetadata() {
            return new ResourceMetadata();
        }
    };

    return abstractResource;
}

@Override
public Resource getResource(ResourceResolver resourceResolver, HttpServletRequest httpServletRequest, String path) {
    return getResource(resourceResolver , path);
}

@Override
public Iterator<Resource> listChildren(Resource resource) {
    return null;
}
}

, , , , ResourceProvider, .

@SlingServlet(
        resourceTypes = TypeServlet.RESOURCE_TYPE,
        methods = {"GET" , "POST"})
public class TypeServlet extends SlingAllMethodsServlet {


    static final String RESOURCE_TYPE = "mycompany/components/service/myservice";

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

        final String [] pathParts = request.getResource().getPath().split("/");
        final String id = pathParts[pathParts.length-1];
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            out.print("<html><body>Hello, received this id: " + id + "</body></html>");
        } finally {
             out.close();
        }
    }
}

Obviously, your servlet will do something much smarter, for example, handle the string "path" more intelligently and, possibly, create JSON.

0
source

All Articles