Search in sources :

Example 51 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project wso2-axis2-transports by wso2.

the class XMLMessage method getMessageElement.

public OMElement getMessageElement() {
    if (type == Type.POX) {
        return payload;
    } else {
        SOAPFactory factory;
        if (type == Type.SOAP11) {
            factory = OMAbstractFactory.getSOAP11Factory();
        } else {
            factory = OMAbstractFactory.getSOAP12Factory();
        }
        SOAPEnvelope envelope = factory.getDefaultEnvelope();
        envelope.getBody().addChild(payload);
        return envelope;
    }
}
Also used : SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 52 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project wso2-axis2-transports by wso2.

the class DefaultSMSMessageBuilderImpl method createSoapEnvelope.

private SOAPEnvelope createSoapEnvelope(MessageContext messageContext, Map params) {
    SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope inEnvlope = soapFactory.getDefaultEnvelope();
    SOAPBody inBody = inEnvlope.getBody();
    XmlSchemaElement xmlSchemaElement;
    AxisOperation axisOperation = messageContext.getAxisOperation();
    if (axisOperation != null) {
        AxisMessage axisMessage = axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
        xmlSchemaElement = axisMessage.getSchemaElement();
        if (xmlSchemaElement == null) {
            OMElement bodyFirstChild = soapFactory.createOMElement(messageContext.getAxisOperation().getName(), inBody);
            createSOAPMessageWithoutSchema(soapFactory, bodyFirstChild, params);
        } else {
            // first get the target namespace from the schema and the wrapping element.
            // create an OMElement out of those information. We are going to extract parameters from
            // url, create OMElements and add them as children to this wrapping element.
            String targetNamespace = xmlSchemaElement.getQName().getNamespaceURI();
            QName bodyFirstChildQName;
            if (targetNamespace != null && !"".equals(targetNamespace)) {
                bodyFirstChildQName = new QName(targetNamespace, xmlSchemaElement.getName());
            } else {
                bodyFirstChildQName = new QName(xmlSchemaElement.getName());
            }
            OMElement bodyFirstChild = soapFactory.createOMElement(bodyFirstChildQName, inBody);
            // Schema should adhere to the IRI style in this. So assume IRI style and dive in to
            // schema
            XmlSchemaType schemaType = xmlSchemaElement.getSchemaType();
            if (schemaType instanceof XmlSchemaComplexType) {
                XmlSchemaComplexType complexType = ((XmlSchemaComplexType) schemaType);
                XmlSchemaParticle particle = complexType.getParticle();
                if (particle instanceof XmlSchemaSequence || particle instanceof XmlSchemaAll) {
                    XmlSchemaGroupBase xmlSchemaGroupBase = (XmlSchemaGroupBase) particle;
                    Iterator iterator = xmlSchemaGroupBase.getItems().getIterator();
                    while (iterator.hasNext()) {
                        XmlSchemaElement innerElement = (XmlSchemaElement) iterator.next();
                        QName qName = innerElement.getQName();
                        if (qName == null && innerElement.getSchemaTypeName().equals(org.apache.ws.commons.schema.constants.Constants.XSD_ANYTYPE)) {
                            createSOAPMessageWithoutSchema(soapFactory, bodyFirstChild, params);
                            break;
                        }
                        long minOccurs = innerElement.getMinOccurs();
                        boolean nillable = innerElement.isNillable();
                        String name = qName != null ? qName.getLocalPart() : innerElement.getName();
                        Object value;
                        OMNamespace ns = (qName == null || qName.getNamespaceURI() == null || qName.getNamespaceURI().length() == 0) ? null : soapFactory.createOMNamespace(qName.getNamespaceURI(), null);
                        // FIXME changed
                        if ((value = params.get(name)) != null) {
                            addRequestParameter(soapFactory, bodyFirstChild, ns, name, value);
                            minOccurs--;
                        }
                        if (minOccurs > 0) {
                            if (nillable) {
                                OMNamespace xsi = soapFactory.createOMNamespace(Constants.URI_DEFAULT_SCHEMA_XSI, Constants.NS_PREFIX_SCHEMA_XSI);
                                OMAttribute omAttribute = soapFactory.createOMAttribute("nil", xsi, "true");
                                soapFactory.createOMElement(name, ns, bodyFirstChild).addAttribute(omAttribute);
                            } else {
                            // throw new AxisFault("Required element " + qName +
                            // " defined in the schema can not be" +
                            // " found in the request");
                            }
                        }
                    }
                }
            }
        }
    }
    return inEnvlope;
}
Also used : QName(javax.xml.namespace.QName) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) SOAPBody(org.apache.axiom.soap.SOAPBody) Iterator(java.util.Iterator)

Example 53 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project webservices-axiom by apache.

the class OMXMLBuilderFactory method createSOAPModelBuilder.

/**
 * Create an MTOM aware model builder from the provided {@link MultipartBody} object using a
 * particular Axiom implementation. The method will determine the SOAP version based on the
 * content type information from the {@link MultipartBody} object. It will configure the
 * underlying parser as specified by {@link StAXParserConfiguration#SOAP}.
 *
 * @param metaFactory
 *            the meta factory for the Axiom implementation to use
 * @param message
 *            the MIME message
 * @return the builder
 * @throws OMException
 *             if an error occurs while processing the content type information from the
 *             {@link MultipartBody} object
 */
