| | |
| | | |
| | | 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.util.Locale; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | import javax.naming.Context; |
| | | import javax.naming.InitialContext; |
| | | import javax.naming.NamingException; |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServlet; |
| | |
| | | public static final int WBX_DEF_MAX_FEED_DEPTH = 3; |
| | | public static final int WBX_DEF_MAX_FEED_ENTRIES = 5; |
| | | |
| | | public static final String NOT_FOUND = " nicht gefunden"; |
| | | public static final String NO_STRING = " ist kein String"; |
| | | |
| | | @Override |
| | | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
| | | ServletOutputStream out = response.getOutputStream(); |
| | |
| | | logger.fine("contextPath: " + contextPath); |
| | | String zielPfad = url.substring(url.indexOf(contextPath)); |
| | | logger.fine(zielPfad); |
| | | String basis = getJNDIParameter(WBX_FILE_BASE); |
| | | 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, getJNDIParameter(WBX_PUB_DIR))); |
| | | 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 = getJNDIInt(WBX_MAX_FEED_ENTRIES, WBX_DEF_MAX_FEED_ENTRIES); |
| | | int maxTiefe = getJNDIInt(WBX_MAX_FEED_DEPTH, WBX_DEF_MAX_FEED_DEPTH); |
| | | 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); |
| | | 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); |
| | | 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) { |
| | | List beitraege, String basis, String pfad, String contextPath, WbxUtils wu) { |
| | | |
| | | long newest = -1; |
| | | String copyright = getJNDIParameter(WBX_FEED_COPYRIGHT); |
| | | String domain = getJNDIParameter(WBX_FEED_DOMAIN); |
| | | 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"; |
| | |
| | | FeedMessage feedEintrag = new FeedMessage(); |
| | | feedEintrag.setTitle(f.getName()); |
| | | //feedEintrag.setDescription("Beschreibung hier"); |
| | | feedEintrag.setAuthor(getJNDIParameter(WBX_FEED_AUTHOR)); |
| | | feedEintrag.setAuthor(wu.getJNDIParameter(WBX_FEED_AUTHOR, WbxUtils.EMPTY_STRING)); |
| | | |
| | | String urlStr = f.getAbsolutePath(); |
| | | urlStr = urlStr.replace(basis, domain); |
| | | urlStr = urlStr.replace(getJNDIParameter(WBX_PUB_DIR), contextPath); |
| | | urlStr = urlStr.replace(wu.getJNDIParameter(WBX_PUB_DIR, WbxUtils.EMPTY_STRING), contextPath); |
| | | |
| | | logger.fine(urlStr); |
| | | |
| | |
| | | } catch (Exception ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | return buf.toString(); |
| | | } |
| | | } |
| | | |
| | | private int getJNDIInt(String paramName, int defaultVal) { |
| | | String jndiStr = getJNDIParameter(paramName); |
| | | try { |
| | | return Integer.parseInt(jndiStr); |
| | | } catch(NumberFormatException ex) { |
| | | return defaultVal; |
| | | } |
| | | } |
| | | |
| | | private String getJNDIParameter(String pname) { |
| | | try { |
| | | // unseren environment naming context ermitteln |
| | | Context initCtx = new InitialContext(); |
| | | Context envCtx = (Context) initCtx.lookup(JNDI_CTX_NAME); |
| | | |
| | | // unseren Parameter lesen |
| | | Object o = envCtx.lookup(pname); |
| | | if(o instanceof String) { |
| | | return o.toString(); |
| | | } else { |
| | | return NO_STRING; |
| | | } |
| | | } catch (NamingException ex) { |
| | | logger.log(Level.SEVERE, ex.getMessage()); |
| | | return NOT_FOUND; |
| | | } |
| | | } |
| | | |