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);
}
}
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"));
}
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();
}
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);
}
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.");
}
Aggregations