001 // Copyright 2009 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.services;
016
017 import org.apache.tapestry5.internal.util.Base64InputStream;
018 import org.apache.tapestry5.services.ClientDataEncoder;
019 import org.apache.tapestry5.services.ClientDataSink;
020 import org.apache.tapestry5.services.URLEncoder;
021
022 import java.io.BufferedInputStream;
023 import java.io.IOException;
024 import java.io.ObjectInputStream;
025 import java.util.zip.GZIPInputStream;
026
027 public class ClientDataEncoderImpl implements ClientDataEncoder
028 {
029 private final URLEncoder urlEncoder;
030
031 public ClientDataEncoderImpl(URLEncoder urlEncoder)
032 {
033 this.urlEncoder = urlEncoder;
034 }
035
036 public ClientDataSink createSink()
037 {
038 try
039 {
040 return new ClientDataSinkImpl(urlEncoder);
041 }
042 catch (IOException ex)
043 {
044 throw new RuntimeException(ex);
045 }
046 }
047
048 public ObjectInputStream decodeClientData(String clientData)
049 {
050 // The clientData is Base64 that's been gzip'ed (i.e., this matches
051 // what ClientDataSinkImpl does.
052
053 try
054 {
055 BufferedInputStream buffered = new BufferedInputStream(
056 new GZIPInputStream(new Base64InputStream(clientData)));
057
058 return new ObjectInputStream(buffered);
059 }
060 catch (IOException ex)
061 {
062 throw new RuntimeException(ex);
063 }
064 }
065
066 public ObjectInputStream decodeEncodedClientData(String clientData) throws IOException
067 {
068 return decodeClientData(urlEncoder.decode(clientData));
069 }
070 }