Class ScriptHelper
- java.lang.Object
-
- org.apache.sling.scripting.core.ScriptHelper
-
- All Implemented Interfaces:
SlingScriptHelper
public class ScriptHelper extends java.lang.Object implements SlingScriptHelper
Simple script helper providing access to the (wrapped) response, the on-demand writer and a simple API for request inclusion. Instances of this class are made available to the scripts as the globalslingvariable. Client code using this object should take care to callcleanup()when the object is not used anymore!
-
-
Constructor Summary
Constructors Constructor Description ScriptHelper(BundleContext ctx, SlingScript script)ScriptHelper(BundleContext ctx, SlingScript script, SlingHttpServletRequest request, SlingHttpServletResponse response)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidcleanup()Clean up this instance.voiddispose()Deprecated.voidforward(java.lang.String path)Same asSlingScriptHelper.forward(String,RequestDispatcherOptions), but using empty options.voidforward(java.lang.String path, java.lang.String options)Helper method to forward the request to a Servlet or script for the givenpathandrequestDispatcherOptions.voidforward(java.lang.String path, RequestDispatcherOptions options)Helper method to forward the request to a Servlet or script for the givenpathandoptions.voidforward(Resource resource)Same asSlingScriptHelper.forward(Resource,RequestDispatcherOptions), but using empty options.voidforward(Resource resource, java.lang.String options)Helper method to forward the request to a Servlet or script for the givenresourceandrequestDispatcherOptions.voidforward(Resource resource, RequestDispatcherOptions options)Helper method to forward the request to a Servlet or script for the givenresourceandoptions.SlingHttpServletRequestgetRequest()Returns theSlingHttpServletRequestrepresenting the input of the request.SlingHttpServletResponsegetResponse()Returns theSlingHttpServletResponserepresenting the output of the request.SlingScriptgetScript()Returns theSlingScriptbeing called to handle the request.<ServiceType>
ServiceTypegetService(java.lang.Class<ServiceType> type)Lookup a single service.<ServiceType>
ServiceType[]getServices(java.lang.Class<ServiceType> serviceType, java.lang.String filter)Lookup one or several services.voidinclude(java.lang.String path)Same asSlingScriptHelper.include(String,RequestDispatcherOptions), but using empty options.voidinclude(java.lang.String path, java.lang.String options)Helper method to include the result of processing the request for the givenpathandrequestDispatcherOptions.voidinclude(java.lang.String path, RequestDispatcherOptions options)Helper method to include the result of processing the request for the givenpathandoptions.voidinclude(Resource resource)Same asSlingScriptHelper.include(Resource,RequestDispatcherOptions), but using empty options.voidinclude(Resource resource, java.lang.String options)Helper method to include the result of processing the request for the givenresourceandrequestDispatcherOptions.voidinclude(Resource resource, RequestDispatcherOptions options)Helper method to include the result of processing the request for the givenresourceandoptions.
-
-
-
Constructor Detail
-
ScriptHelper
public ScriptHelper(BundleContext ctx, SlingScript script)
-
ScriptHelper
public ScriptHelper(BundleContext ctx, SlingScript script, SlingHttpServletRequest request, SlingHttpServletResponse response)
-
-
Method Detail
-
getScript
public SlingScript getScript()
Description copied from interface:SlingScriptHelperReturns theSlingScriptbeing called to handle the request.- Specified by:
getScriptin interfaceSlingScriptHelper- Returns:
- The script
- See Also:
SlingScriptHelper.getScript()
-
getRequest
public SlingHttpServletRequest getRequest()
Description copied from interface:SlingScriptHelperReturns theSlingHttpServletRequestrepresenting the input of the request.- Specified by:
getRequestin interfaceSlingScriptHelper- Returns:
- The request
- See Also:
SlingScriptHelper.getRequest()
-
getResponse
public SlingHttpServletResponse getResponse()
Description copied from interface:SlingScriptHelperReturns theSlingHttpServletResponserepresenting the output of the request.- Specified by:
getResponsein interfaceSlingScriptHelper- Returns:
- The response
- See Also:
SlingScriptHelper.getResponse()
-
include
public void include(java.lang.String path)
Description copied from interface:SlingScriptHelperSame asSlingScriptHelper.include(String,RequestDispatcherOptions), but using empty options.- Specified by:
includein interfaceSlingScriptHelper- Parameters:
path- The path to include- See Also:
SlingScriptHelper.include(java.lang.String)
-
include
public void include(java.lang.String path, java.lang.String options)Description copied from interface:SlingScriptHelperHelper method to include the result of processing the request for the givenpathandrequestDispatcherOptions. This method is intended to be implemented as follows:RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path, "option:xyz"); if (dispatcher != null) { dispatcher.include(getRequest(), getResponse()); }This method creates a
RequestDispatcherOptionsobject by calling theRequestDispatcherOptions(String)constructor.- Specified by:
includein interfaceSlingScriptHelper- Parameters:
path- The path to the resource to include.options- influence the rendering of the included Resource- See Also:
SlingScriptHelper.include(java.lang.String, java.lang.String)
-
include
public void include(java.lang.String path, RequestDispatcherOptions options)Description copied from interface:SlingScriptHelperHelper method to include the result of processing the request for the givenpathandoptions. This method is intended to be implemented as follows:RequestDispatcherOptions opts = new RequestDispatcherOptions(); opts.put("option", "xyz"); RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path, opts); if (dispatcher != null) { dispatcher.include(getRequest(), getResponse()); }- Specified by:
includein interfaceSlingScriptHelper- Parameters:
path- The path to the resource to include.options- influence the rendering of the included Resource- See Also:
SlingScriptHelper.include(java.lang.String, org.apache.sling.api.request.RequestDispatcherOptions)
-
forward
public void forward(java.lang.String path)
Description copied from interface:SlingScriptHelperSame asSlingScriptHelper.forward(String,RequestDispatcherOptions), but using empty options.- Specified by:
forwardin interfaceSlingScriptHelper- Parameters:
path- The path to forward to- See Also:
SlingScriptHelper.forward(java.lang.String)
-
forward
public void forward(java.lang.String path, java.lang.String options)Description copied from interface:SlingScriptHelperHelper method to forward the request to a Servlet or script for the givenpathandrequestDispatcherOptions. This method is intended to be implemented as follows:RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path, "option:xyz"); if (dispatcher != null) { dispatcher.forward(getRequest(), getResponse()); }This method creates a
RequestDispatcherOptionsobject by calling theRequestDispatcherOptions(String)constructor.- Specified by:
forwardin interfaceSlingScriptHelper- Parameters:
path- The path to the resource to forward to.options- influence the rendering of the forwarded Resource- See Also:
SlingScriptHelper.forward(java.lang.String, java.lang.String)
-
forward
public void forward(java.lang.String path, RequestDispatcherOptions options)Description copied from interface:SlingScriptHelperHelper method to forward the request to a Servlet or script for the givenpathandoptions. This method is intended to be implemented as follows:RequestDispatcherOptions opts = new RequestDispatcherOptions(); opts.put("option", "xyz"); RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path, opts); if (dispatcher != null) { dispatcher.forward(getRequest(), getResponse()); }- Specified by:
forwardin interfaceSlingScriptHelper- Parameters:
path- The path to the resource to forward the request to.options- influence the rendering of the forwarded Resource- See Also:
SlingScriptHelper.forward(java.lang.String, org.apache.sling.api.request.RequestDispatcherOptions)
-
dispose
@Deprecated public void dispose()
Deprecated.Description copied from interface:SlingScriptHelperDispose the helper. This method can be used to clean up the script helper after the script is run.- Specified by:
disposein interfaceSlingScriptHelper- See Also:
SlingScriptHelper.dispose()
-
getService
public <ServiceType> ServiceType getService(java.lang.Class<ServiceType> type)
Description copied from interface:SlingScriptHelperLookup a single service.If multiple such services exist, the service with the highest ranking (as specified in its Constants.SERVICE_RANKING property) is returned. If there is a tie in ranking, the service with the lowest service ID (as specified in its Constants.SERVICE_ID property); that is, the service that was registered first is returned.
This is equal to the semantics from BundleContext.getServiceReference(Class).
- Specified by:
getServicein interfaceSlingScriptHelper- Type Parameters:
ServiceType- The type (interface) of the service.- Parameters:
type- The type (interface) of the service.- Returns:
- The service instance, or
nullif no services are registered which implement the specified class. - See Also:
SlingScriptHelper.getService(java.lang.Class)
-
getServices
public <ServiceType> ServiceType[] getServices(java.lang.Class<ServiceType> serviceType, java.lang.String filter) throws InvalidServiceFilterSyntaxExceptionDescription copied from interface:SlingScriptHelperLookup one or several services.The returned array is sorted descending by service ranking (i.e. the service with the highest ranking is returned first). If there is a tie in ranking, the service with the lowest service ID (as specified in its Constants.SERVICE_ID property); that is, the service that was registered first is returned first.
- Specified by:
getServicesin interfaceSlingScriptHelper- Type Parameters:
ServiceType- The type (interface) of the service.- Parameters:
serviceType- The type (interface) of the service.filter- An optional filter (LDAP-like, see OSGi spec)- Returns:
- An array of services objects or
null. - Throws:
InvalidServiceFilterSyntaxException- If thefilterstring is not a valid OSGi service filter string.- See Also:
SlingScriptHelper.getServices(java.lang.Class, java.lang.String)
-
cleanup
public void cleanup()
Clean up this instance.
-
forward
public void forward(Resource resource)
Description copied from interface:SlingScriptHelperSame asSlingScriptHelper.forward(Resource,RequestDispatcherOptions), but using empty options.- Specified by:
forwardin interfaceSlingScriptHelper- Parameters:
resource- The resource to forward to.- See Also:
SlingScriptHelper.forward(org.apache.sling.api.resource.Resource)
-
forward
public void forward(Resource resource, java.lang.String options)
Description copied from interface:SlingScriptHelperHelper method to forward the request to a Servlet or script for the givenresourceandrequestDispatcherOptions. This method is intended to be implemented as follows:RequestDispatcher dispatcher = getRequest().getRequestDispatcher(resource, "option:xyz"); if (dispatcher != null) { dispatcher.forward(getRequest(), getResponse()); }This method creates a
RequestDispatcherOptionsobject by calling theRequestDispatcherOptions(String)constructor.- Specified by:
forwardin interfaceSlingScriptHelper- Parameters:
resource- The resource to forward to.options- influence the rendering of the forwarded Resource- See Also:
SlingScriptHelper.forward(org.apache.sling.api.resource.Resource, java.lang.String)
-
forward
public void forward(Resource resource, RequestDispatcherOptions options)
Description copied from interface:SlingScriptHelperHelper method to forward the request to a Servlet or script for the givenresourceandoptions. This method is intended to be implemented as follows:RequestDispatcherOptions opts = new RequestDispatcherOptions(); opts.put("option", "xyz"); RequestDispatcher dispatcher = getRequest().getRequestDispatcher(resource, opts); if (dispatcher != null) { dispatcher.forward(getRequest(), getResponse()); }- Specified by:
forwardin interfaceSlingScriptHelper- Parameters:
resource- The resource to forward the request to.options- influence the rendering of the forwarded Resource- See Also:
SlingScriptHelper.forward(org.apache.sling.api.resource.Resource, org.apache.sling.api.request.RequestDispatcherOptions)
-
include
public void include(Resource resource)
Description copied from interface:SlingScriptHelperSame asSlingScriptHelper.include(Resource,RequestDispatcherOptions), but using empty options.- Specified by:
includein interfaceSlingScriptHelper- Parameters:
resource- The resource to include- See Also:
SlingScriptHelper.forward(org.apache.sling.api.resource.Resource)
-
include
public void include(Resource resource, java.lang.String options)
Description copied from interface:SlingScriptHelperHelper method to include the result of processing the request for the givenresourceandrequestDispatcherOptions. This method is intended to be implemented as follows:RequestDispatcher dispatcher = getRequest().getRequestDispatcher(resource, "option:xyz"); if (dispatcher != null) { dispatcher.include(getRequest(), getResponse()); }This method creates a
RequestDispatcherOptionsobject by calling theRequestDispatcherOptions(String)constructor.- Specified by:
includein interfaceSlingScriptHelper- Parameters:
resource- The resource to include.options- influence the rendering of the included Resource- See Also:
SlingScriptHelper.include(org.apache.sling.api.resource.Resource, java.lang.String)
-
include
public void include(Resource resource, RequestDispatcherOptions options)
Description copied from interface:SlingScriptHelperHelper method to include the result of processing the request for the givenresourceandoptions. This method is intended to be implemented as follows:RequestDispatcherOptions opts = new RequestDispatcherOptions(); opts.put("option", "xyz"); RequestDispatcher dispatcher = getRequest().getRequestDispatcher(resource, opts); if (dispatcher != null) { dispatcher.include(getRequest(), getResponse()); }- Specified by:
includein interfaceSlingScriptHelper- Parameters:
resource- The resource to include.options- influence the rendering of the included Resource- See Also:
SlingScriptHelper.include(org.apache.sling.api.resource.Resource, org.apache.sling.api.request.RequestDispatcherOptions)
-
-