public static SOAPModelBuilder createSOAPModelBuilder(OMMetaFactory metaFactory, MultipartBody message) {
    String type = message.getRootPart().getContentType().getParameter("type");
    SOAPFactory soapFactory;
    if ("text/xml".equalsIgnoreCase(type)) {
        soapFactory = metaFactory.getSOAP11Factory();
    } else if ("application/soap+xml".equalsIgnoreCase(type)) {
        soapFactory = metaFactory.getSOAP12Factory();
    } else {
        throw new OMException("Unable to determine SOAP version");
    }
    SOAPModelBuilder builder = ((OMMetaFactorySPI) metaFactory).createSOAPModelBuilder(message);
    if (builder.getSOAPMessage().getOMFactory() != soapFactory) {
        throw new SOAPProcessingException("Invalid SOAP namespace URI. " + "Expected " + soapFactory.getSoapVersionURI(), SOAP12Constants.FAULT_CODE_SENDER);
    }
    return builder;
}
Also used : SOAPProcessingException(org.apache.axiom.soap.SOAPProcessingException) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 54 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.

the class IterateWithMultipleElementsTestCase method createRequestPayload.

private OMElement createRequestPayload() {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    OMNamespace omNs = fac.createOMNamespace("http://services.samples", "ns");
    OMElement payload = fac.createOMElement("getQuotes", omNs);
    for (int i = 0; i < 3; i++) {
        OMElement method = fac.createOMElement("getQuote", omNs);
        OMElement request = fac.createOMElement("request", omNs);
        OMElement symbol = fac.createOMElement("symbol", omNs);
        symbol.addChild(fac.createOMText(request, "WSO2"));
        request.addChild(symbol);
        method.addChild(request);
        payload.addChild(method);
    }
    for (int i = 0; i < 3; i++) {
        OMElement method = fac.createOMElement("dummy", omNs);
        OMElement request = fac.createOMElement("request", omNs);
        OMElement symbol = fac.createOMElement("symbol", omNs);
        symbol.addChild(fac.createOMText(request, "WSO2"));
        request.addChild(symbol);
        method.addChild(request);
        payload.addChild(method);
    }
    return payload;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 55 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.

the class MTOMSwASampleClient method sendUsingSWA.

public SampleClientResult sendUsingSWA(String fileName, String targetEPR) {
    clientResult = new SampleClientResult();
    try {
        Options options = new Options();
        options.setTo(new EndpointReference(targetEPR));
        options.setAction("urn:uploadFileUsingSwA");
        options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
        ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(configuration.getClientRepo(), configuration.getAxis2Xml());
        ServiceClient sender = new ServiceClient(configContext, null);
        sender.setOptions(options);
        OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);
        MessageContext mc = new MessageContext();
        log.info("Sending file : " + fileName + " as SwA");
        FileDataSource fileDataSource = new FileDataSource(new File(fileName));
        DataHandler dataHandler = new DataHandler(fileDataSource);
        String attachmentID = mc.addAttachment(dataHandler);
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope env = factory.getDefaultEnvelope();
        OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
        OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns);
        OMElement request = factory.createOMElement("request", ns);
        OMElement imageId = factory.createOMElement("imageId", ns);
        imageId.setText(attachmentID);
        request.addChild(imageId);
        payload.addChild(request);
        env.getBody().addChild(payload);
        mc.setEnvelope(env);
        mepClient.addMessageContext(mc);
        mepClient.execute(true);
        MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
        SOAPBody body = response.getEnvelope().getBody();
        String imageContentId = body.getFirstChildWithName(new QName("http://services.samples", "uploadFileUsingSwAResponse")).getFirstChildWithName(new QName("http://services.samples", "response")).getFirstChildWithName(new QName("http://services.samples", "imageId")).getText();
        Attachments attachment = response.getAttachmentMap();
        dataHandler = attachment.getDataHandler(imageContentId);
        File tempFile = File.createTempFile("swa-", ".gif");
        FileOutputStream fos = new FileOutputStream(tempFile);
        dataHandler.writeTo(fos);
        fos.flush();
        fos.close();
        log.info("Saved response to file : " + tempFile.getAbsolutePath());
        clientResult.incrementResponseCount();
    } catch (Exception e) {
        log.error("Error invoking service", e);
        clientResult.setException(e);
    }
    return clientResult;
}
Also used : Options(org.apache.axis2.client.Options) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) OperationClient(org.apache.axis2.client.OperationClient) QName(javax.xml.namespace.QName) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Attachments(org.apache.axiom.attachments.Attachments) SOAPFactory(org.apache.axiom.soap.SOAPFactory) EndpointReference(org.apache.axis2.addressing.EndpointReference) SOAPBody(org.apache.axiom.soap.SOAPBody) ServiceClient(org.apache.axis2.client.ServiceClient) SampleClientResult(org.apache.synapse.samples.framework.SampleClientResult) FileOutputStream(java.io.FileOutputStream) FileDataSource(javax.activation.FileDataSource) MessageContext(org.apache.axis2.context.MessageContext) File(java.io.File)

Aggregations

SOAPFactory (org.apache.axiom.soap.SOAPFactory)70 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)50 OMElement (org.apache.axiom.om.OMElement)38 QName (javax.xml.namespace.QName)16 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)14 SOAPBody (org.apache.axiom.soap.SOAPBody)12 AxisFault (org.apache.axis2.AxisFault)11 MessageContext (org.apache.axis2.context.MessageContext)11 DataHandler (javax.activation.DataHandler)10 OMNamespace (org.apache.axiom.om.OMNamespace)9 SOAPHeader (org.apache.axiom.soap.SOAPHeader)9 SynapseException (org.apache.synapse.SynapseException)9 Iterator (java.util.Iterator)8 OMNode (org.apache.axiom.om.OMNode)7 SOAPFault (org.apache.axiom.soap.SOAPFault)7 IOException (java.io.IOException)6 EndpointReference (org.apache.axis2.addressing.EndpointReference)6 ArrayList (java.util.ArrayList)5 XMLStreamException (javax.xml.stream.XMLStreamException)4 OMAttribute (org.apache.axiom.om.OMAttribute)4