Search in sources :

Example 41 with SOAPPart

use of javax.xml.soap.SOAPPart in project iaf by ibissource.

the class SoapWrapper method signMessage.

public Message signMessage(Message soapMessage, String user, String password, boolean passwordDigest) {
    try {
        // We only support signing for soap1_1 ?
        // Create an empty message and populate it later. createMessage(MimeHeaders, InputStream) requires proper headers to be set which we do not have...
        MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        SOAPMessage msg = factory.createMessage();
        SOAPPart part = msg.getSOAPPart();
        part.setContent(new StreamSource(soapMessage.asInputStream()));
        // create unsigned envelope
        SOAPEnvelope unsignedEnvelope = part.getEnvelope();
        Document doc = unsignedEnvelope.getOwnerDocument();
        // create security header and insert it into unsigned envelope
        WSSecHeader secHeader = new WSSecHeader(doc);
        secHeader.insertSecurityHeader();
        // add a UsernameToken
        WSSecUsernameToken tokenBuilder = new WSSecUsernameToken(secHeader);
        tokenBuilder.setIdAllocator(idAllocator);
        if (passwordDigest) {
            tokenBuilder.setPasswordType(WSConstants.PASSWORD_DIGEST);
        } else {
            tokenBuilder.setPasswordType(WSConstants.PASSWORD_TEXT);
        }
        tokenBuilder.setPrecisionInMilliSeconds(false);
        tokenBuilder.setUserInfo(user, password);
        WSTimeSource timesource = tokenBuilder.getWsTimeSource();
        tokenBuilder.addNonce();
        tokenBuilder.addCreated();
        tokenBuilder.prepare(null);
        Element element = tokenBuilder.getUsernameTokenElement();
        String nonce = XmlUtils.getChildTagAsString(element, "wsse:Nonce");
        byte[] decodedNonce = org.apache.xml.security.utils.XMLUtils.decode(nonce);
        String created = XmlUtils.getChildTagAsString(element, "wsu:Created");
        WSSecSignature sign = new WSSecSignature(secHeader);
        sign.setIdAllocator(idAllocator);
        sign.setCustomTokenValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
        sign.setCustomTokenId(tokenBuilder.getId());
        sign.setSigCanonicalization(WSConstants.C14N_EXCL_OMIT_COMMENTS);
        sign.setAddInclusivePrefixes(false);
        // conform WS-Trust spec
        String signatureValue = UsernameTokenUtil.doPasswordDigest(decodedNonce, created, password);
        sign.setSecretKey(signatureValue.getBytes(StreamUtil.DEFAULT_CHARSET));
        // UT_SIGNING no longer exists since v1.5.11
        sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
        sign.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
        sign.build(null);
        tokenBuilder.prependToHeader();
        // add a Timestamp
        WSSecTimestamp timestampBuilder = new WSSecTimestamp(secHeader);
        timestampBuilder.setWsTimeSource(timesource);
        timestampBuilder.setTimeToLive(300);
        timestampBuilder.setIdAllocator(idAllocator);
        timestampBuilder.build();
        return new Message(doc);
    } catch (Exception e) {
        throw new RuntimeException("Could not sign message", e);
    }
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) Message(nl.nn.adapterframework.stream.Message) SOAPMessage(javax.xml.soap.SOAPMessage) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) WSSecSignature(org.apache.wss4j.dom.message.WSSecSignature) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) WSSecTimestamp(org.apache.wss4j.dom.message.WSSecTimestamp) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) WSSecHeader(org.apache.wss4j.dom.message.WSSecHeader) SOAPPart(javax.xml.soap.SOAPPart) WSSecUsernameToken(org.apache.wss4j.dom.message.WSSecUsernameToken) WSTimeSource(org.apache.wss4j.common.util.WSTimeSource)

Example 42 with SOAPPart

use of javax.xml.soap.SOAPPart in project iaf by ibissource.

the class SoapWrapperTest method toSoapMessage.

