Search code examples
xmlsoapxml-parsingxml-validation

Restrict values to send in a soap web service


I have created a soap web service and a xml validator, I'd like to know if there are any anotation I can use in order to retrict values to send in any field, example:

   @XmlType
public class ContactoBean implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 6034293066688534650L;

    @XmlElement(required = true)
    private String emailContacto;
    
    @XmlElement(required = true)
    private String telfContacto;
    
    @XmlElement(required = false)
    private String faxContacto;

Are there any anotation to use a regex in telephone in order to validate if it is a correct number? or to validate the correction of the email?

Regards


Solution

  • Are you having the code-first approach or the contract-first one (generating your classes based on a WSDL+XSD files) ?

    If latter, you could extend your related XSD types with xsd:patterns defining a desired regular expression.

    Example:

    <xsd:element name="elementName">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:pattern value="[^*+]"></xsd:pattern>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>    
    

    XMLBeans should than generate the necessary classes, getters and setters for you.

    But if you are creating your Webservice directly via Java Code I assume you need to make use of standard Java methods for regular expressions. I found following description for org.apache.xmlbeans.impl.regex.RegularExpression - maybe it helps:

    A. Standard way

    RegularExpression re = new RegularExpression(regex);
    if (re.matches(text)) { ... }
    

    B. Capturing groups

    RegularExpression re = new RegularExpression(regex);
    Match match = new Match();
    if (re.matches(text, match)) {
        ... // You can refer captured texts with methods of the Match class.
    }
    

    Case-insensitive matching

    RegularExpression re = new RegularExpression(regex, "i");
    if (re.matches(text) >= 0) { ...}
    

    Options

    You can specify options to RegularExpression(regex, options) or setPattern(regex, options). This options parameter consists of the following characters.
    
    "i"
    This option indicates case-insensitive matching.
    "m"
    ^ and $ consider the EOL characters within the text.
    "s"
    . matches any one character.
    "u"
    Redefines \d \D \w \W \s \S \b \B \< \> as becoming to Unicode.
    "w"
    By this option, \b \B \< \> are processed with the method of 'Unicode Regular Expression Guidelines' Revision 4. When "w" and "u" are specified at the same time, \b \B \< \> are processed for the "w" option.
    ","
    The parser treats a comma in a character class as a range separator. [a,b] matches a or , or b without this option. [a,b] matches a or b with this option.
    "X"
    By this option, the engine confoms to XML Schema: Regular Expression. The match() method does not do subsring matching but entire string matching.
    

    Source: http://www.docjar.com/docs/api/org/apache/xmlbeans/impl/regex/RegularExpression.html

    In my opinion, I would suggest the contract-/WSDL-first approach.