In order to process write requests as described above, create a class implementing the
interface ContentWriteInterceptor
. Alternatively, your class can also inherit
from ContentWriteInterceptorBase
, which already defines methods to configure the
content type to which the write interceptor applies, and the priority at which the interceptor
runs compared to other applicable interceptors.
This leaves only the method intercept(ContentWriteRequest)
to be implemented in
custom code. The argument of the intercept
method provides access to all
information needed for processing the current request, which is either an update request or a
create request.
The method getProperties()
of the WriteRequest
object returns a
mutable map from property names to values that represents the intended write request. Write
interceptors can read this map to determine the desired changes. They may also modify the map
(which includes the ability to add additional name/value pairs if required), thereby
requesting modification of the original write request, and/or additional write operations. If
multiple write interceptors run in succession, they see the effects of the previous
interceptors' modifications in this map.
If a blob has been created in the write request by uploading a file via Studio, it is available
as UploadedBlob
in the properties of the WriteRequest
, providing access
to the original filename.
The method getEntity()
returns the content on which an update request is being
executed. A write interceptor may use this method to determine the context of a write request,
for example to determine the site in which the content is placed in a multi-site setting or to
determine the exact type of the content. Do not write to the content object. To modify the
content, update the properties map as explained above.
The method getEntity()
returns null
for a create request, because a
write interceptor is called before a content is created. So that the interceptor is able to
respond to the context of a create request, the ContentWriteRequest
object
provides the methods getParent()
, getName()
, and
getType()
, which provide access to the folder, the name of the document to be
created, and the content type to be instantiated.
Finally, an issues object can be retrieved by calling getIssues()
. This object
functions as shown in Section 7.15.1, “Validators”. In this context, it allows an
interceptor to report problems observed in the write request. If a write interceptor reports
any issues with error severity using the method addIssue(...)
of the issues
object, the write request will automatically be canceled and an error description will be
shown at the client side. If issues of severity warn are detected, the write is executed, but
a message box is still shown. In any case, the issues are not persisted, so that the only
issues shown for a content permanently are the issues computed by the regular validators.
The following example shows the basic structure of a custom interceptor for images. A field
for the name of the affected blob property is provided. The intercept()
method
checks whether the indicated property is updated, retrieves the new value and provides a
replacement value using the properties map.
public class MyInterceptor extends ContentWriteInterceptorBase { private String imageProperty; public void setImageProperty(String imageProperty) { this.imageProperty = imageProperty; } public void intercept(ContentWriteRequest request) { Map<String,Object> properties = request.getProperties(); if (properties.containsKey(imageProperty)) { Object value = properties.get(imageProperty); if (value instanceof Blob) { ... properties.put(imageProperty, updatedValue); } } } }
Example 7.64. Defining a Write Interceptor