use of org.apache.wiki.api.exceptions.RedirectException in project jspwiki by apache.
the class BugReportHandler method execute.
/**
* {@inheritDoc}
*/
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
String title;
String description;
String version;
String submitter = null;
SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATEFORMAT);
ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
title = params.get(PARAM_TITLE);
description = params.get(PARAM_DESCRIPTION);
version = params.get(PARAM_VERSION);
Principal wup = context.getCurrentUser();
if (wup != null) {
submitter = wup.getName();
}
if (title == null)
throw new PluginException(rb.getString("bugreporthandler.titlerequired"));
if (title.length() == 0)
return "";
if (description == null)
description = "";
if (version == null)
version = "unknown";
Properties mappings = parseMappings(params.get(PARAM_MAPPINGS));
try {
StringWriter str = new StringWriter();
PrintWriter out = new PrintWriter(str);
Date d = new Date();
//
// Outputting of basic data
//
out.println("|" + mappings.getProperty(PARAM_TITLE, "Title") + "|" + title);
out.println("|" + mappings.getProperty("date", "Date") + "|" + format.format(d));
out.println("|" + mappings.getProperty(PARAM_VERSION, "Version") + "|" + version);
if (submitter != null) {
out.println("|" + mappings.getProperty("submitter", "Submitter") + "|" + submitter);
}
//
for (Iterator<Map.Entry<String, String>> i = params.entrySet().iterator(); i.hasNext(); ) {
Map.Entry<String, String> entry = i.next();
if (entry.getKey().equals(PARAM_TITLE) || entry.getKey().equals(PARAM_DESCRIPTION) || entry.getKey().equals(PARAM_VERSION) || entry.getKey().equals(PARAM_MAPPINGS) || entry.getKey().equals(PARAM_PAGE) || entry.getKey().startsWith("_")) {
// Ignore this
} else {
//
// If no mapping has been defined, just ignore
// it.
//
String head = mappings.getProperty(entry.getKey(), entry.getKey());
if (head.length() > 0) {
out.println("|" + head + "|" + entry.getValue());
}
}
}
out.println();
out.println(description);
out.close();
//
// Now create a new page for this bug report
//
String pageName = findNextPage(context, title, params.get(PARAM_PAGE));
WikiPage newPage = new WikiPage(context.getEngine(), pageName);
WikiContext newContext = (WikiContext) context.clone();
newContext.setPage(newPage);
context.getEngine().saveText(newContext, str.toString());
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(rb.getString("bugreporthandler.new"));
String[] args = { "<a href=\"" + context.getViewURL(pageName) + "\">" + pageName + "</a>" };
return formatter.format(args);
} catch (RedirectException e) {
log.info("Saving not allowed, reason: '" + e.getMessage() + "', can't redirect to " + e.getRedirect());
throw new PluginException("Saving not allowed, reason: " + e.getMessage());
} catch (WikiException e) {
log.error("Unable to save page!", e);
return rb.getString("bugreporthandler.unable");
}
}
use of org.apache.wiki.api.exceptions.RedirectException 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(WikiContext context, InputStream data, String filename, String errorPage, String parentPage, String changenote, long contentLength) throws RedirectException, IOException, ProviderException {
boolean created = false;
try {
filename = AttachmentManager.validateFileName(filename);
} catch (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);
}
}
Principal user = context.getCurrentUser();
AttachmentManager mgr = m_engine.getAttachmentManager();
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 a 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 Attachment(m_engine, parentPage, filename);
created = true;
}
att.setSize(contentLength);
//
// Check if we're allowed to do this?
//
Permission permission = PermissionFactory.getPagePermission(att, "upload");
if (m_engine.getAuthorizationManager().checkPermission(context.getWikiSession(), permission)) {
if (user != null) {
att.setAuthor(user.getName());
}
if (changenote != null && changenote.length() > 0) {
att.setAttribute(WikiPage.CHANGENOTE, changenote);
}
try {
m_engine.getAttachmentManager().storeAttachment(att, data);
} catch (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.exceptions.RedirectException in project jspwiki by apache.
the class AttachmentServlet method upload.
/**
* Uploads a specific mime multipart input set, intercepts exceptions.
*
* @param req The servlet request
* @return The page to which we should go next.
* @throws RedirectException If there's an error and a redirection is needed
* @throws IOException If upload fails
* @throws FileUploadException
*/
protected String upload(HttpServletRequest req) throws RedirectException, IOException {
String msg = "";
String attName = "(unknown)";
// If something bad happened, Upload should be able to take care of most stuff
String errorPage = m_engine.getURL(WikiContext.ERROR, "", null, false);
String nextPage = errorPage;
String progressId = req.getParameter("progressid");
// Check that we have a file upload request
if (!ServletFileUpload.isMultipartContent(req)) {
throw new RedirectException("Not a file upload", errorPage);
}
try {
FileItemFactory factory = new DiskFileItemFactory();
// Create the context _before_ Multipart operations, otherwise
// strict servlet containers may fail when setting encoding.
WikiContext context = m_engine.createContext(req, WikiContext.ATTACH);
UploadListener pl = new UploadListener();
m_engine.getProgressManager().startProgress(pl, progressId);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
if (!context.hasAdminPermissions()) {
upload.setFileSizeMax(m_maxSize);
}
upload.setProgressListener(pl);
List<FileItem> items = upload.parseRequest(req);
String wikipage = null;
String changeNote = null;
// FileItem actualFile = null;
List<FileItem> fileItems = new java.util.ArrayList<FileItem>();
for (FileItem item : items) {
if (item.isFormField()) {
if (item.getFieldName().equals("page")) {
//
// FIXME: Kludge alert. We must end up with the parent page name,
// if this is an upload of a new revision
//
wikipage = item.getString("UTF-8");
int x = wikipage.indexOf("/");
if (x != -1)
wikipage = wikipage.substring(0, x);
} else if (item.getFieldName().equals("changenote")) {
changeNote = item.getString("UTF-8");
if (changeNote != null) {
changeNote = TextUtil.replaceEntities(changeNote);
}
} else if (item.getFieldName().equals("nextpage")) {
nextPage = validateNextPage(item.getString("UTF-8"), errorPage);
}
} else {
fileItems.add(item);
}
}
if (fileItems.size() == 0) {
throw new RedirectException("Broken file upload", errorPage);
} else {
for (FileItem actualFile : fileItems) {
String filename = actualFile.getName();
long fileSize = actualFile.getSize();
InputStream in = actualFile.getInputStream();
try {
executeUpload(context, in, filename, nextPage, wikipage, changeNote, fileSize);
} finally {
IOUtils.closeQuietly(in);
}
}
}
} catch (ProviderException e) {
msg = "Upload failed because the provider failed: " + e.getMessage();
log.warn(msg + " (attachment: " + attName + ")", e);
throw new IOException(msg);
} catch (IOException e) {
// Show the submit page again, but with a bit more
// intimidating output.
msg = "Upload failure: " + e.getMessage();
log.warn(msg + " (attachment: " + attName + ")", e);
throw e;
} catch (FileUploadException e) {
// Show the submit page again, but with a bit more
// intimidating output.
msg = "Upload failure: " + e.getMessage();
log.warn(msg + " (attachment: " + attName + ")", e);
throw new IOException(msg, e);
} finally {
m_engine.getProgressManager().stopProgress(progressId);
// FIXME: In case of exceptions should absolutely
// remove the uploaded file.
}
return nextPage;
}
Aggregations