DZone
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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Freshness as a First-Class Schema Concern: Modeling Data Staleness Across 83 Data Sources
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Dynamic Tool Selection: A Portable Pattern for Agents Drowning in Tool Schemas

Trending

  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. 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.

By 
Utsab Paul
·
Nov. 08, 16 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Freshness as a First-Class Schema Concern: Modeling Data Staleness Across 83 Data Sources
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Dynamic Tool Selection: A Portable Pattern for Agents Drowning in Tool Schemas

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook