/* * Created on 19.11.2003 * * (c) Stabsstelle IKT-Strategie des Bundes */ package at.gv.egovernment.moa.ss.erechtclient.moainvoker; import java.io.IOException; import java.io.OutputStream; import java.rmi.RemoteException; import java.util.Vector; import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceException; import javax.xml.rpc.ServiceFactory; import org.apache.axis.message.SOAPBodyElement; import org.apache.log4j.Logger; import org.apache.xerces.parsers.DOMParser; import org.apache.xerces.xni.parser.XMLInputSource; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.DOMConfiguration; import org.w3c.dom.Document; import org.w3c.dom.Element; import at.gv.egovernment.moa.ss.erechtclient.ERechtClientException; import at.gv.egovernment.moa.ss.erechtclient.init.Constants; import at.gv.egovernment.moa.ss.erechtclient.util.DOMUtils; /** * @author Gregor Karlinger (mailto:gregor.karlinger@cio.gv.at) */ public class MOAInvoker { private static Logger logger_ = Logger.getLogger(Constants.LH_MOAINVOKER_); private static final String NAME_ERROR_ = "ErrorResponse"; private static final String NAME_ERROR_CODE_ = "ErrorCode"; private static final String NAME_ERROR_INFO_ = "Info"; /** * Invokes MOA SS. * * @param request The XML request to be sent to MOA SS. * * @param endpoint The endpoint of the SOAP service where to send the XML request to. * * @param mOASchemaLoc The schema location URI for the MOA schema (for validating the MOA SS service response). * * @return the XML response from the service. * * @throws Exception if getting the XML response from the SOAP response body fails. * * @throws RemoteException if MOA SS signals an error. * * @throws ServiceException if the SOAP client invoking MOA SS signals an error. */ public static Document invokeSS(Document request, String endpoint, String mOASchemaLoc) throws ERechtClientException { QName serviceQName = new QName(Constants.MI_SS_QNAME_); return invoke(request, endpoint, serviceQName, mOASchemaLoc); } /* ---------------------------------------------------------------------------------------------------- */ private static Document invoke(Document request, String endpoint, QName serviceQName, String mOASchemaLoc) throws ERechtClientException { Document response; try { // Instantiate AXIS service Service service = ServiceFactory.newInstance().createService(serviceQName); // Create and configure service call Call call = service.createCall(); call.setTargetEndpointAddress(endpoint); // Create SOAP body SOAPBodyElement body = new SOAPBodyElement(request.getDocumentElement()); SOAPBodyElement[] params = new SOAPBodyElement[] {body}; // Make call Vector responses = (Vector) call.invoke(params); // Get response SOAPBodyElement responseBody = (SOAPBodyElement) responses.get(0); response = responseBody.getAsDocument(); } catch (Exception e) { String message = "MOA SS Service invocation failed."; logger_.error(message, e); throw new ERechtClientException(message, e); } // Validate response DOMConfiguration docConfig = response.getDomConfig(); DOMErrorHandler errorHandler = new DOMErrorHandler(); docConfig.setParameter("namespaces", Boolean.FALSE); docConfig.setParameter("schema-location", mOASchemaLoc); docConfig.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); docConfig.setParameter("validate", Boolean.TRUE); docConfig.setParameter("error-handler", errorHandler); response.normalizeDocument(); if (errorHandler.getErrorCount() > 0) { String message = "Parsing the MOA SS service response failed:\n" + errorHandler.getErrorSummaryMsg(); logger_.error(message); throw new ERechtClientException(message); } // Check if first child of response root element is an error element Element responseElem = response.getDocumentElement(); Element errorElem = DOMUtils.getChildElem(responseElem, Constants.NSURI_MOA_13_, NAME_ERROR_); if (errorElem != null) { String errorCode = DOMUtils.getChildText(errorElem, Constants.NSURI_MOA_13_, NAME_ERROR_CODE_); String errorInfo = DOMUtils.getChildText(errorElem, Constants.NSURI_MOA_13_, NAME_ERROR_INFO_); String message = "MOA SS Service indicated an error:\n" + "Error code: " + errorCode + "\n" + "Error info: " + errorInfo; logger_.error(message); throw new ERechtClientException(message); } return response; } /* ---------------------------------------------------------------------------------------------------- */ public static void serializeElement(Element rootElem, OutputStream out) throws ERechtClientException { // TODO Replace with DOMImplementationLS OutputFormat format = new OutputFormat(rootElem.getOwnerDocument()); format.setLineSeparator("\n"); format.setIndenting(false); format.setPreserveSpace(true); format.setOmitXMLDeclaration(false); format.setEncoding("UTF-8"); XMLSerializer serializer = new XMLSerializer(out, format); try { serializer.serialize(rootElem); } catch (Exception e) { String message = "Serializing the XML document failed."; logger_.error(message); throw new ERechtClientException(message); } } }