| | |
| | | |
| | | package de.uhilger.wbx.web; |
| | | |
| | | import de.uhilger.wbx.web.FeedStreamWriter; |
| | | import de.vogella.rss.model.Feed; |
| | | import de.vogella.rss.model.FeedMessage; |
| | | import java.io.File; |
| | |
| | | import javax.naming.Context; |
| | | import javax.naming.InitialContext; |
| | | import javax.naming.NamingException; |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServlet; |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | /** |
| | | * Das FeedServlet erstellt Feeds von Ordnern der WebBox |
| | | * <p>Das FeedServlet erstellt Feeds von Ordnern der WebBox</p> |
| | | * |
| | | * Eine Maximaltiefe als Parameter soll regeln, wie viele Ordner tief |
| | | * Dateien gesucht werden |
| | | * <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="wbxPubDir" 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 { |
| | | |
| | |
| | | |
| | | 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; |
| | |
| | | response.setContentType("text/xml"); |
| | | String url = request.getRequestURL().toString(); |
| | | logger.fine("requestURL: " + url); |
| | | String dataUrl = url.replace("feed/", "data/"); |
| | | logger.fine("dataUrl: " + dataUrl); |
| | | int pos = dataUrl.indexOf("data/"); |
| | | String zielPfad = dataUrl.substring(pos); |
| | | String contextPath = request.getContextPath(); |
| | | logger.fine("contextPath: " + contextPath); |
| | | String zielPfad = url.substring(url.indexOf(contextPath)); |
| | | logger.fine(zielPfad); |
| | | //ServletContext ctx = request.getServletContext(); |
| | | //Object o = ctx.getAttribute(FILE_BASE); |
| | | String basis = getJNDIParameter(WBX_FILE_BASE); |
| | | //if(o instanceof String) { |
| | | //String basis = o.toString(); |
| | | logger.fine("basis: " + basis); |
| | | StringBuffer pfad = new StringBuffer(); |
| | | pfad.append(basis); |
| | | pfad.append(zielPfad.replace("data/", "/www/")); |
| | | 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 = getJNDIInt(WBX_MAX_FEED_ENTRIES, WBX_DEF_MAX_FEED_ENTRIES); |
| | | int maxTiefe = getJNDIInt(WBX_MAX_FEED_DEPTH, WBX_DEF_MAX_FEED_DEPTH); |
| | | 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("Newsfeed", "Neueste Inhalte von Ulrich Hilger", out, beitraege, basis, pfad.toString()); |
| | | } else { |
| | | logger.fine(dir.getAbsolutePath() + " ist kein Ordner."); |
| | | logger.fine("basis: " + basis); |
| | | StringBuffer pfad = new StringBuffer(); |
| | | pfad.append(basis); |
| | | pfad.append(zielPfad.replace(contextPath, getJNDIParameter(WBX_PUB_DIR))); |
| | | 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 = getJNDIInt(WBX_MAX_FEED_ENTRIES, WBX_DEF_MAX_FEED_ENTRIES); |
| | | int maxTiefe = getJNDIInt(WBX_MAX_FEED_DEPTH, WBX_DEF_MAX_FEED_DEPTH); |
| | | 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(getJNDIParameter(WBX_FEED_TITLE), |
| | | getJNDIParameter(WBX_FEED_SUBTITLE), out, beitraege, |
| | | basis, pfad.toString(), contextPath); |
| | | } else { |
| | | logger.fine(dir.getAbsolutePath() + " ist kein Ordner."); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | public void writeFeed(String feedTitel, String beschr, ServletOutputStream s, List beitraege, String basis, String pfad) { |
| | | String copyright = "Copyright (c) 2018 Ulrich Hilger"; |
| | | public void writeFeed(String feedTitel, String beschr, ServletOutputStream s, |
| | | List beitraege, String basis, String pfad, String contextPath) { |
| | | String copyright = getJNDIParameter(WBX_FEED_COPYRIGHT); |
| | | String domain = getJNDIParameter(WBX_FEED_DOMAIN); |
| | | String title = feedTitel; |
| | | String description = beschr; |
| | | String language = "de"; |
| | | String link = "https://www.uhilger.de"; |
| | | String link = domain; |
| | | Calendar cal = new GregorianCalendar(); |
| | | Date creationDate = cal.getTime(); |
| | | SimpleDateFormat date_format = new SimpleDateFormat( |
| | |
| | | FeedMessage feedEintrag = new FeedMessage(); |
| | | feedEintrag.setTitle(f.getName()); |
| | | //feedEintrag.setDescription("Beschreibung hier"); |
| | | feedEintrag.setAuthor("ulrich Punkt hilger bei web Punkt de (Ulrich Hilger)"); |
| | | feedEintrag.setAuthor(getJNDIParameter(WBX_FEED_AUTHOR)); |
| | | |
| | | String urlStr = f.getAbsolutePath(); |
| | | urlStr = urlStr.replace(basis, "https://uhilger.de"); |
| | | urlStr = urlStr.replace("www/", "data/"); |
| | | urlStr = urlStr.replace(basis, domain); |
| | | urlStr = urlStr.replace(getJNDIParameter(WBX_PUB_DIR), contextPath); |
| | | |
| | | logger.fine(urlStr); |
| | | |
| | |
| | | } |
| | | |
| | | FeedStreamWriter writer = new FeedStreamWriter(rssFeed, s); |
| | | //RSSFeedWriter writer = new RSSFeedWriter(rssFeed, pfad + "/articles.rss"); |
| | | //ByteArrayOutputStream bs = new ByteArrayOutputStream(); |
| | | //RSSByteFeedWriter wr = new RSSByteFeedWriter(rssFeed, bs); |
| | | try { |
| | | writer.write(); |
| | | //s.print(bs.toString()); |
| | | } catch (Exception e) { |
| | | logger.log(Level.SEVERE, e.getMessage(), e); |
| | | } |