Skip to content

Commit

Permalink
Merges locale support into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ryannewington committed Jul 23, 2016
2 parents 4668421 + 032bbd2 commit 8cc5d2f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Lithnet.ResourceManagement.Client;
using System.IO;
using SwaggerWcf.Attributes;

namespace Lithnet.ResourceManagement.WebService
{
using System.IO;
using Microsoft.ResourceManagement.WebServices.WSResourceManagement;
using SwaggerWcf.Attributes;

[ServiceContract]
public interface IResourceManagementWebServicev1
{
Expand All @@ -23,12 +18,12 @@ public interface IResourceManagementWebServicev1

[SwaggerWcfPath("Get resource by key", "Get resources")]
[OperationContract]
[WebGet(UriTemplate = "/resources/{objectType}/{key}/{keyValue}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
[WebGet(UriTemplate = "/resources/{objectType}/{key}/{keyValue}/?", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
ResourceObject GetResourceByKey(string objectType, string key, string keyValue);

[SwaggerWcfPath("Get resource by id", "Get resources")]
[OperationContract]
[WebGet(UriTemplate = "/resources/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
[WebGet(UriTemplate = "/resources/{id}/?", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
ResourceObject GetResourceByID(string id);

[SwaggerWcfPath("Delete resource", "Delete resource")]
Expand All @@ -43,7 +38,7 @@ public interface IResourceManagementWebServicev1

[SwaggerWcfPath("Update resource", "Update resources")]
[OperationContract]
[WebInvoke(UriTemplate = "/resources/{id}/", Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
[WebInvoke(UriTemplate = "/resources/{id}/?", Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
void UpdateResource(string id, ResourceUpdateRequest request);

[SwaggerWcfPath("Get approval requests", "Get approval requests")]
Expand All @@ -61,7 +56,7 @@ public interface IResourceManagementWebServicev1
[WebInvoke(UriTemplate = "/approvals/{id}/{decision}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
void SetPendingApproval(string id, string decision, ApprovalReason reason);

[SwaggerWcfPath("Get request parameters", "Get request parametters")]
[SwaggerWcfPath("Get request parameters", "Get request parameters")]
[OperationContract]
[WebGet(UriTemplate = "/request/{id}/parameters", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Stream GetRequestParameters(string id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Lithnet.ResourceManagement.Client, Version=1.0.6039.27655, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Lithnet.ResourceManagement.Client.1.0.6039.27655\lib\net40\Lithnet.ResourceManagement.Client.dll</HintPath>
<Reference Include="Lithnet.ResourceManagement.Client, Version=1.0.6048.24319, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Lithnet.ResourceManagement.Client.1.0.6048.24319\lib\net40\Lithnet.ResourceManagement.Client.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System;
using Lithnet.ResourceManagement.Client;
using Microsoft.ResourceManagement.WebServices.WSResourceManagement;
using Newtonsoft.Json;
using SwaggerWcf.Attributes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using Lithnet.ResourceManagement.Client;
using System.Collections;
using System.Net;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Serialization;

namespace Lithnet.ResourceManagement.WebService
{
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Microsoft.ResourceManagement.WebServices.WSResourceManagement;
using Newtonsoft.Json;
using SwaggerWcf.Attributes;
using ApprovalStatus = Client.ApprovalStatus;

[SwaggerWcf("/v1")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[KnownType(typeof(ResourceObject))]
Expand All @@ -35,6 +34,7 @@ public IEnumerable<ResourceObject> GetResources()
string attributes = WebOperationContext.Current?.IncomingRequest.UriTemplateMatch.QueryParameters["attributes"];
string objectType = WebOperationContext.Current?.IncomingRequest.UriTemplateMatch.QueryParameters["objectType"];
string filter = WebOperationContext.Current?.IncomingRequest.UriTemplateMatch.QueryParameters["filter"];
CultureInfo locale = GetLocaleFromParameters();

if (filter == null)
{
Expand All @@ -50,16 +50,16 @@ public IEnumerable<ResourceObject> GetResources()

if (attributes != null)
{
return Global.Client.GetResources(filter, attributes.Split(',')).ToList();
return Global.Client.GetResources(filter, attributes.Split(','), locale).ToList();
}

if (objectType != null)
{
return Global.Client.GetResources(filter, ResourceManagementSchema.ObjectTypes[objectType].Attributes.Select(t => t.SystemName)).ToList();
return Global.Client.GetResources(filter, ResourceManagementSchema.ObjectTypes[objectType].Attributes.Select(t => t.SystemName), locale).ToList();
}
else
{
return Global.Client.GetResources(filter).ToList();
return Global.Client.GetResources(filter, locale).ToList();
}
}
catch (WebFaultException)
Expand All @@ -84,6 +84,18 @@ public IEnumerable<ResourceObject> GetResources()
}
}

private static CultureInfo GetLocaleFromParameters()
{
string locale = WebOperationContext.Current?.IncomingRequest.UriTemplateMatch.QueryParameters["locale"];
CultureInfo culture = null;
if (locale != null)
{
culture = new CultureInfo(locale);
}

return culture;
}

[SwaggerWcfTag("Resources")]
[SwaggerWcfResponse(HttpStatusCode.OK, "Result found")]
[SwaggerWcfResponse(HttpStatusCode.NotFound, "Not found")]
Expand All @@ -95,12 +107,13 @@ public ResourceObject GetResourceByKey(string objectType, string key, string key
{
ResourceManagementSchema.ValidateAttributeName(key);
ResourceManagementSchema.ValidateObjectTypeName(objectType);
CultureInfo locale = GetLocaleFromParameters();

resource = Global.Client.GetResourceByKey(objectType, key, keyValue);
resource = Global.Client.GetResourceByKey(objectType, key, keyValue, locale);

if (resource == null)
{
throw new WebFaultException(HttpStatusCode.NotFound);
throw new ResourceNotFoundException();
}
}
catch (WebFaultException)
Expand All @@ -111,7 +124,11 @@ public ResourceObject GetResourceByKey(string objectType, string key, string key
{
throw;
}
catch(ResourceManagementException ex)
catch (ResourceNotFoundException)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.NotFound);
}
catch (ResourceManagementException ex)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex);
}
Expand All @@ -136,12 +153,13 @@ public ResourceObject GetResourceByID(string id)
try
{
ResourceManagementWebServicev1.ValidateID(id);
CultureInfo locale = GetLocaleFromParameters();

ResourceObject resource = Global.Client.GetResource(id);
ResourceObject resource = Global.Client.GetResource(id, locale);

if (resource == null)
{
throw new WebFaultException(HttpStatusCode.NotFound);
throw new ResourceNotFoundException();
}

return resource;
Expand All @@ -154,6 +172,10 @@ public ResourceObject GetResourceByID(string id)
{
throw;
}
catch (ResourceNotFoundException)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.NotFound);
}
catch (ResourceManagementException ex)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex);
Expand All @@ -178,11 +200,12 @@ public KeyValuePair<string, string[]> GetResourceAttributeByID(string id, string
{
ResourceManagementSchema.ValidateAttributeName(attribute);
ResourceManagementWebServicev1.ValidateID(id);
CultureInfo locale = GetLocaleFromParameters();

ResourceObject resource = Global.Client.GetResource(id, new List<string>() { attribute });
ResourceObject resource = Global.Client.GetResource(id, new List<string>() { attribute }, locale);
if (resource == null)
{
throw new WebFaultException(HttpStatusCode.NotFound);
throw new ResourceNotFoundException();
}

object value = resource.Attributes[attribute].Value;
Expand Down Expand Up @@ -233,6 +256,10 @@ public KeyValuePair<string, string[]> GetResourceAttributeByID(string id, string
{
throw;
}
catch (ResourceNotFoundException)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.NotFound);
}
catch (ResourceManagementException ex)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex);
Expand All @@ -257,12 +284,13 @@ public KeyValuePair<string, string[]> GetResourceAttributeByKey(string objectTyp
{
ResourceManagementSchema.ValidateAttributeName(attribute);
ResourceManagementSchema.ValidateObjectTypeName(objectType);
CultureInfo locale = GetLocaleFromParameters();

ResourceObject resource = Global.Client.GetResourceByKey(objectType, key, keyValue, new List<string>() { attribute });
ResourceObject resource = Global.Client.GetResourceByKey(objectType, key, keyValue, new List<string>() { attribute }, locale);

if (resource == null)
{
throw new WebFaultException(HttpStatusCode.NotFound);
throw new ResourceNotFoundException();
}

object value = resource.Attributes[attribute].Value;
Expand Down Expand Up @@ -313,6 +341,10 @@ public KeyValuePair<string, string[]> GetResourceAttributeByKey(string objectTyp
{
throw;
}
catch (ResourceNotFoundException)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.NotFound);
}
catch (ResourceManagementException ex)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex);
Expand All @@ -339,10 +371,6 @@ public void DeleteResourceByID(string id)

Global.Client.DeleteResource(id);
}
catch (ResourceNotFoundException)
{
throw new WebFaultException(HttpStatusCode.NotFound);
}
catch (WebFaultException)
{
throw;
Expand All @@ -351,6 +379,10 @@ public void DeleteResourceByID(string id)
{
throw;
}
catch (ResourceNotFoundException)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.NotFound);
}
catch (ResourceManagementException ex)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex);
Expand Down Expand Up @@ -439,8 +471,9 @@ public void UpdateResource(string id, ResourceUpdateRequest request)
try
{
ResourceManagementWebServicev1.ValidateID(id);
CultureInfo locale = GetLocaleFromParameters();

ResourceObject resource = Global.Client.GetResource(id);
ResourceObject resource = Global.Client.GetResource(id, locale);
foreach (AttributeValueUpdate kvp in request.Attributes)
{
if (kvp.Value.Length > 1)
Expand All @@ -457,11 +490,7 @@ public void UpdateResource(string id, ResourceUpdateRequest request)
}
}

Global.Client.SaveResource(resource);
}
catch (ResourceNotFoundException)
{
throw new WebFaultException(HttpStatusCode.NotFound);
Global.Client.SaveResource(resource, locale);
}
catch (WebFaultException)
{
Expand All @@ -471,6 +500,10 @@ public void UpdateResource(string id, ResourceUpdateRequest request)
{
throw;
}
catch (ResourceNotFoundException)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.NotFound);
}
catch (ResourceManagementException ex)
{
throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex);
Expand Down Expand Up @@ -502,7 +535,7 @@ public IEnumerable<ResourceObject> GetApprovalRequestsByStatus(string status)
{
try
{
ApprovalStatus approvalStatus;
Client.ApprovalStatus approvalStatus;

if (Enum.TryParse(status, true, out approvalStatus))
{
Expand Down Expand Up @@ -601,7 +634,7 @@ public Stream GetRequestParameters(string id)
{
return new MemoryStream();
}

IList<string> parameters = request.Attributes["RequestParameter"].StringValues;
List<RequestParameter> requestParameters = new List<RequestParameter>();

Expand All @@ -610,7 +643,7 @@ public Stream GetRequestParameters(string id)
requestParameters.Add(XmlDeserializeFromString<RequestParameter>(param));
}

WebOperationContext.Current.OutgoingResponse.ContentType ="application/json; charset=utf-8";
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";

return new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestParameters)));
}
Expand All @@ -636,12 +669,12 @@ public Stream GetRequestParameters(string id)
}
}

public static T XmlDeserializeFromString<T>(string objectData)
internal static T XmlDeserializeFromString<T>(string objectData)
{
return (T)XmlDeserializeFromString(objectData, typeof(T));
}

public static object XmlDeserializeFromString(string objectData, Type type)
internal static object XmlDeserializeFromString(string objectData, Type type)
{
var serializer = new XmlSerializer(type);
object result;
Expand Down
10 changes: 7 additions & 3 deletions src/Lithnet.ResourceManagement.WebService/Web.Debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<!--<system.serviceModel>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="basicAuthBinding">
Expand All @@ -14,8 +14,12 @@
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>-->
</system.serviceModel>

<lithnetResourceManagementClient resourceManagementServiceBaseAddress="http://fimsvc-sync:5725" xdt:Transform="SetAttributes"/>
<lithnetResourceManagementClient resourceManagementServiceBaseAddress="http://fimsvc:5725" xdt:Transform="SetAttributes"/>

<system.web>
<identity impersonate="false" xdt:Transform="SetAttributes"/>
</system.web>

</configuration>
6 changes: 6 additions & 0 deletions src/Lithnet.ResourceManagement.WebService/Web.Release.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
</bindings>
</system.serviceModel>

<lithnetResourceManagementClient resourceManagementServiceBaseAddress="http://localhost:5725" xdt:Transform="SetAttributes"/>

<system.web>
<identity impersonate="true" xdt:Transform="SetAttributes"/>
</system.web>

</configuration>
Loading

0 comments on commit 8cc5d2f

Please sign in to comment.