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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
  • Dynamically Evaluate Dataweave Scripts
  • How to View MongoDB Collections as Diagrams
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion

Trending

  • How I Supercharged My GenAI App and Saved My Sanity
  • Secure Your Oracle Database Passwords in AWS RDS With a Password Verification Function
  • Secure IaC With a Shift-Left Approach
  • Privacy-Preserving AI: How Multimodal Models Are Changing Data Security
  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 user avatar
Utsab Paul
·
Nov. 08, 16 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
8.4K 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.

Related

  • The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
  • Dynamically Evaluate Dataweave Scripts
  • How to View MongoDB Collections as Diagrams
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: