MVC PayPal Helper error: PayPal.Platform.SDK.FATALException - asp.net-mvc-3

I was using PayPal helper http://paypalhelper.codeplex.com and was getting an error PayPal.Platform.SDK.FATALException
After that i took the source code too have look what is happening there and after i debug one of the tests in the test project public void TestImplicitSimplePay() and find out that it is throwing an error in the class SoapEncoder in method Decode in line 96 return (object)serializer.Deserialize(reader);
public static object Decode(string soapEnvelope, Type toType)
{
XmlSerializer serializer = null;
try
{
/// Initializing the XMLSerializer.
serializer = new XmlSerializer(toType);
/// Removing SOAP outer Envelope
soapEnvelope = soapEnvelope.Replace("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body>", string.Empty);
soapEnvelope = soapEnvelope.Replace("</soapenv:Body></soapenv:Envelope>", string.Empty);
soapEnvelope = soapEnvelope.Replace("xmlns:ns2=\"http://svcs.paypal.com/types/ap\"", string.Empty);
soapEnvelope = soapEnvelope.Replace("ns2:", string.Empty);
soapEnvelope = soapEnvelope.Replace("soapenv:", string.Empty);
soapEnvelope = soapEnvelope.Replace("ns3:", string.Empty);
soapEnvelope = soapEnvelope.Replace("xmlns:ns2=\"http://svcs.paypal.com/types/ap\"", string.Empty);
/// Deserializing and Returning the XML
using (MemoryStream reader = new MemoryStream(Encoding.UTF8.GetBytes(soapEnvelope)))
{
return (object)serializer.Deserialize(reader); //Error here
}
}
catch (FATALException)
{
throw;
}
catch (Exception ex)
{
throw new FATALException("Error occurred in SoapEncoder->Decode method", ex);
}
finally
{
serializer = null;
}
}
soapEnvelope xml:
<xml version='1.0' encoding='utf-8'?>
<Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">
<Header/>
<Body>
<PayResponse >
<responseEnvelope>
<timestamp>2011-08-09T00:28:53.399-07:00</timestamp>
<ack>Success</ack>
<correlationId>621854fd57929</correlationId>
<build>2012864</build>
</responseEnvelope>
<payKey>AP-1SE162159L922805T</payKey>
<paymentExecStatus>COMPLETED</paymentExecStatus>
</PayResponse>
May be some one come across that and know what i can do to fix that?

Replace this line:
soapEnvelope = soapEnvelope.Replace("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body>", string.Empty);
With:
soapEnvelope = soapEnvelope.Replace("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body>", string.Empty);
The response from Paypal seems to have altered slightly and the <soapenv:Header /> node no longer has a space before the self closing tag ends.

Related

Add custom SoapHeader using SpringBoot

