Skip to content

Commit

Permalink
Merge pull request #30 from solarwinds/Madhavan-OrionSDK
Browse files Browse the repository at this point in the history
Handling base64 encoding in swql studio
  • Loading branch information
derhally committed Mar 17, 2016
2 parents c01aa0b + a2902ff commit 333fbd4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
61 changes: 61 additions & 0 deletions Src/Contract/InfoServiceDefaultBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;

namespace SolarWinds.InformationService.Contract2
{
class InfoServiceDefaultBehaviour : IEndpointBehavior, IClientMessageInspector
{
private static readonly IDictionary<string, object> _defaultHeaderValue = new Dictionary<string, object>()
{
{ "IsBase64EncodingAccepted", true }
};

#region IEndpointBehavior Members

public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
throw new NotImplementedException();
}

public void Validate(ServiceEndpoint endpoint)
{
}

#endregion

#region IClientMessageInspector Members

public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
foreach (var kv in _defaultHeaderValue)
{
if (request.Headers.FindHeader(kv.Key, Constants.Namespace) == -1)
{
request.Headers.Add(MessageHeader.CreateHeader(kv.Key, Constants.Namespace, kv.Value));
}
}

return null;
}

#endregion
}
}
1 change: 1 addition & 0 deletions Src/Contract/InfoServiceProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private void CorrectChannelFactory()
// ???: how can I detect that channel binding is securited

_activityMonitor = new InfoServiceActivityMonitor();
_channelFactory.Endpoint.Behaviors.Add(new InfoServiceDefaultBehaviour());
_channelFactory.Endpoint.Behaviors.Add(_activityMonitor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ private enum ParserState
private List<ColumnInfo> columns;
private int currentColumnOrdinal;
private bool currentColumnDBNull;
private bool currentColumnEncoded;
private string currentColumnEncodingType;
private object[] values;
private List<object> arrayColumnValues = new List<object>();
private bool closed = false;
Expand All @@ -321,6 +323,10 @@ private enum ParserState
internal const string DBNullPrefix = "xsi";
internal const string DBNullAttribute = "nil";
internal const string DBNullValue = "true";
internal const string DBBase64 = "Base64";

internal const string IsEncodedAttribute = "isEncoded";
internal const string EncodingTypeAttribute = "encodingType";

public List<ErrorMessage> Errors { get; private set; }

Expand Down Expand Up @@ -546,9 +552,6 @@ public override bool Read()
if (this.closed)
throw new InvalidOperationException("DataReader is closed");

if (!this.hasRows)
return false;

return ReadNextEntity();
}

Expand Down Expand Up @@ -768,7 +771,10 @@ private bool ReadNextEntity()
return false;

case ParserState.Root:
ValidateEndElement(reader.LocalName, "queryResult", ParserState.Errors);
if (hasRows)
ValidateEndElement(reader.LocalName, "queryResult", ParserState.Errors);
else
return false;
break;

case ParserState.Data:
Expand Down Expand Up @@ -863,6 +869,9 @@ private void BeginColumn(XmlReader reader)
string dbNullAtt = reader.GetAttribute(DBNullAttribute, DBNullNamespace);
this.currentColumnDBNull = (!string.IsNullOrEmpty(dbNullAtt) && dbNullAtt.CompareTo(DBNullValue) == 0);

bool.TryParse(reader.GetAttribute(IsEncodedAttribute), out this.currentColumnEncoded);
this.currentColumnEncodingType = this.currentColumnEncoded ? reader.GetAttribute(EncodingTypeAttribute) : string.Empty;

ColumnInfo columnInfo = this.columns[this.currentColumnOrdinal];
if (columnInfo.IsArray)
this.state = ParserState.ArrayColumn;
Expand Down Expand Up @@ -919,6 +928,13 @@ private object DeserializeScalarValue(string value, EntityPropertyType columnTyp
{
switch (columnType)
{
case EntityPropertyType.String:
if (this.currentColumnEncoded)
{
if (DBBase64.Equals(this.currentColumnEncodingType, StringComparison.OrdinalIgnoreCase))
value = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value));
}
return Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
case EntityPropertyType.Boolean:
case EntityPropertyType.Byte:
case EntityPropertyType.Char:
Expand All @@ -928,7 +944,6 @@ private object DeserializeScalarValue(string value, EntityPropertyType columnTyp
case EntityPropertyType.Int32:
case EntityPropertyType.Int64:
case EntityPropertyType.Single:
case EntityPropertyType.String:
case EntityPropertyType.Type:
case EntityPropertyType.Uri:
return Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
Expand Down
1 change: 1 addition & 0 deletions Src/Contract/SolarWinds.InformationService.Contract.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Constants.cs" />
<Compile Include="ErrorMessage.cs" />
<Compile Include="InformationServiceClient\InformationServiceXmlReader.cs" />
<Compile Include="InfoServiceDefaultBehaviour.cs" />
<Compile Include="PubSub\INotificationDeliveryService.cs" />
<Compile Include="IStreamInformationService.cs" />
<Compile Include="InfoServiceActivityMonitor.cs" />
Expand Down

0 comments on commit 333fbd4

Please sign in to comment.