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 globalsling
variable. 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 void
cleanup()
Clean up this instance.void
dispose()
Deprecated.void
forward(java.lang.String path)
Same asSlingScriptHelper.forward(String,RequestDispatcherOptions)
, but using empty options.void
forward(java.lang.String path, java.lang.String options)
Helper method to forward the request to a Servlet or script for the givenpath
andrequestDispatcherOptions
.void
forward(java.lang.String path, RequestDispatcherOptions options)
Helper method to forward the request to a Servlet or script for the givenpath
andoptions
.void
forward(Resource resource)
Same asSlingScriptHelper.forward(Resource,RequestDispatcherOptions)
, but using empty options.void
forward(Resource resource, java.lang.String options)
Helper method to forward the request to a Servlet or script for the givenresource
andrequestDispatcherOptions
.void
forward(Resource resource, RequestDispatcherOptions options)
Helper method to forward the request to a Servlet or script for the givenresource
andoptions
.SlingHttpServletRequest
getRequest()
Returns theSlingHttpServletRequest
representing the input of the request.SlingHttpServletResponse
getResponse()
Returns theSlingHttpServletResponse
representing the output of the request.SlingScript
getScript()
Returns theSlingScript
being 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.void
include(java.lang.String path)
Same asSlingScriptHelper.include(String,RequestDispatcherOptions)
, but using empty options.void
include(java.lang.String path, java.lang.String options)
Helper method to include the result of processing the request for the givenpath
andrequestDispatcherOptions
.void
include(java.lang.String path, RequestDispatcherOptions options)
Helper method to include the result of processing the request for the givenpath
andoptions
.void
include(Resource resource)
Same asSlingScriptHelper.include(Resource,RequestDispatcherOptions)
, but using empty options.void
include(Resource resource, java.lang.String options)
Helper method to include the result of processing the request for the givenresource
andrequestDispatcherOptions
.void
include(Resource resource, RequestDispatcherOptions options)
Helper method to include the result of processing the request for the givenresource
andoptions
.
-
-
-
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:SlingScriptHelper
Returns theSlingScript
being called to handle the request.- Specified by:
getScript
in interfaceSlingScriptHelper
- Returns:
- The script
- See Also:
SlingScriptHelper.getScript()
-
getRequest
public SlingHttpServletRequest getRequest()
Description copied from interface:SlingScriptHelper
Returns theSlingHttpServletRequest
representing the input of the request.- Specified by:
getRequest
in interfaceSlingScriptHelper
- Returns:
- The request
- See Also:
SlingScriptHelper.getRequest()
-
getResponse
public SlingHttpServletResponse getResponse()
Description copied from interface:SlingScriptHelper
Returns theSlingHttpServletResponse
representing the output of the request.- Specified by:
getResponse
in interfaceSlingScriptHelper
- Returns:
- The response
- See Also:
SlingScriptHelper.getResponse()
-
include
public void include(java.lang.String path)
Description copied from interface:SlingScriptHelper
Same asSlingScriptHelper.include(String,RequestDispatcherOptions)
, but using empty options.- Specified by:
include
in 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:SlingScriptHelper
Helper method to include the result of processing the request for the givenpath
andrequestDispatcherOptions
. 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
RequestDispatcherOptions
object by calling theRequestDispatcherOptions(String)
constructor.- Specified by:
include
in 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:SlingScriptHelper
Helper method to include the result of processing the request for the givenpath
andoptions
. 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:
include
in 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:SlingScriptHelper
Same asSlingScriptHelper.forward(String,RequestDispatcherOptions)
, but using empty options.- Specified by:
forward
in 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:SlingScriptHelper
Helper method to forward the request to a Servlet or script for the givenpath
andrequestDispatcherOptions
. 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
RequestDispatcherOptions
object by calling theRequestDispatcherOptions(String)
constructor.- Specified by:
forward
in 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:SlingScriptHelper
Helper method to forward the request to a Servlet or script for the givenpath
andoptions
. 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:
forward
in 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:SlingScriptHelper
Dispose the helper. This method can be used to clean up the script helper after the script is run.- Specified by:
dispose
in interfaceSlingScriptHelper
- See Also:
SlingScriptHelper.dispose()
-
getService
public <ServiceType> ServiceType getService(java.lang.Class<ServiceType> type)
Description copied from interface:SlingScriptHelper
Lookup 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:
getService
in interfaceSlingScriptHelper
- Type Parameters:
ServiceType
- The type (interface) of the service.- Parameters:
type
- The type (interface) of the service.- Returns:
- The service instance, or
null
if 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 InvalidServiceFilterSyntaxException
Description copied from interface:SlingScriptHelper
Lookup 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:
getServices
in 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 thefilter
string 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:SlingScriptHelper
Same asSlingScriptHelper.forward(Resource,RequestDispatcherOptions)
, but using empty options.- Specified by:
forward
in 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:SlingScriptHelper
Helper method to forward the request to a Servlet or script for the givenresource
andrequestDispatcherOptions
. 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
RequestDispatcherOptions
object by calling theRequestDispatcherOptions(String)
constructor.- Specified by:
forward
in 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:SlingScriptHelper
Helper method to forward the request to a Servlet or script for the givenresource
andoptions
. 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:
forward
in 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:SlingScriptHelper
Same asSlingScriptHelper.include(Resource,RequestDispatcherOptions)
, but using empty options.- Specified by:
include
in 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:SlingScriptHelper
Helper method to include the result of processing the request for the givenresource
andrequestDispatcherOptions
. 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
RequestDispatcherOptions
object by calling theRequestDispatcherOptions(String)
constructor.- Specified by:
include
in 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:SlingScriptHelper
Helper method to include the result of processing the request for the givenresource
andoptions
. 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:
include
in 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)
-
-