[opencms-dev] R: Add REST API to OpenCMS

giovanni.daquino at intellisync.it giovanni.daquino at intellisync.it
Fri Feb 28 14:09:44 CET 2020


Hello again, 

 

thank you for all the answers. I tried to follow the tips but I can’t still get the endpoints to listen. Here are the steps I followed: 

 

https://documentation.opencms.org/opencms-documentation/extending-opencms/development-setups/

I followed this guide to create a module, set the correct gradle configuration, added the classes JaxRsActivator and NewsResource, added to the OpenCMS web.xml the servlet configuration 

 

<servlet>

    <servlet-name>Resteasy</servlet-name>

    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>

    <init-param>

        <param-name>javax.ws.rs.Application</param-name>

        <param-value>it.company.JaxRsActivator</param-value>

    </init-param>

</servlet>

 

 

<servlet-mapping>

    <servlet-name>Resteasy</servlet-name>

    <url-pattern>/api/*</url-pattern>

</servlet-mapping>

 

 

I am currently running OpenCMS locally on Tomcat, I don’t know if this can help to, I added the dependency on gradle for resteasy.

 

Any help would be greatly appreciated,

 

Thank you 

 

Giovanni Andrea D’Aquino

Software Engineer

 

Descrizione: picturemessage_2w1anxdt.xg3 (1)

 

INTELLISYNC Srl
Via Sclafani, 40/B

Free Mind Foundry Innovation Hub
95024 Acireale (CT) Italy

 

 

Join our communities and get updated

 

 <https://www.linkedin.com/company/intellisync> linkedin_email

 

This email and any files transmitted with it are confidential and may be privileged and are intended solely for the use of the individual or entity to whom they are addressed. 
If you are not an addressee, any unauthorized direct or indirect dissemination, distribution, publication or copying of this message and any attachments is strictly prohibited. 
If you have received this email in error please notify the sender immediately, and delete this email from your system

 

Da: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] Per conto di Stephan Schrader
Inviato: giovedì 27 febbraio 2020 11:54
A: The OpenCms mailing list
Oggetto: Re: [opencms-dev] Add REST API to OpenCMS

 

Hello,

 

we also used JAX-RS as Antonio mentioned.

 

You need to add a class to enable JAX-RS:

 

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/api")
public class JaxRsActivator extends Application {
    public JaxRsActivator() {}
}

 

And the classes for your endpoints:

 

import org.opencms.file.CmsObject;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Providers;
import java.util.List;
import java.util.stream.Collectors;

@Path("news")
public class NewsResource {

    @Context
    private HttpServletRequest request;

    @GET
    @Produces("application/json")
    public Response getNews() {
        CmsObject cms = getCmsObject();
        
        // e.g. use Solr to get relevant News documents
        List<News> news...

        return Response.ok().entity(news).build();
    }

    public CmsObject getCmsObject() {
        CmsObject cmsObject;
        try {
            // try recover cms from session
            cmsObject = OpenCms.getAuthorizationHandler().initCmsObject(request, "oOo", "xXx");
        } catch (CmsAuthentificationException e) {
            cmsObject = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
        } catch (CmsException e) {
            throw new InternalServerErrorException("Failed to initialize CmsObject", e);
        }
        return cmsObject;
    }

}

 

With some Provider magic you could easily map exceptions to response codes or provide the CmsObject as context object.

 

King Regards,

Stephan

 

 

Am Di., 25. Feb. 2020 um 14:22 Uhr schrieb <giovanni.daquino at intellisync.it>:

Hello everyone, 

 

I am wondering if anybody has successfully implemented a REST or really any kind of APIs in OpenCMS. I would like to extend OpenCMS to use it headlessly. I think you should add one or more modules and have the module expose some endpoints. Is this correct?

 

Thank you,

 

 

Giovanni Andrea D’Aquino

Software Engineer

 

Descrizione: picturemessage_2w1anxdt.xg3 (1)

 

INTELLISYNC Srl
Via Sclafani, 40/B

Free Mind Foundry Innovation Hub
95024 Acireale (CT) Italy

Tel/Fax: (+39) 3475744346

 

 

Join our communities and get updated

 

 <https://www.linkedin.com/company/intellisync> linkedin_email

 

This email and any files transmitted with it are confidential and may be privileged and are intended solely for the use of the individual or entity to whom they are addressed. 
If you are not an addressee, any unauthorized direct or indirect dissemination, distribution, publication or copying of this message and any attachments is strictly prohibited. 
If you have received this email in error please notify the sender immediately, and delete this email from your system

 

_______________________________________________
This mail is sent to you from the opencms-dev mailing list
To change your list options, or to unsubscribe from the list, please visit
http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20200228/e33a980a/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 3571 bytes
Desc: not available
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20200228/e33a980a/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 975 bytes
Desc: not available
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20200228/e33a980a/attachment-0001.png>


More information about the opencms-dev mailing list