Skip to content

Commit

Permalink
support URL Query Options for OnlyOffice, logging
Browse files Browse the repository at this point in the history
Issue #122
  • Loading branch information
rsoika committed Jul 9, 2022
1 parent 0f7ad3f commit 63e1b33
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 44 deletions.
1 change: 1 addition & 0 deletions imixs-adapters-wopi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ To setup the Imixs-WOPI Adapter the following environment variables must be set:
| WOPI_PUBLIC_ENDPOINT | Public client endpoint to be called from the Web Browser to open the office application. This endpoint should be SSL encrypted in production |https://openoffice.foo.com
| WOPI_HOST_ENDPOINT | Internal Wopi Host endpoint is called by the Wopi Client to fetch and store file data. This endpoint should not be public accessible | http://my-app:8080/api/wopi/
| WOPI_DISCOVERY_ENDPOINT | Internal discovery endpoint used by the Wopi Host implementation to resolve the public wopi endpoint dynamically. | http://localhost:9980/hosting/discovery
| WOPI_OPTIONS | Optional list of URL query params | e.g. for a dark theme in OnlyOffice: "&thm=2"
| WOPI_FILE_EXTENSIONS | Optional comma separated list of file extensions to be supported. |.odt,.doc,.docx,.ods,.xls,.xlsx,.ppt,.pptx|
| WOPI_FILE_CACHE | file path to cache wopi files temporarily on the wop host | default: /tmp/wopi/
| WOPI_POSTMESSAGEORIGIN | Optional postMessageOrigin | e.g. http://application.foo.com:8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void init() {
} catch (SAXException | IOException | ParserConfigurationException e) {
logger.severe("Failed to parse discovery endpoint '" + wopiDiscoveryEndpoint.get() + "' Error: "
+ e.getMessage());
e.printStackTrace();
// e.printStackTrace();
extensions = null;
mimeTypes = null;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ private String resolvePublicEndpoint(String uri) {
result = publicEndpoint + internalFile;
logger.fine("resolved public Endpint: " + result);
} catch (MalformedURLException e) {
e.printStackTrace();
// e.printStackTrace();
}

} else {
Expand Down Expand Up @@ -433,47 +433,55 @@ void parseDiscoveryURL(String endpoint)

logger.info("...parsing wopi.discovery.endpoint: " + endpoint);

if (endpoint.startsWith("http://")) {
logger.fine("...WOPI Client is running without SSL - this is not recommended for production!");
}

extensions = new HashMap<String, String>();
mimeTypes = new HashMap<String, String>();

// parse the discovery URL
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(endpoint).openStream());

