use of org.apache.synapse.api.version.URLBasedVersionStrategy in project wso2-synapse by wso2.
the class API method process.
public void process(MessageContext synCtx) {
auditDebug("Processing message with ID: " + synCtx.getMessageID() + " through the " + "API: " + name);
synCtx.setProperty(RESTConstants.PROCESSED_API, this);
synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, getName());
synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION, versionStrategy.getVersion());
synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context);
synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY, versionStrategy.getVersionType());
synCtx.setProperty(SynapseConstants.ARTIFACT_NAME, SynapseConstants.FAIL_SAFE_MODE_API + getName());
// get API log for this message and attach to the message context
((Axis2MessageContext) synCtx).setServiceLog(apiLog);
// Calculate REST_URL_POSTFIX from full request path
String restURLPostfix = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH);
if (!synCtx.isResponse() && restURLPostfix != null) {
// Skip for response path
if (!restURLPostfix.startsWith("/")) {
restURLPostfix = "/" + restURLPostfix;
}
if (restURLPostfix.startsWith(context)) {
restURLPostfix = restURLPostfix.substring(context.length());
}
if (versionStrategy instanceof URLBasedVersionStrategy) {
String version = versionStrategy.getVersion();
if (restURLPostfix.startsWith(version)) {
restURLPostfix = restURLPostfix.substring(version.length());
} else if (restURLPostfix.startsWith("/" + version)) {
restURLPostfix = restURLPostfix.substring(version.length() + 1);
}
}
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(NhttpConstants.REST_URL_POSTFIX, restURLPostfix);
}
if (synCtx.isResponse()) {
org.apache.axis2.context.MessageContext context = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
context.setProperty(NhttpConstants.ACTIVITY_ID_STATUS, true);
Map headers = (Map) context.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
String sysRestrictedHeaders = System.getProperty(CorrelationConstants.RESTRICT_HEADERS_SYS_PROP);
List<String> headerList;
if (sysRestrictedHeaders != null) {
headerList = Arrays.asList(sysRestrictedHeaders.split(","));
if (headerList.contains(PassThroughConstants.CORRELATION_DEFAULT_HEADER)) {
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(NhttpConstants.ACTIVITY_ID_STATUS, false);
}
}
Object status = context.getProperty(NhttpConstants.ACTIVITY_ID_STATUS);
if ((status != null) && ((boolean) status)) {
if (headers != null) {
headers.put(PassThroughConfiguration.getInstance().getCorrelationHeaderName(), context.getProperty(CorrelationConstants.CORRELATION_ID));
}
}
}
for (Handler handler : handlers) {
auditDebug("Processing message with ID: " + synCtx.getMessageID() + " through " + "handler: " + handler.getClass().getName());
boolean proceed;
if (synCtx.isResponse()) {
proceed = handler.handleResponse(synCtx);
} else {
proceed = handler.handleRequest(synCtx);
}
if (!proceed) {
return;
}
}
if (synCtx.isResponse()) {
String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
if (resourceName != null) {
Resource resource = resources.get(resourceName);
if (resource != null) {
resource.process(synCtx);
}
} else if (log.isDebugEnabled()) {
auditDebug("No resource information on the response: " + synCtx.getMessageID());
}
return;
}
String path = ApiUtils.getFullRequestPath(synCtx);
String subPath;
if (versionStrategy.getVersionType().equals(VersionStrategyFactory.TYPE_URL)) {
// for URL based
// request --> http://{host:port}/context/version/path/to/resource
subPath = path.substring(context.length() + versionStrategy.getVersion().length() + 1);
} else {
subPath = path.substring(context.length());
}
if ("".equals(subPath)) {
subPath = "/";
}
synCtx.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, subPath);
org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
String hostHeader = getHostHeader(msgCtx);
if (hostHeader != null) {
synCtx.setProperty(RESTConstants.REST_URL_PREFIX, msgCtx.getIncomingTransportName() + "://" + hostHeader);
}
Set<Resource> acceptableResources = new LinkedHashSet<Resource>();
for (Resource r : resources.values()) {
if (isBound(r, synCtx) && r.canProcess(synCtx)) {
acceptableResources.add(r);
}
}
boolean processed = false;
if (!acceptableResources.isEmpty()) {
for (RESTDispatcher dispatcher : ApiUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(synCtx, acceptableResources);
if (resource != null) {
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (!synCtx.isResponse()) {
SynapseWireLogHolder wireLogHolder = (SynapseWireLogHolder) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(SynapseDebugInfoHolder.SYNAPSE_WIRE_LOG_HOLDER_PROPERTY);
if (wireLogHolder == null) {
wireLogHolder = new SynapseWireLogHolder();
}
if (synCtx.getProperty(RESTConstants.SYNAPSE_REST_API) != null && !synCtx.getProperty(RESTConstants.SYNAPSE_REST_API).toString().isEmpty()) {
wireLogHolder.setApiName(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API).toString());
if (resource.getDispatcherHelper() != null) {
if (resource.getDispatcherHelper().getString() != null && !resource.getDispatcherHelper().getString().isEmpty()) {
wireLogHolder.setResourceUrlString(resource.getDispatcherHelper().getString());
}
}
}
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(SynapseDebugInfoHolder.SYNAPSE_WIRE_LOG_HOLDER_PROPERTY, wireLogHolder);
}
}
resource.process(synCtx);
return;
}
}
handleResourceNotFound(synCtx);
} else {
// This will get executed only in unhappy path. So ok to have the iterator.
boolean resourceFound = false;
boolean matchingMethodFound = false;
for (RESTDispatcher dispatcher : ApiUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(synCtx, resources.values());
if (resource != null) {
resourceFound = true;
String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD);
matchingMethodFound = resource.hasMatchingMethod(method);
break;
}
}
if (!resourceFound) {
handleResourceNotFound(synCtx);
} else if (!matchingMethodFound) {
handleMethodNotAllowed(synCtx);
} else {
// Resource found, and matching method also found, which means request is BAD_REQUEST(400)
msgCtx.setProperty(SynapseConstants.HTTP_SC, HttpStatus.SC_BAD_REQUEST);
msgCtx.setProperty("NIO-ACK-Requested", true);
}
}
}
use of org.apache.synapse.api.version.URLBasedVersionStrategy in project wso2-synapse by wso2.
the class BasicAPIMediationTest method testRestURLPostfix1.
public void testRestURLPostfix1() throws Exception {
API api = new API(TEST_API, "/test");
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(TEST_API, api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "");
synCtx = getMessageContext(synapseConfig, false, "/test/me/now", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "/me/now");
synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "?a=5");
api.setVersionStrategy(new URLBasedVersionStrategy(api, "1.0.0", null));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "?a=5");
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo?a=5", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "/foo?a=5");
}
use of org.apache.synapse.api.version.URLBasedVersionStrategy in project wso2-synapse by wso2.
the class APIDispatcherTest method testAPIURLVersionBasedDispatch.
public void testAPIURLVersionBasedDispatch() throws Exception {
API api = new API(TEST_API, "/test");
api.setVersionStrategy(new URLBasedVersionStrategy(api, TEST_API_VERSION, null));
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
// Messages with '/test' context should NOT be dispatched
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo/bar?a=5", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
// Message with '/test' context & URL as a Query Parameter should be dispatched
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=http://localhost.com", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
// Messages WITHOUT the '/test' context should NOT be dispatched
synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
// Messages WITHOUT the '/test' context and proper version should NOT be dispatched
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.1/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/2.0/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/2.0.0.0/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
Aggregations