Dependency injection
The CKFinder 3 for Java connector uses Spring core modules to manage dependency injection. The CKFinder servlet initializes its own internal application context and performs a path scan of the com.cksource.ckfinder
package to discover and register all the components. If you need to add a custom path to be scanned for components, you can use the scan-path
servlet initialization parameter. To read more about path scan, refer to the Path scan section.
# Registering custom components
The default behavior of the CKFinder connector can be changed and extended by creating custom components. The components should be located in com.cksource.ckfinder
or in any other package scanned by the CKFinder’s internal application context.
It is recommended to register custom components and inject dependencies using JSR-330 standard annotations , i.e. @Named
and @Inject
.
Please note that components registered in this way reside in the CKFinder’s internal application scope, so they may not have direct access to services within your application.
The code listing below presents a simple event listener component that listens for the RequestEvent
and logs the query string of the current HTTP request and the server info from the servlet context.
The component class is marked with the @Named
annotation. Thanks to this, the component will be instantiated and registered in the application context.
@Inject
identifies an injectable field of type ServletContext
. This annotation tells the DI container to inject the servlet context instance to the marked class field.
package com.cksource.ckfinder.listener;
import com.cksource.ckfinder.event.RequestEvent;
import com.cksource.ckfinder.listener.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.servlet.ServletContext;
@Named
public class CustomListener implements Listener<RequestEvent> {
private static final Logger logger = LoggerFactory.getLogger(CustomListener.class);
@Inject
private ServletContext servletContext;
@Override
public void onApplicationEvent(RequestEvent requestEvent) {
logger.info(requestEvent.getRequest().getQueryString());
logger.info(servletContext.getServerInfo());
}
}
# Components registered by CKFinder
Below, you can find a list of components registered by the CKFinder connector in its own internal application context. You can access these components inside your own custom components by injecting them through the constructor parameters or class fields.
Type | Scope | Description |
---|---|---|
Acl |
Request |
Access control list component. This component controls access to resources using rules defined in the accessControl configuration option. |
AclRoleProvider |
Application |
ACL role provider. Returns the role of the current user to be checked against rules defined in the accessControl configuration option. SessionRoleProvider is the default concrete implementation, which returns the value of the session attribute whose name is defined in the roleSessionAttribute configuration option. |
ApplicationContext |
Application |
CKFinder’s internal application context. |
ApplicationEventPublisher |
Application |
Application event publisher. |
Authenticator |
Application |
Authenticator component used to determine whether the current user should be able to access CKFinder. This component should be defined in the host application code. |
BackendFactory |
Request |
Backend factory service that provides concrete instances of Backend file system wrappers defined in the configuration. |
Cache |
Request |
CKFinder’s internal cache. |
CKFinder |
Application |
The main CKFinder component |
CommandFactory |
Application |
The command factory component that provides concrete instances of Command used to handle HTTP requests. |
Config |
Request |
The connector configuration to use in the current HTTP request scope. |
CsrfTokenValidator |
Application |
The validator service used to validate HTTP requests for CSRF tokens. |
FileSystemFactory |
Request |
The factory that provides instances of FileSystem for backends defined in the configuration. |
HttpServletRequest |
Request |
The current HTTP request. |
HttpServletResponse |
Request |
The current HTTP response. |
MessageConverter |
Application |
The component responsible for assembling HTTP responses. |
ResizedImageManager |
Request |
Component responsible for processing resized images. |
ThumbManager |
Request |
The component responsible for processing internal image thumbnails. |
Translator |
Application |
The component responsible for translation of error messages. |
ResourceTypeFactory |
Request |
The factory that provides instances of ResourceType for resurceTypes defined in the configuration. |
ServletContext |
Application |
The CKFinder’s servlet context. |
StaticResourcesHandler |
Application |
The component responsible for serving static resources when the serveStaticResources configuration option is enabled. |
WorkingFolder |
Request |
The request-scoped component that represents the current working directory in CKFinder. |