@Test public void testUnsupportedBlob() throws IOException { String url = Helpers.getWOPIURL(getBaseURL(), "view", binDoc, FILE_CONTENT_PROPERTY); doGet(url, response -> assertEquals(404, response.getStatusLine().getStatusCode())); }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); if (path == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Null path"); return; } path = path.substring(1); String[] parts = path.split("/"); int length = parts.length; if (length < 4) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path: " + path); return; } WOPIService wopiService = Framework.getService(WOPIService.class); if (!wopiService.isEnabled()) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "WOPI not enabled"); return; } String action = parts[0]; String repository = parts[1]; String docId = parts[2]; String xpath = String.join("/", Arrays.asList(parts).subList(3, length)); CoreSession session = CoreInstance.getCoreSession(repository); DocumentRef ref = new IdRef(docId); if (!session.exists(ref)) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Document not found"); return; } DocumentModel doc = session.getDocument(ref); Blob blob = Helpers.getEditableBlob(doc, xpath); if (blob == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "No editable blob on document"); return; } String actionURL = wopiService.getActionURL(blob, action); if (actionURL == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot open file with Office Online"); return; } String token = Helpers.createJWTToken(); request.setAttribute(ACCESS_TOKEN_ATTRIBUTE, token); request.setAttribute(ACCESS_TOKEN_TTL_ATTRIBUTE, Helpers.getJWTTokenExp(token)); String baseURL = VirtualHostHelper.getBaseURL(request); String wopiBaseURL = Framework.getProperty(WOPI_BASE_URL_PROPERTY, baseURL); String fileId = FileInfo.computeFileId(doc, xpath); String wopiSrc = URLEncoder.encode(String.format("%s%s%s", wopiBaseURL, FILES_ENDPOINT_PATH, fileId), UTF_8.name()); request.setAttribute(FORM_URL, String.format("%s%s=%s", actionURL, WOPI_SRC, wopiSrc)); RequestDispatcher requestDispatcher = request.getRequestDispatcher(WOPI_JSP); requestDispatcher.forward(request, response); }
WOPIServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); if (path == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Null path"); return; } path = path.substring(1); String[] parts = path.split("/"); int length = parts.length; if (length < 4) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path: " + path); return; } WOPIService wopiService = Framework.getService(WOPIService.class); if (!wopiService.isEnabled()) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "WOPI not enabled"); return; } String action = parts[0]; String repository = parts[1]; String docId = parts[2]; String xpath = String.join("/", Arrays.asList(parts).subList(3, length)); CoreSession session = CoreInstance.getCoreSession(repository); DocumentRef ref = new IdRef(docId); if (!session.exists(ref)) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Document not found"); return; } DocumentModel doc = session.getDocument(ref); Blob blob = Helpers.getEditableBlob(doc, xpath); if (blob == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "No editable blob on document"); return; } String actionURL = wopiService.getActionURL(blob, action); if (actionURL == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot open file with Office Online"); return; } String token = Helpers.createJWTToken(); request.setAttribute(ACCESS_TOKEN_ATTRIBUTE, token); request.setAttribute(ACCESS_TOKEN_TTL_ATTRIBUTE, Helpers.getJWTTokenExp(token)); String baseURL = VirtualHostHelper.getBaseURL(request); String wopiBaseURL = Framework.getProperty(WOPI_BASE_URL_PROPERTY, baseURL); String fileId = FileInfo.computeFileId(doc, xpath); String wopiSrc = URLEncoder.encode(String.format("%s%s%s", wopiBaseURL, FILES_ENDPOINT_PATH, fileId), UTF_8.name()); request.setAttribute(FORM_URL, String.format("%s%s=%s", actionURL, WOPI_SRC, wopiSrc)); RequestDispatcher requestDispatcher = request.getRequestDispatcher(WOPI_JSP); requestDispatcher.forward(request, response); } }
WOPIServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); if (path == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Null path"); return; } path = path.substring(1); String[] parts = path.split("/"); int length = parts.length; if (length < 4) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path: " + path); return; } WOPIService wopiService = Framework.getService(WOPIService.class); if (!wopiService.isEnabled()) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "WOPI not enabled"); return; } String action = parts[0]; String repository = parts[1]; String docId = parts[2]; String xpath = String.join("/", Arrays.asList(parts).subList(3, length)); CoreSession session = CoreInstance.getCoreSession(repository); DocumentRef ref = new IdRef(docId); if (!session.exists(ref)) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Document not found"); return; } DocumentModel doc = session.getDocument(ref); Blob blob = Helpers.getEditableBlob(doc, xpath); if (blob == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "No editable blob on document"); return; } String actionURL = wopiService.getActionURL(blob, action); if (actionURL == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot open file with Office Online"); return; } String token = Helpers.createJWTToken(); request.setAttribute(ACCESS_TOKEN_ATTRIBUTE, token); request.setAttribute(ACCESS_TOKEN_TTL_ATTRIBUTE, Helpers.getJWTTokenExp(token)); String baseURL = VirtualHostHelper.getBaseURL(request); String wopiBaseURL = Framework.getProperty(WOPI_BASE_URL_PROPERTY, baseURL); String fileId = FileInfo.computeFileId(doc, xpath); String wopiSrc = URLEncoder.encode(String.format("%s%s%s", wopiBaseURL, FILES_ENDPOINT_PATH, fileId), UTF_8.name()); request.setAttribute(FORM_URL, String.format("%s%s=%s", actionURL, WOPI_SRC, wopiSrc)); RequestDispatcher requestDispatcher = request.getRequestDispatcher(WOPI_JSP); requestDispatcher.forward(request, response); }  }
WOPIServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); if (path == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Null path"); return; } path = path.substring(1); String[] parts = path.split("/"); int length = parts.length; if (length < 4) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path: " + path); return; } WOPIService wopiService = Framework.getService(WOPIService.class); if (!wopiService.isEnabled()) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "WOPI not enabled"); return; } String action = parts[0]; String repository = parts[1]; String docId = parts[2]; String xpath = String.join("/", Arrays.asList(parts).subList(3, length)); CoreSession session = CoreInstance.getCoreSession(repository); DocumentRef ref = new IdRef(docId); if (!session.exists(ref)) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Document not found"); return; } DocumentModel doc = session.getDocument(ref); Blob blob = Helpers.getEditableBlob(doc, xpath); if (blob == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "No editable blob on document"); return; } String actionURL = wopiService.getActionURL(blob, action); if (actionURL == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot open file with Office Online"); return; } String token = Helpers.createJWTToken(); request.setAttribute(ACCESS_TOKEN_ATTRIBUTE, token); request.setAttribute(ACCESS_TOKEN_TTL_ATTRIBUTE, Helpers.getJWTTokenExp(token)); String baseURL = VirtualHostHelper.getBaseURL(request); String wopiBaseURL = Framework.getProperty(WOPI_BASE_URL_PROPERTY, baseURL); String fileId = FileInfo.computeFileId(doc, xpath); String wopiSrc = URLEncoder.encode(String.format("%s%s%s", wopiBaseURL, FILES_ENDPOINT_PATH, fileId), UTF_8.name()); request.setAttribute(FORM_URL, String.format("%s%s=%s", actionURL, WOPI_SRC, wopiSrc)); RequestDispatcher requestDispatcher = request.getRequestDispatcher(WOPI_JSP); requestDispatcher.forward(request, response); }   }
WOPIServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); if (path == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Null path"); return; } path = path.substring(1); String[] parts = path.split("/"); int length = parts.length; if (length < 4) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path: " + path); return; } WOPIService wopiService = Framework.getService(WOPIService.class); if (!wopiService.isEnabled()) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "WOPI not enabled"); return; } String action = parts[0]; String repository = parts[1]; String docId = parts[2]; String xpath = String.join("/", Arrays.asList(parts).subList(3, length)); CoreSession session = CoreInstance.getCoreSession(repository); DocumentRef ref = new IdRef(docId); if (!session.exists(ref)) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Document not found"); return; } DocumentModel doc = session.getDocument(ref); Blob blob = Helpers.getEditableBlob(doc, xpath); if (blob == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "No editable blob on document"); return; } String actionURL = wopiService.getActionURL(blob, action); if (actionURL == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot open file with Office Online"); return; } String token = Helpers.createJWTToken(); request.setAttribute(ACCESS_TOKEN_ATTRIBUTE, token); request.setAttribute(ACCESS_TOKEN_TTL_ATTRIBUTE, Helpers.getJWTTokenExp(token)); String baseURL = VirtualHostHelper.getBaseURL(request); String wopiBaseURL = Framework.getProperty(WOPI_BASE_URL_PROPERTY, baseURL); String fileId = FileInfo.computeFileId(doc, xpath); String wopiSrc = URLEncoder.encode(String.format("%s%s%s", wopiBaseURL, FILES_ENDPOINT_PATH, fileId), UTF_8.name()); request.setAttribute(FORM_URL, String.format("%s%s=%s", actionURL, WOPI_SRC, wopiSrc)); RequestDispatcher requestDispatcher = request.getRequestDispatcher(WOPI_JSP); requestDispatcher.forward(request, response); }    }