// constructors /// <summary> /// /// </summary> /// <param name="moduleInfo"></param> /// <param name="cryptoManager"></param> /// <param name="itemKind"></param> /// <param name="transient"></param> /// <param name="appScope"></param> /// <param name="name"></param> /// <param name="props"></param> /// <param name="data"></param> /// <param name="dataType"></param> /// <param name="serialFormat"></param> /// <param name="lifetime"></param> public ServerItem( IModuleInfo moduleInfo, ICryptoManager cryptoManager, ItemKind itemKind, bool transient, string appScope, string name, NamedValueSet props, object data, Type dataType, SerialFormat serialFormat, TimeSpan lifetime) : base(itemKind, transient, name, appScope) { _moduleInfo = moduleInfo ?? throw new ArgumentNullException(nameof(moduleInfo)); _cryptoManager = cryptoManager ?? throw new ArgumentNullException(nameof(cryptoManager)); if (dataType == null) { throw new ArgumentNullException(nameof(dataType)); } SysProps.Set(SysPropName.SAlg, (int)serialFormat); AppProps.Add(props); _data = data; _dataTypeType = dataType; DataTypeName = dataType.FullName; _lifetime = lifetime; }
private void Serialise() { if (_text == null && _data != null) { var serialFormat = (SerialFormat)SysProps.GetValue(SysPropName.SAlg, 0); switch (serialFormat) { case SerialFormat.Binary: // try Binary serialiser _text = BinarySerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Binary); break; case SerialFormat.Soap: // try Soap serialiser _text = SoapSerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Soap); break; case SerialFormat.Json: // try Json serialiser _text = JsonSerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Json); break; case SerialFormat.Xml: try { _text = XmlSerializerHelper.SerializeToString(_dataTypeType, _data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Xml); } catch (Exception excp) { throw new ApplicationException( "The XmlSerializer has thrown an exception: '" + excp.GetType().Name + "'. " + "If your intent was to use the BinaryFormatter or SoapFormatter for serialisation, " + "then you should set the SerialFormat property appropriately.", excp); } break; default: // use default xml serialiser try { _text = XmlSerializerHelper.SerializeToString(_dataTypeType, _data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Xml); } catch (Exception excp) { throw new ApplicationException( "The XmlSerializer has thrown an exception: '" + excp.GetType().Name + "'. " + "If your intent was to use the BinaryFormatter or SoapFormatter for serialisation, " + "then you should set the SerialFormat property appropriately.", excp); } break; } } }
/// <summary> /// /// </summary> /// <param name="text"></param> /// <param name="dataType"></param> public void SetText(string text, Type dataType) { CheckNotFrozen(); _text = text; SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Undefined); _data = null; _dataTypeType = dataType; DataTypeName = dataType.FullName; }
/// <summary> /// /// </summary> /// <param name="itemKind"></param> /// <param name="transient"></param> /// <param name="appScope"></param> /// <param name="name"></param> /// <param name="props"></param> /// <param name="serialisedData"></param> /// <param name="dataTypeName"></param> /// <param name="lifetime"></param> public ServerItem( ItemKind itemKind, bool transient, string appScope, string name, NamedValueSet props, string serialisedData, string dataTypeName, TimeSpan lifetime) : base(itemKind, transient, name, appScope) { AppProps.Add(props); _text = serialisedData; SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Undefined); DataTypeName = dataTypeName; _lifetime = lifetime; }
/// <summary> /// /// </summary> public void Freeze() { if (_frozen) { return; } if (Name == null) { throw new ApplicationException("Item name not set!"); } TimeSpan maxLifetime = DateTimeOffset.MaxValue - DateTimeOffset.Now - TimeSpan.FromDays(1); if (_lifetime > maxLifetime) { _lifetime = maxLifetime; } if (_lifetime < TimeSpan.Zero) { _lifetime = TimeSpan.Zero; } Created = DateTimeOffset.Now; Expires = Created.Add(_lifetime); // serialise the data if required Serialise(); if (DataTypeName == null) { DataTypeName = ""; } if (_text == null) { //_Text = ""; SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Undefined); } SysProps.Set(SysPropName.TLen, _text?.Length ?? 0); // compress the data _zData = CompressionHelper.CompressToBuffer(_text); SysProps.Set(SysPropName.ZAlg, 1); SysProps.Set(SysPropName.ZLen, _zData?.Length ?? 0); // do symmetric encryption 1st, if required var xtki = SysProps.GetValue <String>(SysPropName.XTKI, null); if (xtki != null) { _xData = _cryptoManager.EncryptWithTranspKey(xtki, _zData); SysProps.Set(SysPropName.XAlg, 1); } else { _xData = _zData; } SysProps.Set(SysPropName.XLen, _xData?.Length ?? 0); // do asymmetric encryption 2nd, if required var yrki = SysProps.GetValue <String>(SysPropName.YRKI, null); if (yrki != null) { SysProps.Set(SysPropName.YAlg, 1); YData = _cryptoManager.EncryptWithPublicKey(yrki, _xData); } else { YData = _xData; } YDataHash = CalculateBufferHash(YData); SysProps.Set(SysPropName.YLen, YData?.Length ?? 0); // do public signature 3rd, if required var yski = SysProps.GetValue <String>(SysPropName.YSKI, null); if (yski != null) { SysProps.Set(SysPropName.YAlg, 1); YSign = _cryptoManager.CreateSignature(yski, YData); } // add other publisher properties SysProps.Set(SysPropName.ApplName, _moduleInfo.ApplName); SysProps.Set(SysPropName.ApplFVer, _moduleInfo.ApplFVer); SysProps.Set(SysPropName.ApplPTok, _moduleInfo.ApplPTok); SysProps.Set(SysPropName.CoreFVer, _moduleInfo.CoreFVer); SysProps.Set(SysPropName.CorePTok, _moduleInfo.CorePTok); SysProps.Set(SysPropName.HostName, _moduleInfo.HostName); SysProps.Set(SysPropName.UserName, _moduleInfo.UserName); SysProps.Set(SysPropName.UserWDom, _moduleInfo.UserWDom); SysProps.Set(SysPropName.UserIdentity, _moduleInfo.Name); SysProps.Set(SysPropName.UserFullName, _moduleInfo.UserFullName); SysProps.Set(SysPropName.OrgEnvId, EnvHelper.EnvName(_moduleInfo.ConfigEnv)); SysProps.Set(SysPropName.NodeGuid, _moduleInfo.NodeGuid); // done _frozen = true; }