@Override public long length() throws IOException { return fileObject.getContent().getSize(); } }
/** * Creates a file object. This method is called only if the requested file is not cached. */ @Override protected FileObject createFile(final AbstractFileName name) throws Exception { return new HttpFileObject(name, this); } }
HttpRandomAccessContent(final HttpFileObject fileObject, final RandomAccessMode mode) { super(mode); this.fileObject = fileObject; fileSystem = (HttpFileSystem) this.fileObject.getFileSystem(); }
/** * Prepares a HttpMethod object. * * @param method The object which gets prepared to access the file object. * @throws FileSystemException if an error occurs. * @throws URIException if path cannot be represented. * @since 2.0 (was package) */ protected void setupMethod(final HttpMethod method) throws FileSystemException, URIException { final String pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(this.getUrlCharset()); method.setPath(pathEncoded); method.setFollowRedirects(this.getFollowRedirect()); method.setRequestHeader("User-Agent", this.getUserAgent()); }
/** * Creates an input stream to read the file content from. Is only called if {@link #doGetType} returns * {@link FileType#FILE}. * <p> * It is guaranteed that there are no open output streams for this file when this method is called. * <p> * The returned stream does not have to be buffered. */ @Override protected InputStream doGetInputStream() throws Exception { final GetMethod getMethod = new GetMethod(); setupMethod(getMethod); final int status = getAbstractFileSystem().getClient().executeMethod(getMethod); if (status == HttpURLConnection.HTTP_NOT_FOUND) { throw new FileNotFoundException(getName()); } if (status != HttpURLConnection.HTTP_OK) { throw new FileSystemException("vfs.provider.http/get.error", getName(), Integer.valueOf(status)); } return new HttpInputStream(getMethod); }
HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; } method = new HeadMethod(); setupMethod(method); final HttpClient client = getAbstractFileSystem().getClient(); client.executeMethod(method); method.releaseConnection(); return method; }
/** * Determines the type of this file. Must not return null. The return value of this method is cached, so the * implementation can be expensive. */ @Override protected FileType doGetType() throws Exception { // Use the HEAD method to probe the file. final int status = this.getHeadMethod().getStatusCode(); if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_BAD_METHOD /* method is bad, but resource exist */) { return FileType.FILE; } else if (status == HttpURLConnection.HTTP_NOT_FOUND || status == HttpURLConnection.HTTP_GONE) { return FileType.IMAGINARY; } else { throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status)); } }
fileObject.setupMethod(getMethod); getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-"); final int status = fileSystem.getClient().executeMethod(getMethod); if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) { throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(), Long.valueOf(filePointer), Integer.valueOf(status)); final long skipped = mis.skip(filePointer); if (skipped != filePointer) { throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(), Long.valueOf(filePointer), Integer.valueOf(status));
/** * Returns the last modified time of this file. * <p> * This implementation throws an exception. */ @Override protected long doGetLastModifiedTime() throws Exception { final Header header = method.getResponseHeader("last-modified"); if (header == null) { throw new FileSystemException("vfs.provider.http/last-modified.error", getName()); } return DateUtil.parseDate(header.getValue()).getTime(); }
@Override public FileContentInfo create(final FileContent fileContent) throws FileSystemException { final HttpFileObject httpFile = (HttpFileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile()); String contentType = null; String contentEncoding = null; HeadMethod headMethod; try { headMethod = httpFile.getHeadMethod(); } catch (final IOException e) { throw new FileSystemException(e); } final Header header = headMethod.getResponseHeader("content-type"); if (header != null) { final HeaderElement[] element = header.getElements(); if (element != null && element.length > 0) { contentType = element[0].getName(); } } contentEncoding = headMethod.getResponseCharSet(); return new DefaultFileContentInfo(contentType, contentEncoding); } }
/** * Prepares a HttpMethod object. * * @param method The object which gets prepared to access the file object. * @throws FileSystemException if an error occurs. * @throws URIException if path cannot be represented. * @since 2.0 (was package) */ protected void setupMethod(final HttpMethod method) throws FileSystemException, URIException { final String pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(this.getUrlCharset()); method.setPath(pathEncoded); method.setFollowRedirects(this.getFollowRedirect()); method.setRequestHeader("User-Agent", this.getUserAgent()); }
/** * Creates an input stream to read the file content from. Is only called if {@link #doGetType} returns * {@link FileType#FILE}. * <p> * It is guaranteed that there are no open output streams for this file when this method is called. * <p> * The returned stream does not have to be buffered. */ @Override protected InputStream doGetInputStream() throws Exception { final GetMethod getMethod = new GetMethod(); setupMethod(getMethod); final int status = getAbstractFileSystem().getClient().executeMethod(getMethod); if (status == HttpURLConnection.HTTP_NOT_FOUND) { throw new FileNotFoundException(getName()); } if (status != HttpURLConnection.HTTP_OK) { throw new FileSystemException("vfs.provider.http/get.error", getName(), Integer.valueOf(status)); } return new HttpInputStream(getMethod); }
HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; } method = new HeadMethod(); setupMethod(method); final HttpClient client = getAbstractFileSystem().getClient(); client.executeMethod(method); method.releaseConnection(); return method; }
/** * Determines the type of this file. Must not return null. The return value of this method is cached, so the * implementation can be expensive. */ @Override protected FileType doGetType() throws Exception { // Use the HEAD method to probe the file. final int status = this.getHeadMethod().getStatusCode(); if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_BAD_METHOD /* method is bad, but resource exist */) { return FileType.FILE; } else if (status == HttpURLConnection.HTTP_NOT_FOUND || status == HttpURLConnection.HTTP_GONE) { return FileType.IMAGINARY; } else { throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status)); } }
fileObject.setupMethod(getMethod); getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-"); final int status = fileSystem.getClient().executeMethod(getMethod); if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) { throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(), Long.valueOf(filePointer), Integer.valueOf(status)); final long skipped = mis.skip(filePointer); if (skipped != filePointer) { throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(), Long.valueOf(filePointer), Integer.valueOf(status));
/** * Returns the last modified time of this file. * <p> * This implementation throws an exception. */ @Override protected long doGetLastModifiedTime() throws Exception { final Header header = method.getResponseHeader("last-modified"); FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName()); return DateUtil.parseDate(header.getValue()).getTime(); }
@Override public FileContentInfo create(final FileContent fileContent) throws FileSystemException { final HttpFileObject httpFile = (HttpFileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile()); String contentType = null; String contentEncoding = null; HeadMethod headMethod; try { headMethod = httpFile.getHeadMethod(); } catch (final IOException e) { throw new FileSystemException(e); } final Header header = headMethod.getResponseHeader("content-type"); if (header != null) { final HeaderElement[] element = header.getElements(); if (element != null && element.length > 0) { contentType = element[0].getName(); } } contentEncoding = headMethod.getResponseCharSet(); return new DefaultFileContentInfo(contentType, contentEncoding); } }
/** * Prepares a HttpMethod object. * * @param method The object which gets prepared to access the file object. * @throws FileSystemException if an error occurs. * @throws URIException if path cannot be represented. * @since 2.0 (was package) */ protected void setupMethod(final HttpMethod method) throws FileSystemException, URIException { final String pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(this.getUrlCharset()); method.setPath(pathEncoded); method.setFollowRedirects(this.getFollowRedirect()); method.setRequestHeader("User-Agent", this.getUserAgent()); }
/** * Creates an input stream to read the file content from. Is only called if {@link #doGetType} returns * {@link FileType#FILE}. * <p> * It is guaranteed that there are no open output streams for this file when this method is called. * <p> * The returned stream does not have to be buffered. */ @Override protected InputStream doGetInputStream() throws Exception { final GetMethod getMethod = new GetMethod(); setupMethod(getMethod); final int status = getAbstractFileSystem().getClient().executeMethod(getMethod); if (status == HttpURLConnection.HTTP_NOT_FOUND) { throw new FileNotFoundException(getName()); } if (status != HttpURLConnection.HTTP_OK) { throw new FileSystemException("vfs.provider.http/get.error", getName(), Integer.valueOf(status)); } return new HttpInputStream(getMethod); }
HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; } method = new HeadMethod(); setupMethod(method); final HttpClient client = getAbstractFileSystem().getClient(); client.executeMethod(method); method.releaseConnection(); return method; }