Search in sources :

Example 31 with SOAPFactory

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

the class SOAPEventHandler method sendError.

private void sendError(HttpServletResponse res, Object object, String serviceName) throws EventHandlerException {
    try {
        // setup the response
        res.setContentType("text/xml");
        String xmlResults = SoapSerializer.serialize(object);
        XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
        StAXOMBuilder resultsBuilder = (StAXOMBuilder) OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), xmlReader);
        OMElement resultSer = resultsBuilder.getDocumentElement();
        // create the response soap
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope resEnv = factory.createSOAPEnvelope();
        SOAPBody resBody = factory.createSOAPBody();
        OMElement errMsg = factory.createOMElement(new QName((serviceName != null ? serviceName : "") + "Response"));
        errMsg.addChild(resultSer.getFirstElement());
        resBody.addChild(errMsg);
        resEnv.addChild(resBody);
        // The declareDefaultNamespace method doesn't work see (https://issues.apache.org/jira/browse/AXIS2-3156)
        // so the following doesn't work:
        // resService.declareDefaultNamespace(ModelService.TNS);
        // instead, create the xmlns attribute directly:
        OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
        errMsg.addAttribute(defaultNS);
        // log the response message
        if (Debug.verboseOn()) {
            try {
                Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
            } catch (Throwable t) {
            }
        }
        resEnv.serialize(res.getOutputStream());
        res.getOutputStream().flush();
    } catch (Exception e) {
        throw new EventHandlerException(e.getMessage(), e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) SOAPBody(org.apache.axiom.soap.SOAPBody) StringReader(java.io.StringReader) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 32 with SOAPFactory

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

the class TestDataHandlerSerializationWithoutMTOM method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPFactory factory = metaFactory.getSOAP11Factory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    // Construct the original message
    DocumentBean orgObject = new DocumentBean();
    orgObject.setId("123456");
    orgObject.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
    SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
    OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, orgObject));
    orgEnvelope.getBody().addChild(element);
    // Serialize the message
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    orgEnvelope.serialize(out);
    assertFalse(element.isExpanded());
    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, new ByteArrayInputStream(out.toByteArray()), null).getSOAPEnvelope();
    DocumentBean object = (DocumentBean) context.createUnmarshaller().unmarshal(envelope.getBody().getFirstElement().getXMLStreamReader(false));
    assertEquals("some content", IOUtils.toString(object.getContent().getInputStream(), "utf-8"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) JAXBContext(javax.xml.bind.JAXBContext) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)

Example 33 with SOAPFactory

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

the class DataProcessAndPublishingAgentTest method testContentAwareTierPresentAndContentLengthNotPresent.

@Test
public void testContentAwareTierPresentAndContentLengthNotPresent() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper(throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setIsContentAware(true);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    env.getBody().addChild(fac.createOMElement("test", "http://t", "t"));
    org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
    Mockito.when(messageContext.getEnvelope()).thenReturn(env);
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
    TreeMap headers = new TreeMap();
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
    VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setContentAware(false);
    ArrayList<VerbInfoDTO> list = new ArrayList<VerbInfoDTO>();
    list.add(verbInfoDTO);
    API api = new API();
    api.setUuid(UUID.randomUUID().toString());
    api.setApiName(apiName);
    api.setApiVersion(apiVersion);
    api.setApiProvider("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_OBJECT)).thenReturn(api);
    Mockito.when(messageContext.getProperty(APIConstants.VERB_INFO_DTO)).thenReturn(list);
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier, apiLevelThrottleKey, null, subscriptionLevelThrottleKey, subscriptionLevelTier, resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant, apiTenant, appId, messageContext, authenticationContext);
    dataProcessAndPublishingAgent.run();
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ArrayList(java.util.ArrayList) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) TreeMap(java.util.TreeMap) SOAPFactory(org.apache.axiom.soap.SOAPFactory) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 34 with SOAPFactory

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

the class TestSchemaValidator method setMockedRequest.

