Skip to content

Commit

Permalink
refactored the methods to get resources of apis
Browse files Browse the repository at this point in the history
  • Loading branch information
chamikasudusinghe committed Jun 20, 2023
1 parent 18f4415 commit f0010ed
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
28 changes: 5 additions & 23 deletions modules/core/src/main/java/org/apache/synapse/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ public Resource[] getResources() {
return resources.values().toArray(new Resource[resources.size()]);
}

public Map<String, Resource> getResourcesMap() {
return resources;
}

public void addHandler(Handler handler) {
handlers.add(handler);
}
Expand Down Expand Up @@ -443,12 +447,7 @@ public void process(MessageContext synCtx) {
msgCtx.getIncomingTransportName() + "://" + hostHeader);
}

Set<Resource> acceptableResources = new LinkedHashSet<Resource>();
for (Resource r : resources.values()) {
if (isBound(r, synCtx) && r.canProcess(synCtx)) {
acceptableResources.add(r);
}
}
Set<Resource> acceptableResources = ApiUtils.getAcceptableResources(resources, synCtx);

boolean processed = false;
if (!acceptableResources.isEmpty()) {
Expand Down Expand Up @@ -520,23 +519,6 @@ private void handleMethodNotAllowed(MessageContext synCtx) {

}

/**
* Checks whether the provided resource is capable of processing the message from the provided message context.
* The resource becomes capable to do this when the it contains either the name of the api caller,
* or {@value ApiConstants#DEFAULT_BINDING_ENDPOINT_NAME}, in its binds-to.
*
* @param resource Resource object
* @param synCtx MessageContext object
* @return Whether the provided resource is bound to the provided message context
*/
private boolean isBound(Resource resource, MessageContext synCtx) {
Collection<String> bindings = resource.getBindsTo();
Object apiCaller = synCtx.getProperty(ApiConstants.API_CALLER);
if (apiCaller != null) {
return bindings.contains(apiCaller.toString());
}
return bindings.contains(ApiConstants.DEFAULT_BINDING_ENDPOINT_NAME);
}

/**
* Helper method to use when no matching resource found
Expand Down
37 changes: 32 additions & 5 deletions modules/core/src/main/java/org/apache/synapse/api/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class ApiUtils {

Expand Down Expand Up @@ -158,6 +157,34 @@ public static String getSubRequestPath(MessageContext synCtx) {
return (String) synCtx.getProperty(RESTConstants.REST_SUB_REQUEST_PATH);
}

/**
* Checks whether the provided resource is capable of processing the message from the provided message context.
* The resource becomes capable to do this when the it contains either the name of the api caller,
* or {@value ApiConstants#DEFAULT_BINDING_ENDPOINT_NAME}, in its binds-to.
*
* @param resource Resource object
* @param synCtx MessageContext object
* @return Whether the provided resource is bound to the provided message context
*/
public static boolean isBound(Resource resource, MessageContext synCtx) {
Collection<String> bindings = resource.getBindsTo();
Object apiCaller = synCtx.getProperty(ApiConstants.API_CALLER);
if (apiCaller != null) {
return bindings.contains(apiCaller.toString());
}
return bindings.contains(ApiConstants.DEFAULT_BINDING_ENDPOINT_NAME);
}

public static Set<Resource> getAcceptableResources(Map<String, Resource> resources, MessageContext synCtx) {
Set<Resource> acceptableResources = new LinkedHashSet<Resource>();
for (Resource r : resources.values()) {
if (isBound(r, synCtx) && r.canProcess(synCtx)) {
acceptableResources.add(r);
}
}
return acceptableResources;
}

public static List<RESTDispatcher> getDispatchers() {
return dispatchers;
}
Expand All @@ -177,10 +204,10 @@ private static void handleException(String msg, Throwable t) {
* and false if the two values don't match
*/
public static boolean matchApiPath(String path, String context) {
if (!path.startsWith(context + "/") && !path.startsWith(context + "?") &&
!context.equals(path) && !"/".equals(context)) {
if (!path.startsWith(context + "/") && !path.startsWith(context + "?")
&& !context.equals(path) && !"/".equals(context)) {
return false;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.synapse.MessageContext;
import org.apache.synapse.api.Resource;
import org.apache.synapse.rest.RESTConstants;

import java.util.Collection;

Expand All @@ -28,6 +29,7 @@ public class DefaultDispatcher implements RESTDispatcher {
public Resource findResource(MessageContext synCtx, Collection<Resource> resources) {
for (Resource resource : resources) {
if (resource.getDispatcherHelper() == null) {
synCtx.setProperty(RESTConstants.SELECTED_RESOURCE, resource);
return resource;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public Resource findResource(MessageContext synCtx, Collection<Resource> resourc
DispatcherHelper helper = r.getDispatcherHelper();
if (helper instanceof URITemplateHelper) {
URITemplateHelper templateHelper = (URITemplateHelper) helper;
Map<String,String> variables = new HashMap<String,String>();
Map<String, String> variables = new HashMap<String, String>();
if (templateHelper.getUriTemplate().matches(url, variables)) {
for (Map.Entry<String,String> entry : variables.entrySet()) {
for (Map.Entry<String, String> entry : variables.entrySet()) {
synCtx.setProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + entry.getKey(),
entry.getValue());
}
synCtx.setProperty(RESTConstants.SELECTED_RESOURCE, r);
return r;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.synapse.MessageContext;
import org.apache.synapse.api.ApiUtils;
import org.apache.synapse.api.Resource;
import org.apache.synapse.rest.RESTConstants;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -55,6 +56,7 @@ public Resource findResource(MessageContext synCtx, Collection<Resource> resourc
if (log.isDebugEnabled()) {
log.debug("Found exact URL match for: " + url);
}
synCtx.setProperty(RESTConstants.SELECTED_RESOURCE, filteredResources.get(i));
return filteredResources.get(i);
}
}
Expand All @@ -72,6 +74,7 @@ public Resource findResource(MessageContext synCtx, Collection<Resource> resourc
if (log.isDebugEnabled()) {
log.debug("Found path match for: " + url + " with matching length: " + maxLength);
}
synCtx.setProperty(RESTConstants.SELECTED_RESOURCE, matchedResource);
return matchedResource;
}

Expand All @@ -80,6 +83,7 @@ public Resource findResource(MessageContext synCtx, Collection<Resource> resourc
if (log.isDebugEnabled()) {
log.debug("Found extension match for: " + url);
}
synCtx.setProperty(RESTConstants.SELECTED_RESOURCE, filteredResources.get(i));
return filteredResources.get(i);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ public static enum METHODS {
*/
public static final String IS_PROMETHEUS_ENGAGED = "IS_PROMETHEUS_ENGAGED";
public static final String PROCESSED_API = "PROCESSED_API";
public static final String SELECTED_RESOURCE = "SELECTED_RESOURCE";

}

0 comments on commit f0010ed

Please sign in to comment.