WebBox Klassenbibliothek
ulrich
2018-04-05 27d922ec03686599f688e8bb0a64375a3d559129
src/de/uhilger/wbx/WbxUtils.java
@@ -18,7 +18,13 @@
package de.uhilger.wbx;
import de.uhilger.wbx.data.Inhalt;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -43,9 +49,90 @@
  public static final String NO_STRING = " ist kein String";  
  public static final String EMPTY_STRING = "";
  
  public static final String WBX_FILE_BASE = "wbxFileBase";
  public static final String WBX_PUB_DIR_NAME = "wbxPubDirName";
  public static final String WBX_DEFAULT_PUB_DIR_NAME = "/www";
  
  public static final String WBX_PUB_URL_NAME = "wbxPubUrlName";
  public static final String WBX_DEFAULT_PUB_URL_NAME = "/data";
  public List<Inhalt> collectFiles(String requestUrl, String contextPath,
          String relativePath, int maxTiefe, int maxAnzahl, long length) {
    Bild bild = new Bild();
    //WbxUtils wu = new WbxUtils();
    String basis = getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
    String pubDirName = getJNDIParameter(WbxUtils.WBX_PUB_DIR_NAME, WbxUtils.WBX_DEFAULT_PUB_DIR_NAME);
    String pubUrlName = getJNDIParameter(WbxUtils.WBX_PUB_URL_NAME, WbxUtils.WBX_DEFAULT_PUB_URL_NAME);
    String relPath = relativePath.replace(pubUrlName, pubDirName);
    String absPath = basis + relPath;
    ArrayList beitraege = new ArrayList();
    ArrayList<Inhalt> files = new ArrayList<>();
    collectFiles(new File(absPath), 0, beitraege, maxTiefe, maxAnzahl);
    Iterator i = beitraege.iterator();
    while(i.hasNext()) {
      File beitrag = (File) i.next();
      Inhalt cont = new Inhalt();
      cont.setMimetype(bild.getMimeType(beitrag));
      cont.setIsDirectory(beitrag.isDirectory());
      cont.setIsHidden(beitrag.isHidden());
      cont.setLastModified(beitrag.lastModified());
      cont.setLength(beitrag.length());
      /*
        den 'https://..'-Teil bis vor dem
        ContextPath ermitteln
      */
      //String requestUrl = getRequest().getRequestURL().toString();
      //String contextPath = getRequest().getContextPath();
      int pos = requestUrl.indexOf(contextPath);
      /*
        den Teil des Pfades ermitteln, der zwischen dem
        ContextPath zum oeffentlichen Ordner und dem Dateiname
        steht
      */
      String absolutePath = beitrag.getAbsolutePath();
      absolutePath = absolutePath.replace(beitrag.getName(), "");
      absolutePath = absolutePath.replace(pubDirName, "");
      String part = relativePath.replace(pubUrlName, "");
      int pos2 = absolutePath.indexOf(part);
      String mittelteil = absolutePath.substring(pos2);
      mittelteil = mittelteil.replace(part, "");
      cont.setBase(requestUrl.substring(0, pos));
      cont.setUrl(/*requestUrl.substring(0, pos) + "/data" + */ mittelteil + beitrag.getName());
      files.add(cont);
    }
    return files;
  }
  private String getFileContent(File file, long len) {
    try {
      StringBuffer readBuffer = new StringBuffer();
      byte[] buf = new byte[1024];
      long read = 0;
      FileInputStream fis = new FileInputStream(file);
      int bytesRead = fis.read(buf);
      read += bytesRead;
      while(read < len) {
        readBuffer.append(buf);
        bytesRead = fis.read(buf);
        read += bytesRead;
      }
      readBuffer.append(buf);
      return readBuffer.toString();
    } catch (Exception ex) {
      Logger.getLogger(WbxUtils.class.getName()).log(Level.SEVERE, null, ex);
      return EMPTY_STRING;
    }
  }
  /**
   * Diese Methode funktioniert nur, wenn entweder ein Ordner uebergeben 
   * wird, der keine Unterordner enthaelt wie zum Beispiel der Ordner 'neu'