|
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;
|
|
/**
|
* <p>Das FeedServlet erstellt Feeds von Ordnern der WebBox</p>
|
*
|
* <p>Annahmen:</p>
|
* <ul>
|
* <li>Der JNDI-Parameter <code>wbxFileBase</code> enthaelt den absoluten Pfad
|
* zur Dateiablage</li>
|
* <li>Der Parameter 'wbxPubDir' enthaelt den Namen des Ordners mit
|
* oeffentlichen Inhalten und dieser Ordner liegt direkt unterhalb von
|
* <code>wbxFileBase</code></li>
|
* <li>Der Ordner mit dem oeffentlichen Inhalt wird von Tomcat als Context
|
* 1:1 ausgeliefert sodass bei einem URL wie
|
* <code>http://example.com/[Context]/weitere/pfad/angaben/datei.html</code>
|
* die Pfadangabe hinter <code>[Context]/</code> die Inhalte innerhalb von
|
* <code>wbxFileBase/wbxPubDir</code> 1:1 wiedergibt.</li>
|
*
|
* <p>Der JNDI-Parameter <code>wbxMaxFeedDepth</code> 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.</p>
|
*
|
* <p>Der JNDI-Parameter <code>wbxMaxFeedEntries</code> 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.</p>
|
*
|
* <p>Die Datei <code>CATALINA_BASE/conf/context.xml</code> muss also die
|
* folgenden Eintraege enthalten:</p>
|
* <pre>
|
* <Environment name="wbxFileBase" type="java.lang.String" value="absoluter/pfad/zur/dateiablage" override="false" />
|
* <Environment name="wbxPubDirName" type="java.lang.String" value="/www" override="false" />
|
* <Environment name="wbxFeedTitle" type="java.lang.String" value="Newsfeed" override="false" />
|
* <Environment name="wbxFeedSubtitle" type="java.lang.String" value="Text des Untertitels" override="false" />
|
* <Environment name="wbxFeedCopyright" type="java.lang.String" value="Text des Copyright-Hinweises" override="false" />
|
* <Environment name="wbxFeedDomain" type="java.lang.String" value="http://example.com" override="false" />
|
* <Environment name="wbxFeedAuthor" type="java.lang.String" value="Name des Autors" override="false" />
|
* </pre>
|
*/
|
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(WbxUtils.WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
|
logger.fine("basis: " + basis);
|
StringBuffer pfad = new StringBuffer();
|
pfad.append(basis);
|
pfad.append(zielPfad.replace(contextPath, wu.getJNDIParameter(WbxUtils.WBX_PUB_DIR_NAME, WbxUtils.WBX_DEFAULT_PUB_DIR_NAME)));
|
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(WbxUtils.WBX_PUB_DIR_NAME, WbxUtils.WBX_DEFAULT_PUB_DIR_NAME), 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("<a href='");
|
buf.append(urlStr);
|
buf.append("'><img src='");
|
buf.append(noExt);
|
buf.append("_kl");
|
buf.append(ext);
|
buf.append("'></a>");
|
} 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();
|
}
|
}
|
|
}
|