public OutputStream getOutputStream() throws IOException { return restxResponse.getOutputStream(); }
public OutputStream getOutputStream() throws IOException { return restxResponse.getOutputStream(); }
protected void serveResource(RestxResponse resp, URL resource, String contentType, Map<String, String> headers) throws IOException { resp.setLogLevel(RestxLogLevel.QUIET); resp.setStatus(HttpStatus.OK); for(Map.Entry<String,String> headerEntry: headers.entrySet()) { resp.setHeader(headerEntry.getKey(), headerEntry.getValue()); } resp.setContentType(contentType); Resources.asByteSource(resource).copyTo(resp.getOutputStream()); }
protected void serveResource(RestxResponse resp, URL resource, String contentType, Map<String, String> headers) throws IOException { resp.setLogLevel(RestxLogLevel.QUIET); resp.setStatus(HttpStatus.OK); for(Map.Entry<String,String> headerEntry: headers.entrySet()) { resp.setHeader(headerEntry.getKey(), headerEntry.getValue()); } resp.setContentType(contentType); Resources.asByteSource(resource).copyTo(resp.getOutputStream()); }
@Override public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException { String path = match.getPathParam("path"); File file = root.resolve(path).toFile(); if (!file.exists()) { notFound(match, resp); return; } if (file.isFile()) { resp.setStatus(HttpStatus.OK); resp.setContentType(HTTP.getContentTypeFromExtension(file.getName()).or("application/binary")); Files.asByteSource(file).copyTo(resp.getOutputStream()); } else if (file.isDirectory() && allowDirectoryListing) { resp.setStatus(HttpStatus.OK); resp.setContentType("application/json"); PrintWriter writer = resp.getWriter(); writer.println("["); Path dir = file.toPath(); for (Iterator<Path> iterator = java.nio.file.Files.newDirectoryStream(dir).iterator(); iterator.hasNext(); ) { Path s = iterator.next(); boolean isDirectory = s.toFile().isDirectory(); writer.println("\"" + dir.relativize(s) + (isDirectory ? "/" : "") + "\"" + (iterator.hasNext() ? "," : "") +""); } writer.println("]"); } else { throw new WebException(HttpStatus.UNAUTHORIZED); } }
@Override public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException { String path = match.getPathParam("path"); File file = root.resolve(path).toFile(); if (!file.exists()) { notFound(match, resp); return; } if (file.isFile()) { resp.setStatus(HttpStatus.OK); resp.setContentType(HTTP.getContentTypeFromExtension(file.getName()).or("application/binary")); Files.asByteSource(file).copyTo(resp.getOutputStream()); } else if (file.isDirectory() && allowDirectoryListing) { resp.setStatus(HttpStatus.OK); resp.setContentType("application/json"); PrintWriter writer = resp.getWriter(); writer.println("["); Path dir = file.toPath(); for (Iterator<Path> iterator = java.nio.file.Files.newDirectoryStream(dir).iterator(); iterator.hasNext(); ) { Path s = iterator.next(); boolean isDirectory = s.toFile().isDirectory(); writer.println("\"" + dir.relativize(s) + (isDirectory ? "/" : "") + "\"" + (iterator.hasNext() ? "," : "") +""); } writer.println("]"); } else { throw new WebException(HttpStatus.UNAUTHORIZED); } }