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 org.apache.commons.text.StringEscapeUtils; 022import org.apache.wiki.api.core.Context; 023import org.apache.wiki.api.core.Engine; 024import org.apache.wiki.api.exceptions.NoSuchVariableException; 025import org.apache.wiki.variables.VariableManager; 026 027import jakarta.servlet.ServletContext; 028import java.util.ArrayList; 029import java.util.List; 030import org.apache.logging.log4j.LogManager; 031import org.apache.logging.log4j.Logger; 032 033/** 034 * Represents an abstract feed. 035 */ 036public abstract class Feed { 037 protected final List<Entry> m_entries = new ArrayList<>(); 038 private static final Logger LOG = LogManager.getLogger( Feed.class ); 039 040 protected String m_feedURL; 041 protected String m_channelTitle; 042 protected String m_channelDescription; 043 protected String m_channelLanguage; 044 045 protected final Context m_wikiContext; 046 047 protected String m_mode = RSSGenerator.MODE_WIKI; 048 049 /** 050 * Wiki variable storing the blog's name. 051 */ 052 public static final String VAR_BLOGNAME = "blogname"; 053 054 /** 055 * Figure out a site name for a feed. 056 * 057 * @param context the wiki context 058 * @return the site name 059 */ 060 public static String getSiteName( final Context context ) { 061 final Engine engine = context.getEngine(); 062 063 String blogname = null; 064 065 try { 066 blogname = engine.getManager( VariableManager.class ).getValue(context, VAR_BLOGNAME); 067 } catch( final NoSuchVariableException e ) { 068 LOG.debug(e.getMessage(), e); 069 } 070 071 if (blogname == null) { 072 blogname = engine.getApplicationName() + ": " + context.getPage().getName(); 073 } 074 075 return blogname; 076 } 077 078 /** 079 * Create a new Feed for a particular WikiContext. 080 * 081 * @param context The WikiContext. 082 */ 083 public Feed( final Context context) { 084 m_wikiContext = context; 085 } 086 087 /** 088 * Set the mode of the Feed. It can be any of the following: 089 * <ul> 090 * <li>{@link RSSGenerator#MODE_WIKI} - to create a wiki diff list per page.</li> 091 * <li>{@link RSSGenerator#MODE_BLOG} - to assume that the Entries are blog entries.</li> 092 * <li>{@link RSSGenerator#MODE_FULL} - to create a wiki diff list for the entire blog.</li> 093 * </ul> 094 * As the Entry list itself is generated elsewhere, this mostly just affects the way 095 * that the layout and metadata for each entry is generated. 096 * 097 * @param mode As defined in RSSGenerator. 098 */ 099 public void setMode( final String mode) { 100 m_mode = mode; 101 } 102 103 /** 104 * Adds a new Entry to the Feed, at the end of the list. 105 * 106 * @param e The Entry to add. 107 */ 108 public void addEntry( final Entry e) { 109 m_entries.add(e); 110 } 111 112 /** 113 * Returns the XML for the feed contents in a String format. All subclasses must implement. 114 * 115 * @return valid XML, ready to be shoved out. 116 */ 117 public abstract String getString(); 118 119 /** 120 * @return Returns the m_channelDescription. 121 */ 122 public String getChannelDescription() { 123 return m_channelDescription; 124 } 125 126 /** 127 * @param description The m_channelDescription to set. 128 */ 129 public void setChannelDescription( final String description) { 130 m_channelDescription = description; 131 } 132 133 /** 134 * @return Returns the m_channelLanguage. 135 */ 136 public String getChannelLanguage() { 137 return m_channelLanguage; 138 } 139 140 /** 141 * @param language The m_channelLanguage to set. 142 */ 143 public void setChannelLanguage( final String language) { 144 m_channelLanguage = language; 145 } 146 147 /** 148 * @return Returns the m_channelTitle. 149 */ 150 public String getChannelTitle() { 151 return m_channelTitle; 152 } 153 154 /** 155 * @param title The m_channelTitle to set. 156 */ 157 public void setChannelTitle( final String title) { 158 m_channelTitle = title; 159 } 160 161 /** 162 * @return Returns the m_feedURL. 163 */ 164 public String getFeedURL() { 165 return m_feedURL; 166 } 167 168 /** 169 * @param feedurl The m_feedURL to set. 170 */ 171 public void setFeedURL( final String feedurl) { 172 m_feedURL = feedurl; 173 } 174 175 /** 176 * A helper method for figuring out the MIME type for an enclosure. 177 * 178 * @param c A ServletContext 179 * @param name The filename 180 * @return Something sane for a MIME type. 181 */ 182 protected String getMimeType( final ServletContext c, final String name) { 183 String type = c.getMimeType(name); 184 185 if (type == null) { 186 type = "application/octet-stream"; 187 } 188 189 return type; 190 } 191 192 /** 193 * Does the required formatting and entity replacement for XML. 194 * 195 * @param s The String to format. Null is safe. 196 * @return A formatted string. 197 */ 198 public static String format( final String s ) { 199 return StringEscapeUtils.escapeXml11( s ); 200 } 201}