Interface File

All Superinterfaces:
HierarchyItem

public interface File extends HierarchyItem
Represents a file in the WebDAV repository.

Defines methods that WebDAV server file items must implement. In addition to methods provided by HierarchyItem this interface also provides methods for reading and writing file content. getContentType()> method must return the MIME type of the file.

  • Method Details

    • getContentType

      String getContentType() throws ServerException
      Gets the media type of the file.

      Mime-type provided by this method is returned in a Content-Type header with GET request.

      When deciding which action to perform when downloading a file some WebDAV clients and browsers (such as Internet Explorer) rely on file extension, while others (such as Firefox) rely on Content-Type header returned by server. For identical behavior in all browsers and WebDAV clients your server must return a correct mime-type with a requested file.

      Returns:
      MIME type of the file.
      Throws:
      ServerException - In case of an error.
    • getEtag

      String getEtag() throws ServerException
      Gets entity tag - string that identifies current state of resource's content. More information about etags is available here: http://en.wikipedia.org/wiki/HTTP_ETag You can return here either checksum or hash or counter which increases with every modification. This property shall return different value if content changes. Return null to indicate that server doesn't support etags.
      Returns:
      return here either cheksum or hash or counter which increases with every modification. Return null to indicate that server doesn't support etags.
      Throws:
      ServerException - In case of an error
    • getContentLength

      long getContentLength() throws ServerException
      Gets the size of the file content in bytes.
      Returns:
      Length of the file content in bytes.
      Throws:
      ServerException - In case of an error.
    • read

      void read(OutputStream output, long startIndex, long count) throws ServerException, IOException
      Writes the content of the file to the specified stream.

      Client application can request only a part of a file specifying Range header. Download managers may use this header to download single file using several threads at a time.

      Parameters:
      output - Output stream.
      startIndex - Zero-bazed byte offset in file content at which to begin copying bytes to the output stream.
      count - Number of bytes to be written to the output stream.
      Throws:
      ServerException - In case of an error.
      IOException - I/O exception occurred.

      The code below is part of Collection Synchronization sample provided with the SDK.
          @Override
          public void read(OutputStream out, long startIndex, long count) throws ServerException {
              Path fullPath = this.getFullPath();
              byte[] buf = new byte[BUFFER_SIZE];
              int retVal;
              try (InputStream in = Files.newInputStream(fullPath)) {
                  in.skip(startIndex);
                  while ((retVal = in.read(buf)) > 0) {
                      // Strict servlet API doesn't allow to write more bytes then content length. So we do this trick.
                      if (retVal > count) {
                          retVal = (int) count;
                      }
                      out.write(buf, 0, retVal);
                      startIndex += retVal;
                      count -= retVal;
                  }
              } catch (IOException x) {
                  throw new ServerException(x);
              }
          }
      
      
    • write

      long write(InputStream content, String contentType, long startIndex, long totalFileSize) throws LockedException, ServerException, IOException
      Saves the content of the file from the specified stream to the WebDAV repository.

      If totalContentLength is -1 then content parameter contains entire file content. startIndex parameter is always 0 in this case.

      The Java WebDAV Server Engine can process two types of upload requests:

      • PUT upload. Files uploaded via PUT verb is performed by most RFC 4918 and RFC 2518 compliant WebDAV clients and modern Ajax clients.
      • POST upload. Files uploaded via POST verb is performed by legacy Ajax/HTML clients such as Internet Explorer 9.

      To provide information about what segment of a file is being uploaded PUT request will contain optional Content-Range: bytes XXX-XXX/XXX header.

      The following example demonstrates upload to WebDAV server using POST with multipart encoding by legacy web browser. The file will be created in /mydocs/ folder.

       <html>
          <head><title>POST Upload to WebDAV Server</title></head>
           <body>
               <form action="/mydocs/" method="post" enctype="multipart/form-data">
                   <input type="file" name="dummyname" /><br />
                   <input type="submit" />
               </form>
           </body>
       </html>
       

      Parameters:
      content - InputStream to read the content of the file from.
      contentType - Indicates media type of the file.
      startIndex - Index in file to which corresponds first byte in content.
      totalFileSize - Total size of the file being uploaded. -1 if size is unknown.
      Returns:
      Number of bytes written.

      The code below is part of Collection Synchronization sample provided with the SDK.
          @Override
          public long write(InputStream content, String contentType, long startIndex, long totalFileLength)
                  throws LockedException, ServerException, IOException {
              ensureHasToken();
              SeekableByteChannel writer = Files.newByteChannel(getFullPath(), this.allowedOpenFileOptions);
              if (startIndex == 0) {
                  // If we override the file we must set position to 0 because writer could be at not 0 position.
                  writer = writer.truncate(0);
              } else {
                  // We must set to start position in case of resume upload.
                  writer.position(startIndex);
              }
              writeTotalContentLength(totalFileLength);
              incrementSerialNumber();
              byte[] inputBuffer = new byte[BUFFER_SIZE];
              long totalWrittenBytes = startIndex;
              int readBytes;
              try {
                  while ((readBytes = content.read(inputBuffer)) > -1) {
                      ByteBuffer byteBuffer;
                      byteBuffer = ByteBuffer.wrap(inputBuffer, 0, readBytes);
                      writer.write(byteBuffer);
                      totalWrittenBytes += readBytes;
                  }
      
                  try {
                      getEngine().getSearchFacade().getIndexer().indexFile(getName(), decode(getPath()), null, this);
                  } catch (Exception ex){
                      getEngine().getLogger().logError("Errors during indexing.", ex);
                  }
              } catch (Exception ex) {
                  ex.printStackTrace();
              } finally {
                  writer.close();
              }
              getEngine().getWebSocketServer().notifyUpdated(getPath(), getWebSocketID());
              return totalWrittenBytes;
          }
      
      
      Throws:
      LockedException - File was locked and client did not provide lock token.
      ServerException - In case of an error.
      IOException - I/O error.