Magnolia 6.0 reached end of life on June 26, 2019. This branch is no longer supported, see End-of-life policy.
This page provides guidelines and best practice recommendations on how to create a custom Java-based REST endpoint with Magnolia.
Java-based custom REST endpoints give access to the full power of Java and provide a high level of flexibility and configurability. REST endpoints in Magnolia also allow fine-grained security.
Endpoints can be used for many purposes, from triggering a third party service to implementing a custom JSON provider. However, there are also other possibilities to get Magnolia content as JSON.
In this page, we assume that you know Java and the basics of Maven or another similar dependency management tool.
A REST endpoint is a resource located on a server, in our case on a Magnolia instance, which can be accessed with a RESTful URL.
A Java endpoint makes use of the Java API for RESTful Web Services (JAX-RS). The endpoint exposes public methods accessible via distinct URLs. A public method is accessed with a request and produces a response.
Magnolia REST endpoints are JAX-RS endpoints.
The base path for every Magnolia REST resource is /<context>/.rest
, where <context> is either the name of the webapp or /
if the webapp is served from the root context. For details about specific paths of specific REST resources, see Understanding @Path
below.
All requests to /<context>/.rest*
are handled by the RestDispatcherServlet, which is installed and configured by the magnolia-rest-integration
module.
An endpoint can have several methods accomplishing completely different tasks. Basically endpoints can do anything that is possible with Java.
Each publicly exposed method representing a REST resource must produce a response that contains at least some response headers (the minimum is a proper HTTP response code). Both the request and response may or may not contain a "payload" (JSON or XML) wrapped in the request or response body.
An endpoint should implement at least one method that can be accessed by its specific path with one of the standard HTTP methods: GET
, PUT
, POST
or DELETE
.
A typical use case for an endpoint is a data API, which can read, create, update or delete content. A data API endpoint must implement at least one method, such as a GET
method to read data.
Typically, methods for data interaction are called with these HTTP methods:
Data interaction function | Read data | Create data | Update data | Delete data |
HTTP Method | GET | PUT | POST | DELETE |
Request has payload | no | yes | yes | no |
Response has payload | yes | maybe | maybe | no |
Magnolia provides default endpoints to read, create, update and delete nodes and properties of JCR workspaces. For details about the Magnolia default JCR content API, read the section about the magnolia-rest-services
module below or have a look at the REST API page.
Java classes typically are wrapped in modules. When the Java classes of your endpoint are part of a (Maven) module, you can add the module to your bundle or webapp as a .jar file or manage the dependency with Maven in the .pom file of the webapp.
Use the Magnolia Maven archetype to create a new Magnolia Maven module that will host your Java REST endpoint.
Before running the Maven
archetype
command, please read How to use Magnolia Maven archetypes: Check Maven settings.
If you are not familiar with the Maven archetype plugin, please also read How to use Magnolia Maven archetypes: The archetype plugin.
Here is the archetype command call:
mvn archetype:generate -DarchetypeGroupId=info.magnolia.maven.archetypes -DarchetypeArtifactId=magnolia-module-archetype -DarchetypeVersion=RELEASE
Notes:
groupId
and artifactId
.magnolia-bundle-version
that fits your existing Magnolia bundle(s) best. If you are not sure, use the latest released Magnolia version: 6.1.8.magnolia-bundle-parent
. This parent manages the versions of Magnolia modules you rely on.Example:
package com.example.rest.service.v1; @Path("/demo/v1") public class DemoEndpoint { /* more code to be added here later on */ }
com.example.rest.service.v1
.DemoEndpoint
.com.example.rest.service.v1.DemoEndpoint
.If you want to raise the version of the endpoint later on, without disabling the original version, create a package with a higher version such as com.example.rest.service.v2
.
We assume that you know the basics about how to manage dependencies, and that you manage these dependencies with Maven.
The Java API for RESTful Web Services - JAX-RS is defined in the packages The dependencies (for both the interfaces and the implementations) are managed by the javax.ws.rs
and javax.xml.bind
. These are interfaces and sufficient for endpoint classes during compilation. However, on runtime, when the REST resources are used, a webapp also requires implementations of the these two mentioned packages. Magnolia uses RESTeasy for this purpose.magnolia-rest-integration
module.
Your module, which depends on Magnolia REST module(s), automatically (transiently) inherits the dependencies from the Magnolia modules.
The Magnolia REST module contains three submodules.
magnolia-rest-integration
The REST Integration module installs the integration part of REST. The module:/config/<module-name>/rest-endpoints
for any custom endpoints you want to register. The monitoring mechanism is the same as used for observing registered dialogs, templates and apps. RestDispatcherServlet
which dispatches requests to the individual endpoints registered in configuration.
MessageBodyWorkers
in RESTeasy) you might need. The providers are responsible for translating the return object into JSON/XML and vice-versa.rest
role that initially prevents access to unauthorized requests.
magnolia-rest-services
The magnolia-rest-services
module depends on the magnolia-rest-integration
module. Therefore it provides everything explained in the preceding section.
In addition, it contains the Magnolia default REST endpoints NodeEndpoint and PropertyEndpoint and the CommandEndpoint , which are described on the REST API page.
This module also provides the RepositoryMarshaller , which you can use within your custom endpoint.
magnolia-rest-content-delivery
This module provides the Delivery endpoint.
magnolia-rest-tools
The REST Tools module integrates the swagger tools into the Admin UI. These tools ease the development and testing of REST endpoints. The module extends the RestDispatcherServlet
with a custom, API-aware servlet that can read API annotations from all available REST endpoints. The servlet enables the endpoints in the Swagger API explorer. If you write your own endpoint you need to add annotations in the code yourself.
The module is used for development and testing purposes only.
Make sure you use the same version for all Magnolia REST modules on which you depend.
The version of the Magnolia REST modules depends on the version of Magnolia you use to run your custom endpoint. We recommend you check the .pom file (or the parent .pom file) of your Magnolia webapp. The .pom file manages the version of the Magnolia REST modules using the property restVersion
. If you are unsure, use the latest released version, which is currently: 2.2.18.
To create a Java class that acts as a REST resource, write a Java class — your service — and register this service in the Magnolia configuration
workspace. Registering the service makes sure that the REST resource can be accessed by a default Magnolia servlet.
In this section we create a simple endpoint implementation and explain some important details for every custom Java REST endpoint in the context of Magnolia.
Create the class com.example.rest.service.v1.DemoEndpoint
. This is the code for a simple working example:
package com.example.rest.service.v1; import info.magnolia.rest.AbstractEndpoint; import info.magnolia.rest.EndpointDefinition; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("/demo/v1") public class DemoEndpoint<D extends EndpointDefinition> extends AbstractEndpoint<D> { public DemoEndpoint(D endpointDefinition) { super(endpointDefinition); } @Path("/hello") @GET @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response hello() { return Response.ok().build(); } }
javax.ws.rs.core.Response
.Let's have a closer look at some of these details below.
info.magnolia.rest.AbstractEndpoint
See line 13 in the example above. By extending AbstractEndpoint , the endpoint can be configured. We will see later on what the configuration and registration looks like. When a request comes to Magnolia mapped to this endpoint, an instance is created via Inversion of Control (IoC). During this process, the IoC framework also creates an instance of EndpointDefinition , which is injected into the constructor of our endpoint class.
javax.ws.rs.*
annotationsIf you are not yet familiar with the
javax.ws.rs.*
package, we recommend you take some time to learn the basics. This package contains many annotations that can be used to declare the details of Java-based REST resources. We will see how to use these annotations below.
javax.ws.rs.core.Response
javax.ws.rs.core.Response
is the standard response type for a Java REST endpoint.
On line 22 we declare the return type.
On line 23 we use the static method #ok
(without an argument), which ensures that:
#ok
with no argument, no payload is created and the response does not have a body, just headers.@Producing
On line 21, by using the @Produces
annotation, we can declare a list of possible HTTP response content types.
Note that, in our example, there is no need to declare the HTTP response content type because the method only returns response headers but no response body. However, if the method creates a payload (for instance JSON or XML) to be returned as an HTTP response body, then you should declare the response content type with @Produces.
@Path
A method on a REST endpoint has a distinct path to access it with a RESTful URL. A path generally consists of several parts:
/<context>/.rest
. /magnoliaAuthor/.rest
or /magnoliaPublic/.rest
./.rest
.@Path
annotation; see line 12 on the example above. /demo/v1
./v1
here).@Path
annotation; see line 19 on the example above. /hello
. However, the method path can be dynamic in combination with @PathParam
, we will see an example later on.To summarize, the path to a REST "resource" is a combination of:
Magnolia REST base path | + | REST endpoint base path | + | REST endpoint method path |
/<context>/.rest | /demo/v1 | /hello | ||
On line 20 we use the annotation @GET
. This declares that the REST resource defined by this method can be accessed with the HTTP method GET only.
If you try to access the resources using another method (for example with POST), the resource returns an error HTTP response code. We will test this later on with cURL.
You can only declare one HTTP method per method. The Java compiler shows an error if you try to declare two methods (by using two annotations).
REST resources in Magnolia should be registered within your custom module. This ensures that the REST resource and its configuration are known in the Magnolia EndpointDefinitionRegistry .
Example: Registering the endpoint:
class: info.magnolia.rest.service.node.definition.ConfiguredNodeEndpointDefinition implementationClass: com.example.rest.service.v1.DemoEndpoint
Node name | Value |
---|---|
modules | |
<your-module> | |
rest-endpoints | |
demo | |
class | info.magnolia.rest.service.node.definition.ConfiguredNodeEndpointDefinition |
implementationClass | com.example.rest.service.v1.DemoEndpoint |
Properties:
rest-endpoints | required The folder which contains a list of endpoint definitions. The name of the folder must be |
| required The name of the endpoint. The name is arbitrary but must be unique. |
| required The fully qualified class name of the definition class. Typically |
| required The fully qualified class name of the REST endpoint java class. |
The last step before you can access the resource with a client is to grant access to the REST resource in the Security app.
Permissions to issue REST requests are controlled using the standard Magnolia role-based security mechanism .
The magnolia-rest-integration
module installs a rest
role which has the permission to issue requests to the nodes
and properties
endpoints by default.
You can extend the existing rest
role to provide web access to the path of your custom REST resource, or you can duplicate the existing role to create a new one just for your use case. In either case, make sure that you assign this role to the users who need to access this resource.
For our example we create a new role named demo-rest-role
and assign it to superuser
on the magnoliaAuthor instance. You also can download this role from the GIT repository.
Role info:
Access Control Lists (ACL):
Web access:
Access type | Path | Value |
---|---|---|
Deny | /.rest* | Deny all to begin. |
Get&Post | /.rest/swagger* | Allow access to the Swagger tools. This is only required if you have the |
Get&Post | /.rest/demo/v1* | Allow access to our custom endpoint whose access path is Note that |
Access type meanings:
Access type | Meaning |
---|---|
Deny | Everything is denied. |
Get | Only the HTTP method GET is allowed. |
Get&Post | All HTTP methods available are allowed: GET , POST , PUT and DELETE . |
This is the list of roles assigned to superuser:
Depending on your bundle, you may have more roles assigned to superuser
.
Now everything is ready to access the custom endpoint.
cURL is a useful tool to test REST resources. To check if you have it installed, open a command shell and type:
curl
curl: try 'curl --help' or 'curl --manual' for more information
curl -i --user superuser:superuser -X GET --header 'Accept: application/json' 'http://localhost:8080/magnoliaAuthor/.rest/demo/v1/hello'
-i
option, cURL displays the response header. Remember that the requested resource does not produce a payload, so there is no response body.HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=624D8A0283867F9FA07D02F72B9B72EA; Path=/magnoliaAuthor/; HttpOnly Pragma: no-cache Cache-Control: no-cache, no-store, must-revalidate, max-age=0 Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: text/html;charset=UTF-8 Content-Length: 0 Date: Tue, 04 Jul 2017 14:36:35 GMT
POST
method:curl -i --user superuser:superuser -X POST --header 'Accept: application/json' 'http://localhost:8080/magnoliaAuthor/.rest/demo/v1/hello'
HTTP/1.1 500 Internal Server Error Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=C4A7CB1D1AA09967FD4E413E61D9AA4C; Path=/magnoliaAuthor/; HttpOnly Pragma: no-cache Cache-Control: no-cache, no-store, must-revalidate, max-age=0 Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: text/plain;charset=UTF-8 Content-Length: 79 Date: Tue, 04 Jul 2017 14:40:17 GMT Connection: close RESTEASY003650: No resource method found for POST, return 405 with Allow header
#hello
is annotated with
@GET
declaring it can only be accessed with the HTTP method GET
. See the section declaring the HTTP method.Note that you pass the username and password in clear text in this cURL call. This is acceptable when testing on a local machine, however, we advise against doing this to access a production environment server.
An endpoint dealing with data must manage two types of data conversion:
If the endpoint handles both read (GET) and write (PUT to create and POST to update) actions, marshalling and data conversion must also work in both directions.
In this section we focus on some aspects of this kind of data conversion.
Regardless of the type of data you deal with, in the context of a REST endpoint, at some point you typically transform "data" into a POJO (and vice versa).
There is no "one size fits all" for transforming a data object into a POJO and vice versa. The best solution for you depends on your use case.
The magnolia-rest-services
module provides classes to transform JCR nodes and properties into POJOs and vice versa.
The Magnolia endpoints PropertyEndpoint and NodeEndpoint use these classes. You also can use the classes for your custom endpoints. However, note that the structure of the POJO and of the resulting payload after marshalling may not cover your requirements.
Transforming a POJO into JSON or XML is known as marshalling. Transforming JSON or XML into a POJO is known as unmarshalling. Both processes are sometimes referred to as marshalling.
Marshalling is handled by the REST framework by the JAX-RS API implementation.
However, when dealing with payload (XML or JSON) in your endpoint, note the following:
PUT
a POJO into the response of the endpoint method.Imagine you have a POJO with the name Lunch
and two properties: food
and beverage
.
lunch
payload. Endpoint method(s) providing a Lunch
object would look like this:@Path("/lunch2") @GET @Produces({MediaType.APPLICATION_JSON}) public Response lunch2() { Lunch pojo = new Lunch("Rösti mit Geschnetzeltem", "Ueli Weizen"); return Response.ok(pojo).build(); } @Path("/lunch") @GET @Produces({MediaType.APPLICATION_JSON}) public Lunch lunch() { Lunch pojo = new Lunch("Svíčková na smetaně ", "Gambrinus Plzen"); return pojo; }
#lunch
and #lunch2
. Both produce the same payload when called from outside, however, their implementation is slightly different.#lunch2
javax.ws.rs.core.Response
.Lunch
) into this response; see line 6.This approach provides high flexibility concerning the response code, for instance you can do things like this:
return Response.status(Response.Status.NOT_ACCEPTABLE).build(); //... return Response.status(Response.Status.BAD_REQUEST).build();
#lunch
com.example.rest.pojos.Lunch
.Note that both method produce the same response headers and identical payload (structure, in this example only the values of the food and beverage properties differ).
Both approaches to produce payload with POJOs shown here work in more complex scenarios. For example: you could return a list of POJOs; the properties of the POJO could also be POJOs; and so on.
Endpoint methods that receive a payload (JSON or XML) are typically used to create or update an object.
Let's imagine an example where the endpoint allows you add a lunch object to a store. In the context of a data store, adding is like creating. To create an object, a REST service is called with the the HTTP method PUT
.
This code snippet shows the method to add a lunch:
@Path("/store-lunch") @Consumes({MediaType.APPLICATION_JSON}) @PUT @Produces({MediaType.APPLICATION_JSON}) public Response storeLunch(Lunch lunch){ if(lunch != null){ try { store.add(lunch); return Response.ok(lunch).build(); } catch (Exception e) { log.error("Failed to store the lunch"); return Response.status(Response.Status.NOT_ACCEPTABLE).build(); } }else{ return Response.status(Response.Status.BAD_REQUEST).build(); } }// eof: storeLunch
Line 140: It is generally considered good practice to tell the service what type of data it consumes. However, it is not required.
Line 143: The most important point in the current context is the method signature.
The method must contain a parameter of the type to which the payload should be mapped to.
store
actually is a mock store, it does not really store data in the example. (See
BlackboxDatastore
on Git.). However, store#add
may throw an exception if it fails.food
has the value Bad food
.You can test the new method using cURL.
Implementing a method to update an object (and using the HTTP method POST
) works in a similar way. In such a case, the payload sent to the REST endpoint must contain the object id in order to update the correct item on the data store.
Let's assume you want to use a data API endpoint to serve content managed with Magnolia apps to independent applications located "outside" of Magnolia. For example, you want to use an AngularJS application to display your content.
We also assume Magnolia and the Angular application are hosted on completely different systems.
According to the same-origin policy: (...) a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin. Therefore, if your application resides within a domain that is different from the domain that hosts your data, the web browser will actively prevent accessing content stored under the host domain. An elegant solution to solve the same-origin policy without changing Apache configuration is the Magnolia Add HTTP Headers filter. This fiter is available out-of-the-box; you only need to configure it.
The sample code for the examples on this page is available on the Magnolia GIT repository.