Skip to content

Commit

Permalink
removing regex usage for finding the resource path
Browse files Browse the repository at this point in the history
used the implementation of URITemplateBasedDispatcher for resource matching

issue: wso2/api-manager#1564
  • Loading branch information
chamikasudusinghe committed Jun 12, 2023
1 parent c2f7798 commit 5b3818c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@

package org.wso2.carbon.apimgt.gateway.handlers;

import org.apache.axis2.Constants;
import org.apache.http.HttpHeaders;
import org.apache.synapse.MessageContext;
import org.apache.synapse.api.API;
import org.apache.synapse.api.ApiUtils;
import org.apache.synapse.api.Resource;
import org.apache.synapse.api.dispatch.DispatcherHelper;
import org.apache.synapse.api.dispatch.RESTDispatcher;
import org.apache.synapse.api.dispatch.URITemplateBasedDispatcher;
import org.apache.synapse.api.dispatch.URITemplateHelper;
import org.apache.synapse.api.version.DefaultStrategy;
import org.apache.synapse.api.version.VersionStrategy;
import org.apache.synapse.config.xml.rest.VersionStrategyFactory;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.RESTConstants;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.impl.APIConstants;

import java.util.Map;
import java.util.*;

/**
* Provides util methods for the LogsHandler
Expand Down Expand Up @@ -126,6 +138,78 @@ protected static String getHTTPMethod(org.apache.synapse.MessageContext messageC
return httpMethod;
}

protected static void fetchLogLevel(MessageContext messageContext,
Map<Map<String, String>, String> logProperties) {
Collection<API> apiSet = messageContext.getEnvironment().getSynapseConfiguration().getAPIs();
List<API> duplicateApiSet = new ArrayList<API>(apiSet);
API selectedApi = null;
String apiContext = ((Axis2MessageContext)messageContext).getAxis2MessageContext().
getProperty("TransportInURL").toString();
for (API api : duplicateApiSet) {
if (apiContext.contains(api.getContext())) {
selectedApi = api;
//break;
}
}
String httpMethod = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
getProperty(Constants.Configuration.HTTP_METHOD);
org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext)
.getAxis2MessageContext();
Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
String corsRequestMethod = (String) headers.get(APIConstants.CORSHeaders.ACCESS_CONTROL_REQUEST_METHOD);
if (selectedApi != null) {
String path = ApiUtils.getFullRequestPath(messageContext);
String subPath;
VersionStrategy versionStrategy = new DefaultStrategy(selectedApi);
if (versionStrategy.getVersionType().equals(VersionStrategyFactory.TYPE_URL)) {
//for URL based
//request --> http://{host:port}/context/version/path/to/resource
subPath = path.substring(selectedApi.getContext().length() + versionStrategy.getVersion().length() + 1);
} else {
subPath = path.substring(selectedApi.getContext().length());
}
if ("".equals(subPath)) {
subPath = "/";
}
messageContext.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, subPath);
Resource[] allAPIResources = selectedApi.getResources();
Set<Resource> acceptableResources = new LinkedHashSet<>();
for (Resource resource : allAPIResources) {
//If the requesting method is OPTIONS or if the Resource contains the requesting method
if ((RESTConstants.METHOD_OPTIONS.equals(httpMethod) && resource.getMethods() != null &&
Arrays.asList(resource.getMethods()).contains(corsRequestMethod)) ||
(resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
acceptableResources.add(resource);
}
}

if (!acceptableResources.isEmpty()) {
for (RESTDispatcher dispatcher : ApiUtils.getDispatchers()) {
if (dispatcher instanceof URITemplateBasedDispatcher){
Resource selectedResource = dispatcher.findResource(messageContext, acceptableResources);
if (selectedResource != null) {
messageContext.setProperty(LogsHandler.SELECTED_RESOURCE, selectedResource);
DispatcherHelper helper = selectedResource.getDispatcherHelper();
if (helper instanceof URITemplateHelper) {
URITemplateHelper templateHelper = (URITemplateHelper) helper;
for (Map.Entry<Map<String, String>, String> entry : logProperties.entrySet()) {
Map<String, String> key = entry.getKey();
if (httpMethod.equals(key.get("resourceMethod"))) {
boolean flag = templateHelper.getString().equals(key.get("resourcePath"));
System.out.println(flag);
String resourceLogLevel = entry.getValue();
String resourcePath = key.get("resourcePath");
String resourceMethod = key.get("resourceMethod");
}
}
}
}
}
}
}
}
}

protected static String getMatchingLogLevel(MessageContext ctx,
Map<Map<String, String>, String> logProperties) {
String apiCtx = LogUtils.getTransportInURL(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class LogsHandler extends AbstractSynapseHandler {
protected static final String LOG_LEVEL = "LOG_LEVEL";
protected static final String RESOURCE_PATH = "RESOURCE_PATH";
protected static final String RESOURCE_METHOD = "RESOURCE_METHOD";
public static final String SELECTED_RESOURCE = "SELECTED_RESOURCE";

private static final String REQUEST_BODY_SIZE_ERROR = "Error occurred while building the message to calculate" +
" the response body size";
Expand Down Expand Up @@ -304,6 +305,7 @@ private String getAPILogLevel(MessageContext ctx) {
Map<Map<String, String>, String> logProperties = APILoggerManager.getInstance().getPerAPILoggerList();
// if the logging API data holder is empty or null return null
if (!logProperties.isEmpty()) {
LogUtils.fetchLogLevel(ctx, logProperties);
return LogUtils.getMatchingLogLevel(ctx, logProperties);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.synapse.api.dispatch.RESTDispatcher;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.gateway.MethodStats;
import org.wso2.carbon.apimgt.gateway.handlers.LogsHandler;
import org.wso2.carbon.apimgt.gateway.handlers.Utils;
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.APIConstants;
Expand Down Expand Up @@ -179,11 +180,20 @@ public boolean handleRequest(MessageContext messageContext) {
}

if (!acceptableResources.isEmpty()) {
for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(messageContext, acceptableResources);
if (resource != null) {
if (messageContext.getProperty(LogsHandler.SELECTED_RESOURCE) != null){
Resource resource = (Resource) messageContext.getProperty(LogsHandler.SELECTED_RESOURCE);
String [] resourceMethods = resource.getMethods();
if (Arrays.asList(resourceMethods).contains(httpMethod)) {
selectedResource = resource;
break;
}
}
else {
for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(messageContext, acceptableResources);
if (resource != null) {
selectedResource = resource;
break;
}
}
}
if (selectedResource == null) {
Expand Down

0 comments on commit 5b3818c

Please sign in to comment.