Java integration SDK
ONLYOFFICE Docs Integration SDK provides common interfaces and default implementations for integrating ONLYOFFICE Docs into your own website or application on Java.
Prerequisites
- Java: version 11.x and higher (download from the Oracle official website);
- Apache Maven (download from the official website).
SDK structure
SDK consists of 5 main managers and 4 services.
Managers
Manager | Default implementation | Description |
---|---|---|
DocumentManager | DefaultDocumentManager (abstract) | This manager is used for working with files and document formats. |
RequestManager | DefaultRequestManager | This manager is used to make requests to the ONLYOFFICE Docs. |
JwtManager | DefaultJwtManager | This manager is used for generating and verifying authorization tokens. |
SettingsManager | DefaultSettingsManager (abstract) | This manager is used to manage integration application settings. |
UrlManager | DefaultUrlManager | This manager is used as a URL provider. |
Services
Service | Default implementation | Description |
---|---|---|
ConfigService | DefaultConfigService | This configuration generation service is used for opening the document editor. |
CallbackService | DefaultCallbackService | This service is used for processing the response of the document server. |
ConvertService | DefaultConvertService | This service is used for converting documents. |
SettingsValidationService | DefaultSettingsValidationService | This service is used for checking document server connection settings. |
Usage
Let's look at the demo example, which shows how the SDK works in a classic Spring web application:
Implement the methods of the abstract DefaultSettingsManager class:
@Override public String getSetting(final String name) { return properties.getProperty(name); } @Override public void setSetting(final String name, final String value) { properties.setProperty(name, value); }
The full example code can be found here.
Implement the methods of the abstract DefaultDocumentManager class:
@Override public String getDocumentKey(final String fileId, final boolean embedded) { return String.valueOf(fileId.hashCode()); } @Override public String getDocumentName(final String fileId) { return "sample.docx"; }
The full example code can be found here.
Implement the UrlManager methods. To open the editor in the editing mode, you need to define:
@Override public String getFileUrl(final String fileId) { return getServerUrl() + "/file/download"; } @Override public String getCallbackUrl(final String fileId) { return getServerUrl() + "/callback"; }
The full example code can be found here.
Initialize the JwtManager and RequestManager Spring beans and add them to the DI container. This allows you to call the registered beans anywhere in the application:
@Bean public JwtManager jwtManager(final SettingsManager settingsManager) { return new DefaultJwtManager(settingsManager); } @Bean public RequestManager requestManager(final UrlManager urlManager, final JwtManager jwtManager, final SettingsManager settingsManager) { return new DefaultRequestManager(urlManager, jwtManager, settingsManager); }
The full example code can be found here.
After this, you can use all available services in their default implementations or by overriding and extending them:
- To use the callback service, create the callback controller where the request body is wrapped in the Callback model and the callback service is called to handle this model.
- To demonstrate the config service work, @GetMapping("/editor") is used in the main controller. When the user opens this address, the editor page is requested. At the same time, the config service is called and generates the Config model to open the editor:
@GetMapping("/editor") public String main(final Model model) throws JsonProcessingException { Config config = configService.createConfig("1", Mode.EDIT, Type.DESKTOP); model.addAttribute("config", config); model.addAttribute("documentServerApiUrl", urlManager.getDocumentServerApiUrl()); return "editor"; }
Create the file controller to get a file for opening in the editor. When the config service generates the config model, the URL for downloading a file is specified in this model. To open the document, the editor sends a request to this URL, and the file controller responds to this request and returns the document.