Set faultCode to SENDER in a custom SoapFault? - soap

I want to throw my own custom exceptions through Spring-WS.
I write my Exception class like this :
#SoapFault(faultCode = FaultCode.CUSTOM, customFaultCode = "{http://www.myorg.com/test}INVALID_INPUT_DATA")
public class InvalidDataException extends Exception
{
public InvalidDataException(String errorMsg)
{
super(errorMsg);
}
public InvalidDataException(Throwable t)
{
super(t);
}
}
When my service throws this exception with a text parameter, I get a good soapFault with my customFaultCode as a subCode :
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Receiver</env:Value>
<env:Subcode>
<env:Value xmlns:ns1="http://www.myorg.com/test">ns1:INVALID_INPUT_DATA</env:Value>
</env:Subcode>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">WTF! Something wrong with your input data...</env:Text>
</env:Reason>
</env:Fault>
</env:Body>
</env:Envelope>
But I don't want to have a faultCode env:Receiver, I'd like to change it to env:Sender because the problem with input data is on client side !
But I don't know how to do. If I change faultCode to FaultCode.SENDER in my exception class, I get well a env:Sender faultCode but my customFaultCode is not considered and I get :
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Sender</env:Value>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">WTF! Something wrong with your input data...</env:Text>
</env:Reason>
</env:Fault>
</env:Body>
</env:Envelope>
I saw in the javadoc of SimpleSoapExceptionResolver that the fault code is always set to Receiver (for SOAP 1.2).
How can I change this ?
Stieuma

Related

System exception occurred during an invocation on EJB caused by illegal argument exception

