Intercepting SOAP request Enum parameter to throe message to user if request type is different than defined in Enum

978 views Asked by At

We have SOAP webservice and our approach is WSDL first. Below you see the request object generated from wsdl via wsdl to java plugin in pom.xml. This is how request object looks like:

@XmlRootElement(name = "XXXRequest") public class GetXXXRequest {

@XmlElement(name = "XXX", required = true)
@XmlJavaTypeAdapter(XX .class)
protected String x;
@XmlElement(name = "YYY", required = true)
@XmlJavaTypeAdapter(XX .class)
protected String y;
@XmlElement(name = "ZZZ", required = true)
@XmlJavaTypeAdapter(XX .class)
protected String z;
@XmlElement(name = "TTT", required = true)
@XmlJavaTypeAdapter(XX.class)
protected String t;
@XmlElement(name = "TypeOfRequest", required = true)
protected TypeOfRequest type;
@XmlElement(name = "Criteria", required = true)
protected Criteria criteria;

TypeOfRequest IS ENUM. Now TypeOfRequest had 7 values. Let us say User send any other value than the 7 values defined for it in TypeOfRequest then it comes as NULL and our validation can not validate it to see if user really did not chose any TypeOfRequest or gave some other value than 7 values. SO in nut shell i want to validate and send meaningful message if user chose any other value than 7 value. I am not able to achieve it. Is there any way i can do it ? I have an idea that i can intercept it but need guidance to implement it. We are using cxf , jaxws and Spring framework.

3

There are 3 answers

1
BeUndead On

Your tags imply you're using Spring; in which case you may want to look into spring validation. Using this, you can add annotations which specify contracts instances of your class should uphold: @NonNull, etc., and then add @Valid (or @Validated) in your controller method where you're injecting the instance.

public class XXXRequest {
    // ...
    protected @NonNull TypeOfRequest type;
    // ...
}

And in controller:

@GetMapping("/{id}")
public ResponseEntity<?> get(final @Valid XXXRequest request) {
    // ...
}
0
user3330509 On

Thanks. We are using SPring but it is not a web app it is SOAP webservice so there is no controller. Second thing is in case user send different value for TYpeOfRequest that is Enum then we get null. I want to put validation so that i can validate the wrong value of Enum.

0
user3330509 On

I was able to resolve it. Steps i did were: 1) Created a custom adapter.

public class RequestXXXXAdapter extends XmlAdapter{

@Override
public ReqXXXX unmarshal(String v) throws Exception {
    try {
        return ReqXXX.valueOf(v);
    } catch(Exception e) {
        throw new JAXBException("Invalid parameter");
    }
}

@Override
public String marshal(ReqXXXX type) throws Exception {
    return type.name();
}

}

Step 2: Added below in xsd with ReqXXXX enum.

<xs:annotation>
   <xs:appinfo>
    <xjc:javaType name="com.XX.XX.XX.ReqXXXX"
      adapter="com.xx.xx..yy.ReqXXXAdapter" /> 
   </xs:appinfo>
</xs:annotation>

... ..

So above code generated the annotation XmlJavaTypeAdapter @XmlJavaTypeAdapter(ReqTXXXXAdapter.class) protected ReqXXXX reqxxxx;

So now with this change i can validate the Enum before even my serviceimpl hits.