Pass OnlyOffice API through Tomcat

I want to integrate the OnlyOffice JS API into my web app. In the backend, I’m using Java with Tomcat and Jersey. I want to edit office documents from my web app using OnlyOffice. Since I also want to leverage the security of my web app, I’d like to route the communication through my server. So far, this is working quite well. However, the API establishes a WebSocket connection with the URL path that matches the HTTP requests. I can’t distinguish this using Jersey. It seems that you can’t use the same path for both a WebSocket and HTTP request with Jersey. Can you provide me with any tips on how to handle this? I’m also open to completely different solutions."

My onlyoffice version: 7.0.1-37

That’s my servlet with which I route the HTTP requests.

package de.parceval.portal.platform.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Singleton;

@Singleton
public class OnlyOfficeServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String externalBaseUrl = "http://MY-ONLYOFFICE-SERVER";
			String requestUri = request.getPathInfo();
			String externalURL = externalBaseUrl + requestUri;
			URL url = new URL(externalURL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod(request.getMethod());
			
			Enumeration<String> headerNames = request.getHeaderNames();
			while (headerNames.hasMoreElements()) {
				String headerName = headerNames.nextElement();
				String headerValue = request.getHeader(headerName);
				connection.setRequestProperty(headerName, headerValue);
			}

			InputStream inputStream = connection.getInputStream();
			OutputStream outputStream = response.getOutputStream();
			for (Map.Entry<String, java.util.List<String>> entry : connection.getHeaderFields().entrySet()) {
				String headerName = entry.getKey();
				if (headerName == null || headerName.equals("Transfer-Encoding"))
					continue;
				for (String headerValue : entry.getValue()) {
					response.addHeader(headerName, headerValue);
				}
			}

			byte[] buffer = new byte[1024];
			int bytesRead;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, bytesRead);
			}
			outputStream.flush();
			connection.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

Hello @parceval
Probably these titles will be useful:
ONLYOFFICE Api Documentation - Example - java sample of integration. You can use it as guideline for your own Java integration.
Using ONLYOFFICE Docs behind the proxy - ONLYOFFICE - samples of proxy configuration files.
ONLYOFFICE Api Documentation - How It Works - general information about the integration process.

My onlyoffice version: 7.0.1-37

I would recommend using the latest version of Document server (v.7.4.1). Each new version contains fixes for known bugs and new features: DocumentServer/CHANGELOG.md at master · ONLYOFFICE/DocumentServer · GitHub

As far as I understand, you’re independent integrator, but the situation itself is still unclear to me. Please update Document server to the latest version and provide us with exact issue scenario (you can make screenshots).

Thanks a lot. But it’s much work to unterstand the java example and the example is very buggy and don’t work out of the box :frowning:

Hello @parceval

It should. Please clarify the exact issue you have faced.