To better understand the Request Context Contributor, you’ll explore how to
create one. First, you’ll create the SampleRequestContentContributor
class
file, which contains the contribute
method that contributes a new field to
the context with a custom attribute. You can view the
full project on Github.
-
Inside the module, create a package named
com.liferay.segments.context.extension.sample.internal.context.contributor
-
Create a Java class named
SampleRequestContentContributor
. -
Inside the file, insert the
@Component
declaration:@Component( immediate = true, property = { "request.context.contributor.key=" + SampleRequestContextContributor.KEY, "request.context.contributor.type=boolean" }, service = RequestContextContributor.class )
-
Add the class declaration:
public class SampleRequestContextContributor implements RequestContextContributor { }
-
Create the attribute that you’re adding. In this case, it’s just a static string.
public static final String KEY = "sample";
-
Create the
contribute
method:@Override public void contribute( Context context, HttpServletRequest httpServletRequest) { context.put(KEY, GetterUtil.getBoolean(httpServletRequest.getAttribute("sample.attribute"))); }
To customize your field label or add a set of selectable options, you can add
an optional SegmentsFieldCustomizer
service associated to your contributed
field by its key. Create one now.
-
Inside the module, create a package named
com.liferay.context.extension.sample.internal.field.customizer
-
Create a Java class named
SampleSegmentsFieldCustomizer
. -
Inside the file, insert the
@Component
declaration:@Component( immediate = true, property = { "segments.field.customizer.entity.name=Context", "segments.field.customizer.key=" + SampleSegmentsFieldCustomizer.KEY, "segments.field.customizer.priority:Integer=50" }, service = SegmentsFieldCustomizer.class )
-
Create the class declaration:
public class SampleSegmentsFieldCustomizer implements SegmentsFieldCustomizer { }
-
Create the
KEY
value:public static final String KEY = "sample";
-
Create the methods to provide a list of fields to be displayed when configuring the criteria.
@Override public List<String> getFieldNames() { return _fieldNames; } @Override public String getKey() { return KEY; } @Override public String getLabel(String fieldName, Locale locale) { ResourceBundle resourceBundle = ResourceBundleUtil.getBundle( "content.Language", locale, getClass()); return LanguageUtil.get(resourceBundle, "sample-field-label"); } private static final List<String> _fieldNames = ListUtil.fromArray( new String[] {"sample"});
Once you deploy your extensions, the session section of the segment criteria editor includes your new context-based field.
Figure 1: The sample field appears.
Great! You’ve created a Request Context Contributor!