Skip to content

Commit

Permalink
Added ability to map HTTP Accept headers to Document Format IDs in th…
Browse files Browse the repository at this point in the history
…e configuration file
  • Loading branch information
PatrickRitchie committed Dec 11, 2023
1 parent ccac9ce commit 0829904
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
14 changes: 14 additions & 0 deletions agent/Modules/MTConnect.NET-AgentModule-HttpServer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ This Agent Module implements a MTConnect REST Protocol Http server
allowPut: true
indentOutput: true
documentFormat: xml
accept:
text/xml: xml
application/json: json-cppagent
responseCompression:
- gzip
- br
Expand All @@ -45,6 +48,8 @@ This Agent Module implements a MTConnect REST Protocol Http server

* `port` - The port number the agent binds to for requests.

* `accept` - Maps HTTP Accept Headers to the corresponding Document Format ID

* `responseCompression` - Sets the List of Encodings (ex. gzip, br, deflate) to pass to the Accept-Encoding HTTP Header

* `version` - Sets the MTConnect Version to use for the response document. This can be used to request a document using an older version for legacy clients
Expand Down Expand Up @@ -116,6 +121,15 @@ Specify the port and hostname with TLS (PFX Certificate)
certificatePassword: mtconnect
```

### Example 5
Specify with custom Accept headers
```yaml
- http-server:
accept:
text/xml: xml
application/json: json-cppagent
```


## Contribution / Feedback
- Please use the [Issues](https://github.com/TrakHound/MTConnect.NET/issues) tab to create issues for specific problems that you may encounter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MTConnect.Http;
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Http;
using MTConnect.Tls;
using System.Collections.Generic;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -26,6 +29,12 @@ public class HttpServerConfiguration : IHttpServerConfiguration
[JsonPropertyName("tls")]
public TlsConfiguration Tls { get; set; }

/// <summary>
/// Maps HTTP Accept Headers to the corresponding Document Format ID
/// </summary>
[JsonPropertyName("accept")]
public Dictionary<string, string> Accept { get; set; }

/// <summary>
/// Gets or Sets the List of Encodings (ex. gzip, br, deflate) to pass to the Accept-Encoding HTTP Header
/// </summary>
Expand Down Expand Up @@ -127,9 +136,14 @@ public HttpServerConfiguration()
{
Server = null;
Port = 5000;

Accept = new Dictionary<string, string>();
Accept["text/xml"] = "XML";
Accept["application/xml"] = "XML";
Accept["application/json"] = "JSON";

AllowPut = false;
AllowPutFrom = null;
//MaxStreamingThreads = 5;
IndentOutput = true;
OutputComments = false;
OutputValidationLevel = OutputValidationLevel.Ignore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using MTConnect.Http;
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Http;
using MTConnect.Tls;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MTConnect.Configurations
{
Expand All @@ -17,8 +19,16 @@ public interface IHttpServerConfiguration
/// </summary>
string Server { get; set; }

/// <summary>
/// Gets or Sets the TLS settings
/// </summary>
TlsConfiguration Tls { get; set; }

/// <summary>
/// Maps HTTP Accept Headers to the corresponding Document Format ID
/// </summary>
Dictionary<string, string> Accept { get; set; }

/// <summary>
/// Gets or Sets the List of Encodings (ex. gzip, br, deflate) to pass to the Accept-Encoding HTTP Header
/// </summary>
Expand All @@ -34,11 +44,6 @@ public interface IHttpServerConfiguration
/// </summary>
IEnumerable<string> AllowPutFrom { get; set; }

///// <summary>
///// The maximum number of Threads to use for the Http Stream Requests
///// </summary>
//int MaxStreamingThreads { get; set; }


/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ protected async override Task<MTConnectHttpResponse> OnRequestReceived(IHttpCont
if (version == null) version = _mtconnectAgent.MTConnectVersion;

// Read DocumentFormat from Query string
var documentFormatString = httpRequest.QueryString["documentFormat"];
var documentFormat = _serverConfiguration.DocumentFormat;
if (!string.IsNullOrEmpty(documentFormatString)) documentFormat = documentFormatString;
var documentFormat = httpRequest.QueryString["documentFormat"];
if (string.IsNullOrEmpty(documentFormat) && !_serverConfiguration.Accept.IsNullOrEmpty() && httpRequest.Headers["Accept"] != null)
{
if (_serverConfiguration.Accept.ContainsKey(httpRequest.Headers["Accept"])) documentFormat = _serverConfiguration.Accept[httpRequest.Headers["Accept"]];
}
if (string.IsNullOrEmpty(documentFormat)) documentFormat = _serverConfiguration.DocumentFormat;

// Read ValidationLevel from Query string
int validationLevel = (int)_serverConfiguration.OutputValidationLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ protected async override Task<MTConnectHttpResponse> OnRequestReceived(IHttpCont
if (version == null) version = _mtconnectAgent.MTConnectVersion;

// Read DocumentFormat from Query string
var documentFormatString = httpRequest.QueryString["documentFormat"];
var documentFormat = _serverConfiguration.DocumentFormat;
if (!string.IsNullOrEmpty(documentFormatString)) documentFormat = documentFormatString;
var documentFormat = httpRequest.QueryString["documentFormat"];
if (string.IsNullOrEmpty(documentFormat) && !_serverConfiguration.Accept.IsNullOrEmpty() && httpRequest.Headers["Accept"] != null)
{
if (_serverConfiguration.Accept.ContainsKey(httpRequest.Headers["Accept"])) documentFormat = _serverConfiguration.Accept[httpRequest.Headers["Accept"]];
}
if (string.IsNullOrEmpty(documentFormat)) documentFormat = _serverConfiguration.DocumentFormat;

// Read ValidationLevel from Query string
int validationLevel = (int)_serverConfiguration.OutputValidationLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ protected async override Task<MTConnectHttpResponse> OnRequestReceived(IHttpCont
if (version == null) version = _mtconnectAgent.MTConnectVersion;

// Read DocumentFormat from Query string
var documentFormatString = httpRequest.QueryString["documentFormat"];
var documentFormat = _serverConfiguration.DocumentFormat;
if (!string.IsNullOrEmpty(documentFormatString)) documentFormat = documentFormatString;
var documentFormat = httpRequest.QueryString["documentFormat"];
if (string.IsNullOrEmpty(documentFormat) && !_serverConfiguration.Accept.IsNullOrEmpty() && httpRequest.Headers["Accept"] != null)
{
if (_serverConfiguration.Accept.ContainsKey(httpRequest.Headers["Accept"])) documentFormat = _serverConfiguration.Accept[httpRequest.Headers["Accept"]];
}
if (string.IsNullOrEmpty(documentFormat)) documentFormat = _serverConfiguration.DocumentFormat;

// Read ValidationLevel from Query string
int validationLevel = (int)_serverConfiguration.OutputValidationLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ protected async override Task<MTConnectHttpResponse> OnRequestReceived(IHttpCont
if (version == null) version = _mtconnectAgent.MTConnectVersion;

// Read DocumentFormat from Query string
var documentFormatString = httpRequest.QueryString["documentFormat"];
var documentFormat = _serverConfiguration.DocumentFormat;
if (!string.IsNullOrEmpty(documentFormatString)) documentFormat = documentFormatString;
var documentFormat = httpRequest.QueryString["documentFormat"];
if (string.IsNullOrEmpty(documentFormat) && !_serverConfiguration.Accept.IsNullOrEmpty() && httpRequest.Headers["Accept"] != null)
{
if (_serverConfiguration.Accept.ContainsKey(httpRequest.Headers["Accept"])) documentFormat = _serverConfiguration.Accept[httpRequest.Headers["Accept"]];
}
if (string.IsNullOrEmpty(documentFormat)) documentFormat = _serverConfiguration.DocumentFormat;

// Read ValidationLevel from Query string
int validationLevel = (int)_serverConfiguration.OutputValidationLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ protected async override Task<MTConnectHttpResponse> OnRequestReceived(IHttpCont
if (version == null) version = _mtconnectAgent.MTConnectVersion;

// Read DocumentFormat from Query string
var documentFormatString = httpRequest.QueryString["documentFormat"];
var documentFormat = _serverConfiguration.DocumentFormat;
if (!string.IsNullOrEmpty(documentFormatString)) documentFormat = documentFormatString;
var documentFormat = httpRequest.QueryString["documentFormat"];
if (string.IsNullOrEmpty(documentFormat) && !_serverConfiguration.Accept.IsNullOrEmpty() && httpRequest.Headers["Accept"] != null)
{
if (_serverConfiguration.Accept.ContainsKey(httpRequest.Headers["Accept"])) documentFormat = _serverConfiguration.Accept[httpRequest.Headers["Accept"]];
}
if (string.IsNullOrEmpty(documentFormat)) documentFormat = _serverConfiguration.DocumentFormat;

// Read ValidationLevel from Query string
int validationLevel = (int)_serverConfiguration.OutputValidationLevel;
Expand Down

0 comments on commit 0829904

Please sign in to comment.