001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.rss; 020 021import java.io.IOException; 022import java.io.StringWriter; 023import java.text.SimpleDateFormat; 024import java.util.*; 025 026import javax.servlet.ServletContext; 027 028import org.jdom2.Element; 029import org.jdom2.output.Format; 030import org.jdom2.output.XMLOutputter; 031import org.apache.wiki.Release; 032import org.apache.wiki.WikiContext; 033import org.apache.wiki.WikiEngine; 034import org.apache.wiki.WikiPage; 035import org.apache.wiki.api.exceptions.ProviderException; 036import org.apache.wiki.attachment.Attachment; 037 038/** 039 * Represents an RSS 2.0 feed (with enclosures). This feed provides no 040 * fizz-bang features. 041 * 042 * @since 2.2.27 043 */ 044public class RSS20Feed extends Feed 045{ 046 /** 047 * Creates an RSS 2.0 feed for the specified Context. 048 * 049 * @param context The WikiContext. 050 */ 051 public RSS20Feed( WikiContext context ) 052 { 053 super( context ); 054 } 055 056 private List<Element> getItems() 057 { 058 ArrayList<Element> list = new ArrayList<>(); 059 SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'"); 060 061 WikiEngine engine = m_wikiContext.getEngine(); 062 ServletContext servletContext = null; 063 064 if( m_wikiContext.getHttpRequest() != null ) 065 servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext(); 066 067 for( Iterator< Entry > i = m_entries.iterator(); i.hasNext(); ) 068 { 069 Entry e = i.next(); 070 WikiPage p = e.getPage(); 071 072 String url = e.getURL(); 073 074 Element item = new Element("item"); 075 076 item.addContent( new Element("link").setText(url) ); 077 078 item.addContent( new Element("title").setText( e.getTitle()) ); 079 080 item.addContent( new Element("description").setText( e.getContent()) ); 081 082 // 083 // Attachments for enclosures 084 // 085 086 if( engine.getAttachmentManager().hasAttachments(p) && servletContext != null ) 087 { 088 try 089 { 090 List< Attachment > c = engine.getAttachmentManager().listAttachments(p); 091 092 for( Iterator< Attachment > a = c.iterator(); a.hasNext(); ) 093 { 094 Attachment att = a.next(); 095 096 Element attEl = new Element("enclosure"); 097 attEl.setAttribute( "url", engine.getURL(WikiContext.ATTACH, att.getName(), null, true ) ); 098 attEl.setAttribute( "length", Long.toString(att.getSize()) ); 099 attEl.setAttribute( "type", getMimeType( servletContext, att.getFileName() ) ); 100 101 item.addContent( attEl ); 102 } 103 } 104 catch( ProviderException ex ) 105 { 106 // FIXME: log.info("Can't get attachment data",ex); 107 } 108 } 109 110 // 111 // Modification date. 112 // 113 Calendar cal = Calendar.getInstance(); 114 cal.setTime( p.getLastModified() ); 115 cal.add( Calendar.MILLISECOND, 116 - (cal.get( Calendar.ZONE_OFFSET ) + 117 (cal.getTimeZone().inDaylightTime( p.getLastModified() ) ? cal.get( Calendar.DST_OFFSET ) : 0 )) ); 118 119 item.addContent( new Element("pubDate").setText(fmt.format(cal.getTime())) ); 120 121 list.add( item ); 122 } 123 124 return list; 125 } 126 127 /** 128 * {@inheritDoc} 129 */ 130 @Override 131 public String getString() 132 { 133 WikiEngine engine = m_wikiContext.getEngine(); 134 Element root = new Element("rss"); 135 root.setAttribute("version","2.0"); 136 137 Element channel = new Element("channel"); 138 root.addContent( channel ); 139 140 // 141 // Mandatory parts 142 // 143 channel.addContent( new Element("title").setText( getChannelTitle() ) ); 144 channel.addContent( new Element("link").setText(engine.getBaseURL())); 145 channel.addContent( new Element("description").setText( getChannelDescription() )); 146 147 // 148 // Optional 149 // 150 channel.addContent( new Element("language").setText(getChannelLanguage())); 151 channel.addContent( new Element("generator").setText("JSPWiki "+Release.VERSTR)); 152 153 String mail = engine.getVariable(m_wikiContext,RSSGenerator.PROP_RSS_AUTHOREMAIL); 154 if( mail != null ) 155 { 156 String editor = engine.getVariable( m_wikiContext,RSSGenerator.PROP_RSS_AUTHOR ); 157 158 if( editor != null ) 159 mail = mail + " ("+editor+")"; 160 161 channel.addContent( new Element("managingEditor").setText(mail) ); 162 } 163 164 // 165 // Items 166 // 167 168 channel.addContent( getItems() ); 169 170 // 171 // aaand output 172 // 173 XMLOutputter output = new XMLOutputter(); 174 175 output.setFormat( Format.getPrettyFormat() ); 176 177 try 178 { 179 StringWriter res = new StringWriter(); 180 output.output( root, res ); 181 182 return res.toString(); 183 } 184 catch( IOException e ) 185 { 186 return null; 187 } 188 } 189 190}