DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Schema Validation of XML/JAXB Annotated Object Containing MTOM Attachment [Code Snippet]

Schema Validation of XML/JAXB Annotated Object Containing MTOM Attachment [Code Snippet]

In this article, we will explain how can we validate an XML/JAXB annotated object containing an MTOM attachment against an XSD without losing the MTOM attachment.

Utsab Paul user avatar by
Utsab Paul
·
Nov. 08, 16 · Java Zone · Code Snippet
Like (1)
Save
Tweet
7.32K Views

Join the DZone community and get the full member experience.

Join For Free

Issue: JIRA reference https://java.net/jira/browse/JAXB-588

Resolution: Won't Fix

Description: If an XML or its corresponding JAXB annotated object which contains an MTOM attachment is validated using javax.xml.validation.Validator against an XSD, the referenced MTOM attachment is lost.

Workaround: In this article, we will explain how can we validate an XML/JAXB annotated object containing an MTOM attachment against an XSD without losing the MTOM attachment. In case an XML is used, it is advised to convert the same into a JAXB annotated object as it makes it easier to manipulate with the element which contains the MTOM attachment. Below Code shows a sample XSD snippet and it's corresponding JAVA code for schema validation.


SampleSchema.xsd snippet :

<xs:element name="request">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="requestDetails" type="ns:requestDetails" minOccurs="0" maxOccurs="1" nillable="true"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="requestDetails">
    <xs:sequence>
        <xs:element name="fileName" type="xs:string" minOccurs="0"/>
        <xs:element xmlns:xmime="http://www.w3.org/2005/05/xmlmime" name="fileMtomAttachment" type="xs:base64Binary" xmime:expectedContentTypes="application/octet-stream"/>
    </xs:sequence>
</xs:complexType>

Code snippet :

import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

...

try {
    //creating Schema Source
    Source schemaFile = new StreamSource(new File("xsd/SampleSchema.xsd"));
    SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaFile);

    Validator validator = schema.newValidator();

    // refPayload is the reference to the JAXB annotated object representing the XML.
    Request request = (Request)refPayload; 

    //create a temporary handler to handle the original MTOM attachment
    javax.activation.DataHandler tempHandler = null;
    // check for availability of MTOM attachment
        if (null != request.requestDetails() && null != request.requestDetails().getValue().getFileMtomAttachment()) {
        // setting the original MTOM attachment in the temporary handler
        tempHandler = request.requestDetails().getValue().getFileMtomAttachment();

        // setting an empty object in the original JAXB annotated object which is to be validated
        request.requestDetails().getValue().setFileMtomAttachment(new javax.activation.DataHandler(new Object() , ""));

    } else {
        request.requestDetails().getValue().setFileMtomAttachment(null);
    }
    // Validate as JAXB source
    JAXBContext jaxbContext = JAXBContext.newInstance(Request.class);
    JAXBSource jaxbSource = new JAXBSource(jaxbContext, request);
    validator.validate(jaxbSource);

    System.out.println("Jaxb Object is valid");

    //Set original MTOM attachment in case Schema validation is successful
    request.requestDetails().getValue().setFileMtomAttachment(tempHandler);

} catch (SAXException se) {
    System.out.println("Jaxb Object is NOT valid");
    System.out.println("Reason: " + se.getLocalizedMessage());
} catch (IOException e) {
    e.printStackTrace();
}

This is a simple way to preserve the MTOM attachment while schema validating the rest of the elements and attributes inside an XML.

Snippet (programming) Object (computer science) Schema

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Pub/Sub Design Pattern in .NET Distributed Cache
  • How to Get GDPR and Customer Communications Right
  • Version Number Anti-Patterns
  • On Architects, Architecture, and Failures

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo