WCF JSON service with request content encrypted - json

a Json WCF service method looks like this:
getFoo(MyDataType data){ ...
WCF automaticly translates the HTTP request that looks like:
randomGuy{
name:'John',
age:18
}
into the C# defined type
MyDataType{
string name {get;set;}
int age {get;set;}
}
But if I encrypt the request content, instead of receiving a request that looks like
randomGuy{
name:'John',
age:18
}
I will receive
wceweuicn23cb38v934vb398v03c264vb834gv3v7b3vb3647v34vb3v83vh38v43vhv3hv
Is there any way to first decrypt the request content into randomGuy{
name:'John',
age:18
} before WCF translates the JSON object into the .net defined type MyDataType?

To me, you have about 2 options:
Option 1: Use SSL for the connection so the message is protected.
Option 2: Intercept the message
In summary, I believe that the answer you are looking for are along the lines of an IClientMessageInspector which will allow you to manipulate messages and intercept them if neccessary on you WCF client. This article should explain it somewhat - enter link description here

Looks like the IDispatchMessageInspector or IDispatchMessageFormatter may help you in this case. And in general check the System.ServiceModel.Dispatcher namespace - there are other helpful staff.
The idea with IDispatchMessageInspector, that you are able to change (decrypt in your case) the incoming message before it's converted to a .NET object from JSON one.

Related

What is the specific type of the JSON being sent to a Webhook?

I can register a so called webhook in JIRA if I want JIRA to inform an external application about changes. Some JSON is generated when an issue is changed and sent to the webhook.
Is this JSON ...just a Stirng...or is it the representation of a specific Java class? If this would be the case: which class is it?
Or: how to I have to handle this JSON when it is sent to my SpringBoot application (webhook)? Just as a String or can I map this JSON via Jackson to a particular class...and how do I have to do this?
A callback for an issue-related event is structured like this...

Spring RESTful service returning JSON from another service

I have been creating Spring RESTful services for a while and typically I am building my own services so I create domain objects and populate them and the framework takes care of the conversion to JSON.
I have a situation now where I simply need my service to act as a pass through to another system's service that is already RESTful and returns JSON.
URL https://:/service/serviceInfo
Method GET
HTTP Content Type Produces: application/json
I simply want to wrap this call (I will apply some security checks on the service) and pass that JSON returned straight back to my service without mapping it back to Java objects, only to return as JSON to the client. Is there a simple way to do this, with minimal code?
Thanks in advance.
Can you see if this works for you?
#RequestMapping("/child")
public String testMethod(#RequestParam String param) {
return new RestTemplate().exchange("https://api.twitter.com/1/statuses/user_timeline.json", HttpMethod.GET, null, String.class).getBody();
}
You just replace the url for your own. I can also guide you to using the RestTemplate with POST or DELETE requests etc. if you need. Also adding parameters or headers if you need. I've used it extensively in some projects.

General pattern for WCF/Web API request and response messages structure

We have a Web API service of our own which in turn calls the third party WCF web service.
I'm trying to come up with a pattern where we get a response from WCF within which there will be response.status and response.errorDescription. Based on these properties we will translate in to user friendly error messages. This message then will need to be passed on to client via Web API or Controller/Action.
Wondering if there is already a pattern to pass response status and message to client.
Also should all WCF web service must return the response status so that client can look at this status first and process the data? What's the best practice pattern here?
Thanks.
If you have control over third party WCF their contract will need to be adjusted to send response the way you want it.
We have got many services running which has the same pattern. To our clients we expose the contracts like this.
..............................................
[OperationContract]
ResponseType GetData (string request)
..............................................
[DataContract]
public class ResponseType
{
[DataMember]
public string Status {get;set;}
public string ErrorDescription {get;set;}
}
now when clients get the response, they can look for status and ErrorDescription.
This is one pattern which is mostly recommended.
There is another way you can do it which is not recommended and is complex. That is by creating a WCF Extension that intercepts the response and converts to a meaningful result.

Tell modelbinding that MVC action parameter is JSON

I am using an upload control to send a file to a JsonResult, but I am also sending up a JSON string as a second parameter. This is all getting posted with the Content-Type:multipart/form-data;
[HttpPost]
public JsonResult UploadDocument(HttpPostedFileBase file, DocumentViewModel model)
{ ... }
I know MVC is capable of binding directly to a viewmodel if the content type is set to application/json but I don't think it's possible for me to set that in this case.
Is there any way for me to get MVC to automatically bind my posted json string to model?
That's not possible out-of-the-box. You will have to manually deserialize the JSON string parameter that you would read from the request to your view model inside the controller action or write a custom model binder for it that will do the job. Ideally you shouldn't be posting the model data as a JSON string but rather respect the content type you specified : multipart/form-data. So the correct way to handle this scenario is to modify the client code that is sending the request in order to respect the content type.
As I was unable to change the content-type I found this blog to be exactly what i needed.
"... our whole request stream(data) won’t be json string. Only the guest parameter will be supplied as json string..."
http://ishwor.cyberbudsonline.com/2012/07/fun-with-aspnet-mvc-3-custom-json-model-binder.html

WCF service to return iCal

How do I get my service written with WCF to return an iCal? The examples I see use either xml or json as the way to format the response. What is the best way to return other types of response bodies?
Something like this:
[WebGet(UriTemplate="{id}.ics")]
[OperationContract]
Stream GetCalendar(int id)
{
WebOperationContext.Current.OutgoingResponse.ContentType="text/calendar";
//Now just return the appropriate data in iCal format in the Stream...
}
So now you can do an HTTP GET to e.g. yourService.svc/123.ics and get an iCal back.
The reason this works is that "Stream" is special-cased in WCF REST (used for non-XML, non-JSON responses).
Remember that you have to use both the WebHttpBinding and WebHttp behavior for this to work.
The simplest solution would be to return the iCal representation in XML or JSON format (your pick) as a simple string from your WCF call:
[ServiceContract]
interface IMyCalService
{
[OperationContract]
string GetiCal(.......);
}
and then go on and handle it further on the client, once you've received the string containing the iCal XML (or JSON). This can be done with standard WCF using SOAP.
Other ways to do it might be to use a WCF REST service which returns a response in iCal format when you hit a particular URL - this would require installing the WCF Rest Starter Kit for the time being (in .NET 3.0/3.5). I'm not intimately familiar with the iCal format, but I'm sure one way or another, you'll be able to construct the necessary XML format to satisfy the iCal requirements.
Marc