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();
}
}
}