use of org.apache.axis2.dispatchers.RequestURIBasedDispatcher in project wso2-synapse by wso2.
the class NHttpTransportListenerTest method testRequestAndResponse.
/**
* Test the Source Handler respond to client.
* Send a message to http listener and get the same request message as a response using PassThroughHttpSender
*/
@Test
public void testRequestAndResponse() throws Exception {
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);
ServiceContext svcCtx = new ServiceContext();
OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
axis2MessageContext.setServiceContext(svcCtx);
axis2MessageContext.setOperationContext(opCtx);
axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
axis2MessageContext.setTo(null);
axis2MessageContext.setProperty("synapse.isresponse", true);
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</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);
}
use of org.apache.axis2.dispatchers.RequestURIBasedDispatcher in project wso2-synapse by wso2.
the class ServerWorker method handleRESTUrlPost.
/**
* Method will setup the necessary parameters for the rest url post action
*
* @param
* @return
* @throws FactoryConfigurationError
*/
public SOAPEnvelope handleRESTUrlPost(String contentTypeHdr) throws FactoryConfigurationError {
SOAPEnvelope soapEnvelope = null;
String contentType = contentTypeHdr != null ? TransportUtils.getContentType(contentTypeHdr, msgContext) : null;
// recipient should consider it as application/octet-stream (rfc2616)
if (contentType == null || contentType.isEmpty()) {
contentType = PassThroughConstants.APPLICATION_OCTET_STREAM;
// Temp fix for https://github.com/wso2/product-ei/issues/2001
if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(HTTP_METHOD)) || "DELETE".equals(msgContext.getProperty(HTTP_METHOD))) {
contentType = HTTPConstants.MEDIA_TYPE_X_WWW_FORM;
}
}
if (HTTPConstants.MEDIA_TYPE_X_WWW_FORM.equals(contentType) || (PassThroughConstants.APPLICATION_OCTET_STREAM.equals(contentType) && contentTypeHdr == null)) {
msgContext.setTo(new EndpointReference(request.getRequest().getRequestLine().getUri()));
String charSetEncoding;
if (contentTypeHdr != null) {
msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentTypeHdr);
charSetEncoding = BuilderUtil.getCharSetEncoding(contentTypeHdr);
} else {
msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
}
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEncoding);
try {
RESTUtil.dispatchAndVerify(msgContext);
} catch (AxisFault e1) {
log.error("Error while building message for REST_URL request", e1);
}
try {
/**
* This reverseProxyMode was introduce to avoid the LB exposing
* it's own web service when REST call was initiated
*/
boolean reverseProxyMode = PassThroughConfiguration.getInstance().isReverseProxyMode();
AxisService axisService = null;
if (!reverseProxyMode) {
RequestURIBasedDispatcher requestDispatcher = new RequestURIBasedDispatcher();
axisService = requestDispatcher.findService(msgContext);
}
// the logic determines which service dispatcher to get invoke,
// this will be determine
// based on parameter defines at disableRestServiceDispatching,
// and if super tenant invoke, with isTenantRequest
// identifies whether the request to be dispatch to custom REST
// Dispatcher Service.
boolean isCustomRESTDispatcher = false;
String requestURI = request.getRequest().getRequestLine().getUri();
if (requestURI.matches(PassThroughConfiguration.getInstance().getRestUriApiRegex()) || requestURI.matches(PassThroughConfiguration.getInstance().getRestUriProxyRegex())) {
isCustomRESTDispatcher = true;
}
if (!isCustomRESTDispatcher) {
if (axisService == null) {
String defaultSvcName = PassThroughConfiguration.getInstance().getPassThroughDefaultServiceName();
axisService = msgContext.getConfigurationContext().getAxisConfiguration().getService(defaultSvcName);
msgContext.setAxisService(axisService);
}
} else {
String multiTenantDispatchService = PassThroughConfiguration.getInstance().getRESTDispatchService();
axisService = msgContext.getConfigurationContext().getAxisConfiguration().getService(multiTenantDispatchService);
msgContext.setAxisService(axisService);
}
} catch (AxisFault e) {
handleException("Error processing " + request.getMethod() + " request for : " + request.getUri(), e);
}
try {
soapEnvelope = TransportUtils.createSOAPMessage(msgContext, null, contentType);
} catch (Exception e) {
log.error("Error while building message for REST_URL request");
}
// msgContext.setProperty(Constants.Configuration.CONTENT_TYPE,"application/xml");
msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
}
return soapEnvelope;
}
use of org.apache.axis2.dispatchers.RequestURIBasedDispatcher 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