@Test public void downloadWhenOutsideRestRange() throws XWikiException, IOException { Date d = new Date(); createAttachment(d, DEFAULT_FILE_NAME); setRequestExpectations(DEFAULT_URI, null, null, "bytes=129-", -1L, DEFAULT_FILE_NAME); assertNull(this.action.render(this.oldcore.getXWikiContext())); verify(this.response).setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); }
@Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); XWikiDocument doc = context.getDoc(); String filename = getFileName(); XWikiAttachment attachment = getAttachment(request, doc, filename); Map<String, Object> backwardCompatibilityContextObjects = null; if (attachment == null) { Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context); if (result == null) { throwNotFoundException(filename); } XWikiDocument backwardCompatibilityDocument = result.getLeft(); attachment = result.getRight(); backwardCompatibilityContextObjects = new HashMap<>(); pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference()); } try { XWikiPluginManager plugins = context.getWiki().getPluginManager(); attachment = plugins.downloadAttachment(attachment, context); if (attachment == null) { throwNotFoundException(filename); } try { attachment.getContentLongSize(context); } catch (XWikiException e) { Object[] args = { filename }; throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args); } long lastModifiedOnClient = request.getDateHeader("If-Modified-Since"); long lastModifiedOnServer = attachment.getDate().getTime(); if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return null; } if (request.getHeader(RANGE_HEADER_NAME) != null) { try { if (sendPartialContent(attachment, request, response, context)) { return null; } } catch (IOException ex) { } } sendContent(attachment, request, response, context); return null; } finally { if (backwardCompatibilityContextObjects != null) { popDocumentFromContext(backwardCompatibilityContextObjects); } } }
DownloadAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); XWikiDocument doc = context.getDoc(); String filename = getFileName(); XWikiAttachment attachment = getAttachment(request, doc, filename); Map<String, Object> backwardCompatibilityContextObjects = null; if (attachment == null) { Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context); if (result == null) { throwNotFoundException(filename); } XWikiDocument backwardCompatibilityDocument = result.getLeft(); attachment = result.getRight(); backwardCompatibilityContextObjects = new HashMap<>(); pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference()); } try { XWikiPluginManager plugins = context.getWiki().getPluginManager(); attachment = plugins.downloadAttachment(attachment, context); if (attachment == null) { throwNotFoundException(filename); } try { attachment.getContentLongSize(context); } catch (XWikiException e) { Object[] args = { filename }; throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args); } long lastModifiedOnClient = request.getDateHeader("If-Modified-Since"); long lastModifiedOnServer = attachment.getDate().getTime(); if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return null; } if (request.getHeader(RANGE_HEADER_NAME) != null) { try { if (sendPartialContent(attachment, request, response, context)) { return null; } } catch (IOException ex) { } } sendContent(attachment, request, response, context); return null; } finally { if (backwardCompatibilityContextObjects != null) { popDocumentFromContext(backwardCompatibilityContextObjects); } } } }
DownloadAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); XWikiDocument doc = context.getDoc(); String filename = getFileName(); XWikiAttachment attachment = getAttachment(request, doc, filename); Map<String, Object> backwardCompatibilityContextObjects = null; if (attachment == null) { Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context); if (result == null) { throwNotFoundException(filename); } XWikiDocument backwardCompatibilityDocument = result.getLeft(); attachment = result.getRight(); backwardCompatibilityContextObjects = new HashMap<>(); pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference()); } try { XWikiPluginManager plugins = context.getWiki().getPluginManager(); attachment = plugins.downloadAttachment(attachment, context); if (attachment == null) { throwNotFoundException(filename); } try { attachment.getContentLongSize(context); } catch (XWikiException e) { Object[] args = { filename }; throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args); } long lastModifiedOnClient = request.getDateHeader("If-Modified-Since"); long lastModifiedOnServer = attachment.getDate().getTime(); if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return null; } if (request.getHeader(RANGE_HEADER_NAME) != null) { try { if (sendPartialContent(attachment, request, response, context)) { return null; } } catch (IOException ex) { } } sendContent(attachment, request, response, context); return null; } finally { if (backwardCompatibilityContextObjects != null) { popDocumentFromContext(backwardCompatibilityContextObjects); } } } DownloadAction(); }
DownloadAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); XWikiDocument doc = context.getDoc(); String filename = getFileName(); XWikiAttachment attachment = getAttachment(request, doc, filename); Map<String, Object> backwardCompatibilityContextObjects = null; if (attachment == null) { Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context); if (result == null) { throwNotFoundException(filename); } XWikiDocument backwardCompatibilityDocument = result.getLeft(); attachment = result.getRight(); backwardCompatibilityContextObjects = new HashMap<>(); pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference()); } try { XWikiPluginManager plugins = context.getWiki().getPluginManager(); attachment = plugins.downloadAttachment(attachment, context); if (attachment == null) { throwNotFoundException(filename); } try { attachment.getContentLongSize(context); } catch (XWikiException e) { Object[] args = { filename }; throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args); } long lastModifiedOnClient = request.getDateHeader("If-Modified-Since"); long lastModifiedOnServer = attachment.getDate().getTime(); if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return null; } if (request.getHeader(RANGE_HEADER_NAME) != null) { try { if (sendPartialContent(attachment, request, response, context)) { return null; } } catch (IOException ex) { } } sendContent(attachment, request, response, context); return null; } finally { if (backwardCompatibilityContextObjects != null) { popDocumentFromContext(backwardCompatibilityContextObjects); } } } DownloadAction(); @Override String render(XWikiContext context); }
DownloadAction extends XWikiAction { @Override public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); XWikiDocument doc = context.getDoc(); String filename = getFileName(); XWikiAttachment attachment = getAttachment(request, doc, filename); Map<String, Object> backwardCompatibilityContextObjects = null; if (attachment == null) { Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context); if (result == null) { throwNotFoundException(filename); } XWikiDocument backwardCompatibilityDocument = result.getLeft(); attachment = result.getRight(); backwardCompatibilityContextObjects = new HashMap<>(); pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference()); } try { XWikiPluginManager plugins = context.getWiki().getPluginManager(); attachment = plugins.downloadAttachment(attachment, context); if (attachment == null) { throwNotFoundException(filename); } try { attachment.getContentLongSize(context); } catch (XWikiException e) { Object[] args = { filename }; throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args); } long lastModifiedOnClient = request.getDateHeader("If-Modified-Since"); long lastModifiedOnServer = attachment.getDate().getTime(); if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return null; } if (request.getHeader(RANGE_HEADER_NAME) != null) { try { if (sendPartialContent(attachment, request, response, context)) { return null; } } catch (IOException ex) { } } sendContent(attachment, request, response, context); return null; } finally { if (backwardCompatibilityContextObjects != null) { popDocumentFromContext(backwardCompatibilityContextObjects); } } } DownloadAction(); @Override String render(XWikiContext context); static final String ACTION_NAME; static final String ATTACHMENT; static final List<String> MIMETYPE_WHITELIST; static final String WHITELIST_PROPERTY; static final String BLACKLIST_PROPERTY; }