private Message toSoapMessage(URL url) throws Exception {
    MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
    SOAPMessage msg = factory.createMessage();
    SOAPPart part = msg.getSOAPPart();
    part.setContent(new StreamSource(url.openStream()));
    // create unsigned envelope
    SOAPEnvelope unsignedEnvelope = part.getEnvelope();
    Document doc = unsignedEnvelope.getOwnerDocument();
    return new Message(doc);
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) Message(nl.nn.adapterframework.stream.Message) SOAPMessage(javax.xml.soap.SOAPMessage) StreamSource(javax.xml.transform.stream.StreamSource) SOAPPart(javax.xml.soap.SOAPPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 43 with SOAPPart

use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.

the class XMLABaseComponent method executeQuery.

/**
 * Execute query
 *
 * @param query   - MDX to be executed
 * @param catalog
 * @param handler Callback handler
 * @throws XMLAException
 */
public boolean executeQuery(final String query, final String catalog) throws XMLAException {
    Object[][] columnHeaders = null;
    Object[][] rowHeaders = null;
    Object[][] data = null;
    int columnCount = 0;
    int rowCount = 0;
    SOAPConnection connection = null;
    SOAPMessage reply = null;
    try {
        connection = scf.createConnection();
        SOAPMessage msg = mf.createMessage();
        MimeHeaders mh = msg.getMimeHeaders();
        // $NON-NLS-1$
        mh.setHeader("SOAPAction", XMLABaseComponent.EXECUTE_ACTION);
        SOAPPart soapPart = msg.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.setEncodingStyle(XMLABaseComponent.ENCODING_STYLE);
        SOAPBody body = envelope.getBody();
        // $NON-NLS-1$//$NON-NLS-2$
        Name nEx = envelope.createName("Execute", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eEx = body.addChildElement(nEx);
        eEx.setEncodingStyle(XMLABaseComponent.ENCODING_STYLE);
        // add the parameters
        // COMMAND parameter
        // <Command>
        // <Statement>select [Measures].members on Columns from
        // Sales</Statement>
        // </Command>
        // $NON-NLS-1$ //$NON-NLS-2$
        Name nCom = envelope.createName("Command", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eCommand = eEx.addChildElement(nCom);
        // $NON-NLS-1$ //$NON-NLS-2$
        Name nSta = envelope.createName("Statement", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eStatement = eCommand.addChildElement(nSta);
        eStatement.addTextNode(query);
        // <Properties>
        // <PropertyList>
        // <DataSourceInfo>Provider=MSOLAP;Data
        // Source=local</DataSourceInfo>
        // <Catalog>Foodmart 2000</Catalog>
        // <Format>Multidimensional</Format>
        // <AxisFormat>TupleFormat</AxisFormat> oder "ClusterFormat"
        // </PropertyList>
        // </Properties>
        Map paraList = new HashMap();
        // $NON-NLS-1$
        paraList.put("DataSourceInfo", dataSource);
        // $NON-NLS-1$
        paraList.put("Catalog", catalog);
        // $NON-NLS-1$ //$NON-NLS-2$
        paraList.put("Format", "Multidimensional");
        // $NON-NLS-1$ //$NON-NLS-2$
        paraList.put("AxisFormat", "TupleFormat");
        // $NON-NLS-1$ //$NON-NLS-2$
        addParameterList(envelope, eEx, "Properties", "PropertyList", paraList);
        msg.saveChanges();
        // $NON-NLS-1$
        debug("Request for Execute");
        logSoapMsg(msg);
        // run the call
        reply = connection.call(msg, url);
        // $NON-NLS-1$
        debug("Reply from Execute");
        logSoapMsg(reply);
        // error check
        errorCheck(reply);
        // process the reply
        SOAPElement eRoot = findExecRoot(reply);
        // for each axis, get the positions (tuples)
        // $NON-NLS-1$ //$NON-NLS-2$
        Name name = envelope.createName("Axes", "", XMLABaseComponent.MDD_URI);
        SOAPElement eAxes = selectSingleNode(eRoot, name);
        if (eAxes == null) {
            // $NON-NLS-1$
            throw new XMLAException("Excecute result has no Axes element");
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        name = envelope.createName("Axis", "", XMLABaseComponent.MDD_URI);
        Iterator itAxis = eAxes.getChildElements(name);
        AxisLoop: for (int iOrdinal = 0; itAxis.hasNext(); ) {
            SOAPElement eAxis = (SOAPElement) itAxis.next();
            // $NON-NLS-1$
            name = envelope.createName("name");
            String axisName = eAxis.getAttributeValue(name);
            int axisOrdinal;
            if (axisName.equals("SlicerAxis")) {
                // $NON-NLS-1$
                continue;
            } else {
                axisOrdinal = iOrdinal++;
            }
            // $NON-NLS-1$//$NON-NLS-2$
            name = envelope.createName("Tuples", "", XMLABaseComponent.MDD_URI);
            SOAPElement eTuples = selectSingleNode(eAxis, name);
            if (eTuples == null) {
                // what else?
                continue AxisLoop;
            }
            // $NON-NLS-1$//$NON-NLS-2$
            name = envelope.createName("Tuple", "", XMLABaseComponent.MDD_URI);
            Iterator itTuple = eTuples.getChildElements(name);
            // loop over tuples
            int positionOrdinal = 0;
            while (itTuple.hasNext()) {
                // TupleLoop
                SOAPElement eTuple = (SOAPElement) itTuple.next();
                if ((axisOrdinal == XMLABaseComponent.AXIS_COLUMNS) && (columnHeaders == null)) {
                    // $NON-NLS-1$
                    columnCount = getChildCount(envelope, eTuples, "Tuple");
                    // $NON-NLS-1$
                    columnHeaders = new Object[getChildCount(envelope, eTuple, "Member")][columnCount];
                } else if ((axisOrdinal == XMLABaseComponent.AXIS_ROWS) && (rowHeaders == null)) {
                    // $NON-NLS-1$
                    rowCount = getChildCount(envelope, eTuples, "Tuple");
                    // $NON-NLS-1$
                    rowHeaders = new Object[rowCount][getChildCount(envelope, eTuple, "Member")];
                }
                int index = 0;
                // $NON-NLS-1$//$NON-NLS-2$
                name = envelope.createName("Member", "", XMLABaseComponent.MDD_URI);
                Iterator itMember = eTuple.getChildElements(name);
                while (itMember.hasNext()) {
                    // MemberLoop
                    SOAPElement eMem = (SOAPElement) itMember.next();
                    // loop over children nodes
                    String caption = null;
                    Iterator it = eMem.getChildElements();
                    InnerLoop: while (it.hasNext()) {
                        Node n = (Node) it.next();
                        if (!(n instanceof SOAPElement)) {
                            continue InnerLoop;
                        }
                        SOAPElement el = (SOAPElement) n;
                        String enam = el.getElementName().getLocalName();
                        if (enam.equals("Caption")) {
                            // $NON-NLS-1$
                            caption = el.getValue();
                        }
                    }
                    if (axisOrdinal == XMLABaseComponent.AXIS_COLUMNS) {
                        columnHeaders[index][positionOrdinal] = caption;
                    } else if (axisOrdinal == XMLABaseComponent.AXIS_ROWS) {
                        rowHeaders[positionOrdinal][index] = caption;
                    }
                    ++index;
                }
                // MemberLoop
                ++positionOrdinal;
            }
        // TupleLoop
        }
        // AxisLoop
        data = new Object[rowCount][columnCount];
        // loop over cells in result set
        // $NON-NLS-1$//$NON-NLS-2$
        name = envelope.createName("CellData", "", XMLABaseComponent.MDD_URI);
        SOAPElement eCellData = selectSingleNode(eRoot, name);
        // $NON-NLS-1$//$NON-NLS-2$
        name = envelope.createName("Cell", "", XMLABaseComponent.MDD_URI);
        Iterator itSoapCell = eCellData.getChildElements(name);
        while (itSoapCell.hasNext()) {
            // CellLoop
            SOAPElement eCell = (SOAPElement) itSoapCell.next();
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            name = envelope.createName("CellOrdinal", "", "");
            String cellOrdinal = eCell.getAttributeValue(name);
            int ordinal = Integer.parseInt(cellOrdinal);
            // $NON-NLS-1$//$NON-NLS-2$
            name = envelope.createName("Value", "", XMLABaseComponent.MDD_URI);
            Object value = selectSingleNode(eCell, name).getValue();
            int rowLoc = ordinal / columnCount;
            int columnLoc = ordinal % columnCount;
            data[rowLoc][columnLoc] = value;
        }
        // CellLoop
        MemoryResultSet resultSet = new MemoryResultSet();
        MemoryMetaData metaData = new MemoryMetaData(columnHeaders, rowHeaders);
        resultSet.setMetaData(metaData);
        for (Object[] element : data) {
            resultSet.addRow(element);
        }
        rSet = resultSet;
        if (resultSet != null) {
            if (getResultOutputName() != null) {
                setOutputValue(getResultOutputName(), resultSet);
            }
            return true;
        }
        return false;
    } catch (SOAPException se) {
        throw new XMLAException(se);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SOAPException e) {
                // log and ignore
                // $NON-NLS-1$
                error("?", e);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Node(javax.xml.soap.Node) SOAPConnection(javax.xml.soap.SOAPConnection) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) Name(javax.xml.soap.Name) MimeHeaders(javax.xml.soap.MimeHeaders) SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 44 with SOAPPart

use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.

the class XMLABaseComponent method findDiscoverRoot.

/**
 * locate "root" in DisoverResponse
 */
private SOAPElement findDiscoverRoot(final SOAPMessage reply) throws SOAPException, XMLAException {
    SOAPPart sp = reply.getSOAPPart();
    SOAPEnvelope envelope = sp.getEnvelope();
    SOAPBody body = envelope.getBody();
    Name childName;
    SOAPElement eResponse = null;
    if (provider == 0) {
        // unknown provider - recognize by prefix of DiscoverResponse
        Iterator itBody = body.getChildElements();
        while (itBody.hasNext()) {
            Node n = (Node) itBody.next();
            if (!(n instanceof SOAPElement)) {
                continue;
            }
            Name name = ((SOAPElement) n).getElementName();
            if (name.getLocalName().equals("DiscoverResponse")) {
                // $NON-NLS-1$
                eResponse = (SOAPElement) n;
                provider = getProviderFromDiscoverResponse(envelope, eResponse);
                break;
            }
        }
        if (eResponse == null) {
            throw new XMLAException(// $NON-NLS-1$
            Messages.getInstance().getString("XMLABaseComponent.ERROR_0013_NO_DISCOVER_RESPONSE"));
        }
    } else {
        if ((provider == XMLABaseComponent.PROVIDER_MICROSOFT) || (provider == XMLABaseComponent.PROVIDER_ESSBASE)) {
            // Microsoft
            // or
            // Essbase
            childName = // $NON-NLS-1$ //$NON-NLS-2$
            envelope.createName("DiscoverResponse", "m", XMLABaseComponent.XMLA_URI);
        } else if ((provider == XMLABaseComponent.PROVIDER_SAP) || (provider == XMLABaseComponent.PROVIDER_MONDRIAN)) {
            // SAP
            // or
            // Mondrian
            childName = // $NON-NLS-1$ //$NON-NLS-2$
            envelope.createName("DiscoverResponse", "", XMLABaseComponent.XMLA_URI);
        } else {
            throw new IllegalArgumentException(Messages.getInstance().getString(// $NON-NLS-1$
            "XMLABaseComponent.ERROR_0014_NO_PROVIDER_SPEC"));
        }
        eResponse = selectSingleNode(body, childName);
        if (eResponse == null) {
            throw new XMLAException(Messages.getInstance().getString(// $NON-NLS-1$
            "XMLABaseComponent.ERROR_0015_NO_DISCOVER_RESPONSE_ELEMENT"));
        }
    }
    SOAPElement eReturn = getDiscoverReturn(envelope, eResponse);
    if (eReturn == null) {
        throw new XMLAException(Messages.getInstance().getString(// $NON-NLS-1$
        "XMLABaseComponent.ERROR_0016_NO_RESULT_RETURN_ELEMENT"));
    }
    SOAPElement eRoot = getDiscoverRoot(envelope, eReturn);
    if (eRoot == null) {
        throw new XMLAException(// $NON-NLS-1$
        Messages.getInstance().getString("XMLABaseComponent.ERROR_0017_NO_RESULT_ROOT_ELEMENT"));
    }
    return eRoot;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) Node(javax.xml.soap.Node) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) Iterator(java.util.Iterator) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Name(javax.xml.soap.Name)

Example 45 with SOAPPart

use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.

the class XMLABaseComponent method findExecRoot.

/**
 * locate "root" in ExecuteResponse
 */
private SOAPElement findExecRoot(final SOAPMessage reply) throws SOAPException, XMLAException {
    SOAPPart sp = reply.getSOAPPart();
    SOAPEnvelope envelope = sp.getEnvelope();
    SOAPBody body = envelope.getBody();
    Name name;
    // $NON-NLS-1$//$NON-NLS-2$
    name = envelope.createName("ExecuteResponse", "m", XMLABaseComponent.XMLA_URI);
    SOAPElement eResponse = selectSingleNode(body, name);
    if (eResponse == null) {
        throw new XMLAException(Messages.getInstance().getString(// $NON-NLS-1$
        "XMLABaseComponent.ERROR_0011_NO_EXECUTE_RESPONSE_ELEMENT"));
    }
    // $NON-NLS-1$//$NON-NLS-2$
    name = envelope.createName("return", "m", XMLABaseComponent.XMLA_URI);
    SOAPElement eReturn = selectSingleNode(eResponse, name);
    // $NON-NLS-1$//$NON-NLS-2$
    name = envelope.createName("root", "", XMLABaseComponent.MDD_URI);
    SOAPElement eRoot = selectSingleNode(eReturn, name);
    if (eRoot == null) {
        throw new XMLAException(Messages.getInstance().getString("XMLABaseComponent.ERROR_0012_NO_RESPONSE_ROOT_ELEMENT"));
    }
    // $NON-NLS-1$
    return eRoot;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Name(javax.xml.soap.Name)

Aggregations

SOAPPart (javax.xml.soap.SOAPPart)45 SOAPMessage (javax.xml.soap.SOAPMessage)30 SOAPBody (javax.xml.soap.SOAPBody)28 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)26 SOAPException (javax.xml.soap.SOAPException)26 MessageFactory (javax.xml.soap.MessageFactory)17 SOAPElement (javax.xml.soap.SOAPElement)14 Iterator (java.util.Iterator)13 Element (org.w3c.dom.Element)13 StreamSource (javax.xml.transform.stream.StreamSource)12 QName (javax.xml.namespace.QName)11 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 Name (javax.xml.soap.Name)10 NodeList (org.w3c.dom.NodeList)9 BufferedWriter (java.io.BufferedWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 SOAPHeader (javax.xml.soap.SOAPHeader)6