Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxantron committed Jul 11, 2022
2 parents 34edc3b + 1c38798 commit 26dfa55
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.2
3.5.0
5 changes: 4 additions & 1 deletion src/Moryx.Asp.Extensions/MoryxServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) 2022, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down
40 changes: 40 additions & 0 deletions src/Moryx.Asp.Extensions/WebModuleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2022, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;

namespace Moryx.Asp.Integration
{
/// <summary>
/// Decorator for web modules
/// </summary>
public class WebModuleAttribute : Attribute
{
/// <summary>
/// Unique route of this module
/// </summary>
public string Route { get; }

/// <summary>
/// Recognizable icon of this module
/// </summary>
public string Icon { get; }

/// <summary>
/// Export page as web module under given route
/// </summary>
public WebModuleAttribute(string route) : this(route, "favorite")
{

}

/// <summary>
/// Export page as web module under given route and icon
/// </summary>
public WebModuleAttribute(string route, string icon)
{
Route = route;
Icon = icon;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ private async Task EvaluateResponse(Task<Communication.Endpoints.Endpoint[]> res
// Compare version
if (VersionCompare.ClientMatch(serverVersion, clientVersion))
{
// Create new base address client
HttpClient = new HttpClient { BaseAddress = new Uri(endpoint.Address) };
// Create HttpClient
var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig;
HttpClient = HttpClientBuilder.GetClient(endpoint.Address, proxyConfig);

if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag))
HttpClient.DefaultRequestHeaders.AcceptLanguage
Expand Down
39 changes: 39 additions & 0 deletions src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Net;
using System.Net.Http;

namespace Moryx.Communication.Endpoints
{
/// <summary>
/// Helper class to get a configured HttpClient
/// </summary>
public static class HttpClientBuilder
{
/// <summary>
/// Creates a HttpClient based on the proxy config and base address
/// </summary>
public static HttpClient GetClient(string baseAddress, IProxyConfig proxyConfig)
{
var httpClient = new HttpClient();

if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy)
{
var proxy = new WebProxy
{
Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true
};

httpClient = new HttpClient(new HttpClientHandler { Proxy = proxy });
}

httpClient.BaseAddress = new Uri(baseAddress);

return httpClient;
}
}
}
16 changes: 16 additions & 0 deletions src/Moryx/Communication/Endpoints/Connector/IProxyConfigAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

namespace Moryx.Communication.Endpoints
{
/// <summary>
/// Enables the access to the used proxy configuration
/// </summary>
public interface IProxyConfigAccess
{
/// <summary>
/// Used proxy configuration
/// </summary>
IProxyConfig ProxyConfig { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
Expand All @@ -13,7 +12,7 @@ namespace Moryx.Communication.Endpoints
/// <summary>
/// Service manager base class for provide active endpoints of the current application runtime
/// </summary>
public abstract class VersionServiceManager<TEndpoint> : IVersionServiceManager
public abstract class VersionServiceManager<TEndpoint> : IVersionServiceManager, IProxyConfigAccess
where TEndpoint : Endpoint
{
private const string ServiceName = "endpoints";
Expand All @@ -22,30 +21,17 @@ public abstract class VersionServiceManager<TEndpoint> : IVersionServiceManager
/// Underlying http client for the requests
/// </summary>
protected HttpClient Client { get; set; }

/// <inheritdoc/>
public IProxyConfig ProxyConfig { get; private set; }

/// <summary>
/// Creates a new instance of the <see cref="VersionServiceManager{TEndpoint}"/>
/// </summary>
protected VersionServiceManager(IProxyConfig proxyConfig, string host, int port)
{
// Create HttpClient
if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy)
{
var proxy = new WebProxy
{
Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true
};

Client = new HttpClient(new HttpClientHandler { Proxy = proxy });
}
else
{
Client = new HttpClient();
}

Client.BaseAddress = new Uri($"http://{host}:{port}/{ServiceName}/");
ProxyConfig = proxyConfig;
Client = HttpClientBuilder.GetClient($"http://{host}:{port}/{ServiceName}/", ProxyConfig);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ private async Task EvaluateResponse(Task<Endpoint[]> resp)
// Compare version
if (VersionCompare.ClientMatch(serverVersion, clientVersion))
{
// Create new base address client
HttpClient = new HttpClient { BaseAddress = new Uri(endpoint.Address) };
// Create HttpClient
var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig;
HttpClient = HttpClientBuilder.GetClient(endpoint.Address, proxyConfig);

if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag))
HttpClient.DefaultRequestHeaders.AcceptLanguage
Expand Down
2 changes: 1 addition & 1 deletion src/Moryx/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public IModuleLogger GetChild(string name, Type targetType)
var config = _config.ChildConfigs.FirstOrDefault(item => item.LoggerName == childName);
if (config == null)
{
config = new ModuleLoggerConfig { LoggerName = childName };
config = new ModuleLoggerConfig { LoggerName = childName, ActiveLevel = Parent?.ActiveLevel ?? LogLevel.Info };
_config.ChildConfigs.Add(config);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Moryx.Serialization
{
public class ArrayIListStrategy : ICollectionStrategy
{
private readonly IList _list;
private readonly IList _toDelete = new List<object>();
private readonly ICustomSerialization _customSerialization;
private readonly ICustomAttributeProvider _property;
private readonly object _instance;
private readonly IList _toAdd = new List<object>();

public ArrayIListStrategy(IList list, ICustomSerialization customSerialization,
ICustomAttributeProvider attributeProvider, object instance)
{
_list = list;
_customSerialization = customSerialization;
_property = attributeProvider;
_instance = instance;
}

public IEnumerable<Entry> Serialize()
{
var entries = new List<Entry>();
for (int index = 0; index < _list.Count; index++)
{
var entry = CollectionStrategyTools.CreateSub(_list[index], index, _customSerialization);
entries.Add(entry);
}
return entries;
}

public IEnumerable<string> Keys()
{
return CollectionStrategyTools.GenerateKeys(_list.Count);
}

public object ElementAt(string key)
{
return _list[int.Parse(key)];
}

public void Added(Entry entry, object addedValue)
{
_toAdd.Add(addedValue);
}

public void Updated(Entry entry, object updatedValue)
{
_list[int.Parse(entry.Identifier)] = updatedValue;
}

public void Removed(string key)
{
_toDelete.Add(_list[int.Parse(key)]);
}

public void Flush()
{

if(_property is PropertyInfo propertyInfo)
{
var type = propertyInfo.PropertyType;
var elementType = type.GenericTypeArguments[0];
var list = Array.CreateInstance(elementType,_list.Count - _toDelete.Count + _toAdd.Count);
var index = 0;
foreach (var e in _list)
{
if (!_toDelete.Contains(e))
{
list.SetValue(e, index++);
}
}
foreach(var e in _toAdd)
{
list.SetValue(e, index++);
}

propertyInfo.SetValue(_instance, list);
}
}
}
}
18 changes: 14 additions & 4 deletions src/Moryx/Serialization/EntryConvert/EntryConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ public static object UpdateInstance(object instance, Entry encoded, ICustomSeria
{
var property = mapped.Property;
var propertyType = mapped.Property.PropertyType;
var doNotUpdateProperty = false;

// Do not operate on faulty properties or read-only properties
// For security reasons read the flag from the property again
Expand All @@ -591,8 +592,10 @@ public static object UpdateInstance(object instance, Entry encoded, ICustomSeria
else if (IsCollection(propertyType) && mapped.Entry != null)
{
// Pick collection strategy
var strategy = CreateStrategy(value, currentValue, propertyType, customSerialization);
var strategy = CreateStrategy(value, currentValue, propertyType, customSerialization, mapped.Property, instance);
UpdateCollection(currentValue, mapped.Property.PropertyType, mapped.Property, mapped.Entry, strategy, customSerialization);
if (strategy is ArrayIListStrategy)
doNotUpdateProperty = true;
}
// Update class
else if (propertyType.IsClass)
Expand All @@ -605,7 +608,7 @@ public static object UpdateInstance(object instance, Entry encoded, ICustomSeria
}

// Write new value to property
if (property.CanWrite)
if (property.CanWrite && !doNotUpdateProperty)
{
mapped.Property.SetValue(instance, value);
}
Expand Down Expand Up @@ -693,7 +696,9 @@ private static object CreatePrimitiveOrEnum(Type propertyType, EntryValue entryV
/// <summary>
/// Create strategy for collection
/// </summary>
private static ICollectionStrategy CreateStrategy(object collection, object currentCollection, Type collectionType, ICustomSerialization serialization)
private static ICollectionStrategy CreateStrategy(object collection, object currentCollection,
Type collectionType, ICustomSerialization serialization, ICustomAttributeProvider attributeProvider = null,
object instance = null)
{
ICollectionStrategy strategy;
if (collectionType.IsArray)
Expand All @@ -706,7 +711,12 @@ private static ICollectionStrategy CreateStrategy(object collection, object curr
}
else if (collection is IList)
{
strategy = new ListStrategy((IList)collection, serialization);
if(currentCollection is Array)
{
strategy = new ArrayIListStrategy(
(IList)collection, serialization, attributeProvider, instance);
}else
strategy = new ListStrategy((IList)collection, serialization);
}
else
{
Expand Down
10 changes: 10 additions & 0 deletions src/Moryx/Serialization/EntryConvert/EntryValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public class EntryValidation : ICloneable
[DataMember]
public bool IsRequired { get; set; }

/// <summary>
/// Creates a new <see cref="EntryValidation"/> instance initializing <see cref="EntryValidation.Maximum"/>
/// and <see cref="EntryValidation.Minimum"/> validation to the largest possible range.
/// </summary>
public EntryValidation()
{
Minimum = double.MinValue;
Maximum = double.MaxValue;
}

/// <see cref="ICloneable"/>
public object Clone()
{
Expand Down
Loading

0 comments on commit 26dfa55

Please sign in to comment.