Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/projectapollo/demo/General/GZIP/GZIPBroker.java


1   package org.projectapollo.demo.General.GZIP;
2   
3   import apollo.*;
4   
5   import java.util.zip.*;
6   import java.io.*;
7   
8   import apollo.Log.*;
9   
10  import apollo.Template.*;
11  import apollo.Storable.*;
12  import apollo.Session.*;
13  
14  import org.projectapollo.demo.Storable.*;
15  import org.projectapollo.demo.General.GZIP.Download.*;
16  
17  import java.util.*;
18  
19  
20  
21  public class GZIPBroker extends PageBroker {
22    private Vector AS;
23    
24    AcceptClause displayEntry;
25    AcceptClause uploadFile;
26    public GZIPBroker(ManagerTracker MT, String fquid, PageBroker PB) throws TemplatePageException {
27      super(MT,fquid,PB);
28      this.registerPageHandler("Download",new DownloadBroker(MT,fquid+".Download", this));
29      MT.getPM().registerTemplatePage(FQUID, new TemplatePage(MT,this));
30  
31      AS = new Vector();
32      displayEntry= new AcceptClause("DisplayEntry");
33      uploadFile= new AcceptClause("UploadFile");
34      uploadFile.addAcceptClause(AcceptClause.STRING, "Upload", AcceptClause.REQUIRED);
35  
36      AS.addElement(uploadFile);
37      AS.addElement(displayEntry);
38    }
39    
40    public HTTPResponse render(TransactionTracker TT, HTTPRequest req, WebSession thisSession) throws ApolloException {
41      EntryAssertionManager EAM = new EntryAssertionManager(MT, AS);
42      EAM.evaluate(req);
43  
44      User user = (User)thisSession.getValue("User");
45  
46      Hashtable RT = new Hashtable();
47  
48      if (EAM.isAccept(uploadFile)) {
49        HTTPFile file = req.getFile("GZIPFile");
50        if (file.getSize()!=0) {
51          thisSession.setValue("GZIPFileSize", new Integer(file.getFileData().length));
52          //GZIP the data
53          try {
54            ByteArrayOutputStream GZIPOutputDataStream = new ByteArrayOutputStream();
55            GZIPOutputStream GZIPOS = new GZIPOutputStream(GZIPOutputDataStream);
56  
57            GZIPOS.write(file.getFileData());
58            GZIPOS.finish();
59            thisSession.setValue("GZIPedFileSize",new Integer(GZIPOutputDataStream.toString().length()));
60          } catch (Exception e){
61            thisSession.remove("GZIPFileSize");
62            thisSession.remove("GZIPedFileSize");
63          }
64        }
65      }
66      if (thisSession.hasValue("GZIPFileSize")) {
67        int originalFileSize = ((Integer)thisSession.getValue("GZIPFileSize")).intValue();
68        int newFileSize = ((Integer)thisSession.getValue("GZIPedFileSize")).intValue();
69  
70        double percentage = 100-((double)newFileSize/(double)originalFileSize)*100;
71        RT.put("BeforeSize", new Integer(originalFileSize));
72        RT.put("AfterSize", new Integer(newFileSize));
73        RT.put("Percentage", new Integer((int) percentage));
74        RT.put("PercentageDecimal", new Integer((int) percentage));
75        RT.put("UncompressedSeconds",new Integer((int)((double)originalFileSize/(double)2500)));
76        RT.put("CompressedSeconds",new Integer((int)((double)newFileSize/(double)2500)));
77             
78      }
79      RT.put("FileStatsAvailable", new Boolean(thisSession.hasValue("GZIPFileSize")));
80      return MT.getPM().getPage(FQUID).render(RT,thisSession);
81  
82    }//  end render
83    
84  }