/** {@inheritDoc} */ public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); String uri = request.getRequestURI(); // Locate the temporary file. 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"); } // Write temporary file into response. response.setDateHeader("Last-Modified", tempFile.lastModified()); String contentType = MimeTypes.OCTET_STREAM; try { contentType = tika.detect(tempFile); } catch (IOException ex) { LOG.warn( String.format( "Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), ex); } response.setContentType(contentType); 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 public String render(XWikiContext context) throws XWikiException { XWikiRequest request = context.getRequest(); XWikiResponse response = context.getResponse(); XWikiDocument doc = context.getDoc(); String path = request.getRequestURI(); String filename = Util.decodeURI(getFileName(path, ACTION_NAME), context); XWikiAttachment attachment = null; final String idStr = request.getParameter("id"); if (StringUtils.isNumeric(idStr)) { int id = Integer.parseInt(idStr); if (doc.getAttachmentList().size() > id) { attachment = doc.getAttachmentList().get(id); } } else { attachment = doc.getAttachment(filename); } if (attachment == null) { Object[] args = {filename}; throw new XWikiException( XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment {0} not found", null, args); } XWikiPluginManager plugins = context.getWiki().getPluginManager(); attachment = plugins.downloadAttachment(attachment, context); // Try to load the attachment content just to make sure that the attachment really exists // This will throw an exception if the attachment content isn't available try { attachment.getContentSize(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; } // Sending the content of the attachment if (request.getHeader(RANGE_HEADER_NAME) != null) { try { if (sendPartialContent(attachment, request, response, context)) { return null; } } catch (IOException ex) { // Broken response... } } sendContent(attachment, request, response, filename, context); return null; }