Clover coverage report - Code Coverage for tapestry release 4.0.3
Coverage timestamp: Fri May 5 2006 21:17:42 CDT
file stats: LOC: 196   Methods: 5
NCLOC: 113   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PersistentPropertyDataEncoderImpl.java 100% 100% 100% 100%
coverage
 1    // Copyright 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.record;
 16   
 17    import java.io.BufferedInputStream;
 18    import java.io.BufferedOutputStream;
 19    import java.io.ByteArrayInputStream;
 20    import java.io.ByteArrayOutputStream;
 21    import java.io.IOException;
 22    import java.io.InputStream;
 23    import java.io.ObjectInputStream;
 24    import java.io.ObjectOutputStream;
 25    import java.util.ArrayList;
 26    import java.util.Collections;
 27    import java.util.Iterator;
 28    import java.util.List;
 29    import java.util.zip.GZIPInputStream;
 30    import java.util.zip.GZIPOutputStream;
 31   
 32    import org.apache.commons.codec.binary.Base64;
 33    import org.apache.hivemind.ApplicationRuntimeException;
 34    import org.apache.hivemind.ClassResolver;
 35    import org.apache.hivemind.HiveMind;
 36    import org.apache.hivemind.util.Defense;
 37    import org.apache.tapestry.util.io.ResolvingObjectInputStream;
 38    import org.apache.tapestry.util.io.TeeOutputStream;
 39   
 40    /**
 41    * Responsible for converting lists of {@link org.apache.tapestry.record.PropertyChange}s back and
 42    * forth to a URL safe encoded string.
 43    * <p>
 44    * A possible improvement would be to encode the binary data with encryption both on and off, and
 45    * select the shortest (prefixing with a character that identifies whether encryption should be used
 46    * to decode).
 47    *
 48    * @author Howard M. Lewis Ship
 49    * @since 4.0
 50    */
 51    public class PersistentPropertyDataEncoderImpl implements PersistentPropertyDataEncoder
 52    {
 53    private ClassResolver _classResolver;
 54   
 55    /**
 56    * Prefix on the MIME encoding that indicates that the encoded data is not encoded.
 57    */
 58   
 59    public static final String BYTESTREAM_PREFIX = "B";
 60   
 61    /**
 62    * Prefix on the MIME encoding that indicates that the encoded data is encoded with GZIP.
 63    */
 64   
 65    public static final String GZIP_BYTESTREAM_PREFIX = "Z";
 66   
 67  15 public String encodePageChanges(List changes)
 68    {
 69  15 Defense.notNull(changes, "changes");
 70   
 71  15 if (changes.isEmpty())
 72  3 return "";
 73   
 74  12 try
 75    {
 76  12 ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
 77  12 ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
 78   
 79  12 GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
 80   
 81  12 TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
 82   
 83  12 ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
 84   
 85  12 writeChangesToStream(changes, oos);
 86   
 87  9 oos.close();
 88   
 89  9 boolean useCompressed = bosCompressed.size() < bosPlain.size();
 90   
 91  9 byte[] data = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
 92   
 93  9 byte[] encoded = Base64.encodeBase64(data);
 94   
 95  9 String prefix = useCompressed ? GZIP_BYTESTREAM_PREFIX : BYTESTREAM_PREFIX;
 96   
 97  9 return prefix + new String(encoded);
 98    }
 99    catch (Exception ex)
 100    {
 101  3 throw new ApplicationRuntimeException(RecordMessages.encodeFailure(ex), ex);
 102    }
 103    }
 104   
 105  18 public List decodePageChanges(String encoded)
 106    {
 107  18 if (HiveMind.isBlank(encoded))
 108  3 return Collections.EMPTY_LIST;
 109   
 110  15 String prefix = encoded.substring(0, 1);
 111   
 112  15 if (!(prefix.equals(BYTESTREAM_PREFIX) || prefix.equals(GZIP_BYTESTREAM_PREFIX)))
 113  3 throw new ApplicationRuntimeException(RecordMessages.unknownPrefix(prefix));
 114   
 115  12 try
 116    {
 117    // Strip off the prefix, feed that in as a MIME stream.
 118   
 119  12 byte[] decoded = Base64.decodeBase64(encoded.substring(1).getBytes());
 120   
 121  12 InputStream is = new ByteArrayInputStream(decoded);
 122   
 123  12 if (prefix.equals(GZIP_BYTESTREAM_PREFIX))
 124  9 is = new GZIPInputStream(is);
 125   
 126    // I believe this is more efficient; the buffered input stream should ask the
 127    // GZIP stream for large blocks of un-gzipped bytes, with should be more efficient.
 128    // The object input stream will probably be looking for just a few bytes at
 129    // a time. We use a resolving object input stream that knows how to find
 130    // classes not normally acessible.
 131   
 132  9 ObjectInputStream ois = new ResolvingObjectInputStream(_classResolver,
 133    new BufferedInputStream(is));
 134   
 135  9 List result = readChangesFromStream(ois);
 136   
 137  9 ois.close();
 138   
 139  9 return result;
 140    }
 141    catch (Exception ex)
 142    {
 143  3 throw new ApplicationRuntimeException(RecordMessages.decodeFailure(ex), ex);
 144    }
 145    }
 146   
 147  12 private void writeChangesToStream(List changes, ObjectOutputStream oos) throws IOException
 148    {
 149  12 oos.writeInt(changes.size());
 150   
 151  12 Iterator i = changes.iterator();
 152  12 while (i.hasNext())
 153    {
 154  69 PropertyChange pc = (PropertyChange) i.next();
 155   
 156  69 String componentPath = pc.getComponentPath();
 157  69 String propertyName = pc.getPropertyName();
 158  69 Object value = pc.getNewValue();
 159   
 160  69 oos.writeBoolean(componentPath != null);
 161   
 162  69 if (componentPath != null)
 163  33 oos.writeUTF(componentPath);
 164   
 165  69 oos.writeUTF(propertyName);
 166  69 oos.writeObject(value);
 167    }
 168    }
 169   
 170  9 private List readChangesFromStream(ObjectInputStream ois) throws IOException,
 171    ClassNotFoundException
 172    {
 173  9 List result = new ArrayList();
 174   
 175  9 int count = ois.readInt();
 176   
 177  9 for (int i = 0; i < count; i++)
 178    {
 179  66 boolean hasPath = ois.readBoolean();
 180  66 String componentPath = hasPath ? ois.readUTF() : null;
 181  66 String propertyName = ois.readUTF();
 182  66 Object value = ois.readObject();
 183   
 184  66 PropertyChangeImpl pc = new PropertyChangeImpl(componentPath, propertyName, value);
 185   
 186  66 result.add(pc);
 187    }
 188   
 189  9 return result;
 190    }
 191   
 192  33 public void setClassResolver(ClassResolver resolver)
 193    {
 194  33 _classResolver = resolver;
 195    }
 196    }