diff --git a/ChangeLog b/ChangeLog index fc11de07..80e7a41a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ +12-JUL-2019: 10.9.6 + +- Adds local files, Google Drive in Office Add-in + 11-JUL-2019: 10.9.5 +- Improves error handling for saving files - Fixes desktop startup 11-JUL-2019: 10.9.4 diff --git a/VERSION b/VERSION index 785719b5..9e7c47cf 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -10.9.5 \ No newline at end of file +10.9.6 \ No newline at end of file diff --git a/src/main/java/com/mxgraph/online/AbsAuthServlet.java b/src/main/java/com/mxgraph/online/AbsAuthServlet.java new file mode 100644 index 00000000..fca25177 --- /dev/null +++ b/src/main/java/com/mxgraph/online/AbsAuthServlet.java @@ -0,0 +1,176 @@ +/** + * Copyright (c) 2006-2019, JGraph Ltd + */ +package com.mxgraph.online; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.URL; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@SuppressWarnings("serial") +abstract public class AbsAuthServlet extends HttpServlet +{ + + static public class Config + { + public String DEV_CLIENT_SECRET = null, CLIENT_SECRET = null, DEV_CLIENT_ID = null, CLIENT_ID = null, + DEV_REDIRECT_URI = null, REDIRECT_URI = null, AUTH_SERVICE_URL = null; + } + + protected Config getConfig() + { + return null; + } + + protected String processAuthResponse(String authRes, boolean jsonResponse) + { + return ""; + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) + */ + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException + { + String code = request.getParameter("code"); + String refreshToken = request.getParameter("refresh_token"); + Config CONFIG = getConfig(); + String secret, client, redirectUri; + + if ("127.0.0.1".equals(request.getServerName())) + { + secret = CONFIG.DEV_CLIENT_SECRET; + client = CONFIG.DEV_CLIENT_ID; + redirectUri = CONFIG.DEV_REDIRECT_URI; + } + else + { + secret = CONFIG.CLIENT_SECRET; + client = CONFIG.CLIENT_ID; + redirectUri = CONFIG.REDIRECT_URI; + } + + if (code == null && refreshToken == null) + { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + else + { + HttpURLConnection con = null; + + try + { + String url = CONFIG.AUTH_SERVICE_URL; + URL obj = new URL(url); + con = (HttpURLConnection) obj.openConnection(); + + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + + boolean jsonResponse = false; + StringBuilder urlParameters = new StringBuilder(); + + urlParameters.append("client_id="); + urlParameters.append(client); + urlParameters.append("&redirect_uri="); + urlParameters.append(redirectUri); + urlParameters.append("&client_secret="); + urlParameters.append(secret); + + if (code != null) + { + urlParameters.append("&code="); + urlParameters.append(code); + urlParameters.append("&grant_type=authorization_code"); + } + else + { + urlParameters.append("&refresh_token="); + urlParameters.append(refreshToken); + urlParameters.append("&grant_type=refresh_token"); + jsonResponse = true; + } + + // Send post request + con.setDoOutput(true); + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); + wr.writeBytes(urlParameters.toString()); + wr.flush(); + wr.close(); + + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer authRes = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) + { + authRes.append(inputLine); + } + in.close(); + + response.setStatus(con.getResponseCode()); + + OutputStream out = response.getOutputStream(); + + PrintWriter writer = new PrintWriter(out); + + // Writes JavaScript code + writer.println(processAuthResponse(authRes.toString(), jsonResponse)); + + writer.flush(); + writer.close(); + } + catch(IOException e) + { + e.printStackTrace(); + + if (con != null) + { + try + { + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getErrorStream())); + + String inputLine; + + while ((inputLine = in.readLine()) != null) + { + System.err.println(inputLine); + } + in.close(); + } + catch (Exception e2) + { + // Ignore + } + } + + if (e.getMessage().contains("401")) + { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + else + { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } + catch (Exception e) + { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } + } + +} diff --git a/src/main/java/com/mxgraph/online/GoogleAuthServlet.java b/src/main/java/com/mxgraph/online/GoogleAuthServlet.java new file mode 100644 index 00000000..7de08c88 --- /dev/null +++ b/src/main/java/com/mxgraph/online/GoogleAuthServlet.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2006-2019, JGraph Ltd + */ +package com.mxgraph.online; + +import java.io.IOException; + +@SuppressWarnings("serial") +public class GoogleAuthServlet extends AbsAuthServlet +{ + public static String CLIENT_SECRET_FILE_PATH = "/WEB-INF/google_client_secret"; + public static String CLIENT_ID_FILE_PATH = "/WEB-INF/google_client_id"; + private static Config CONFIG = null; + + protected Config getConfig() + { + if (CONFIG == null) + { + CONFIG = new Config(); + + try + { + CONFIG.CLIENT_SECRET = Utils + .readInputStream(getServletContext() + .getResourceAsStream(CLIENT_SECRET_FILE_PATH)) + .replaceAll("\n", ""); + CONFIG.DEV_CLIENT_SECRET = CONFIG.CLIENT_SECRET; + } + catch (IOException e) + { + throw new RuntimeException("Client secret path invalid"); + } + + try + { + CONFIG.CLIENT_ID = Utils + .readInputStream(getServletContext() + .getResourceAsStream(CLIENT_ID_FILE_PATH)) + .replaceAll("\n", ""); + CONFIG.DEV_CLIENT_ID = CONFIG.CLIENT_ID; + } + catch (IOException e) + { + throw new RuntimeException("Client ID path invalid"); + } + + CONFIG.AUTH_SERVICE_URL = "https://www.googleapis.com/oauth2/v4/token"; + CONFIG.DEV_REDIRECT_URI = "https://test.draw.io/google"; + CONFIG.REDIRECT_URI = "https://www.draw.io/google"; + } + + return CONFIG; + } + + protected String processAuthResponse(String authRes, boolean jsonResponse) + { + StringBuffer res = new StringBuffer(); + + //Call the opener callback function directly with the given json + if (!jsonResponse) + { + res.append("
"); + res.append(""); + res.append(""); + res.append(""); + } + + return res.toString(); + } +} diff --git a/src/main/java/com/mxgraph/online/MSGraphAuthServlet.java b/src/main/java/com/mxgraph/online/MSGraphAuthServlet.java index b174cd04..27a6c093 100644 --- a/src/main/java/com/mxgraph/online/MSGraphAuthServlet.java +++ b/src/main/java/com/mxgraph/online/MSGraphAuthServlet.java @@ -3,92 +3,27 @@ */ package com.mxgraph.online; -import java.io.BufferedReader; -import java.io.DataOutputStream; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.PrintWriter; -import java.net.HttpURLConnection; -import java.net.URL; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") -public class MSGraphAuthServlet extends HttpServlet +public class MSGraphAuthServlet extends AbsAuthServlet { - - /** - * - */ - public static final String DEV_CLIENT_SECRET_FILE_PATH = "/WEB-INF/msgraph_dev_client_secret"; - - /** - * - */ - private static String DEV_CLIENT_SECRET = null; - - /** - * - */ - public static final String CLIENT_SECRET_FILE_PATH = "/WEB-INF/msgraph_client_secret"; - - /** - * - */ - private static String CLIENT_SECRET = null; - - /** - * - */ - public static final String DEV_CLIENT_ID_FILE_PATH = "/WEB-INF/msgraph_dev_client_id"; - - /** - * - */ - private static String DEV_CLIENT_ID = null; - - /** - * - */ - public static final String CLIENT_ID_FILE_PATH = "/WEB-INF/msgraph_client_id"; - - /** - * - */ - private static String CLIENT_ID = null; - - /** - * - */ - public static final String DEV_REDIRECT_URI = "https://test.draw.io/microsoft"; - - /** - * - */ - public static final String REDIRECT_URI = "https://www.draw.io/microsoft"; - - /** - * @see HttpServlet#HttpServlet() - */ - public MSGraphAuthServlet() + public static String DEV_CLIENT_SECRET_FILE_PATH = "/WEB-INF/msgraph_dev_client_secret"; + public static String CLIENT_SECRET_FILE_PATH = "/WEB-INF/msgraph_client_secret"; + public static String DEV_CLIENT_ID_FILE_PATH = "/WEB-INF/msgraph_dev_client_id"; + public static String CLIENT_ID_FILE_PATH = "/WEB-INF/msgraph_client_id"; + + private static Config CONFIG = null; + + protected Config getConfig() { - super(); - } - - /** - * Loads the key. - */ - protected void updateKeys() - { - if (DEV_CLIENT_SECRET == null) + if (CONFIG == null) { + CONFIG = new Config(); + try { - DEV_CLIENT_SECRET = Utils + CONFIG.DEV_CLIENT_SECRET = Utils .readInputStream(getServletContext() .getResourceAsStream(DEV_CLIENT_SECRET_FILE_PATH)) .replaceAll("\n", ""); @@ -97,13 +32,10 @@ public class MSGraphAuthServlet extends HttpServlet { throw new RuntimeException("Dev client secret path invalid."); } - } - - if (CLIENT_SECRET == null) - { + try { - CLIENT_SECRET = Utils + CONFIG.CLIENT_SECRET = Utils .readInputStream(getServletContext() .getResourceAsStream(CLIENT_SECRET_FILE_PATH)) .replaceAll("\n", ""); @@ -112,13 +44,10 @@ public class MSGraphAuthServlet extends HttpServlet { throw new RuntimeException("Client secret path invalid."); } - } - - if (DEV_CLIENT_ID == null) - { + try { - DEV_CLIENT_ID = Utils + CONFIG.DEV_CLIENT_ID = Utils .readInputStream(getServletContext() .getResourceAsStream(DEV_CLIENT_ID_FILE_PATH)) .replaceAll("\n", ""); @@ -127,13 +56,10 @@ public class MSGraphAuthServlet extends HttpServlet { throw new RuntimeException("Dev client ID invalid."); } - } - - if (CLIENT_ID == null) - { + try { - CLIENT_ID = Utils + CONFIG.CLIENT_ID = Utils .readInputStream(getServletContext() .getResourceAsStream(CLIENT_ID_FILE_PATH)) .replaceAll("\n", ""); @@ -142,139 +68,40 @@ public class MSGraphAuthServlet extends HttpServlet { throw new RuntimeException("Client ID invalid."); } + + CONFIG.DEV_REDIRECT_URI = "https://test.draw.io/microsoft"; + CONFIG.REDIRECT_URI = "https://www.draw.io/microsoft"; + CONFIG.AUTH_SERVICE_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token"; } - } + + return CONFIG; + } - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) - */ - protected void doGet(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException + protected String processAuthResponse(String authRes, boolean jsonResponse) { - String code = request.getParameter("code"); - String refreshToken = request.getParameter("refresh_token"); - updateKeys(); - String secret, client, redirectUri; + StringBuffer res = new StringBuffer(); - if ("127.0.0.1".equals(request.getServerName())) + //Call the opener callback function directly with the given json + if (!jsonResponse) { - secret = DEV_CLIENT_SECRET; - client = DEV_CLIENT_ID; - redirectUri = DEV_REDIRECT_URI; - } - else - { - secret = CLIENT_SECRET; - client = CLIENT_ID; - redirectUri = REDIRECT_URI; + res.append(""); - } - - response.setStatus(con.getResponseCode()); - - OutputStream out = response.getOutputStream(); - - PrintWriter writer = new PrintWriter(out); - - // Writes JavaScript code - writer.println(res.toString()); - - writer.flush(); - writer.close(); - } - catch(IOException e) - { - if (e.getMessage().contains("401")) - { - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - } - else - { - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } - catch (Exception e) - { - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } + res.append(";"); + res.append("if (window.opener != null && window.opener.onOneDriveCallback != null)"); + res.append("{"); + res.append(" window.opener.onOneDriveCallback(authInfo, window);"); + res.append("} else {"); + res.append(" Office.initialize = function () { Office.context.ui.messageParent(JSON.stringify(authInfo));}"); + res.append("}"); + res.append("