I'm not too sure what is wrong with it. It keeps on showing these error messages. Seems like there are some null values but i can't seem to identify and correct those issues. Can anyone help to take a look?
Update: I'm thinking the issue should either in the managedbean or post construct method but i can't seem to find out why..
Summarized Error Message
System exception occurred during an invocation on EJB RequestController,
method: public entity.Request ejb.session.stateless.RequestController.retrieveRequestByRequestId(java.lang.Long) throws exception.RequestNotFoundException
Caused by: java.lang.IllegalArgumentException: An instance of a null PK has been incorrectly provided for this find operation.
Post Construct In DeleteRequestManagedBean
#PostConstruct
public void postConstruct() {
requestId = (Long)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("request");
try {
request = requestControllerLocal.retrieveRequestByRequestId(requestId);
}
//Removed some irrelevant codes
Delete Request Method In DeleteRequestManagedBean
public void deleteRequest() {
requestControllerLocal.deleteRequest(requestId);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Request has been successfully deleted: ", null));
}
Button to delete request in xhtml
<p:commandButton value="Remove" icon ="fa fa-fw fa-remove" styleClass="p-btnDelete" actionListener="#{deleteRequestManagedBean.deleteRequest}"/>
Delete Request Method in RequestController (Stateless Sessionbean)
#Override
public void deleteRequest(Long requestId) throws RequestNotFoundException, DeleteRequestException {
//Removed some irrelevant codes
Request requestToDelete = retrieveRequestByRequestId(requestId);
requestToDelete.getCategory().getRequests().remove(requestToDelete);
em.remove(requestToDelete);
}
Please feel free to let me know if there are any other information you need. Thank you!
It turns out that both my managedbean and xhtml page has issues.
I have to use getAttribute() instead and get the attribute "postRToDelete" from the xhtml page. Without getting retrieving this, it keeps on giving me the null pointers, illegal argument exceptions.
Here it is for the benefit for those who made the same mistake as me.
DeleteRequestManagedbean
Request request = (Request) event.getComponent().getAttributes().get("postRToDelete");
xhtml page
<p:commandButton value="Delete" icon ="fa fa-fw fa-remove" styleClass="p-btnDelete" actionListener="#{profileManagedBean.deletePostedRequest}" update="#form">
<!--attribute from here-->
<f:attribute name="postRToDelete" value="#{postedReq}"/>
</p:commandButton>

Catching custom exception with Service Fabric remoting

The following document details exception handling in remoting for Service Fabric (I use V2):
https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-remoting#remoting-exception-handling
It has the following paragraph:
All remote exceptions thrown by the service API are sent back to the
client as AggregateException. RemoteExceptions should be DataContract
serializable; if they are not, the proxy API throws ServiceException
with the serialization error in it.
I have made the following exception, in a .NET Core class library shared between the services:
[DataContract]
public class DuplicateEntityException : Exception
{
public DuplicateEntityException(string message = "Duplicate entity.") : base(message) { }
}
I get the following message after throwing the exception in the called service:
System.AggregateException: One or more errors occurred. (The exception DuplicateEntityException was unhandled on the service and could not be serialized for transferring to the client.
If I just throw Exception it serializes correctly.
Any help in making my exception class DataContract serializable would be much appreciated.
All I had to do was:
[Serializable()]
public class DuplicateEntityException : Exception, ISerializable
{
public DuplicateEntityException(string message = "Duplicate entity.") : base(message) { }
public DuplicateEntityException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

Empty uri in rest response

I am developing a plugin for nexus oss .My app creates a rest call response(to a request from server) . But when the server receives it , it throws error as follows
javax.xml.bind.UnmarshalException:
unexpected element (uri:"", local:"com.collabnet.teamforge.ia.types.GetConfigurationParametersResponse").
Expected elements are
\lt{http://www.collab.net/teamforge/integratedapp}CreateProjectConfigurationRequest\gt,
\lt{http://www.collab.net/teamforge/integratedapp}GetConfigurationParametersRequest\gt,
\lt{http://www.collab.net/teamforge/integratedapp}GetConfigurationParametersResponse\gt,
\lt{http://www.collab.net/teamforge/integratedapp}GetPageComponentParametersRequest>
I guess the reason behind this exception is that the response doesn't match with the expected because the uri ( this is just my guess , if it's wrong please correct me),that is the namespace in response is not set .
snip of the Code in my plugin is as follows
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"configurationParameter"
})
#XmlRootElement(name = "GetConfigurationParametersResponse", namespace = "http://www.collab.net/teamforge/integratedapp")
public class GetConfigurationParametersResponse
extends BaseResponse
{
Why is name space not picked up while creating response ?
Even correct me if the real reason for the exception is not the empty uri. If so what is the real reason behind this exception ?
Please help .
Based on the error message the XML document being passed to JAXB is. It appears as though this XML is being created by something other than JAXB (I suspect XStream).
<com.collabnet.teamforge.ia.types.GetConfigurationParametersResponse>
...
</com.collabnet.teamforge.ia.types.GetConfigurationParametersResponse>
Your JAXB mappings are expecting an XML document like the following:
<GetConfigurationParametersResponse xmlns="http://www.collab.net/teamforge/integratedapp">
...
</GetConfigurationParametersResponse>
If you need to interact with the following XML:
<com.collabnet.teamforge.ia.types.GetConfigurationParametersResponse>
...
</com.collabnet.teamforge.ia.types.GetConfigurationParametersResponse>
Then you can change your mapping to be:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"configurationParameter"
})
#XmlRootElement(name = "com.collabnet.teamforge.ia.types.GetConfigurationParametersResponse")
public class GetConfigurationParametersResponse
extends BaseResponse
{

Unit testing an implementation of IExpectException

I've created an expected exception attribute by implementing the NUnit.Framework.IExpectException interface, as documented there (http://www.nunit.org/index.php?p=exception&r=2.6.2 ) and it works pretty well. It validates that the thrown exception is of the right type and validates the value of some properties on the exception.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class ExpectedHttpErrorExceptionAttribute : ExpectedExceptionAttribute, IExpectException
{
public HttpStatusCode StatusCode { get; set; }
public ExpectedHttpErrorExceptionAttribute()
:base(typeof(HttpError))
{}
public ExpectedHttpErrorExceptionAttribute(HttpStatusCode statusCode)
:this()
{
StatusCode = statusCode;
}
public void HandleException(Exception ex)
{
Assert.That(ex, Is.TypeOf(ExpectedException), "Expected exception of type '{0}' but an exception of type '{1}' has been throwned.", typeof(HttpError).FullName, ex.GetType().FullName);
var httpStatusCode = ((HttpError) ex).StatusCode;
Assert.That(httpStatusCode, Is.EqualTo(StatusCode), "Expected status code '{0}' but was '{1}'.", StatusCode, httpStatusCode);
}
}
My problem is that I want to unit test that attribute, but I can't figure a way to test the following case :
A unit test that have the attribute should fail if no exception are thrown.
I just can't figure a way to write a unit test for that case, since the HandleException is not called when no exception are thrown.
Any suggestion?
The reason you are failing to write a test here is that you are trying to test functionality which is out of responsibility of your class.
Your class validates the exception only in case if exception is thrown. When there is no exception thrown there is nothing to validate :)
It is responsibility of test runner (NUnit or some another) to fail the test when test method is decorated with ExpectedExceptionAttribute (or inherited attribute) and there is no exception thrown during test.
So, you don't need to write a test fot such a scenario.

XSD2Code namespace issue

I am using XSD2Code to generate C# class from XSD file.
I got stuck with the following problem.
XML file looks something like
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Notification xmlns="http://message.domain.com">
<Object xmlns="http://type.domain.com" ID="97440" />
</Notification>
XML gets succefsully deserialized when xmls for Object is empty. But when there is a value like in the sample above, I get an error "Object reference not set to an instance of an object".
What could cause this error?
you have to change the Serializer to something like that
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(Notification), "http://message.domain.com");
}
return serializer;
}
}
To turn off encoding, disable encoding on the Serialization tab