001    /*
002     * This file is part of the Echo Point Project.  This project is a
003     * collection of Components that have extended the Echo Web Application
004     * Framework Version 3.
005     *
006     * Version: MPL 1.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     */
018    package echopoint.tucana;
019    
020    import echopoint.tucana.event.UploadProgressEvent;
021    import nextapp.echo.webcontainer.Connection;
022    import nextapp.echo.webcontainer.ContentType;
023    import nextapp.echo.webcontainer.Service;
024    import nextapp.echo.webcontainer.WebContainerServlet;
025    
026    import java.io.IOException;
027    
028    /**
029     * Provides information about file upload progress.
030     *
031     * @author Echo File Transfer Library
032     * @version $Id: UploadProgressService.java 106 2009-02-03 16:00:33Z sptrakesh $
033     */
034    public class UploadProgressService extends BaseUploadService
035    {
036      private static final Service INSTANCE = new UploadProgressService();
037    
038      static
039      {
040        WebContainerServlet.getServiceRegistry().add( INSTANCE );
041      }
042    
043      private UploadProgressService()
044      {
045        super();
046      }
047    
048      /** Installs this service. */
049      public static void install()
050      {
051        // Do nothing, simply ensure static directives are executed.
052      }
053    
054      /** @see nextapp.echo.webcontainer.Service#getId() */
055      public String getId()
056      {
057        return "echopoint.tucana.UploadProgressService";
058      }
059    
060      /** @see nextapp.echo.webcontainer.Service#getVersion() */
061      public int getVersion()
062      {
063        return DO_NOT_CACHE;
064      }
065    
066      /** {@inheritDoc} */
067      public void service( final Connection conn,
068          final FileUploadSelector uploadSelect, final String uploadIndex )
069        throws IOException
070      {
071        final UploadRenderState renderState = FileUploadSelectorPeer.getRenderState(
072            uploadSelect, conn.getUserInstance() );
073        final UploadProgress progress = renderState.getProgress( uploadIndex );
074    
075        final StringBuilder buff = new StringBuilder( 128 );
076        if ( progress != null && progress.getBytesRead() > 0 )
077        {
078          if ( !renderState.isUploadEnded( uploadIndex ) )
079          {
080            uploadSelect.notifyCallback(
081                new UploadProgressEvent( uploadSelect, uploadIndex, progress ) );
082          }
083    
084          buff.append( "{" );
085          buff.append( "r:" ).append( progress.getBytesRead() ).append( "," );
086          buff.append( "cl:" ).append( progress.getContentLength() ).append( "," );
087          buff.append( "pc:" ).append( progress.getPercentCompleted() ).append( "," );
088          buff.append( "tr:" ).append( progress.getTransferRate() ).append( "," );
089          buff.append( "tl:" ).append( progress.getEstimatedTimeLeft() ).append( "," );
090          buff.append( "s:'" ).append( progress.getStatus() ).append( "'," );
091          buff.append( "m:'" ).append( progress.getMessage() ).append( "'" );
092          buff.append( "}" );
093        }
094        else
095        {
096          buff.append( "{r:0,cl:0,pc:0,tr:0,tl:0,s:'failed',m:''}" );
097        }
098    
099        conn.setContentType( ContentType.TEXT_PLAIN );
100        conn.getWriter().write( buff.toString() );
101        conn.getWriter().flush();
102      }
103    }