org.apache.commons.net.io.CopyStreamListener Java Examples

The following examples show how to use org.apache.commons.net.io.CopyStreamListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Node.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ODataResponse getEntityMedia (ODataSingleProcessor processor)
      throws ODataException
{
   initNode ();
   if (hasStream ())
   {
      try
      {
         User u = Security.getCurrentUser();
         String user_name = (u == null ? null : u.getUsername ());
         
         InputStream is = new BufferedInputStream (getStream());
         
         RegulatedInputStream.Builder builder = 
            new RegulatedInputStream.Builder (is, TrafficDirection.OUTBOUND);
         builder.userName (user_name);

         CopyStreamAdapter adapter = new CopyStreamAdapter ();
         CopyStreamListener recorder = new DownloadActionRecordListener (
               this.getId (), this.getName (), u);
         CopyStreamListener closer = new DownloadStreamCloserListener (is);
         adapter.addCopyStreamListener (recorder);
         adapter.addCopyStreamListener (closer);
         builder.copyStreamListener (adapter);
         if (getContentLength ()>0) builder.streamSize(getContentLength());
         is = builder.build();

         String etag = getName () + "-" + getContentLength ();

         // A priori Node never change, so the lastModified should be as
         // far as possible than today.
         long last_modified =  System.currentTimeMillis () - ONE_YEAR_MS;

         // If node is not a data file, it cannot be downloaded and set to -1
         // As a stream exists, this control is probably obsolete.
         long content_length = getContentLength ()==0?-1:getContentLength ();

         return MediaResponseBuilder.prepareMediaResponse(etag, getName(),
            getContentType (), last_modified, content_length,
            processor.getContext (), is);
      }
      catch (Exception e)
      {
         throw new ODataException (
            "An exception occured while creating the stream for node " + 
            getName(), e);
      }
   }
   else
   {
      throw new ODataException ("No stream for node " + getName ());
   }
}