org.apache.sling.scripting.core
Class ScriptHelper

java.lang.Object
  extended by org.apache.sling.scripting.core.ScriptHelper
All Implemented Interfaces:
SlingScriptHelper

public class ScriptHelper
extends 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 global sling variable. Client code using this object should take care to call cleanup() when the object is not used anymore!


Field Summary
protected  org.osgi.framework.BundleContext bundleContext
          The bundle context.
protected  List<org.osgi.framework.ServiceReference> references
          The list of references - we don't need to synchronize this as we are running in one single request.
protected  Map<String,Object> services
          A map of found services.
 
Constructor Summary
ScriptHelper(org.osgi.framework.BundleContext ctx, SlingScript script)
           
ScriptHelper(org.osgi.framework.BundleContext ctx, SlingScript script, SlingHttpServletRequest request, SlingHttpServletResponse response)
           
 
Method Summary
 void cleanup()
          Clean up this instance.
 void dispose()
          Deprecated. 
 void forward(Resource resource)
          Same as SlingScriptHelper.forward(Resource,RequestDispatcherOptions), but using empty options.
 void forward(Resource resource, RequestDispatcherOptions options)
          Helper method to forward the request to a Servlet or script for the given resource and options.
 void forward(Resource resource, String options)
          Helper method to forward the request to a Servlet or script for the given resource and requestDispatcherOptions.
 void forward(String path)
          Same as SlingScriptHelper.forward(String,RequestDispatcherOptions), but using empty options.
 void forward(String path, RequestDispatcherOptions options)
          Helper method to forward the request to a Servlet or script for the given path and options.
 void forward(String path, String options)
          Helper method to forward the request to a Servlet or script for the given path and requestDispatcherOptions.
 SlingHttpServletRequest getRequest()
          Returns the SlingHttpServletRequest representing the input of the request.
 SlingHttpServletResponse getResponse()
          Returns the SlingHttpServletResponse representing the output of the request.
 SlingScript getScript()
          Returns the SlingScript being called to handle the request.
<ServiceType>
ServiceType
getService(Class<ServiceType> type)
          Lookup a single service
<ServiceType>
ServiceType[]
getServices(Class<ServiceType> serviceType, String filter)
          Lookup one or several services
 void include(Resource resource)
          Same as SlingScriptHelper.include(Resource,RequestDispatcherOptions), but using empty options.
 void include(Resource resource, RequestDispatcherOptions options)
          Helper method to include the result of processing the request for the given resource and options.
 void include(Resource resource, String options)
          Helper method to include the result of processing the request for the given resource and requestDispatcherOptions.
 void include(String path)
          Same as SlingScriptHelper.include(String,RequestDispatcherOptions), but using empty options.
 void include(String path, RequestDispatcherOptions options)
          Helper method to include the result of processing the request for the given path and options.
 void include(String path, String options)
          Helper method to include the result of processing the request for the given path and requestDispatcherOptions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bundleContext

protected final org.osgi.framework.BundleContext bundleContext
The bundle context.


references

protected List<org.osgi.framework.ServiceReference> references
The list of references - we don't need to synchronize this as we are running in one single request.


services

protected Map<String,Object> services
A map of found services.

Constructor Detail

ScriptHelper

public ScriptHelper(org.osgi.framework.BundleContext ctx,
                    SlingScript script)

ScriptHelper

public ScriptHelper(org.osgi.framework.BundleContext ctx,
                    SlingScript script,
                    SlingHttpServletRequest request,
                    SlingHttpServletResponse response)
Method Detail

getScript

public SlingScript getScript()
Description copied from interface: SlingScriptHelper
Returns the SlingScript being called to handle the request.

Specified by:
getScript in interface SlingScriptHelper
See Also:
SlingScriptHelper.getScript()

getRequest

public SlingHttpServletRequest getRequest()
Description copied from interface: SlingScriptHelper
Returns the SlingHttpServletRequest representing the input of the request.

Specified by:
getRequest in interface SlingScriptHelper
See Also:
SlingScriptHelper.getRequest()

getResponse