Using SoapUI I am able to send a request with a custom SOAP header like this:
<soap:Header>
<To xmlns="http://www.w3.org/2005/08/addressing">ws://xxx.com/PP/QM/GPMService/Vx</To>
<Action xmlns="http://www.w3.org/2005/08/addressing">http://xmldefs.xxx.com/PP/QM/GPMService/Vx/AbcService/GetServiceInfoRequest</Action>
<MessageID xmlns="http://www.w3.org/2005/08/addressing">ITEST-2018-04-16-0001</MessageID>
<Stage xmlns="http://xmldefs.xxx.com/Technical/Addressing/V1">ProdX</Stage>
</soap:Header>
and get a reasonable response.
I can't achieve this in my SpringBoot application.
I have a service extending WebServiceGatewaySupport:
#Service
public class AbcService extends WebServiceGatewaySupport{
private AbcConfiguration abcConfiguration;
#Autowired
public void setAbcConfiguration(final AbcConfiguration abcConfiguration) {
this.abcConfiguration = abcConfiguration;
}
public GetServiceInfoResponse GetServiceInfo() {
final String actionStr = "GetServiceInfo";
final ObjectFactory factory = new ObjectFactory();
GetServiceInfo getServiceInfo = factory.createGetServiceInfo();
JAXBElement<GetServiceInfo> gsiRequest = factory.createGetServiceInfo(getServiceInfo);
WebServiceTemplate wst = this.getWebServiceTemplate();
#SuppressWarnings("unchecked")
JAXBElement<GetServiceInfoResponse> gsiResponse = (JAXBElement<GetServiceInfoResponse>)wst
.marshalSendAndReceive("https://ws-gateway-cert.xxx.com/services/", gsiRequest, new WebServiceMessageCallback() {
#Override
public void doWithMessage(WebServiceMessage message) {
try {
SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
SoapHeaderElement toElem = soapHeader.addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "To"));
toElem.setText("ws://xxx.com/PP/QM/GPMService/Vx");
...
} catch (Exception e) {
logger.error("Error during marshalling of the SOAP headers", e);
}
}
});
return gsiResponse.getValue();
}
}
What am I doing wrong? Can anybody tell me how I can do this?
Okay. I got it working so far and the SOAP XML looks as demanded and running the request (being generated form my SpringBoot app) in SoapUI I get the demanded result.
public GetServiceInfoResponse GetServiceInfo() {
final String actionStr = "GetServiceInfo";
final ObjectFactory factory = new ObjectFactory();
GetServiceInfo getServiceInfo = factory.createGetServiceInfo();
JAXBElement<GetServiceInfo> gsiRequest = factory.createGetServiceInfo(getServiceInfo);
WebServiceTemplate wst = this.getWebServiceTemplate();
#SuppressWarnings("unchecked")
JAXBElement<GetServiceInfoResponse> gsiResponse = (JAXBElement<GetServiceInfoResponse>)wst
.marshalSendAndReceive(kpmConfiguration.getEndpoint(), gsiRequest, new WebServiceMessageCallback() {
#Override
public void doWithMessage(WebServiceMessage message) {
System.out.println(message.toString());
try {
// get the header from the SOAP message
final SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
final SaajSoapMessage ssMessage = (SaajSoapMessage)message;
final SOAPEnvelope envelope = ssMessage.getSaajMessage().getSOAPPart().getEnvelope();
System.out.println("envelope.getPrefix(): " + envelope.getPrefix());
envelope.removeNamespaceDeclaration("SOAP-ENV");
envelope.setPrefix(NAMESPACE_PREFIX_SOAP);
System.out.println("envelope.getPrefix(): " + envelope.getPrefix());
envelope.getBody().setPrefix(NAMESPACE_PREFIX_SOAP);
envelope.getHeader().setPrefix(NAMESPACE_PREFIX_SOAP);
envelope.addNamespaceDeclaration(NAMESPACE_PREFIX_SOAP, NAMESPACE_PREFIX_SOAP_DEF);
envelope.addNamespaceDeclaration(NAMESPACE_PREFIX_V2, NAMESPACE_PREFIX_V2_DEF);
envelope.addNamespaceDeclaration(NAMESPACE_PREFIX_WSSE, NAMESPACE_PREFIX_WSSE_DEF);
final SoapHeaderElement toElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_ADDRESSING, "To"));
toElem.setText(TO_VALUE);
final SoapHeaderElement actionElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_ADDRESSING, "Action"));
actionElem.setText(NAMESPACE_PREFIX_V2_DEF + "/AbcService/" + actionStr + "Request");
final SoapHeaderElement messageIdElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_ADDRESSING, "MessageID"));
messageIdElem.setText(MESSAGE_ID_VALUE + UUID.randomUUID());
final SoapHeaderElement stageElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_VWA, "Stage"));
stageElem.setText("Production");
final NodeList nl = ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().getChildNodes();
ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().removeChild(nl.item(0));
final SOAPElement se = ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().addBodyElement(new QName(actionStr));
se.setPrefix(NAMESPACE_PREFIX_V2);
final SOAPElement userAuthElem = se.addChildElement(new QName("UserAuthentification"));
final SOAPElement userIdElem = userAuthElem.addChildElement("UserId");
userIdElem.setTextContent(kpmConfiguration.getCredentials().getUsername());
System.out.println(userIdElem.getTextContent());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(ssMessage.getPayloadSource(), soapHeader.getResult());
} catch (Exception e) {
logger.error("Error during marshalling of the SOAP headers", e);
}
}
});
return gsiResponse.getValue();
}
However, when I submit the request from my SpringBoot app I always get an exception:
java.net.SocketException: Unexpected end of file from server
Am I missing something in the code?
See the answer to the original question above in the edited question.
Concerning the java.net.SocketException: Unexpected end of file from server it seemed to come from redirecting the request through Eclipse's TCP/IP Monitor. When sending the request directly to the server I get a meaningful response with:
INFO_001
Method compelted successfully
:-)

Couldn't prepare mail with inline image using Thymeleaf templateEngine?

