use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class AttachmentServlet method doGet.
/**
* Serves a GET with two parameters: 'wikiname' specifying the wikiname
* of the attachment, 'version' specifying the version indicator.
*/
// FIXME: Messages would need to be localized somehow.
@Override
public void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
final Context context = Wiki.context().create(m_engine, req, ContextEnum.PAGE_ATTACH.getRequestContext());
final AttachmentManager mgr = m_engine.getManager(AttachmentManager.class);
final AuthorizationManager authmgr = m_engine.getManager(AuthorizationManager.class);
final String version = req.getParameter(HDR_VERSION);
final String nextPage = req.getParameter("nextpage");
final String page = context.getPage().getName();
int ver = WikiProvider.LATEST_VERSION;
if (page == null) {
log.info("Invalid attachment name.");
res.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try (final OutputStream out = res.getOutputStream()) {
log.debug("Attempting to download att " + page + ", version " + version);
if (version != null) {
ver = Integer.parseInt(version);
}
final Attachment att = mgr.getAttachmentInfo(page, ver);
if (att != null) {
//
// Check if the user has permission for this attachment
//
final Permission permission = PermissionFactory.getPagePermission(att, "view");
if (!authmgr.checkPermission(context.getWikiSession(), permission)) {
log.debug("User does not have permission for this");
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
//
if (HttpUtil.checkFor304(req, att.getName(), att.getLastModified())) {
log.debug("Client has latest version already, sending 304...");
res.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
final String mimetype = getMimeType(context, att.getFileName());
res.setContentType(mimetype);
//
// We use 'inline' instead of 'attachment' so that user agents
// can try to automatically open the file.
//
res.addHeader("Content-Disposition", "inline; filename=\"" + att.getFileName() + "\";");
res.addDateHeader("Last-Modified", att.getLastModified().getTime());
if (!att.isCacheable()) {
res.addHeader("Pragma", "no-cache");
res.addHeader("Cache-control", "no-cache");
}
// If a size is provided by the provider, report it.
if (att.getSize() >= 0) {
// log.info("size:"+att.getSize());
res.setContentLength((int) att.getSize());
}
try (final InputStream in = mgr.getAttachmentStream(context, att)) {
int read;
final byte[] buffer = new byte[BUFFER_SIZE];
while ((read = in.read(buffer)) > -1) {
out.write(buffer, 0, read);
}
}
log.debug("Attachment {} sent to {} on {}", att.getFileName(), req.getRemoteUser(), HttpUtil.getRemoteAddress(req));
if (nextPage != null) {
res.sendRedirect(validateNextPage(TextUtil.urlEncodeUTF8(nextPage), m_engine.getURL(ContextEnum.WIKI_ERROR.getRequestContext(), "", null)));
}
} else {
final String msg = "Attachment '" + page + "', version " + ver + " does not exist.";
log.info(msg);
res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
}
} catch (final ProviderException pe) {
log.debug("Provider failed while reading", pe);
//
// This might fail, if the response is already committed. So in that
// case we just log it.
//
sendError(res, "Provider error: " + pe.getMessage());
} catch (final NumberFormatException nfe) {
log.warn("Invalid version number: " + version);
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid version number");
} catch (final SocketException se) {
//
// These are very common in download situations due to aggressive
// clients. No need to try and send an error.
//
log.debug("I/O exception during download", se);
} catch (final IOException ioe) {
//
// Client dropped the connection or something else happened.
// We don't know where the error came from, so we'll at least
// try to send an error and catch it quietly if it doesn't quite work.
//
log.debug("I/O exception during download", ioe);
sendError(res, "Error: " + ioe.getMessage());
}
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class AttachmentServlet method executeUpload.
/**
* @param context the wiki context
* @param data the input stream data
* @param filename the name of the file to upload
* @param errorPage the place to which you want to get a redirection
* @param parentPage the page to which the file should be attached
* @param changenote The change note
* @param contentLength The content length
* @return <code>true</code> if upload results in the creation of a new page;
* <code>false</code> otherwise
* @throws RedirectException If the content needs to be redirected
* @throws IOException If there is a problem in the upload.
* @throws ProviderException If there is a problem in the backend.
*/
protected boolean executeUpload(final Context context, final InputStream data, String filename, final String errorPage, final String parentPage, final String changenote, final long contentLength) throws RedirectException, IOException, ProviderException {
boolean created = false;
try {
filename = AttachmentManager.validateFileName(filename);
} catch (final WikiException e) {
// here we have the context available, so we can internationalize it properly :
throw new RedirectException(Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE).getString(e.getMessage()), errorPage);
}
if (!context.hasAdminPermissions()) {
if (contentLength > m_maxSize) {
// FIXME: Does not delete the received files.
throw new RedirectException("File exceeds maximum size (" + m_maxSize + " bytes)", errorPage);
}
if (!isTypeAllowed(filename)) {
throw new RedirectException("Files of this type may not be uploaded to this wiki", errorPage);
}
}
final Principal user = context.getCurrentUser();
final AttachmentManager mgr = m_engine.getManager(AttachmentManager.class);
log.debug("file=" + filename);
if (data == null) {
log.error("File could not be opened.");
throw new RedirectException("File could not be opened.", errorPage);
}
// Check whether we already have this kind of page. If the "page" parameter already defines an attachment
// name for an update, then we just use that file. Otherwise, we create a new attachment, and use the
// filename given. Incidentally, this will also mean that if the user uploads a file with the exact
// same name than some other previous attachment, then that attachment gains a new version.
Attachment att = mgr.getAttachmentInfo(context.getPage().getName());
if (att == null) {
att = new org.apache.wiki.attachment.Attachment(m_engine, parentPage, filename);
created = true;
}
att.setSize(contentLength);
// Check if we're allowed to do this?
final Permission permission = PermissionFactory.getPagePermission(att, "upload");
if (m_engine.getManager(AuthorizationManager.class).checkPermission(context.getWikiSession(), permission)) {
if (user != null) {
att.setAuthor(user.getName());
}
if (changenote != null && !changenote.isEmpty()) {
att.setAttribute(Page.CHANGENOTE, changenote);
}
try {
m_engine.getManager(AttachmentManager.class).storeAttachment(att, data);
} catch (final ProviderException pe) {
// here we have the context available, so we can internationalize it properly :
throw new ProviderException(Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE).getString(pe.getMessage()));
}
log.info("User " + user + " uploaded attachment to " + parentPage + " called " + filename + ", size " + att.getSize());
} else {
throw new RedirectException("No permission to upload a file", errorPage);
}
return created;
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class DefaultAclManager method getPermissions.
/**
* {@inheritDoc}
*/
@Override
public Acl getPermissions(final Page page) {
// Does the page already have cached ACLs?
Acl acl = page.getAcl();
log.debug("page=" + page.getName() + "\n" + acl);
if (acl == null) {
// If null, try the parent.
if (page instanceof Attachment) {
final Page parent = m_engine.getManager(PageManager.class).getPage(((Attachment) page).getParentName());
acl = getPermissions(parent);
} else {
// Or, try parsing the page
final Context ctx = Wiki.context().create(m_engine, page);
ctx.setVariable(Context.VAR_EXECUTE_PLUGINS, Boolean.FALSE);
m_engine.getManager(RenderingManager.class).getHTML(ctx, page);
if (page.getAcl() == null) {
page.setAcl(Wiki.acls().acl());
}
acl = page.getAcl();
}
}
return acl;
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class TestEngine method addAttachment.
/**
* Adds an attachment to a page for testing purposes.
*
* @param pageName page name
* @param attachmentName attachment name
* @param data attachment data
*/
public void addAttachment(final String pageName, final String attachmentName, final byte[] data) throws ProviderException, IOException {
final Attachment att = Wiki.contents().attachment(this, pageName, attachmentName);
getManager(AttachmentManager.class).storeAttachment(att, new ByteArrayInputStream(data));
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class JSPWikiMarkupParserTest method testAttachmentLink.
@Test
public void testAttachmentLink() throws Exception {
newPage("Test");
final Attachment att = Wiki.contents().attachment(testEngine, "Test", "TestAtt.txt");
att.setAuthor("FirstPost");
testEngine.getManager(AttachmentManager.class).storeAttachment(att, testEngine.makeAttachmentFile());
final String src = "This should be an [attachment link|Test/TestAtt.txt]";
Assertions.assertEquals("This should be an <a class=\"attachment\" href=\"/test/attach/Test/TestAtt.txt\">attachment link</a>" + "<a href=\"/test/PageInfo.jsp?page=Test/TestAtt.txt\" class=\"infolink\"><img src=\"/test/images/attachment_small.png\" border=\"0\" alt=\"(info)\" /></a>", translate(src));
}
Aggregations