public SlingHttpServletResponse getResponse()
Description copied from interface: SlingScriptHelper
Returns the SlingHttpServletResponse representing the output of the request.

Specified by:
getResponse in interface SlingScriptHelper
See Also:
SlingScriptHelper.getResponse()

include

public void include(String path)
Description copied from interface: SlingScriptHelper
Same as SlingScriptHelper.include(String,RequestDispatcherOptions), but using empty options.

Specified by:
include in interface SlingScriptHelper
See Also:
SlingScriptHelper.include(java.lang.String)

include

public void include(String path,
                    String options)
Description copied from interface: SlingScriptHelper
Helper method to include the result of processing the request for the given path and requestDispatcherOptions. 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 the RequestDispatcherOptions.RequestDispatcherOptions(String) constructor.

Specified by:
include in interface SlingScriptHelper
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(String path,
                    RequestDispatcherOptions options)
Description copied from interface: SlingScriptHelper
Helper method to include the result of processing the request for the given path and options. 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 interface SlingScriptHelper
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(String path)
Description copied from interface: SlingScriptHelper
Same as SlingScriptHelper.forward(String,RequestDispatcherOptions), but using empty options.

Specified by:
forward in interface SlingScriptHelper
See Also:
SlingScriptHelper.forward(java.lang.String)

forward

public void forward(String path,
                    String options)
Description copied from interface: SlingScriptHelper
Helper method to forward the request to a Servlet or script for the given path and requestDispatcherOptions. 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 the RequestDispatcherOptions.RequestDispatcherOptions(String) constructor.

Specified by:
forward in interface SlingScriptHelper
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(String path,
                    RequestDispatcherOptions options)
Description copied from interface: SlingScriptHelper
Helper method to forward the request to a Servlet or script for the given path and options. 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 interface SlingScriptHelper
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 interface SlingScriptHelper
See Also:
SlingScriptHelper.dispose()

getService

public <ServiceType> ServiceType getService(Class<ServiceType> type)
Description copied from interface: SlingScriptHelper
Lookup a single service

Specified by:
getService in interface SlingScriptHelper
Parameters:
type - The type (interface) of the service.
Returns:
The service instance, or null if the service is not available.
See Also:
SlingScriptHelper.getService(java.lang.Class)

getServices

public <ServiceType> ServiceType[] getServices(Class<ServiceType> serviceType,
                                               String filter)
                          throws InvalidServiceFilterSyntaxException
Description copied from interface: SlingScriptHelper
Lookup one or several services

Specified by:
getServices in interface SlingScriptHelper
Parameters:
serviceType - The type (interface) of the service.
filter - An optional filter (LDAP-like, see OSGi spec)
Returns:
The services object or null.
Throws:
InvalidServiceFilterSyntaxException - If the filter 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 as SlingScriptHelper.forward(Resource,RequestDispatcherOptions), but using empty options.

Specified by:
forward in interface SlingScriptHelper
See Also:
SlingScriptHelper.forward(org.apache.sling.api.resource.Resource)

forward

public void forward(Resource resource,
                    String options)
Description copied from interface: SlingScriptHelper
Helper method to forward the request to a Servlet or script for the given resource and requestDispatcherOptions. 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 the RequestDispatcherOptions.RequestDispatcherOptions(String) constructor.

Specified by:
forward in interface SlingScriptHelper
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 given resource and options. 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 interface SlingScriptHelper
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 as SlingScriptHelper.include(Resource,RequestDispatcherOptions), but using empty options.

Specified by:
include in interface SlingScriptHelper
See Also:
SlingScriptHelper.forward(org.apache.sling.api.resource.Resource)

include

public void include(Resource resource,
                    String options)
Description copied from interface: SlingScriptHelper
Helper method to include the result of processing the request for the given resource and requestDispatcherOptions. 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 the RequestDispatcherOptions.RequestDispatcherOptions(String) constructor.

Specified by:
include in interface SlingScriptHelper
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 given resource and options. 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 interface SlingScriptHelper
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)


Copyright © 2007-2011 The Apache Software Foundation. All Rights Reserved.