I am trying to send a html email with an inline-image attached using spring mvc and Thymeleaf. My code works without attaching the image. I get the html email successfully without image. But When I added the following line to my code I get the below error message.
Without this line code works and I get the email without image:
message.addInline(image.getName(), imageSource, image.getContentType());
This is the method:
public void sendUserRegisterEmail(String receiver, String receiverEmailAddress){
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
Path path = Paths.get("/assets/dist/img/dialog_tv_logo-white-01.png");
String fileName = "dialog_tv_logo-white-01.png";
String originalFileName = "dialog_tv_logo-white-01.png";
String contentType = "image/png";
byte[] content = null;
try {
content = Files.readAllBytes(path);
} catch (final IOException e) {
}
MultipartFile image = new MockMultipartFile(fileName,
originalFileName, contentType, content);
Locale locale = Locale.getDefault();
final Context ctx = new Context(locale);
ctx.setVariable("name", receiver);
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("imageResourceName", image.getName());
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setSubject(USER_REGISTER_MESSAGE_SUBJECT);
message.setTo(receiverEmailAddress);
message.setFrom(SENDER_EMAIL_ADDRESS);
final String htmlContent = emailTemplateEngine.process("email-inlineimage", ctx);
message.setText(htmlContent, true /* isHtml */);
final InputStreamSource imageSource = new ByteArrayResource(image.getBytes());
message.addInline(image.getName(), imageSource, image.getContentType());
}
};
sendEmail(preparator);
}
Following error message I get when the image prepare line is added:
org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.IllegalStateException: Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.
Can any one figure out what is the issue here?
Finally able to fix the error after a lot of effort. Changing the following line fixed the issue. Create MimeMessageHelper by passing true
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

IE image not getting loaded with X-Content-Type-Options:nosniff

Intro
I have spring MVC application I'm loading image from controller. For security purpose, I added X-Content-Type-Options:nosniff to my Spring application
By setting the following in springConfig xml <security:content-type-options/>
Problem : after this IE is not loading the images responded by controller. I suspect the content type is not set in the response. Because another site which is responding X-Content-Type-Options:nosniff and Content-Type:image/png; is working fine.
TRY1
I tried to change my controller to set content type. But it is not happening.
#RequestMapping(value = "/getUserImage" , produces = org.springframework.http.MediaType.IMAGE_PNG_VALUE)
public #ResponseBody
void getUserImage(
#RequestParam(value = "userId", required = false) int userId,
HttpServletRequest request, HttpServletResponse response) {
try {
//Get file and add it to response
IOUtils.copy(inputStream, response.getOutputStream());
response.getOutputStream().flush();
response.setContentType(org.springframework.http.MediaType.IMAGE_PNG_VALUE);
response.setHeader("Content-Type","image/png");
response.flushBuffer();
inputStream.close();
} catch (Exception e){
}
}
TRY2
I tried to add response header as the same way in method interceptor but still no luck.
But the same thing working in Chrome and Firefox.
Try this :
#RequestMapping(value = "/image/{personId}")
#ResponseBody
public HttpEntity<byte[]> getPhoto(#PathVariable int personId) {
Person person = this.personService.getPersonById(personId);
if (person != null && person.getProfileThumbnail() != null) {
try {
byte[] image;
try {
image = org.apache.commons.io.FileUtils.readFileToByteArray(new File(msg + "/" + person.getUsername() + "/" + personId + ".png"));
} catch (FileNotFoundException e) {
image = org.apache.commons.io.FileUtils.readFileToByteArray(new File(defaultProfilePath));
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
headers.setContentLength(image.length);
return new HttpEntity<>(image, headers);
} catch (IOException ignored) {
}
}
}
What I am basically doing is checking if there is an image on File-system for the user, if not then I am loading a default image. Right now it works on all browser, so even if personid is 0, I get default image back, with the else cause, which I have not posted here.

Send JSON Array to .net server using KSOAP library in android

I want to send JSON object / JSON Array to .net Serverusing KSOAP library.
Here is my code
sendJSON{
JSONObject json = new JSONObject();
try {
CallingSoap cs=new CallingSoap();
String macid="1";
String latStr= StaticVariableClass.latitude_str;
String longStr= StaticVariableClass.longitude_str;
String datetimeStr="23/04/2015";
json.put("MacID",macid);
json.put("DateTime",datetimeStr);
json.put("Latitude",latStr);
json.put("Longitude",longStr );
String JSONString= json.toString();
Log.e("JSON", JSONString);
// String resp=cs.demo("test");
String resp=cs.demo(json); // I need to send this json to my asp.net web server
Log.d("Response from server",resp);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//CallingSoap .java
public class CallSoap {
public String demo(JSONObject a)
//public String demo(String a)
{
final String SOAP_ACTIONS= "http://tempuri.org/GetLatLongJson";
final String OPERATION_NAMES= "GetLatLongJson";
final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
final String SOAP_ADDRESS = "http://10.208.36.33/samtadoot2/samtadootwebservice.asmx";
SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAMES);
PropertyInfo pi=new PropertyInfo();
pi.setName("jsonobject");
pi.setValue(a);
pi.setType(JSONObject.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
new MarshalBase64().register(envelope);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTIONS, envelope);
response = envelope.getResponse();
}
catch(Exception ex)
{
response=ex.toString();
}
//JSONArray JSONArray = null;
return response.toString();
}
}
it is throwing an exception of cannot seralize
04-27 12:52:46.378: D/Response from server(5982): java.lang.RuntimeException: Cannot serialize: {"MacID":"1","Latitude":"18.5647613","Longitude":"73.8069672"}
Thanks in advance
that:
pi.setType(JSONObject.class);
will not work, becouse there is no standard serialization defined for JSONObject in ksoap2. But what i suppouse what You didnt written is that You try to send JSON string to theserver. It i'm right, then You have to change Your code to send String and encode JSONObject to JSON string using toString this way:
SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAMES);
PropertyInfo pi=new PropertyInfo();
pi.setName("jsonobject");
pi.setValue(a.toString());
pi.setType(String.class);
request.addProperty(pi);
If i'm wrong, then You have to parse whole JSONObject to structure of SoapObjects/Primitives.
Regards, Marcin