private void setMockedRequest(String httpMethod, String resourcePath, String xmlMessage) throws XMLStreamException, IOException {
    SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    OMElement messageStore = AXIOMUtil.stringToOM(xmlMessage);
    env.getBody().addChild(messageStore);
    log.info(" Running the test case to validate the request content against the defined schemas.");
    String contentType = "application/json";
    String ApiId = "admin-SwaggerPetstore-1.0.0";
    File swaggerJsonFile = new File(Thread.currentThread().getContextClassLoader().getResource("swaggerEntry/swagger.json").getFile());
    String swaggerValue = FileUtils.readFileToString(swaggerJsonFile);
    Mockito.doReturn(env).when(messageContext).getEnvelope();
    // Mockito.when()
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgContext);
    Mockito.when((String) axis2MsgContext.getProperty(APIMgtGatewayConstants.REST_CONTENT_TYPE)).thenReturn(contentType);
    Mockito.when((String) axis2MsgContext.getProperty(APIMgtGatewayConstants.HTTP_REQUEST_METHOD)).thenReturn(httpMethod);
    Mockito.when(messageContext.getConfiguration()).thenReturn(synapseConfiguration);
    Mockito.when(synapseConfiguration.getLocalRegistry()).thenReturn(map);
    Mockito.when(messageContext.getConfiguration()).thenReturn(synapseConfiguration);
    Mockito.when((String) messageContext.getProperty((APIMgtGatewayConstants.API_ELECTED_RESOURCE))).thenReturn(resourcePath);
    Mockito.when((String) messageContext.getProperty(RESTConstants.REST_SUB_REQUEST_PATH)).thenReturn(resourcePath);
    Mockito.when(synapseConfiguration.getLocalRegistry()).thenReturn(map);
    Mockito.when(map.get(ApiId)).thenReturn(entry);
    Mockito.when((String) entry.getValue()).thenReturn(swaggerValue);
    Mockito.when((String) messageContext.getProperty(APIMgtGatewayConstants.ELECTED_REQUEST_METHOD)).thenReturn(httpMethod);
    Mockito.when((String) axis2MsgContext.getProperty(APIMgtGatewayConstants.HTTP_REQUEST_METHOD)).thenReturn(httpMethod);
    Mockito.when((String) messageContext.getProperty(APIMgtGatewayConstants.OPEN_API_STRING)).thenReturn(swaggerValue);
    Map<String, String> headers = new HashMap<>();
    headers.put(CONTENT_TYPE_HEADER, contentType);
    Mockito.when(axis2MsgContext.getProperty(TRANSPORT_HEADERS)).thenReturn(headers);
    Mockito.when(messageContext.getProperty(RESOURCE_TAG)).thenReturn(resourcePath);
}
Also used : HashMap(java.util.HashMap) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) File(java.io.File) SOAPFactory(org.apache.axiom.soap.SOAPFactory) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 35 with SOAPFactory

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

the class RegularExpressionProtectorTest method testIsContentAware.

/**
 * This is the test case to check the return value of the isContentAware method.
 */
@Test
public void testIsContentAware() {
    log.info("Running the test case to check the return status of the isContentAware method.");
    SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope env = fac.createSOAPEnvelope();
    fac.createSOAPBody(env);
    env.getBody().addChild(fac.createOMElement("test", "Content aware", "MessageBody"));
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgContext);
    Mockito.doReturn(env).when(axis2MsgContext).getEnvelope();
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.ENABLED_CHECK_BODY)).thenReturn(String.valueOf(enabledStatus));
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.ENABLED_CHECK_HEADERS)).thenReturn(String.valueOf("false"));
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.ENABLED_CHECK_PATHPARAM)).thenReturn(String.valueOf("false"));
    regularExpressionProtector = new RegularExpressionProtector();
    regularExpressionProtector.mediate(messageContext);
    String enabledBuild = String.valueOf(regularExpressionProtector.isContentAware());
    Assert.assertEquals(enabledStatus, enabledBuild);
    log.info("Successfully completed testIsContentAware test case.");
}
Also used : SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Aggregations

SOAPFactory (org.apache.axiom.soap.SOAPFactory)69 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)49 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 MessageContext (org.apache.axis2.context.MessageContext)11 DataHandler (javax.activation.DataHandler)10 AxisFault (org.apache.axis2.AxisFault)10 OMNamespace (org.apache.axiom.om.OMNamespace)9 SOAPHeader (org.apache.axiom.soap.SOAPHeader)9 SynapseException (org.apache.synapse.SynapseException)8 Iterator (java.util.Iterator)7 OMNode (org.apache.axiom.om.OMNode)7 SOAPFault (org.apache.axiom.soap.SOAPFault)7 EndpointReference (org.apache.axis2.addressing.EndpointReference)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 OMAttribute (org.apache.axiom.om.OMAttribute)4