use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.
the class MessageConverter method toMessageContext.
/**
* Converts a message read from the message store to a Synapse Message Context object.
* @param message Message from the message store
* @param axis2Ctx Final Axis2 Message Context
* @param synCtx Final Synapse message Context
* @return Final Synapse Message Context
*/
public static MessageContext toMessageContext(StorableMessage message, org.apache.axis2.context.MessageContext axis2Ctx, MessageContext synCtx) {
if (message == null) {
logger.error("Cannot create Message Context. Message is null.");
return null;
}
AxisConfiguration axisConfig = axis2Ctx.getConfigurationContext().getAxisConfiguration();
if (axisConfig == null) {
logger.warn("Cannot create AxisConfiguration. AxisConfiguration is null.");
return null;
}
Axis2Message axis2Msg = message.getAxis2message();
try {
SOAPEnvelope envelope = getSoapEnvelope(axis2Msg.getSoapEnvelope());
axis2Ctx.setEnvelope(envelope);
// set the RMSMessageDto properties
axis2Ctx.getOptions().setAction(axis2Msg.getAction());
if (axis2Msg.getRelatesToMessageId() != null) {
axis2Ctx.addRelatesTo(new RelatesTo(axis2Msg.getRelatesToMessageId()));
}
axis2Ctx.setMessageID(axis2Msg.getMessageID());
axis2Ctx.getOptions().setAction(axis2Msg.getAction());
axis2Ctx.setDoingREST(axis2Msg.isDoingPOX());
axis2Ctx.setDoingMTOM(axis2Msg.isDoingMTOM());
axis2Ctx.setDoingSwA(axis2Msg.isDoingSWA());
AxisService axisService;
if (axis2Msg.getService() != null && (axisService = axisConfig.getServiceForActivation(axis2Msg.getService())) != null) {
AxisOperation axisOperation = axisService.getOperation(axis2Msg.getOperationName());
axis2Ctx.setFLOW(axis2Msg.getFLOW());
ArrayList executionChain = new ArrayList();
if (axis2Msg.getFLOW() == org.apache.axis2.context.MessageContext.OUT_FLOW) {
executionChain.addAll(axisOperation.getPhasesOutFlow());
executionChain.addAll(axisConfig.getOutFlowPhases());
} else if (axis2Msg.getFLOW() == org.apache.axis2.context.MessageContext.OUT_FAULT_FLOW) {
executionChain.addAll(axisOperation.getPhasesOutFaultFlow());
executionChain.addAll(axisConfig.getOutFlowPhases());
}
axis2Ctx.setExecutionChain(executionChain);
ConfigurationContext configurationContext = axis2Ctx.getConfigurationContext();
axis2Ctx.setAxisService(axisService);
ServiceGroupContext serviceGroupContext = configurationContext.createServiceGroupContext(axisService.getAxisServiceGroup());
ServiceContext serviceContext = serviceGroupContext.getServiceContext(axisService);
OperationContext operationContext = serviceContext.createOperationContext(axis2Msg.getOperationName());
axis2Ctx.setServiceContext(serviceContext);
axis2Ctx.setOperationContext(operationContext);
axis2Ctx.setAxisService(axisService);
axis2Ctx.setAxisOperation(axisOperation);
} else if (axis2Ctx.getOperationContext() == null) {
axis2Ctx.setOperationContext(new OperationContext(new InOutAxisOperation(), new ServiceContext()));
}
if (axis2Msg.getReplyToAddress() != null) {
axis2Ctx.setReplyTo(new EndpointReference(axis2Msg.getReplyToAddress().trim()));
}
if (axis2Msg.getFaultToAddress() != null) {
axis2Ctx.setFaultTo(new EndpointReference(axis2Msg.getFaultToAddress().trim()));
}
if (axis2Msg.getFromAddress() != null) {
axis2Ctx.setFrom(new EndpointReference(axis2Msg.getFromAddress().trim()));
}
if (axis2Msg.getToAddress() != null) {
axis2Ctx.getOptions().setTo(new EndpointReference(axis2Msg.getToAddress().trim()));
}
Object map = axis2Msg.getProperties().get(ABSTRACT_MC_PROPERTIES);
axis2Msg.getProperties().remove(ABSTRACT_MC_PROPERTIES);
axis2Ctx.setProperties(axis2Msg.getProperties());
axis2Ctx.setTransportIn(axisConfig.getTransportIn(axis2Msg.getTransportInName()));
axis2Ctx.setTransportOut(axisConfig.getTransportOut(axis2Msg.getTransportOutName()));
Object headers = axis2Msg.getProperties().get(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers instanceof Map) {
setTransportHeaders(axis2Ctx, (Map) headers);
}
if (map instanceof Map) {
Map<String, Object> abstractMCProperties = (Map) map;
Iterator<String> properties = abstractMCProperties.keySet().iterator();
while (properties.hasNext()) {
String property = properties.next();
Object value = abstractMCProperties.get(property);
axis2Ctx.setProperty(property, value);
}
}
// That is to get the existing headers into the new envelope.
if (axis2Msg.getJsonStream() != null) {
JsonUtil.getNewJsonPayload(axis2Ctx, new ByteArrayInputStream(axis2Msg.getJsonStream()), true, true);
}
SynapseMessage synMsg = message.getSynapseMessage();
synCtx.setTracingState(synMsg.getTracingState());
synCtx.setMessageFlowTracingState(synMsg.getMessageFlowTracingState());
Iterator<String> properties = synMsg.getProperties().keySet().iterator();
while (properties.hasNext()) {
String key = properties.next();
Object value = synMsg.getProperties().get(key);
synCtx.setProperty(key, value);
}
Iterator<String> propertyObjects = synMsg.getPropertyObjects().keySet().iterator();
while (propertyObjects.hasNext()) {
String key = propertyObjects.next();
Object value = synMsg.getPropertyObjects().get(key);
if (key.startsWith(OM_ELEMENT_PREFIX)) {
String originalKey = key.substring(OM_ELEMENT_PREFIX.length(), key.length());
ByteArrayInputStream is = new ByteArrayInputStream((byte[]) value);
StAXOMBuilder builder = new StAXOMBuilder(is);
OMElement omElement = builder.getDocumentElement();
synCtx.setProperty(originalKey, omElement);
}
}
synCtx.setFaultResponse(synMsg.isFaultResponse());
synCtx.setResponse(synMsg.isResponse());
return synCtx;
} catch (Exception e) {
logger.error("Cannot create Message Context. Error:" + e.getLocalizedMessage(), e);
return null;
}
}
use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.
the class RecipientListEndpointTest method createMessageContext.
/**
* Create a empty message context
*
* @return A context with empty message
* @throws AxisFault on an error creating a context
*/
private MessageContext createMessageContext() throws AxisFault {
Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
ServiceContext svcCtx = new ServiceContext();
OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
axis2MC.setServiceContext(svcCtx);
axis2MC.setOperationContext(opCtx);
MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
mc.setMessageID(UIDGenerator.generateURNString());
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
return mc;
}
use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.
the class ProxyServiceMessageReceiverTest method createMessageContext.
/**
* Create a empty message context.
*
* @return A context with empty message
* @throws AxisFault on an error creating a context
*/
private MessageContext createMessageContext() throws AxisFault {
Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
ServiceContext svcCtx = new ServiceContext();
OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
axis2MC.setServiceContext(svcCtx);
axis2MC.setOperationContext(opCtx);
MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
mc.setMessageID(UIDGenerator.generateURNString());
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
return mc;
}
use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.
the class HttpEndpointTest method createMessageContext.
/**
* Create a empty message context
*
* @return A context with empty message
* @throws AxisFault on an error creating a context
*/
private MessageContext createMessageContext() throws AxisFault {
Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
ServiceContext svcCtx = new ServiceContext();
OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
axis2MC.setServiceContext(svcCtx);
axis2MC.setOperationContext(opCtx);
axis2MC.setTransportIn(new TransportInDescription("http"));
// axis2MC.setTo(new EndpointReference("http://localhost:9000/services/SimpleStockQuoteService"));
MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
mc.setMessageID(UIDGenerator.generateURNString());
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
return mc;
}
use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.
the class NHttpTransportListenerTest method testBackendResponse.
/**
* Test the Source Handler respond to client.
* Send a message to http listener and get the response from backend server.
*/
@Test
public void testBackendResponse() throws Exception {
final SimpleHttpServer helloServer = new SimpleHttpServer();
try {
helloServer.start();
RequestURIBasedDispatcher requestURIBasedDispatcher = Mockito.mock(RequestURIBasedDispatcher.class);
Mockito.when(requestURIBasedDispatcher.findService(any(MessageContext.class))).thenReturn(new AxisService("myservice"));
PowerMockito.whenNew(RequestURIBasedDispatcher.class).withNoArguments().thenReturn(requestURIBasedDispatcher);
PowerMockito.mockStatic(AxisEngine.class);
PowerMockito.doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) throws Exception {
MessageContext axis2MessageContext = invocation.getArgument(0);
if (axis2MessageContext.getServiceContext() == null) {
// request path
ServiceContext svcCtx = new ServiceContext();
OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
axis2MessageContext.setServiceContext(svcCtx);
axis2MessageContext.setOperationContext(opCtx);
axis2MessageContext.setProperty("TransportURL", helloServer.getServerUrl());
inMsgContext = axis2MessageContext;
} else {
// response path
axis2MessageContext.setTo(null);
axis2MessageContext.setProperty("synapse.isresponse", true);
axis2MessageContext.removeProperty("TransportURL");
axis2MessageContext.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, inMsgContext.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
}
HttpCoreNIOSender sender = new HttpCoreNIOSender();
ConfigurationContext cfgCtx = new ConfigurationContext(new AxisConfiguration());
sender.init(cfgCtx, new TransportOutDescription("http"));
sender.invoke(axis2MessageContext);
return null;
}
}).when(AxisEngine.class, "receive", any(MessageContext.class));
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(ServiceUtils.getServiceEndpoint("myservice", HOST, PORT));
method.setRequestHeader("Content-Type", "application/xml");
StringRequestEntity stringRequestEntity = new StringRequestEntity("<msg>hello server</msg>", "application/xml", "UTF-8");
method.setRequestEntity(stringRequestEntity);
int responseCode = client.executeMethod(method);
Assert.assertEquals("Response code mismatched", 200, responseCode);
String response = method.getResponseBodyAsString();
Assert.assertEquals("Response", "<msg>hello</msg>", response);
} finally {
helloServer.stop();
}
}
Aggregations