Getting the HttpActionExecutedContext Result values

I have created a filter which inherits the System.Web.Http.Filters.ActionFilterAttribute in the asp.net web api and would like to access some of the data inside the HttpActionExecutedContext result object.
At what stage/when does this object get populated? As I looked at it when overriding the OnActionExecuted method and its always null?
Any ideas?
Edit:
for example here in my custom filter:
public override OnActionExecuted(HttpActionExecutedContext context)
{
//context.Result.Content is always null
base.OnActionExecuted(context);
}
Use this function to get body of request in web api
private string GetBodyFromRequest(HttpActionExecutedContext context)
{
string data;
using (var stream = context.Request.Content.ReadAsStreamAsync().Result)
{
if (stream.CanSeek)
{
stream.Position = 0;
}
data = context.Request.Content.ReadAsStringAsync().Result;
}
return data;
}
Ended up using ReadAsStringAsync on the content result.
I was trying to access the property before the actual request had finished.
While the awarded answer referred to ReadAsStringAsync, the answer had no example. I followed the advice from gdp and derived a somewhat working example...
I created a single class called MessageInterceptor. I did nothing more than derive from ActionFilterAttribute and it immediately started to intercept webAPI method calls prior to the controller getting it, and after the controller finished. Here is my final class. This example uses the XML Serializer to get both the request and response into an XML string. This example finds the request and response as populated objects, this means deserialization has already occurred. Collecting the data from a populated model and serializing into an XML string is a representation of the request and response - not the actual post request and response sent back by IIS.
Code example - MessageInterceptor
using System.IO;
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Xml.Serialization;
namespace webapi_test
{
public class MessageInterceptor : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
var headers = actionContext.Request.Content.Headers.ToString();
var request = actionContext.ActionArguments.FirstOrDefault().Value;
var xml = SerializeXMLSerializer(request, "");
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
var headers = actionExecutedContext.Response.Content.Headers.ToString();
var response = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;
var xml = SerializeXMLSerializer(response, "");
}
public static string SerializeXMLSerializer(object o, string nameSpace)
{
string serializedValue;
var writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(o.GetType(), nameSpace);
serializer.Serialize(writer, o);
serializedValue = writer.ToString();
return serializedValue;
}
}
}
Use below to read Response string:
public static string GetResponseContent(HttpResponseMessage Response)
{
string rawResponse = string.Empty;
try
{
using (var stream = new StreamReader(Response.Content.ReadAsStreamAsync().Result))
{
stream.BaseStream.Position = 0;
rawResponse = stream.ReadToEnd();
}
}
catch (Exception ex) { throw; }
return rawResponse;
}

Resources