|
Glassfish example source code file (WrappedMessage.java)
The Glassfish WrappedMessage.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ /* * WrappedMessage.java * * Created on November 20, 2006, 6:03 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package com.sun.enterprise.jbi.serviceengine.comm; import com.sun.enterprise.jbi.serviceengine.core.JavaEEServiceEngineContext; import com.sun.enterprise.jbi.serviceengine.util.DOMUtil; import com.sun.enterprise.jbi.serviceengine.util.JBIConstants; import com.sun.enterprise.jbi.serviceengine.util.soap.EndpointMetaData; import com.sun.enterprise.jbi.serviceengine.util.soap.SOAPConstants; import com.sun.logging.LogDomains; import com.sun.xml.bind.api.Bridge; import com.sun.xml.ws.api.message.AttachmentSet; import com.sun.xml.ws.api.message.HeaderList; import com.sun.xml.ws.api.message.Message; import com.sun.xml.ws.message.DataHandlerAttachment; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import javax.wsdl.Part; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.ContentHandler; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; /** * * @author bhavani */ public class WrappedMessage extends Message implements JBIConstants, SOAPConstants { private static Logger logger = LogDomains.getLogger(WrappedMessage.class, LogDomains.SERVER_LOGGER); private static DocumentBuilderFactory dbf; /** * Holds the contents of the wrapped message. */ Source wrappedMessage; /** * Variables used for wrapping the message. */ Message abstractMessage; QName wsdlMessageType; String wsdlBindingStyle; String wsdlMessageName; List<Part> wsdlOrderedParts; int[] wsdlPartBindings; // represents whether the part is bound to 'header', 'body', or 'attachment'. public void setAbstractMessage(Message abstractMessage) { this.abstractMessage = abstractMessage; } public void setWSDLMessageType(QName wsdlMessageType) { this.wsdlMessageType = wsdlMessageType; } public void setWSDLBindingStyle(String wsdlBindingStyle) { this.wsdlBindingStyle = wsdlBindingStyle; } public void setWSDLMessageName(String wsdlMessageName) { this.wsdlMessageName = wsdlMessageName; } public void setWSDLOrderedParts(List<Part> wsdlOrderedParts) { this.wsdlOrderedParts = wsdlOrderedParts; } public void setWSDLPartBindings(int[] wsdlPartBindings) { this.wsdlPartBindings = wsdlPartBindings; } private boolean log = false; private void setLog() { if(logger.isLoggable(Level.FINE)) { log = true; } } public WrappedMessage() { if(dbf == null) { dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); } setLog(); } public void wrap() throws Exception { if(JavaEEServiceEngineContext.getInstance().isServiceMix()) { if(log) { logger.log(Level.FINE, "Skipping the wrapping..."); } wrappedMessage = abstractMessage.readPayloadAsSource(); } else { if(RPC_STYLE.equalsIgnoreCase(wsdlBindingStyle)) { new RPCStyleWrapper().wrap(); } else { new DocumentStyleWrapper().wrap(); } } } private void writeJBIHeader(XMLStreamWriter writer) throws Exception { writer.writeStartDocument(); writer.writeStartElement(WRAPPER_MESSAGE_QNAME); writeMessageAttributes(writer, wsdlMessageName); } private void writeJBIFooter(XMLStreamWriter writer) throws Exception { writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); } private void setMessage(byte[] data) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(data); String usedWith = System.getProperty(USED_WITH); if(usedWith != null && usedWith.indexOf(USED_WITH_HTTP_SOAP_BC) != -1) { DocumentBuilder db = dbf.newDocumentBuilder(); wrappedMessage = new DOMSource(db.parse(bais)); } else { wrappedMessage = new StreamSource(bais); } } class DocumentStyleWrapper { void wrap() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter writer = XOF.createXMLStreamWriter(baos, "UTF-8"); writeJBIHeader(writer); for(int i=0, headerIndex=0; i<wsdlPartBindings.length; i++) { switch(wsdlPartBindings[i]) { case SOAP_BODY_BINDING : if(log) { logger.log(Level.FINE, "Adding part number " + i + " from payLoad"); } if(abstractMessage.hasPayload()) { writer.writeStartElement(WRAPPER_PART_QNAME); // assuming only one part in the response. writer.writeCharacters(""); // Force completion of open elems writePayloadTo(writer); writer.writeEndElement(); } break; case SOAP_HEADER_BINDING : if(log) { logger.log(Level.FINE, "Adding part number " + i + " from header"); } writer.writeStartElement(WRAPPER_PART_QNAME); // assuming only one part in the response. writer.writeCharacters(""); // Force completion of open elems writer.flush(); abstractMessage.getHeaders().get(headerIndex++).writeTo(writer); writer.writeEndElement(); break; case SOAP_ATTACHMENT_BINDING : if(log) { logger.log(Level.FINE, "Part number " + i + " is an attachment"); } break; } } writeJBIFooter(writer); setMessage(baos.toByteArray()); if(log) { logger.log(Level.FINE, "\n\nWrapped message = " + baos.toString() + "\n\n"); } } catch(Exception ex) { logger.log(Level.SEVERE, ex.getMessage(), ex); } } } class RPCStyleWrapper { void wrap() throws Exception { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter writer = XOF.createXMLStreamWriter(baos, "UTF-8"); writeJBIHeader(writer); XMLStreamReader payLoad = abstractMessage.readPayload(); if(payLoad.getEventType() == XMLStreamReader.START_DOCUMENT) { payLoad.next(); } Map<String,String> payLoadNSAttrs = new HashMap Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish WrappedMessage.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.