| | |
| | | import de.uhilger.httpserver.base.actor.FileActor; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | | * Die Klasse FileHandler dient zur Auslieferung von Dateiinhalten über |
| | |
| | | */ |
| | | public class FileHandler implements HttpHandler { |
| | | |
| | | /* Der Logger fuer diesen FileHandler */ |
| | | private static final Logger logger = Logger.getLogger(FileHandler.class.getName()); |
| | | |
| | | /* Headernamen */ |
| | | public static final String RANGE_HEADER = "Range"; |
| | | public static final String CONTENT_RANGE_HEADER = "Content-Range"; |
| | | //public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges"; |
| | | //public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified"; |
| | | public static final String CONTENT_TYPE = "Content-Type"; |
| | | public static final String CONTENT_LENGTH = "Content-Length"; |
| | | |
| | |
| | | public static final int SC_UNPROCESSABLE_ENTITY = 422; |
| | | |
| | | /* String Konstanten */ |
| | | //public static final String STR_BYTES = "bytes"; |
| | | public static final String STR_SLASH = "/"; |
| | | public static final String STR_BLANK = " "; |
| | | public static final String STR_DASH = "-"; |
| | | public static final String STR_COMMA = ","; |
| | | public static final String STR_DOT = "."; |
| | | //public static final String STR_NOT_FOUND = " not found."; |
| | | //public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz"; |
| | | public static final String RANGE_PATTERN = "[^\\d-,]"; |
| | | public static final String WELCOME_FILE = "index.html"; |
| | | |
| | | /* Ablageort fuer Webinhalte */ |
| | | //protected final String fileBase; |
| | | |
| | | public static final String ATTR_FILE_BASE = "fileBase"; |
| | | |
| | | /* moegliche Dateinamen, wenn kein Name angegeben wurde */ |
| | | public static final String ATTR_WELCOME_FILES = "welcomeFiles"; |
| | | |
| | | /** |
| | |
| | | HttpResponder fs = new HttpResponder(); |
| | | fs.sendNotFound(e, fName); |
| | | } else { |
| | | File fileToDeliver = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName); |
| | | Headers headers = e.getRequestHeaders(); |
| | | if (headers.containsKey(RANGE_HEADER)) { |
| | | FileActor fa = new FileActor(); |
| | | fa.serveFileParts(e, new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName)); |
| | | new FileActor().serveFileParts(e, fileToDeliver); |
| | | } else { |
| | | HttpResponder fs = new HttpResponder(); |
| | | File file = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName); |
| | | if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { |
| | | HttpHelper helper = new HttpHelper(); |
| | | File welcomeFile = helper.tryWelcomeFiles(e, fName); |
| | | if(welcomeFile != null) { |
| | | file = welcomeFile; |
| | | fileToDeliver = welcomeFile; |
| | | } |
| | | } |
| | | fs.serveFile(e, file); |
| | | new HttpResponder().serveFile(e, fileToDeliver); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |