-
Notifications
You must be signed in to change notification settings - Fork 4
ResourceController
A ResourceController services client requests for static system or dynamically generated resources. The requests originate from widget instances that are embedded in a Page or Document in the user’s web browser. Resources requested for could be in image, portable document, video and audio format or any other format supported by the client. When a ResourceController receives a request, it identifies the target resource and streams it back to the requesting client using the OutputStream object passed to its execute() method.
Static resources, like pictures, Javascript files and cascaded style sheet files, which typically do not change are streamed from the server’s file system, database or application jar files. The framework provides a set of concrete ResourceController classes like FileResourceController, RealPathResourceController, DownloadPathResourceController and ClassLoaderResourceController, for fetching file resources and also the classes like ScopeResourceController and FileAttachmentResourceController that stream data from memory from different application scopes.
To improve on the response time of retrieving a resource, some of the file resource controller implementations can be configured to cache frequently accessed static resources in-memory for some specified period of time.
Generally, resource controllers are non-singletons; that is a new instance is created for every client request.
Every resource controller must implement the ResourceController component interface. The interface has methods that are manipulated by the framework at runtime to service requests for resources.
- The setContentType() and getContentType() methods are used for specifying the expected and the returned MIME string constant for the resource type respectively.
- The setResourceName() is used by the request processing mechanism to set the name of the resource, the target resource, that the requesting client is expecting.
- The getMetaDataKeys() returns a set of string keys for meta-data that is associated with retrieved resource. The response mechanism uses these keys to get the actual meta-data values by iteratively calling the controller’s getMetaData() method with the keys supplied as parameters.
- The prepareExecution() method is called just before the primary execute() method of the controller. Here the controller is expected analyze the target resource, pull its meta-data and prepare it for streaming.
- The execute() method extracts the target resource and streams it to the OutputStream object that is supplied to it.
You do not necessarily have to implement all of these methods. The framework provides a convenient AbstractResourceController class which your implementation class can extend. The abstract class implements all ResourceController interface methods except for the prepareExecution() and execute() methods. It also provides utility methods for obtaining the target resource name and preparing the resource meta-data.
Listing 1: Simple File Resource Controller
@Component(name = "/resource/simplefile")
public class SimpleFileResourceController extends AbstractResourceController {
private File file;
public SimpleFileResourceController() {
super(true);// Secure: client must be logged in
}
@Override
public void prepareExecution() throws UnifyException {
// Do preparation here.
// Get File object and prepare meta-data
setContentDisposition(getResourceName());
file = new File(getResourceName());
if (file.exists()) {
setContentLength(file.length());
}
setContentType("application/octet-stream");
}
@Override
public void execute(OutputStream outputStream) throws UnifyException {
// Extract and atream resource
if (file.exists()) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
IOUtils.writeAll(outputStream, inputStream);
} catch (FileNotFoundException e) {
throwOperationErrorException(e);
} finally {
IOUtils.close(inputStream);
}
}
}
}- Unify Container
- Unify Component
- Unify Page Language
- Core Components
-
Web Components
-
Basic Widgets
- AssignmentBox
- Button
- CheckBox
- CheckList
- DateField
- DecimalField
- DropdownCheckList
- DynamicField
- FileAttachment
- FileDownload
- FileUpload
- Image
- IntegerField
- Label
- MoneyField
- MultiDynamic
- MultiSelect
- NameField
- PasswordField
- Picture
- Rack
- RadioButtons
- SearchField
- SingleSelect
- Table
- TextArea
- TextField
- TimeField
- WordField
- Container Widgets
- Layout
-
Controllers
- PageController
- ResourceController
- RemoteCallController
- PlainController
-
Basic Widgets