// Returns Success public async Task <bool> DecompressAsync(IMessage message) { var metadata = message.GetMetadata(); if (metadata.Encrypted) { return(false); } // Don't decompress before decryption. if (metadata.Compressed) { try { message.Body = (await CompressionProvider.DecompressAsync(message.Body).ConfigureAwait(false)).ToArray(); metadata.Compressed = false; metadata.CustomFields[Constants.HeaderForCompressed] = false; if (metadata.CustomFields.ContainsKey(Constants.HeaderForCompression)) { metadata.CustomFields.Remove(Constants.HeaderForCompression); } } catch { return(false); } } return(true); }
// Returns Success public async Task <bool> DecompressAsync(Letter letter) { if (letter.LetterMetadata.Encrypted) { return(false); } // Don't decompress before decryption. if (letter.LetterMetadata.Compressed) { try { letter.Body = await CompressionProvider.DecompressAsync(letter.Body).ConfigureAwait(false); letter.LetterMetadata.Compressed = false; letter.LetterMetadata.CustomFields[Constants.HeaderForCompressed] = false; if (letter.LetterMetadata.CustomFields.ContainsKey(Constants.HeaderForCompression)) { letter.LetterMetadata.CustomFields.Remove(Constants.HeaderForCompression); } } catch { return(false); } } return(true); }
public void Compress_ObjectsWithDifferentContent_HaveDifferentResultBytes() { var subject1 = (string)new Word(); var subject2 = (string)new Word(); var provider = new CompressionProvider(); var result1 = provider.Compress(subject1); var result2 = provider.Compress(subject2); CollectionAssert.AreNotEquivalent(result1, result2); }
protected object DeserializeReplyInternal(Message message, object[] parameters) { var retParamInfo = ParameterTypes.FirstOrDefault(x => x.ParamType == ParamType.Return); if (retParamInfo == null) { return(null); } var store = ObjectBuilder.GetModelStore(); var serializer = ObjectBuilder.GetSerializer(); var reader = message.GetReaderAtBodyContents(); reader.Read(); var model = store.GetModel(retParamInfo.Type, ModeType.Wcf); if (model == null) { throw new InvalidOperationException("The model cannot be null, meta data fetch failed. Type: " + retParamInfo.Type.FullName); } reader.Read(); var val = reader.Value; var data = BinaryConverter.FromString(val); var compressionType = GetMessageCompressionTypeOptions(message); if (compressionType != CompressionTypeOptions.None) { var compressionProvider = new CompressionProvider(); data = compressionProvider.DeCompress(data, compressionType); } object retVal; try { retVal = serializer.Deserialize(data, model.MetaData, retParamInfo.Type, ModeType.Wcf); } catch (SerializationException) { store.RemoveModel(retParamInfo.Type, ModeType.Wcf); throw; } return(retVal); }
public void Compress_CompressedContentSize_IsLessThanOriginal() { var subject = string.Join( " ", Enumerable.Range(0, 5).Select(_ => new Paragraph(Tongue.Babylonian)) ); var provider = new CompressionProvider(); var result = provider.Compress(subject); Assert.Less(result.Length, subject.Length); }
protected void DeserializeRequestInternal(Message message, object[] parameters) { var provider = ObjectBuilder.GetModelProvider(); var serializer = ObjectBuilder.GetSerializer(); var compressionType = GetMessageCompressionTypeOptions(message); CompressionProvider compressionProvider = null; if (compressionType != CompressionTypeOptions.None) { compressionProvider = new CompressionProvider(); } var reader = message.GetReaderAtBodyContents(); reader.Read(); for (var i = 0; i < parameters.Length; i++) { var model = provider.CreateModelInfo(ParameterTypes[i].Type, ModeType.Wcf); reader.Read(); var val = reader.Value; var data = BinaryConverter.FromString(val); if (compressionProvider != null) { data = compressionProvider.DeCompress(data, compressionType); } object retVal; try { retVal = serializer.Deserialize(data, model.MetaData, ParameterTypes[i].Type, ModeType.Wcf); } catch (SerializationException) { throw FaultException.CreateFault( MessageFault.CreateFault( new FaultCode(Constants.SerializationFaultCode.ToString(CultureInfo.InvariantCulture)), "Serialization failed, meta data removal is recommended.")); } parameters[i] = retVal; } }
protected Message SerializeReplyInternal(MessageVersion messageVersion, object[] parameters, object result) { var retParamInfo = ParameterTypes.FirstOrDefault(x => x.ParamType == ParamType.Return); //if (retParamInfo == null) // throw new InvalidOperationException("The return parameter type was not found."); var compressionType = DefaultCompressionType; Func <string[]> valueGetter = () => { if (retParamInfo == null) { return(new string[0]); } var modelProvider = ObjectBuilder.GetModelProvider(); var model = modelProvider.CreateModelInfo(retParamInfo.Type, ModeType.Wcf); var serializer = ObjectBuilder.GetSerializer(); var data = (serializer.Serialize(result, model.MetaData, ModeType.Wcf) ?? new SerializationResult(new byte[0], null)).Data; if (compressionType != CompressionTypeOptions.None) { var compressionProvider = new CompressionProvider(); data = compressionProvider.Compress(data, compressionType); } var value = BinaryConverter.ToString(data); return(new[] { value }); }; var message = Message.CreateMessage(messageVersion, ContractInfo.Action + "Response", new ProtoBodyWriter(ContractInfo.OperationContractName, ContractInfo.ServiceNamespace, valueGetter)); if (compressionType != CompressionTypeOptions.None) { AddCompressionHeader(message, DefaultCompressionType); } return(message); }
public void CompressDecompress_ResultingAndOriginalObject_AreEquivalent() { var subject = new { String = "√abcdef€z$*%´´ràéµ£", List = new[] { 123, 456 }, Object = new { Value = 123 } }.ToJson(); var provider = new CompressionProvider(); var result = provider.Decompress(provider.Compress(subject)); Assert.AreEqual(subject, result); }
// Returns Success public async Task <bool> CompressAsync(Letter letter) { if (letter.LetterMetadata.Encrypted) { return(false); } // Don't compress after encryption. if (!letter.LetterMetadata.Compressed) { letter.Body = await CompressionProvider.CompressAsync(letter.Body).ConfigureAwait(false); letter.LetterMetadata.Compressed = true; letter.LetterMetadata.CustomFields[Constants.HeaderForCompressed] = true; letter.LetterMetadata.CustomFields[Constants.HeaderForCompression] = CompressionProvider.Type; return(true); } return(true); }
// Returns Success public async Task <bool> CompressAsync(IMessage message) { var metadata = message.GetMetadata(); if (metadata.Encrypted) { return(false); } // Don't compress after encryption. if (!metadata.Compressed) { message.Body = (await CompressionProvider.CompressAsync(message.Body).ConfigureAwait(false)).ToArray(); metadata.Compressed = true; metadata.CustomFields[Constants.HeaderForCompressed] = true; metadata.CustomFields[Constants.HeaderForCompression] = CompressionProvider.Type; return(true); } return(true); }
protected Message SerializeRequestInternal(MessageVersion messageVersion, object[] parameters) { var compressionType = DefaultCompressionType; var parameterTypes = ParameterTypes; Func <string[]> valueGetter = () => { var store = ObjectBuilder.GetModelStore(); CompressionProvider compressionProvider = null; if (compressionType != CompressionTypeOptions.None) { compressionProvider = new CompressionProvider(); } var serializer = ObjectBuilder.GetSerializer(); var retVal = new string[parameters.Length]; for (var i = 0; i < parameters.Length; i++) { var param = parameters[i]; var model = store.GetModel(parameterTypes[i].Type, ModeType.Wcf); if (model == null) { throw new InvalidOperationException("The model cannot be null, meta data fetch failed. Type: " + parameterTypes[i].Type.FullName); } SerializationResult data; try { data = serializer.Serialize(param, model.MetaData, ModeType.Wcf) ?? new SerializationResult(new byte[0], null); } catch (SerializationException) { if (param != null) { store.RemoveModel(param.GetType(), ModeType.Wcf); } throw; } var byteData = data.Data; if (compressionProvider != null) { byteData = compressionProvider.Compress(byteData, compressionType); } retVal[i] = BinaryConverter.ToString(byteData); } return(retVal); }; var message = Message.CreateMessage(messageVersion, ContractInfo.Action, new ProtoBodyWriter(ContractInfo.OperationContractName, ContractInfo.ServiceNamespace, valueGetter)); if (compressionType != CompressionTypeOptions.None) { AddCompressionHeader(message, DefaultCompressionType); } return(message); }