@Test public void renderNormalBehavior() throws Exception { XWikiRequest request = mock(XWikiRequest.class); XWikiResponse response = mock(XWikiResponse.class); BufferOutputStream out = new BufferOutputStream(); oldcore.getXWikiContext().setRequest(request); oldcore.getXWikiContext().setResponse(response); when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/file.txt"); when(response.getOutputStream()).thenReturn(out); oldcore.getXWikiContext().setWikiId("wiki"); createFile("temp/module/wiki/Space/Page/file.txt", "Hello World!"); action.render(oldcore.getXWikiContext()); assertArrayEquals("Hello World!".getBytes(), out.getContentsAsByteArray()); verify(response, never()).addHeader("Content-disposition", "attachment; filename*=utf-8''file.txt"); }
@Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); String uri = request.getRequestURI(); File tempFile = getTemporaryFile(uri, context); if (null == tempFile) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "Invalid temporary resource URL"); } response.setDateHeader("Last-Modified", tempFile.lastModified()); String contentType = MimeTypes.OCTET_STREAM; try { contentType = TikaUtils.detect(tempFile); } catch (IOException ex) { LOGGER.warn( String.format("Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), ex); } response.setContentType(contentType); if ("1".equals(request.getParameter("force-download"))) { String fileName = StringUtils.defaultIfBlank(request.getParameter("force-filename"), tempFile.getName()); fileName = Util.encodeURI(fileName, context).replaceAll("\\+", "%20"); response.addHeader("Content-disposition", "attachment; filename*=utf-8''" + fileName); } try { response.setContentLength((int) tempFile.length()); IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream()); } catch (IOException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e); } return null; }
TempResourceAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); String uri = request.getRequestURI(); File tempFile = getTemporaryFile(uri, context); if (null == tempFile) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "Invalid temporary resource URL"); } response.setDateHeader("Last-Modified", tempFile.lastModified()); String contentType = MimeTypes.OCTET_STREAM; try { contentType = TikaUtils.detect(tempFile); } catch (IOException ex) { LOGGER.warn( String.format("Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), ex); } response.setContentType(contentType); if ("1".equals(request.getParameter("force-download"))) { String fileName = StringUtils.defaultIfBlank(request.getParameter("force-filename"), tempFile.getName()); fileName = Util.encodeURI(fileName, context).replaceAll("\\+", "%20"); response.addHeader("Content-disposition", "attachment; filename*=utf-8''" + fileName); } try { response.setContentLength((int) tempFile.length()); IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream()); } catch (IOException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e); } return null; } }
TempResourceAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); String uri = request.getRequestURI(); File tempFile = getTemporaryFile(uri, context); if (null == tempFile) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "Invalid temporary resource URL"); } response.setDateHeader("Last-Modified", tempFile.lastModified()); String contentType = MimeTypes.OCTET_STREAM; try { contentType = TikaUtils.detect(tempFile); } catch (IOException ex) { LOGGER.warn( String.format("Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), ex); } response.setContentType(contentType); if ("1".equals(request.getParameter("force-download"))) { String fileName = StringUtils.defaultIfBlank(request.getParameter("force-filename"), tempFile.getName()); fileName = Util.encodeURI(fileName, context).replaceAll("\\+", "%20"); response.addHeader("Content-disposition", "attachment; filename*=utf-8''" + fileName); } try { response.setContentLength((int) tempFile.length()); IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream()); } catch (IOException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e); } return null; }  }
TempResourceAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); String uri = request.getRequestURI(); File tempFile = getTemporaryFile(uri, context); if (null == tempFile) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "Invalid temporary resource URL"); } response.setDateHeader("Last-Modified", tempFile.lastModified()); String contentType = MimeTypes.OCTET_STREAM; try { contentType = TikaUtils.detect(tempFile); } catch (IOException ex) { LOGGER.warn( String.format("Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), ex); } response.setContentType(contentType); if ("1".equals(request.getParameter("force-download"))) { String fileName = StringUtils.defaultIfBlank(request.getParameter("force-filename"), tempFile.getName()); fileName = Util.encodeURI(fileName, context).replaceAll("\\+", "%20"); response.addHeader("Content-disposition", "attachment; filename*=utf-8''" + fileName); } try { response.setContentLength((int) tempFile.length()); IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream()); } catch (IOException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e); } return null; }  @Override String render(XWikiContext context); }
TempResourceAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); String uri = request.getRequestURI(); File tempFile = getTemporaryFile(uri, context); if (null == tempFile) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "Invalid temporary resource URL"); } response.setDateHeader("Last-Modified", tempFile.lastModified()); String contentType = MimeTypes.OCTET_STREAM; try { contentType = TikaUtils.detect(tempFile); } catch (IOException ex) { LOGGER.warn( String.format("Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), ex); } response.setContentType(contentType); if ("1".equals(request.getParameter("force-download"))) { String fileName = StringUtils.defaultIfBlank(request.getParameter("force-filename"), tempFile.getName()); fileName = Util.encodeURI(fileName, context).replaceAll("\\+", "%20"); response.addHeader("Content-disposition", "attachment; filename*=utf-8''" + fileName); } try { response.setContentLength((int) tempFile.length()); IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream()); } catch (IOException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e); } return null; }  @Override String render(XWikiContext context); static final Pattern URI_PATTERN; }