org.ojbc.adapters.identifier.MessageUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.ojbc.adapters.identifier.MessageUtils.java

Source

/*
 * Unless explicitly acquired and licensed from Licensor under another license, the contents of
 * this file are subject to the Reciprocal Public License ("RPL") Version 1.5, or subsequent
 * versions as allowed by the RPL, and You may not copy or use this file in either source code
 * or executable form, except in compliance with the terms and conditions of the RPL
 *
 * All software distributed under the RPL is provided strictly on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND LICENSOR HEREBY DISCLAIMS ALL SUCH
 * WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific language
 * governing rights and limitations under the RPL.
 *
 * http://opensource.org/licenses/RPL-1.5
 *
 * Copyright 2012-2015 Open Justice Broker Consortium
 */
package org.ojbc.adapters.identifier;

import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.camel.Exchange;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.model.ModelCamelContext;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.headers.Header;
import org.apache.cxf.message.MessageImpl;
import org.ojbc.util.camel.helper.OJBUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MessageUtils {

    private static DocumentBuilder documentBuilder;

    public static Map<String, Object> createHeaders() throws Exception {

        if (documentBuilder == null) {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
        }

        Map<String, Object> headers = new HashMap<String, Object>();
        //Set the WS-Address Message ID
        Map<String, Object> requestContext = OJBUtils.setWSAddressingMessageID("123456789");

        //Set the operation name and operation namespace for the CXF exchange
        headers.put(Client.REQUEST_CONTEXT, requestContext);

        Document doc = documentBuilder.newDocument();
        List<SoapHeader> soapHeaders = new ArrayList<SoapHeader>();
        soapHeaders.add(makeSoapHeader(doc, "http://www.w3.org/2005/08/addressing", "MessageID", "123456789"));
        headers.put(Header.HEADER_LIST, soapHeaders);
        return headers;
    }

    protected static Exchange createSenderExchange(ModelCamelContext context, String pathToInputFile)
            throws Exception, IOException {
        //Create a new exchange
        Exchange senderExchange = new DefaultExchange(context);

        //Set the WS-Address Message ID
        Map<String, Object> requestContext = OJBUtils.setWSAddressingMessageID("123456789");

        //Set the operation name and operation namespace for the CXF exchange
        senderExchange.getIn().setHeader(Client.REQUEST_CONTEXT, requestContext);

        Document doc = createDocument();
        List<SoapHeader> soapHeaders = new ArrayList<SoapHeader>();
        soapHeaders.add(
                MessageUtils.makeSoapHeader(doc, "http://www.w3.org/2005/08/addressing", "MessageID", "12345"));
        senderExchange.getIn().setHeader(Header.HEADER_LIST, soapHeaders);

        org.apache.cxf.message.Message message = new MessageImpl();

        senderExchange.getIn().setHeader(CxfConstants.CAMEL_CXF_MESSAGE, message);

        //Read the firearm search request file from the file system
        File inputFile = new File(pathToInputFile);
        String inputStr = FileUtils.readFileToString(inputFile);

        assertNotNull(inputStr);

        //Set it as the message message body
        senderExchange.getIn().setBody(inputStr);
        return senderExchange;
    }

    public static Document createDocument() throws Exception {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document doc = dbf.newDocumentBuilder().newDocument();

        return doc;
    }

    public static SoapHeader makeSoapHeader(Document doc, String namespace, String localName, String value) {
        Element messageId = doc.createElementNS(namespace, localName);
        messageId.setTextContent(value);
        SoapHeader soapHeader = new SoapHeader(new QName(namespace, localName), messageId);
        return soapHeader;
    }

}