gov.nih.nci.cacis.sa.transcend.client.TranscendSemanticAdapter.java Source code

Java tutorial

Introduction

Here is the source code for gov.nih.nci.cacis.sa.transcend.client.TranscendSemanticAdapter.java

Source

/*
 * Copyright Ekagra and SemanticBits, LLC
 * 
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/ihub/LICENSE.txt for details.
 */
package gov.nih.nci.cacis.sa.transcend.client;

import gov.nih.nci.cacis.sa.transcend.AcceptSourceFault;
import gov.nih.nci.cacis.sa.transcend.CaCISError;
import gov.nih.nci.cacis.sa.transcend.CaCISFault;
import gov.nih.nci.cacis.sa.transcend.CaCISRequest;
import gov.nih.nci.cacis.sa.transcend.CaCISResponse;
import gov.nih.nci.cacis.sa.transcend.ErrorType;
import gov.nih.nci.cacis.sa.transcend.ResponseStatusType;
import gov.nih.nci.integration.exception.IntegrationError;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mirth.connect.connectors.ws.AcceptMessage;
import com.mirth.connect.connectors.ws.WebServiceMessageReceiver;

/**
 * This class was generated by Apache CXF 2.4.1 2012-04-11T16:58:55.116-04:00 Generated source version: 2.4.1
 * 
 */

@javax.jws.WebService(serviceName = "TranscendSemanticAdapter", portName = "AcceptSource_Port_Soap11", targetNamespace = "http://cacis.nci.nih.gov", endpointInterface = "gov.nih.nci.cacis.sa.transcend.AcceptSourcePortType")
public class TranscendSemanticAdapter extends AcceptMessage {

    private static final Logger LOG = LoggerFactory.getLogger(TranscendSemanticAdapter.class.getName());

    private JAXBContext jc = null;

    /**
     * Constructor
     * 
     * @param webServiceMessageReceiver - WebServiceMessageReceiver
     */
    public TranscendSemanticAdapter(WebServiceMessageReceiver webServiceMessageReceiver) {
        super(webServiceMessageReceiver);
    }

    /**
     * AcceptSource operation accepts data from Clinical Source system and sends it to Mirth Connect for processing
     * 
     * @param parameter caCISRequest
     * @return CaCISResponse
     * @throws AcceptSourceFault Web Service Fault
     */
    @WebResult(name = "caCISResponse", targetNamespace = "http://cacis.nci.nih.gov", partName = "parameter")
    @WebMethod
    public CaCISResponse acceptSource(
            @WebParam(partName = "parameter", name = "caCISRequest", targetNamespace = "http://cacis.nci.nih.gov") CaCISRequest parameter)
            throws AcceptSourceFault {

        LOG.debug("Executing operation acceptSource");

        final gov.nih.nci.cacis.sa.transcend.CaCISResponse response = new CaCISResponse();

        final String reqstr = getCaCISRequestxml(parameter);

        if (StringUtils.isEmpty(reqstr)) {
            throw new AcceptSourceFault("Error marshalling CaCISRequest!");
        }

        String mcResponse = webServiceMessageReceiver.processData(reqstr);

        if (LOG.isDebugEnabled()) {
            LOG.debug("TranscendSemanticAdapter..MC RESPONSE:" + mcResponse);
        }

        if (mcResponse != null && (mcResponse.indexOf("Error") > -1 || mcResponse.indexOf("Exception") > -1
                || mcResponse.indexOf("ERROR") > -1 || mcResponse.indexOf("error") > -1)) {
            mcResponse = StringUtils.remove(mcResponse, "SUCCESS:");
            mcResponse = StringUtils.remove(mcResponse, "FAILURE:");
            AcceptSourceFault fault;
            try {
                fault = new AcceptSourceFault("Error processing Data from Source System",
                        getCaCISFaultFromXml(mcResponse));
                throw fault;
            } catch (JAXBException ex) {
                final CaCISFault cf = new CaCISFault();
                final CaCISError ce = new CaCISError();
                final IntegrationError ie = IntegrationError._1000;
                ce.setErrorCode(String.valueOf(ie.getErrorCode()));
                ce.setErrorMessage(ie.getMessage((Object) null));
                ce.setErrorType(ErrorType.TRANSMISSION);
                ce.setDetail(stackTraceAsString(ex));
                cf.getCaCISError().add(ce);
                fault = new AcceptSourceFault("Error accepting Data from Source System!" + ex.getMessage(), cf);
                throw fault;
            }

        }
        response.setStatus(ResponseStatusType.SUCCESS);

        return response;

    }

    private String getCaCISRequestxml(final CaCISRequest parameter) {

        final StringWriter sw = new StringWriter();
        try {
            getMarshaller().marshal(parameter, sw);
        } catch (JAXBException e) {
            LOG.error("Error marshalling CaCISRequest!", e);
            return null;
        }
        return sw.toString();

    }

    private CaCISFault getCaCISFaultFromXml(String faultXML) throws JAXBException {
        final StreamSource ss = new StreamSource(new StringReader(faultXML));
        return ((JAXBElement<CaCISFault>) getUnmarshaller().unmarshal(ss, CaCISFault.class)).getValue();
    }

    private Marshaller getMarshaller() throws JAXBException {
        if (jc == null) {
            jc = JAXBContext.newInstance(CaCISRequest.class);
        }
        return jc.createMarshaller();
    }

    private Unmarshaller getUnmarshaller() throws JAXBException {
        if (jc == null) {
            jc = JAXBContext.newInstance(CaCISRequest.class);
        }
        return jc.createUnmarshaller();
    }

    private String stackTraceAsString(Exception e) {
        try {
            return URLEncoder.encode(ExceptionUtils.getFullStackTrace(e), "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            return e.getMessage();
        }
    }

}