// parse all <app> nodes
NodeList appList = doc.getElementsByTagName("app");
for (int i = 0; i < appList.getLength(); i++) {
Node appNode = appList.item(i);

if (appNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) appNode;
String appName = eElement.getAttribute("name");
logger.finest("...app=" + appName);
// now get all action urls...
NodeList actionElements = eElement.getElementsByTagName("action");
for (int j = 0; j < actionElements.getLength(); j++) {
Node actionNode = actionElements.item(j);
if (actionNode.getNodeType() == Node.ELEMENT_NODE) {
Element eActionElement = (Element) actionNode;
String actionExt = eActionElement.getAttribute("ext");
String actionName = eActionElement.getAttribute("name");
String actionurlsrc = eActionElement.getAttribute("urlsrc");

if (actionExt != null && !actionExt.isEmpty()) {
extensions.put(actionExt, actionurlsrc);
logger.finest("...ext=" + actionExt + " -> " + actionurlsrc);
} else {
// this can be a mimetype...
if (appName.contains("/")) {
mimeTypes.put(appName, actionurlsrc);
logger.finest("...mimetype=" + appName + " -> " + actionurlsrc);
try {
// parse the discovery URL
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(endpoint).openStream());

// parse all <app> nodes
NodeList appList = doc.getElementsByTagName("app");
for (int i = 0; i < appList.getLength(); i++) {
Node appNode = appList.item(i);

if (appNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) appNode;
String appName = eElement.getAttribute("name");
logger.finest("...app=" + appName);
// now get all action urls...
NodeList actionElements = eElement.getElementsByTagName("action");
for (int j = 0; j < actionElements.getLength(); j++) {
Node actionNode = actionElements.item(j);
if (actionNode.getNodeType() == Node.ELEMENT_NODE) {
Element eActionElement = (Element) actionNode;
String actionExt = eActionElement.getAttribute("ext");
String actionName = eActionElement.getAttribute("name");
String actionurlsrc = eActionElement.getAttribute("urlsrc");

if (actionExt != null && !actionExt.isEmpty()) {
extensions.put(actionExt, actionurlsrc);
logger.finest("...ext=" + actionExt + " -> " + actionurlsrc);
} else {
// this can be a mimetype...
if (appName.contains("/")) {
mimeTypes.put(appName, actionurlsrc);
logger.finest("...mimetype=" + appName + " -> " + actionurlsrc);
}
}
}
}
}

}
}
} catch (java.net.MalformedURLException e) {
logger.warning(e.getMessage());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public class WopiController implements Serializable {
@ConfigProperty(name = "wopi.file.extensions", defaultValue = ".odt,.doc,.docx,.docm,.rtf,.ods,.xls,.xlsx,.odp,.ppt,.pptx,.odg,.dxf,.emf,.wmf,.vsd,.vsdx")
String wopiFileExtensions;

@Inject
@ConfigProperty(name = "wopi.options", defaultValue = "none")
String wopiOptions;

@Inject
WopiAccessHandler wopiAccessHandler;

Expand Down Expand Up @@ -166,6 +170,12 @@ public String getWopiAccessURL(String uniqueid, String file, String userid, Stri
if (baseURL.indexOf("&<rs=")>-1) {
baseURL=baseURL.substring(0,baseURL.indexOf("&<rs="));
}

// add optional WOPI Params (e.g. thm=2
if (wopiOptions != null && !"none".equals(wopiOptions)) {
baseURL=baseURL+"&"+wopiOptions;
}


// test file extension
String[] extensions = wopiFileExtensions.split(",");
Expand All @@ -190,12 +200,9 @@ public String getWopiAccessURL(String uniqueid, String file, String userid, Stri
}

String token = generateAccessToken(userid, username);
baseURL = baseURL + "WOPISrc=" + wopiHostEndpoint + uniqueid + "/files/" + file + "?access_token=" + token;
if (baseURL.startsWith("http://")) {
logger.fine("...WOPI Client is running without SSL - this is not recommended for production!");
}

logger.info("WOP Access URL=" + baseURL);
baseURL = baseURL + "WOPISrc=" + wopiHostEndpoint + uniqueid + "/files/" + file + "&access_token=" + token;

logger.fine("WOP Access URL=" + baseURL);
return baseURL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public Response getFileInfo(@PathParam("uniqueid") String uniqueid, @PathParam("
return Response.status(Response.Status.NOT_FOUND).build();
}

logger.info("...... GET getFileInfo: " + uniqueid + "/" + file);
logger.info("......GET getFileInfo: " + uniqueid + "/" + file);


// create the json object
Expand Down Expand Up @@ -227,7 +227,7 @@ public Response getFileContents(@PathParam("uniqueid") String uniqueid, @PathPar
}

// load the FileData
logger.info("...... GET getFileContents: " + uniqueid + "/" + file);
logger.info("......GET getFileContents: " + uniqueid + "/" + file);
FileData fileData = loadFileData(uniqueid, file, accessToken);
if (fileData == null) {
logger.warning("no file data found '" + uniqueid + "'!");
Expand Down Expand Up @@ -269,7 +269,7 @@ public Response getFileContents(@PathParam("uniqueid") String uniqueid, @PathPar
public Response postFileContents(@PathParam("uniqueid") String uniqueid, @PathParam("file") String file,
InputStream contentStream, @QueryParam("access_token") String accessToken, @Context UriInfo info) {

logger.info("...... POST postFileContents: " + uniqueid + "/" + file);
logger.info("......POST postFileContents: " + uniqueid + "/" + file);
// analyze header X-LOOL-WOPI-Timestamp, X-LOOL-WOPI-IsAutosave, X-LOOL-WOPI-IsExitSave
// We do ignroe the X-LOOL-WOPI-IsExitSave event
String wopiHeader=servletRequest.getHeader("X-LOOL-WOPI-IsExitSave");
Expand Down

0 comments on commit 63e1b33

Please sign in to comment.