Getting junk character while sending encrypted json request to webservice via resttemplate - encryption

Requirement : Need to send the encrypted json request to the webservice and service will send the decrypted result back to the client.
Issue : We are using Spring RestTemplate. While sending the encrypted data we are sending junk character to webservice so in webservice we are getting below error.
Servlet.service() for servlet [dispatcherServlet] in context with path [/SpringBootRestApi] threw exception [Request processing failed; nested exception is com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException: You must initialize the xml-security library correctly before you use it. Call the static method "com.sun.org.apache.xml.internal.security.Init.init();" to do that before you use any functionality from that library.] with root cause

Related

WCF-WebHttp Error Handling For Rest API BizTalk 2016

I have a rest API with POST Method where I am posting and receiving success response as JSON format which works absolutely fine. During the failure I receive
Error details: System.Net.WebException: The remote server returned an unexpected response:
(400) Bad Request The creation failed because the name or email address of the is invalid
Which I see in Application log and send port gets suspended. I need to capture this in the BizTalk Orchestration where I need to send email if I get any error response.
As the Error Response is not in JSON format don't know how it can be handled. Any idea?
Enable Routing for failed messages on the Send Port to stop the suspended messages. You might have to update the filter in the send port that subscribes to ErrorReport.FailureCode Exists to exclude your API send port if you only want to handle these in the Orchestration. If you don't have such a port, you might have to create one, and send the failed messages to a NULL adapter or similar.
Have your Orchestration have an scope with an exception block to catch System Exceptions, in there have Exception = SystemException.ToString(); in an Expression shape where Exception is a System.String Variable, and SystemException is the Exception Object Name you defined in the Catch block.
Have logic in your catch block that decides what sort of exception you got, I use a If shape with Exception.Contains("XXXX") where XXXX is text in the exception, or handle it however you want.

How to handle Incorrect JSON syntax in Spring MVC

I have a spring MVC Project, wherein I need to handle incorrect JSON syntax.
Spring MVC throws an error as text/html, which looks like:
Apache Tomcat/7.0.47 - Error report HTTP Status 400 - type Status reportmessage description The request sent by the client was syntactically incorrect.Apache Tomcat/7.0.47
However I want to throw my own exception in JSON format.
Is there any way to do it?
Thanks
I just ran into this as well, here's what I found to fix it:
The Spring MVC docs reference HttpMessageConverter as responsible for converting a #ResponseBody. For JSON, you're likely using a MappingJackson2HttpMessageConverter, which when attempting to convert a message can throw a HttpMessageNotReadableException.
If you have some form of exception handler for that, you can intercept the 400 response.
You can debug to see which Exception is actually thrown (might be a TypeMismatchException or something like that). Then, you can specify an error handler in your controller like
#ExceptionHandler(TypeMismatchException.class)
public String handleTypeMismatchException(TypeMismatchException ex) {
}
Now, in this controller method you can do whatever you want, like passing it the model, request, response etc. You can send to a view, redirect or in your case use a ResponseBody or however else you would like to serve your JSON error.

ASP.NET Web Service validating incoming XML

I have ASP.NET 4 Web Service (asmx). I use IXmlSerializable interface for manually serialization/deserialization. In the function ReadXml (part of IXmlSerializable) I use functin XmlReader.ReadString and if client send me invalidate XML this function throw exception. How I validate incoming XMl before deserialization? Because exception is very hard operation for perfomance.
put the deserializatin in a try catch block and handle exception in catch. there is no point in validating xml upfront as it might through an exception when you attempt to validate as well.

HTTP Error 500.0 - Internal Server Error in Secure WCF Service

Getting following error after hosting WCF Service in IIS.
HTTP Error 500.0 - Internal Server Error. The page cannot be displayed
because an internal server error has occurred.
Module IsapiModule
Notification ExecuteRequestHandler
Handler svc-ISAPI-4.0_32bit
Error Code 0x00000000
It is HTTPS having a secure certificate WCF Service.
Please help me to solve this problem.
It can be caused by many different things. You should try the following solutions:
1 First way
Right click the folder where your site is located: "C:\Users\NAME\SiteName" and select Properties.
Select the Security tab and click on Edit.
Add.. and type in "IIS_IUSRS".
2 Second way
By default, WCF Service OperationContracts can only be invoked using an HTTP POST. When you call open() on the Titanium HTTPClient, are you specifying a GET or POST for HTTP method parameter?
Secondly, since your service binding is using SOAP 1.1, you need to pass a SOAPAction header in your request so that WCF can route the message to the GetData method. If an Action parameter is not specified in the service's OperationContract attribute, the Action should be the method name preceded by the namespace and service contract name (probably http://tempuri.org/IService1/GetData if you're using what the default WCF application created). You'll also need to specify a content-type. So, you'd need to setup your xhr like this prior to calling send:
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-16');
xhr.setRequestHeader('SOAPAction',
'"http://tempuri.org/IService1/GetData"'); xhr.send(s); Also, you can
explicitly specify an action for a WCF service operation:
[OperationContract(Action = "MyAction")] string GetData() {
// ...snip... }
xhr.setRequestHeader('SOAPAction', '"MyAction"');
And lastly, you can allow service operations to be invoked via an HTTP GET by decorating the method with the [WebGet] attribute. This allows the operation to be called in REST fashion: http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx

Access wrapped request web method from web service in asp.net

I have a web service method taking multiple parameters.
Therefor the BodyStyle = WebMessageBodyStyle.Wrapped is set.
I want to access this method from a web service client therefor I did add the url of the service method to Reference->Add Services and I get the following error.
"There was an error downloading 'http://localhost:8080/Api/StoreI'.
The request failed with HTTP status 405: Method Not Allowed.
Metadata contains a reference that cannot be resolved: 'http://localhost:8080/Api/StoreI'.
The content type application/json of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8).
If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.
The first 323 bytes of the response were: '{"ErrorCode":110,"ErrorDetails":null,"Message":"The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.
This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."}'.
The remote server returned an error: (500) Internal Server Error.
If the service is defined in the current solution, try building the solution and adding the service reference again."
What should I do in order to resolve this error?
What are the steps to access a secure web service from a client?
I an new to web services.
A reply would be highly appreciated.
Thanks.
I am not expert but change
BodyStyle = WebMessageBodyStyle.Wrapped
to
BodyStyle = WebMessageBodyStyle.Bare
The usage of the wrapped on the operation description does not add a layer of security on the message body. It simply wraps the request (or the response) in an XML element

Resources