Java Source Code: com.ecyrd.jspwiki.rss.AtomFeed


   1: package com.ecyrd.jspwiki.rss;
   2: 
   3: import java.io.IOException;
   4: import java.io.StringWriter;
   5: import java.util.ArrayList;
   6: import java.util.Collection;
   7: import java.util.Date;
   8: import java.util.Iterator;
   9: 
  10: import javax.servlet.ServletContext;
  11: 
  12: import org.apache.commons.lang.time.DateFormatUtils;
  13: import org.jdom.Element;
  14: import org.jdom.Namespace;
  15: import org.jdom.output.Format;
  16: import org.jdom.output.XMLOutputter;
  17: 
  18: import com.ecyrd.jspwiki.Release;
  19: import com.ecyrd.jspwiki.WikiContext;
  20: import com.ecyrd.jspwiki.WikiEngine;
  21: import com.ecyrd.jspwiki.WikiPage;
  22: import com.ecyrd.jspwiki.attachment.Attachment;
  23: import com.ecyrd.jspwiki.providers.ProviderException;
  24: 
  25: /**
  26:  *  Provides an Atom 1.0 standard feed, with enclosures.
  27:  *  
  28:  * @author jalkanen
  29:  *
  30:  */
  31: public class AtomFeed extends Feed
  32:	  {
  33:    private Namespace m_atomNameSpace = Namespace.getNamespace("http://www.w3.org/2005/Atom");
  34:    
  35:    public static final String RFC3339FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZ";
  36:    
  37:    public AtomFeed( WikiContext c )
  38:	      {
  39:        super(c);
  40:    }
  41:    
  42:    /**
  43:     *   This is a bit complicated right now, as there is no proper metadata
  44:     *   store in JSPWiki.
  45:     *   
  46:     *   @return
  47:     */
  48:    private String getFeedID()
  49:	      {
  50:        return m_wikiContext.getEngine().getBaseURL(); // FIXME: This is not a feed id
  51:    }
  52:
  53:    private String getEntryID( Entry e )
  54:	      {
  55:        return e.getURL(); // FIXME: Not really a feed id!
  56:    }
  57:    
  58:    private Collection getItems()
  59:	      {
  60:        ArrayList list = new ArrayList();
  61:        
  62:        WikiEngine engine = m_wikiContext.getEngine();
  63:        ServletContext servletContext = null;
  64:        
  65:        if( m_wikiContext.getHttpRequest() != null )
  66:            servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
  67:
  68:        for( Iterator i = m_entries.iterator(); i.hasNext(); )
  69:	          {
  70:            Entry e = (Entry)i.next();
  71:            
  72:            WikiPage p = e.getPage();
  73:            
  74:            Element entryEl = getElement("entry");
  75:            
  76:            //
  77:            //  Mandatory elements
  78:            //
  79:            
  80:            entryEl.addContent( getElement("id").setText( getEntryID(e)) );
  81:            entryEl.addContent( getElement("title").setAttribute("type","html").setText( e.getTitle() ));
  82:            entryEl.addContent( getElement("updated").setText( DateFormatUtils.formatUTC(p.getLastModified(),
  83:                                                                                         RFC3339FORMAT )));
  84:            //
  85:            //  Optional elements
  86:            //
  87:            
  88:            entryEl.addContent( getElement("author").addContent( getElement("name").setText( e.getAuthor() )));
  89:            entryEl.addContent( getElement("link").setAttribute("rel","alternate").setAttribute("href",e.getURL()));
  90:            entryEl.addContent( getElement("content").setAttribute("type","html").setText( e.getContent() ));
  91:            
  92:            //
  93:            //  Check for enclosures
  94:            //
  95:            
  96:            if( engine.getAttachmentManager().hasAttachments(p) && servletContext != null )
  97:	              {
  98:                try
  99:	                  {
 100:                    Collection c = engine.getAttachmentManager().listAttachments(p);
 101:                
 102:                    for( Iterator a = c.iterator(); a.hasNext(); )
 103:	                      {
 104:                        Attachment att = (Attachment) a.next();
 105:                    
 106:                        Element attEl = getElement("link");
 107:                        attEl.setAttribute( "rel","enclosure" );
 108:                        attEl.setAttribute( "href", engine.getURL(WikiContext.ATTACH, att.getName(), null, true ) );
 109:                        attEl.setAttribute( "length", Long.toString(att.getSize()) );
 110:                        attEl.setAttribute( "type", getMimeType( servletContext, att.getFileName() ) );
 111:                        
 112:                        entryEl.addContent( attEl );
 113:                    }
 114:                }
 115:                catch( ProviderException ex )
 116:	                  {
 117:                    // FIXME: log.info("Can't get attachment data",ex);
 118:                }
 119:            }
 120:
 121:            
 122:            list.add( entryEl );
 123:        }
 124:        
 125:        return list;
 126:    }
 127:    
 128:    public String getString()
 129:	      {
 130:        Element root = getElement("feed");
 131:        WikiEngine engine = m_wikiContext.getEngine();
 132:        
 133:        Date lastModified = new Date(0L);
 134:        
 135:        for( Iterator i = m_entries.iterator(); i.hasNext(); )
 136:	          {
 137:            Entry e = (Entry)i.next();
 138:            
 139:            if( e.getPage().getLastModified().after(lastModified) )
 140:                lastModified = e.getPage().getLastModified();
 141:        }
 142:        
 143:        //
 144:        //  Mandatory parts
 145:        //
 146:        root.addContent( getElement("title").setText( getChannelTitle() ) );
 147:        root.addContent( getElement("id").setText(getFeedID()) );
 148:        root.addContent( getElement("updated").setText(DateFormatUtils.formatUTC( lastModified,
 149:                                                                                  RFC3339FORMAT ) ));
 150:        
 151:        //
 152:        //  Optional
 153:        //
 154:        // root.addContent( getElement("author").addContent(getElement("name").setText(format())))
 155:        root.addContent( getElement("link").setAttribute("href",engine.getBaseURL()));
 156:        root.addContent( getElement("generator").setText("JSPWiki "+Release.VERSTR));
 157:        
 158:        String rssFeedURL  = engine.getURL(WikiContext.NONE, "rss.jsp", 
 159:                                           "page="+engine.encodeName(m_wikiContext.getPage().getName())+
 160:                                           "&mode="+m_mode+
 161:                                           "&type=atom",
 162:                                           true );
 163:        Element self = getElement("link").setAttribute("rel","self");
 164:        self.setAttribute("href",rssFeedURL);
 165:        root.addContent(self);
 166:        
 167:        //
 168:        //  Items
 169:        //
 170:        
 171:        root.addContent( getItems() );
 172:        
 173:        //
 174:        //  aaand output
 175:        //
 176:        XMLOutputter output = new XMLOutputter();
 177:        
 178:        output.setFormat( Format.getPrettyFormat() );
 179:        
 180:        try
 181:	          {
 182:            StringWriter res = new StringWriter();
 183:            output.output( root, res );
 184:
 185:            return res.toString();
 186:        }
 187:        catch( IOException e )
 188:	          {
 189:            return null;
 190:        }
 191:    }
 192:
 193:    private final Element getElement( String name )
 194:	      {
 195:        return new Element( name, m_atomNameSpace );
 196:    }
 197:}