[opencms-dev] Add REST API to OpenCMS
Stephan Schrader
zstephanz at gmail.com
Thu Feb 27 11:54:29 CET 2020
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*
>
>
>
> [image: 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
>
>
>
> [image: linkedin_email] <https://www.linkedin.com/company/intellisync>
>
>
>
> 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/20200227/9d9b4926/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/20200227/9d9b4926/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/20200227/9d9b4926/attachment-0001.png>
More information about the opencms-dev
mailing list