WikiAttachment
paul s <[email protected]> Sat, 04 Nov 2006 19:46:32 -0500
| Newsgroups | gmane.comp.java.webmacro.user |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------040206050601090004080800
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
so attached is a servlet that handles attachments for a page. everything
works great however i can't get at the current WikiSystem object. any
thoughts? reason being new page creation doesn't seem to load into the
current WikiSystem and i would imagine that i am writing the new page to
a new WikiSystem by going through a servlet. any help would be appreciated.
as for lucene when i updated the add.doc fields for 2.0 i had them
un_tockenized, that seems to be working now.
it seems i can't delete a admin user, reagular users i can delete and
add and change passwords...
cheers
paul
hi everyone - long time... has the WikiAttachment been implemented? if
so, how? code snip? similarly with lucene, indexing works, but the find
textbox doesn't seem to use the index. had an opportunity to roll out a
fresh one and its running under tomcat5. there also seems to be a bug in
the update password? willing to help sort these out...
cheers
paul
--------------040206050601090004080800
Content-Type: text/x-java;
name="SavePageMulitpart.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="SavePageMulitpart.java"
package org.tcdi.opensource.wiki.servlet;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.tcdi.opensource.wiki.Wiki;
import org.tcdi.opensource.wiki.WikiAttachment;
import org.tcdi.opensource.wiki.WikiPage;
import org.tcdi.opensource.wiki.WikiSystem;
import org.tcdi.opensource.wiki.WikiUser;
import org.tcdi.opensource.wiki.WikiUtil;
import org.webmacro.servlet.WebContext;
/**
* Saves the WikiPage that the user modified. If the page doesn't exist, it is
* created. Otherwise, the existing page is saved as <PageName>.<oldversion>
* and this page is saved in its place as <PageName>.
*
* @author e_ridge
*/
public class SavePageMulitpart extends HttpServlet implements Servlet {
private WikiServlet ws = new WikiServlet();
private WikiSystem _wiki;
public void init(ServletConfig arg0) throws ServletException {
super.init(arg0);
try {
ws.init(arg0);
_wiki = new Wiki( this.getInitParameter("properties") );
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
WebContext ctx = ws.getWebContext(req, res);
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
HashMap FormField = new HashMap();
List attachments = new ArrayList();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
FormField.put(item.getFieldName(), item.getString());
} else {
long sizeInBytes = item.getSize();
if (sizeInBytes > 0) {
attachments.add(item);
}
}
}
ctx.put("Form", FormField);
ctx.put("PageAttachments", attachments);
String pageName = getWikiPageName(_wiki, ctx);
WikiPage wikiPage = _wiki.getPage( pageName );
WikiUser user = getUser(ctx);
stuffContext(ctx, wikiPage, user, pageName);
perform(_wiki, ctx, user, wikiPage);
ctx.getResponse().sendRedirect(pageName);
// throw new PageAction.RedirectException(pageName);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getWikiPageName(WikiSystem wiki, WebContext ctx) {
String pageName = (String) ((HashMap) ctx.get("Form")).get("save");
pageName = WikiUtil.formatAsWikiTitle(pageName);
return pageName;
}
/**
* can only save a page if the request is POST and "?save=<pagename>" is in
* the request
*/
public boolean accept(WikiSystem wiki, WebContext wc, WikiUser user) {
// and then only accept if this is a get request
return wc.getRequest().getMethod().equalsIgnoreCase("POST") && wc.getForm("save") != null;
}
/**
* do the saving of the page. When we're done, we redirect to the page so
* the user can view his changes.
*/
public void perform(WikiSystem wiki, WebContext wc, WikiUser user, WikiPage page) throws PageAction.PageActionException {
if (page != null && page.getIsModerated() && !user.getIsModerator()) {
System.out.println("This page can only be saved by moderators");
}
try {
if (page == null)
page = createNewPage(wiki, wc, user);
else
modifyExistingPage(wiki, wc, user, page);
} catch (Exception e) {
e.printStackTrace();
throw new PageAction.PageActionException(e.toString());
}
}
protected WikiPage createNewPage(WikiSystem wiki, WebContext ctx, WikiUser user) throws Exception {
// get the page elements from the request
String text = (String) ((HashMap) ctx.get("Form")).get("TEXT");
String editor = user.getIdentifier();
boolean moderated = ((HashMap) ctx.get("Form")).get("MODERATED") != null && ((HashMap) ctx.get("Form")).get("MODERATED").toString().equals("true");
String keywords = (String) ((HashMap) ctx.get("Form")).get("RELATED_TITLES");
String pageName = getWikiPageName(_wiki, ctx);
// create the page
WikiPage newPage = wiki.createPage(pageName, editor, text);
newPage.setRelatedTitles(keywordsToStringArray(keywords));
newPage.setIsModerated(moderated);
String dir = _wiki.getProperties().getProperty ("PageStore.Attachments").trim();
File f = new File(dir);
if(!f.canRead()) { f.mkdir(); }
List attachments = (List) ctx.get("PageAttachments");
for (Iterator iterator = attachments.iterator(); iterator.hasNext();) {
FileItem item = (FileItem) iterator.next();
WikiAttachment wa = new WikiAttachment(item.getName(), item.getContentType(), item.getName(), pageName);
newPage.addAttachment( wa );
item.write(new File(dir + wa.getIdentifier()));
}
// make sure to save the page
wiki.savePage(newPage);
return newPage;
}
protected void modifyExistingPage(WikiSystem wiki, WebContext ctx, WikiUser user, WikiPage page) throws Exception {
// get the page elements from the request
String text = (String) ((HashMap) ctx.get("Form")).get("TEXT");
String editor = user.getIdentifier();
boolean moderated = ((HashMap) ctx.get("Form")).get("MODERATED") != null && ((HashMap) ctx.get("Form")).get("MODERATED").toString().equals("true");
String keywords = (String) ((HashMap) ctx.get("Form")).get("RELATED_TITLES");
String pageName = getWikiPageName(_wiki, ctx);
// make sure the page wasn't modified by somebody else
// System.out.println("==================" + (String) ((HashMap) ctx.get("Form")).get("VERSION"));
long version = Long.parseLong((String) ((HashMap) ctx.get("Form")).get("VERSION"));
if (version != page.getVersion())
throw new ConcurrentModificationException(text);
page.addEditor(editor);
page.setUnparsedData(text);
page.setIsModerated(moderated);
page.setRelatedTitles(keywordsToStringArray(keywords));
String dir = _wiki.getProperties().getProperty ("PageStore.Attachments").trim();
File f = new File(dir);
if(!f.canRead()) { f.mkdir(); }
List attachments = (List) ctx.get("PageAttachments");
for (Iterator iterator = attachments.iterator(); iterator.hasNext();) {
FileItem item = (FileItem) iterator.next();
WikiAttachment wa = new WikiAttachment(item.getName(), item.getContentType(), item.getName(), pageName);
page.addAttachment( wa );
item.write(new File(dir + wa.getIdentifier()));
}
// parse the page and save it
wiki.parsePage(page);
wiki.savePage(page);
}
private String[] keywordsToStringArray(String input) {
List titles = new ArrayList();
StringTokenizer st = new StringTokenizer(input);
StringBuffer keyword = new StringBuffer();
while (st.hasMoreElements()) {
String tmp = (String) st.nextElement();
int size = tmp.length();
for (int x = 0; x < size; x++) {
char ch = tmp.charAt(x);
if (Character.isLetterOrDigit(ch))
keyword.append(ch);
}
titles.add(keyword.toString());
keyword.setLength(0);
}
return (String[]) titles.toArray(new String[0]);
}
/**
* stuff the context with objects required by most templates
* @param user
*/
public void stuffContext (WebContext wc, WikiPage page, WikiUser user, String pageName) {
wc.put ("Wiki", _wiki);
wc.put ("WikiUtil", WikiUtil.getInstance());
wc.put ("Renderer", _wiki.getPageRenderer());
wc.put ("Page", page);
wc.put ("User", user);
wc.put ("PageName", pageName);
}
/**
* @return a WikiUser object from cookie. null if no cookie or if user not found
*/
private WikiUser getUser (WebContext wc) {
try {
Cookie cookie = wc.getCookie(_wiki.getProperties().getProperty ("CookieName").trim());
String uid = null;
String password = null;
if (cookie != null) {
String val = cookie.getValue();
int idx = val.indexOf('|');
uid = val.substring(0, idx);
password = val.substring(idx+1);
}
WikiUser user = (WikiUser) ((uid == null) ? null : _wiki.getUser(uid));
if (user != null) {
// update the last accessed attribute for this user
user.setAttribute("LastAccessed", new Date().toString());
user.setAttribute("IPAddress", wc.getRequest().getRemoteAddr());
}
return user;
}
catch (Exception e) {
return null;
}
}
}
--------------040206050601090004080800
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
--------------040206050601090004080800
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Webmacro-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/webmacro-user
--------------040206050601090004080800--