use of org.apache.synapse.rest.dispatch.RESTDispatcher in project wso2-synapse by wso2.
the class API method process.
void process(MessageContext synCtx) {
auditDebug("Processing message with ID: " + synCtx.getMessageID() + " through the " + "API: " + name);
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());
// 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);
}
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 = RESTUtils.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 (r.canProcess(synCtx)) {
acceptableResources.add(r);
}
}
boolean processed = false;
if (!acceptableResources.isEmpty()) {
for (RESTDispatcher dispatcher : RESTUtils.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 : RESTUtils.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 (resourceFound && !matchingMethodFound) {
// Resource found, but in that resource, requested method not allowed. So sending method not allowed http status (405)
msgCtx.setProperty(SynapseConstants.HTTP_SC, HttpStatus.SC_METHOD_NOT_ALLOWED);
msgCtx.removeProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
msgCtx.setProperty("NIO-ACK-Requested", true);
} 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);
}
}
}
Aggregations