Coverage Report - org.trails.io.CallbackStackAdaptor
 
Classes in this File Line Coverage Branch Coverage Complexity
CallbackStackAdaptor
44% 
0% 
0
 
 1  
 package org.trails.io;
 2  
 
 3  
 import java.io.BufferedInputStream;
 4  
 import java.io.BufferedOutputStream;
 5  
 import java.io.BufferedReader;
 6  
 import java.io.ByteArrayInputStream;
 7  
 import java.io.ByteArrayOutputStream;
 8  
 import java.io.IOException;
 9  
 import java.io.InputStreamReader;
 10  
 import java.util.zip.GZIPInputStream;
 11  
 import java.util.zip.GZIPOutputStream;
 12  
 
 13  
 import org.apache.commons.codec.binary.Base64;
 14  
 import org.apache.commons.logging.Log;
 15  
 import org.apache.commons.logging.LogFactory;
 16  
 import org.apache.tapestry.callback.ICallback;
 17  
 import org.apache.tapestry.services.DataSqueezer;
 18  
 import org.apache.tapestry.util.io.SqueezeAdaptor;
 19  
 import org.trails.callback.CallbackStack;
 20  
 
 21  
 /**
 22  
  * Squeezes a {@link CallbackStack}
 23  
  */
 24  8
 public class CallbackStackAdaptor implements SqueezeAdaptor
 25  
 {
 26  
 
 27  4
         private static final Log LOG = LogFactory.getLog(CallbackStackAdaptor.class);
 28  
 
 29  
         public static final String PREFIX = "L";
 30  
         public static final String DELIMITER = "@C";
 31  
 
 32  
         public Class getDataClass()
 33  
         {
 34  0
                 return CallbackStack.class;
 35  
         }
 36  
 
 37  
         public String getPrefix()
 38  
         {
 39  0
                 return PREFIX;
 40  
         }
 41  
 
 42  
         public String squeeze(DataSqueezer squeezer, Object object)
 43  
         {
 44  0
                 CallbackStack stack = (CallbackStack) object;
 45  
 
 46  0
                 StringBuilder builder = new StringBuilder();
 47  
 
 48  0
                 for (ICallback callback : stack)
 49  
                 {
 50  0
                         builder.append(squeezer.squeeze(callback)).append(DELIMITER);
 51  
                 }
 52  
 
 53  0
                 return new StringBuilder(PREFIX).append(compress(builder.toString())).toString();
 54  
         }
 55  
 
 56  
         public Object unsqueeze(DataSqueezer squeezer, String string)
 57  
         {
 58  
 
 59  0
                 String uncompressed = uncompress(string.substring(PREFIX.length()));
 60  0
                 CallbackStack stack = new CallbackStack();
 61  
 
 62  0
                 if (uncompressed != null)
 63  
                 {
 64  0
                         String[] values = uncompressed.split(DELIMITER);
 65  
 
 66  0
                         for (String s : values)
 67  
                         {
 68  0
                                 stack.add((ICallback) squeezer.unsqueeze(s));
 69  
                         }
 70  
                 }
 71  
 
 72  0
                 return stack;
 73  
         }
 74  
 
 75  
         public String compress(String string)
 76  
         {
 77  
                 try
 78  
                 {
 79  4
                         ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
 80  4
                         GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
 81  4
                         BufferedOutputStream oos = new BufferedOutputStream(gos);
 82  4
                         oos.write(string.getBytes());
 83  4
                         oos.close();
 84  
 
 85  4
                         byte[] byteArray = bosCompressed.toByteArray();
 86  4
                         byte[] encoded = Base64.encodeBase64(byteArray);
 87  
 
 88  4
                         return new String(encoded);
 89  
                 }
 90  0
                 catch (Exception ex)
 91  
                 {
 92  0
                         ex.printStackTrace();
 93  
                 }
 94  0
                 return null;
 95  
         }
 96  
 
 97  
         public String uncompress(String string)
 98  
         {
 99  
 
 100  
 
 101  
                 try
 102  
                 {
 103  4
                         byte[] decoded = Base64.decodeBase64(string.getBytes());
 104  
 
 105  4
                         BufferedInputStream is = new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(decoded)));
 106  4
                         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 107  4
                         String uncompressed = reader.readLine();
 108  4
                         is.close();
 109  4
                         return uncompressed;
 110  
 
 111  0
                 } catch (IOException e)
 112  
                 {
 113  0
                         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 114  
                 }
 115  0
                 return null;
 116  
         }
 117  
 }