package de.uhilger.wbx.web; import de.uhilger.wbx.WbxUtils; import de.vogella.rss.model.Feed; import de.vogella.rss.model.FeedMessage; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** *
Das FeedServlet erstellt Feeds von Ordnern der WebBox
* *Annahmen:
*wbxFileBase
enthaelt den absoluten Pfad
* zur DateiablagewbxFileBase
http://example.com/[Context]/weitere/pfad/angaben/datei.html
* die Pfadangabe hinter [Context]/
die Inhalte innerhalb von
* wbxFileBase/wbxPubDir
1:1 wiedergibt.Der JNDI-Parameter wbxMaxFeedDepth
regelt, wie viele Ebenen der
* Ordnerstruktur maximal nach Eintraegen durchsucht werden. Dabei haben die
* tiefsten Ebenen Vorrang vor hoeher gelegenen Ebenen, d.h. Dateien auf
* tieferen Ebenen werden vor Dateien auf hoeheren Ebenen in den Feed
* aufgenommen.
Der JNDI-Parameter wbxMaxFeedEntries
regelt, wie viele Beitraege
* maximal im Feed erscheinen. Der Standardwert ist fuenf Beitraege, d.h., die
* ersten fuenf gefundenen Beitraege werden chronologisch absteigend sortiert
* nach ihrem letzten Aenderungsdatum ausgegeben.
Die Datei CATALINA_BASE/conf/context.xml
muss also die
* folgenden Eintraege enthalten:
**/ public class FeedServlet extends HttpServlet { private static final Logger logger = Logger.getLogger(FeedServlet.class.getName()); public static final String JNDI_CTX_NAME = "java:comp/env"; public static final String WBX_FILE_BASE = "wbxFileBase"; public static final String WBX_PUB_DIR = "wbxPubDir"; public static final String WBX_MAX_FEED_DEPTH = "wbxMaxFeedDepth"; public static final String WBX_MAX_FEED_ENTRIES = "wbxMaxFeedEntries"; public static final String WBX_FEED_TITLE = "wbxFeedTitle"; public static final String WBX_FEED_SUBTITLE = "wbxFeedSubtitle"; public static final String WBX_FEED_COPYRIGHT = "wbxFeedCopyright"; public static final String WBX_FEED_DOMAIN = "wbxFeedDomain"; public static final String WBX_FEED_AUTHOR = "wbxFeedAuthor"; public static final int WBX_DEF_MAX_FEED_DEPTH = 3; public static final int WBX_DEF_MAX_FEED_ENTRIES = 5; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ServletOutputStream out = response.getOutputStream(); response.setContentType("text/xml"); String url = request.getRequestURL().toString(); logger.fine("requestURL: " + url); String contextPath = request.getContextPath(); logger.fine("contextPath: " + contextPath); String zielPfad = url.substring(url.indexOf(contextPath)); logger.fine(zielPfad); WbxUtils wu = new WbxUtils(); String basis = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING); logger.fine("basis: " + basis); StringBuffer pfad = new StringBuffer(); pfad.append(basis); pfad.append(zielPfad.replace(contextPath, wu.getJNDIParameter(WBX_PUB_DIR, WbxUtils.EMPTY_STRING))); logger.fine("pfad: " + pfad); String dirStr = pfad.toString().substring(0, pfad.lastIndexOf("/")); logger.fine("dirStr: " + dirStr); File dir = new File(dirStr); if (dir.isDirectory()) { List beitraege = new ArrayList(); int maxBeitraege = wu.getJNDIInt(WBX_MAX_FEED_ENTRIES, WBX_DEF_MAX_FEED_ENTRIES); int maxTiefe = wu.getJNDIInt(WBX_MAX_FEED_DEPTH, WBX_DEF_MAX_FEED_DEPTH); wu.collectFiles(dir, 0, beitraege, maxTiefe, maxBeitraege); Iterator i = beitraege.iterator(); while (i.hasNext()) { File f = (File) i.next(); logger.fine(f.getAbsolutePath() + " " + f.lastModified()); } writeFeed(wu.getJNDIParameter(WBX_FEED_TITLE, WbxUtils.EMPTY_STRING), wu.getJNDIParameter(WBX_FEED_SUBTITLE, WbxUtils.EMPTY_STRING), out, beitraege, basis, pfad.toString(), contextPath, wu); } else { logger.fine(dir.getAbsolutePath() + " ist kein Ordner."); } } public void writeFeed(String feedTitel, String beschr, ServletOutputStream s, List beitraege, String basis, String pfad, String contextPath, WbxUtils wu) { long newest = -1; String copyright = wu.getJNDIParameter(WBX_FEED_COPYRIGHT, WbxUtils.EMPTY_STRING); String domain = wu.getJNDIParameter(WBX_FEED_DOMAIN, WbxUtils.EMPTY_STRING); String title = feedTitel; String description = beschr; String language = "de"; String link = domain; newest = ((File) beitraege.get(0)).lastModified(); Calendar cal = new GregorianCalendar(); cal.setTime(new Date(newest)); Date creationDate = cal.getTime(); SimpleDateFormat date_format = new SimpleDateFormat( "EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US); String pubdate = date_format.format(creationDate); Feed rssFeed = new Feed(title, link, description, language, copyright, pubdate); Iterator i = beitraege.iterator(); while (i.hasNext()) { File f = (File) i.next(); FeedMessage feedEintrag = new FeedMessage(); feedEintrag.setTitle(f.getName()); //feedEintrag.setDescription("Beschreibung hier"); feedEintrag.setAuthor(wu.getJNDIParameter(WBX_FEED_AUTHOR, WbxUtils.EMPTY_STRING)); String urlStr = f.getAbsolutePath(); urlStr = urlStr.replace(basis, domain); urlStr = urlStr.replace(wu.getJNDIParameter(WBX_PUB_DIR, WbxUtils.EMPTY_STRING), contextPath); logger.fine(urlStr); feedEintrag.setGuid(urlStr); feedEintrag.setLink(urlStr); logger.fine(f.getName()); feedEintrag.setDescription(readFilePart(f, urlStr)); rssFeed.getMessages().add(feedEintrag); } FeedStreamWriter writer = new FeedStreamWriter(rssFeed, s); try { writer.write(); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } } private String readFilePart(File f, String urlStr) { StringBuffer buf = new StringBuffer(); Reader reader = null; try { String fn = f.getName().toLowerCase(); if(fn.endsWith(".jpg") || fn.endsWith(".jpeg") || fn.endsWith(".png")) { String noExt; String ext; int dotpos = urlStr.lastIndexOf('.'); if(dotpos > -1) { noExt = urlStr.substring(0, dotpos); ext = urlStr.substring(dotpos); } else { noExt = ""; ext = ""; } buf.append(""); } else { reader = new FileReader(f); char[] readBuffer = new char[1024]; int charsRead = reader.read(readBuffer); buf.append(readBuffer); reader.close(); } return buf.toString(); } catch (Exception ex) { logger.log(Level.SEVERE, null, ex); return buf.toString(); } } }* * * * * * *