public static void fileToHttpResponse(String fullPathFile, HttpResponse response, String contentType, boolean useFileChannels) { int status; final File file = new File(fullPathFile); if (!file.exists()) status = HttpStatus.SC_NOT_FOUND; else if (!file.canRead() || file.isDirectory()) status = HttpStatus.SC_FORBIDDEN; else { status = HttpStatus.SC_OK; NFileEntity entity = new NFileEntity(file, contentType, useFileChannels); response.setEntity(entity); } response.setStatusCode(status); }
/** * @deprecated (4.2) use {@link #NFileEntity(File, ContentType, boolean)} */ @Deprecated public NFileEntity(final File file, final String contentType, final boolean useFileChannels) { Args.notNull(file, "File"); this.file = file; this.useFileChannels = useFileChannels; setContentType(contentType); }
/** * {@inheritDoc} * * @deprecated (4.2) use {@link #close()} */ @Deprecated public void finish() throws IOException { close(); }
/** * @deprecated (4.2) use {@link #NFileEntity(File, ContentType, boolean)} */ @Deprecated public NFileEntity(final File file, final String contentType, final boolean useFileChannels) { Args.notNull(file, "File"); this.file = file; this.useFileChannels = useFileChannels; setContentType(contentType); }
/** * {@inheritDoc} * * @deprecated (4.2) use {@link #close()} */ @Deprecated public void finish() throws IOException { close(); }
public static void fileToHttpResponse(String fullPathFile, HttpResponse response, String contentType, boolean useFileChannels) { int status; final File file = new File(fullPathFile); if (!file.exists()) status = HttpStatus.SC_NOT_FOUND; else if (!file.canRead() || file.isDirectory()) status = HttpStatus.SC_FORBIDDEN; else { status = HttpStatus.SC_OK; NFileEntity entity = new NFileEntity(file, contentType, useFileChannels); response.setEntity(entity); } response.setStatusCode(status); }
/** * Creates new instance of NFileEntity from the given source {@link File} * with the given content type. If {@code useFileChannels} is set to * {@code true}, the entity will try to use {@link FileContentEncoder} * interface to stream file content directly from the file channel. * * @param file the source file. * @param contentType the content type of the file. * @param useFileChannels flag whether the direct transfer from the file * channel should be attempted. * * @since 4.2 */ public NFileEntity(final File file, final ContentType contentType, final boolean useFileChannels) { Args.notNull(file, "File"); this.file = file; this.useFileChannels = useFileChannels; if (contentType != null) { setContentType(contentType.toString()); } }
@Override public void produceContent(final ContentEncoder encoder, final IOControl ioctrl) throws IOException { if (accessfile == null) { accessfile = new RandomAccessFile(this.file, "r"); } if (fileChannel == null) { fileChannel = accessfile.getChannel(); idx = 0; } final long transferred; if (useFileChannels && encoder instanceof FileContentEncoder) { transferred = ((FileContentEncoder)encoder) .transfer(fileChannel, idx, Long.MAX_VALUE); } else { transferred = fileChannel. transferTo(idx, Long.MAX_VALUE, new ContentEncoderChannel(encoder)); } if (transferred > 0) { idx += transferred; } if (idx >= fileChannel.size()) { encoder.complete(); close(); } }
public static void fileToHttpResponse(String fullPathFile, HttpResponse response, String contentType, boolean useFileChannels) { int status; final File file = new File(fullPathFile); if (!file.exists()) status = HttpStatus.SC_NOT_FOUND; else if (!file.canRead() || file.isDirectory()) status = HttpStatus.SC_FORBIDDEN; else { status = HttpStatus.SC_OK; NFileEntity entity = new NFileEntity(file, contentType, useFileChannels); response.setEntity(entity); } response.setStatusCode(status); }
/** * Creates new instance of NFileEntity from the given source {@link File} * with the given content type. If {@code useFileChannels} is set to * {@code true}, the entity will try to use {@link FileContentEncoder} * interface to stream file content directly from the file channel. * * @param file the source file. * @param contentType the content type of the file. * @param useFileChannels flag whether the direct transfer from the file * channel should be attempted. * * @since 4.2 */ public NFileEntity(final File file, final ContentType contentType, final boolean useFileChannels) { Args.notNull(file, "File"); this.file = file; this.useFileChannels = useFileChannels; if (contentType != null) { setContentType(contentType.toString()); } }
@Override public void produceContent(final ContentEncoder encoder, final IOControl ioctrl) throws IOException { if (accessfile == null) { accessfile = new RandomAccessFile(this.file, "r"); } if (fileChannel == null) { fileChannel = accessfile.getChannel(); idx = 0; } final long transferred; if (useFileChannels && encoder instanceof FileContentEncoder) { transferred = ((FileContentEncoder)encoder) .transfer(fileChannel, idx, Long.MAX_VALUE); } else { transferred = fileChannel. transferTo(idx, Long.MAX_VALUE, new ContentEncoderChannel(encoder)); } if (transferred > 0) { idx += transferred; } if (idx >= fileChannel.size()) { encoder.complete(); close(); } }
final NFileEntity body = new NFileEntity(file, ContentType.create("text/html")); response.setEntity(body); if (!response.containsHeader(HttpHeaders.LAST_MODIFIED))
final HttpEntity body = file.isDirectory() ? new NStringEntity(file.toString(), ContentType.create(mimeType)) : new NFileEntity(file, ContentType.create(mimeType)); response.setEntity(body); if (!response.containsHeader(HttpHeaders.LAST_MODIFIED)) {
final HttpEntity body = file.isDirectory() ? new NStringEntity(file.toString(), ContentType.create(mimeType)) : new NFileEntity(file, ContentType.create(mimeType)); response.setEntity(body); if (!response.containsHeader(HttpHeaders.LAST_MODIFIED)) {