File was renamed from src/de/uhilger/mediaz/handler/FileHandler.java |
| | |
| | | You should have received a copy of the GNU Affero General Public License |
| | | along with this program. If not, see <https://www.gnu.org/licenses/>. |
| | | */ |
| | | package de.uhilger.mediaz.handler; |
| | | package de.uhilger.mediaz.api; |
| | | |
| | | import com.sun.net.httpserver.Headers; |
| | | import com.sun.net.httpserver.HttpExchange; |
| | | import com.sun.net.httpserver.HttpHandler; |
| | | import de.uhilger.mediaz.App; |
| | | import de.uhilger.mediaz.Server; |
| | | import static de.uhilger.mediaz.Server.RB_SLASH; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | |
| | | public static final String HTTP_GET = "GET"; |
| | | |
| | | /* String Konstanten */ |
| | | public static final String STR_BYTES = "bytes"; |
| | | 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"; |
| | | |
| | | /* ResourceBundle-Kennungen */ |
| | | public static final String RB_BYTES = "bytes"; |
| | | public static final String RB_DASH = "dash"; |
| | | public static final String RB_NOT_FOUND = "notFound"; |
| | | public static final String RB_LM_PATTERN = "lmPattern"; |
| | | public static final String RB_RANGE_PATTERN = "rangePattern"; |
| | | public static final String RB_WELCOME_FILE = "welcomeFile"; |
| | | |
| | | /* Ablageort fuer Webinhalte */ |
| | | private final String fileBase; |
| | |
| | | if (headers.containsKey(RANGE_HEADER)) { |
| | | serveFileParts(e, new File(fileBase, fName)); |
| | | } else { |
| | | if (fName.length() < 1 || fName.endsWith(Server.STR_SLASH)) { |
| | | fName += WELCOME_FILE; |
| | | if (fName.length() < 1 || fName.endsWith(App.getRs(RB_SLASH))) { |
| | | fName += App.getRs(RB_WELCOME_FILE); |
| | | } |
| | | serveFile(e, new File(fileBase, fName)); |
| | | } |
| | |
| | | Der regulaere Ausdruck "[^\\d-,]" bezeichnet alle Zeichen, die keine |
| | | Ziffern 0-9, Bindestrich oder Komma sind. |
| | | */ |
| | | rangeHeader = rangeHeader.replaceAll(RANGE_PATTERN, ""); |
| | | rangeHeader = rangeHeader.replaceAll(App.getRs(RB_RANGE_PATTERN), ""); |
| | | |
| | | /* |
| | | Die Ranges ermitteln. |
| | |
| | | String[] rangeArray = rangeHeader.split(STR_COMMA); |
| | | for (String rangeStr : rangeArray) { |
| | | Range range = new Range(); |
| | | String[] values = rangeStr.split(STR_DASH); |
| | | String[] values = rangeStr.split(App.getRs(RB_DASH)); |
| | | if (values.length < 2) { |
| | | // Fall 3 |
| | | range.setStart(Long.parseLong(values[0])); |
| | |
| | | */ |
| | | protected String contentRangeHdr(Range range, File file) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append(STR_BYTES); |
| | | sb.append(App.getRs(RB_BYTES)); |
| | | sb.append(STR_BLANK); |
| | | sb.append(range.getStart()); |
| | | sb.append(STR_DASH); |
| | | sb.append(App.getRs(RB_DASH)); |
| | | sb.append(range.getEnd()); |
| | | sb.append(Server.STR_SLASH); |
| | | sb.append(App.getRs(RB_SLASH)); |
| | | sb.append(file.length()); |
| | | return sb.toString(); |
| | | } |
| | |
| | | */ |
| | | protected void setHeaders(HttpExchange e, File file) throws IOException { |
| | | Headers resHeaders = e.getResponseHeaders(); |
| | | resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES); |
| | | resHeaders.add(ACCEPT_RANGES_HEADER, App.getRs(RB_BYTES)); |
| | | String mimeType = Files.probeContentType(file.toPath()); |
| | | if (mimeType != null) { |
| | | resHeaders.add(CONTENT_TYPE, mimeType); |
| | | } |
| | | SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN); |
| | | SimpleDateFormat sdf = new SimpleDateFormat(App.getRs(RB_LM_PATTERN)); |
| | | Date date = new Date(file.lastModified()); |
| | | resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date)); |
| | | } |
| | |
| | | */ |
| | | protected void sendNotFound(HttpExchange e, String fname) throws IOException { |
| | | OutputStream os = e.getResponseBody(); |
| | | String response = fname + STR_NOT_FOUND; |
| | | String response = fname + STR_BLANK + App.getRs(RB_NOT_FOUND); |
| | | byte[] bytes = response.getBytes(StandardCharsets.UTF_8); |
| | | e.sendResponseHeaders(SC_NOT_FOUND, bytes.length); |
| | | os.write(bytes); |