use of org.apache.wiki.api.exceptions.ProviderException 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;
}
use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.
the class PageManager method actionPerformed.
/**
* Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
* events. If a user profile's name changes, each page ACL is inspected. If an entry contains
* a name that has changed, it is replaced with the new one. No events are emitted
* as a consequence of this method, because the page contents are still the same; it is
* only the representations of the names within the ACL that are changing.
*
* @param event The event
*/
public void actionPerformed(WikiEvent event) {
if (!(event instanceof WikiSecurityEvent)) {
return;
}
WikiSecurityEvent se = (WikiSecurityEvent) event;
if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
UserProfile[] profiles = (UserProfile[]) se.getTarget();
Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
// Examine each page ACL
try {
int pagesChanged = 0;
Collection pages = getAllPages();
for (Iterator it = pages.iterator(); it.hasNext(); ) {
WikiPage page = (WikiPage) it.next();
boolean aclChanged = changeAcl(page, oldPrincipals, newPrincipal);
if (aclChanged) {
// If the Acl needed changing, change it now
try {
m_engine.getAclManager().setPermissions(page, page.getAcl());
} catch (WikiSecurityException e) {
log.error("Could not change page ACL for page " + page.getName() + ": " + e.getMessage(), e);
}
pagesChanged++;
}
}
log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + pagesChanged + " page ACLs to change also.");
} catch (ProviderException e) {
// Oooo! This is really bad...
log.error("Could not change user name in Page ACLs because of Provider error:" + e.getMessage(), e);
}
}
}
Aggregations