Skip to content

Commit

Permalink
Merge pull request #12723 from dakshina99/options-resource-backend
Browse files Browse the repository at this point in the history
Route OPTIONS Resource Traffic to the Backend
  • Loading branch information
AnuGayan authored Dec 11, 2024
2 parents 9c8f912 + 1c59aea commit f11cb95
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.common.gateway.constants.HealthCheckConstants;
import org.wso2.carbon.apimgt.common.gateway.constants.JWTConstants;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.gateway.InMemoryAPIDeployer;
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.gateway.utils.GatewayUtils;
Expand Down Expand Up @@ -88,6 +89,7 @@ public boolean handleRequestInFlow(MessageContext messageContext) {
String selectedPath = selectedAPIS.firstKey();
API selectedAPI = selectedAPIS.get(selectedPath);
if (selectedAPI != null) {
messageContext.setProperty(APIMgtGatewayConstants.API_OBJECT, selectedAPI);
if (GatewayUtils.isOnDemandLoading()) {
if (!selectedAPI.isDeployed()) {
synchronized ("LoadAPI_".concat(selectedAPI.getContext()).intern()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.cache.Caching;
import javax.xml.namespace.QName;

Expand Down Expand Up @@ -822,17 +836,19 @@ public static API getAPIByContext(MessageContext messageContext) {
*/
public static Set<Resource> getAcceptableResources(Resource[] allAPIResources,
String httpMethod, String corsRequestMethod) {
Set<Resource> acceptableResources = new LinkedHashSet<>();
List<Resource> acceptableResourcesList = new LinkedList<>();
for (Resource resource : allAPIResources) {
//If the requesting method is OPTIONS or if the Resource contains the requesting method
String [] resourceMethods = resource.getMethods();
if ((RESTConstants.METHOD_OPTIONS.equals(httpMethod) && resourceMethods != null
&& Arrays.asList(resourceMethods).contains(corsRequestMethod))
|| (resourceMethods != null && Arrays.asList(resourceMethods).contains(httpMethod))) {
acceptableResources.add(resource);
if (resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod) &&
RESTConstants.METHOD_OPTIONS.equals(httpMethod)) {
acceptableResourcesList.add(0, resource);
} else if ((RESTConstants.METHOD_OPTIONS.equals(httpMethod) && resource.getMethods() != null &&
Arrays.asList(resource.getMethods()).contains(corsRequestMethod)) ||
(resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
acceptableResourcesList.add(resource);
}
}
return acceptableResources;
return new LinkedHashSet<>(acceptableResourcesList);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -399,16 +400,21 @@ public List<VerbInfoDTO> findMatchingVerb(MessageContext synCtx) throws Resource
if (selectedApi != null) {
Resource[] selectedAPIResources = selectedApi.getResources();

Set<Resource> acceptableResources = new LinkedHashSet<Resource>();
List<Resource> acceptableResourcesList = new LinkedList<>();

for (Resource resource : selectedAPIResources) {
//If the requesting method is OPTIONS or if the Resource contains the requesting method
if (RESTConstants.METHOD_OPTIONS.equals(httpMethod) ||
if (RESTConstants.METHOD_OPTIONS.equals(httpMethod) &&
(resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
acceptableResources.add(resource);
acceptableResourcesList.add(0, resource);
} else if (RESTConstants.METHOD_OPTIONS.equals(httpMethod) ||
(resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
acceptableResourcesList.add(resource);
}
}

Set<Resource> acceptableResources = new LinkedHashSet<>(acceptableResourcesList);

if (acceptableResources.size() > 0) {
for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(synCtx, acceptableResources);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@
<imp.package.version.osgi.framework>[1.6.0, 2.0.0)</imp.package.version.osgi.framework>

<!-- Misc Versions -->
<synapse.version>4.0.0-wso2v131</synapse.version>
<synapse.version>4.0.0-wso2v152</synapse.version>
<orbit.version.json>3.0.0.wso2v1</orbit.version.json>

<!-- orbit httpmime versions-->
Expand Down

0 comments on commit f11cb95

Please sign in to comment.