package com.storedesk.jspwiki; import org.apache.mailet.GenericMailet; import org.apache.mailet.Mail; import org.apache.mailet.MailetConfig; import javax.mail.MessagingException; import com.ecyrd.jspwiki.*; import com.ecyrd.jspwiki.auth.UserProfile; import java.io.*; import java.util.Properties; import java.util.Date; import java.text.MessageFormat; /** * Created by IntelliJ IDEA. * User: geert * Date: Jan 6, 2005 * Time: 12:00:06 PM * To change this template use File | Settings | File Templates. */ public class WikiMailet extends GenericMailet { WikiEngine engine; MessageFormat format; public void init(MailetConfig mailetConfig) throws MessagingException { super.init(mailetConfig); String file = mailetConfig.getInitParameter("config"); String formatPattern = mailetConfig.getInitParameter("format"); if (formatPattern == null){ formatPattern = "{4} / {2} \\\\{0}\r\n\r\n----\r\n\r\n{1}"; } format = new MessageFormat(formatPattern); if (file == null) file = "jspwiki.properties"; // Try to get engine try { // todo custom modification?? //engine = WikiEngine.getInstance(file); InputStream propertyStream = null; propertyStream = new FileInputStream( new File(file) ); Properties props = new Properties( ); //TextUtil.createProperties( DEFAULT_PROPERTIES ) ); props.load( propertyStream ); propertyStream.close(); engine = new WikiEngine(props); } catch (WikiException e) { log("WikiEngine Not Initialized "+e); System.out.println("WikiEngine Not Initialized "+e); } catch (IOException e) { log("WikiEngine Not Initialized "+e); System.out.println("WikiEngine Not Initialized "+e); } } public void service(Mail mail) throws MessagingException { //To change body of implemented methods use File | Settings | File Templates. if (engine != null){ try { String subject = mail.getMessage().getSubject(); String user = mail.getSender().getUser(); String content = getContent(mail); if (content != null){ savePage(subject,user,content); } } catch (MessagingException e) { log("bookmark error "+e); } catch (ClassCastException e){ log("bookmark error "+e); System.out.println("error"+ e); } catch (IOException e) { log("bookmark error "+e); } } } private void savePage(String subject, String user, String content) { String pageName = subject; WikiPage page = engine.getPage(pageName); // System.out.println("found page "+pageName+ ": "+engine.getHTML(pageName)); //content = translate(content); try { String original = engine.getPureText(page); // {4} / {2} \\{0}\r\n\r\n----\r\n\r\n{1} Object[] values = { content, original, user, subject, new Date() }; String newText = format.format(values); WikiContext context = new WikiContext(engine,page); //context.setCurrentUser(new UserProfile()); //todo engine.saveText(context,newText); System.out.println("-----------------------------"); } catch (Exception e) { System.out.println("Save page "+pageName+" failed "+e); log("Save page "+pageName+" failed "+e ); } } private String translate(String content) { return content; } private String getContent(Mail mail) throws MessagingException, IOException { Object o = mail.getMessage().getContent(); if (o instanceof String){ System.out.println("Found text "+o); return (String)o; } else { System.out.println("Not found "+o); } // todo multipart & attachments return null; } }