From 4b2bdaebd5ba7fc6792c8e945724b67a210fba57 Mon Sep 17 00:00:00 2001 From: Kannan Kirishikesan Date: Thu, 2 Nov 2023 12:38:49 +0530 Subject: [PATCH] Fix for https://github.com/wso2/api-manager/issues/2226 --- .../main/java/org/apache/synapse/api/API.java | 44 ++++++++++++------- .../java/org/apache/synapse/api/Resource.java | 8 +--- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/api/API.java b/modules/core/src/main/java/org/apache/synapse/api/API.java index d7e6d23c64..78cfb298c6 100644 --- a/modules/core/src/main/java/org/apache/synapse/api/API.java +++ b/modules/core/src/main/java/org/apache/synapse/api/API.java @@ -457,22 +457,8 @@ public void process(MessageContext synCtx) { 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); + initWirelogHolder(synCtx, resource); } - } resource.process(synCtx); return; @@ -483,8 +469,9 @@ public void process(MessageContext synCtx) { //This will get executed only in unhappy path. So ok to have the iterator. boolean resourceFound = false; boolean matchingMethodFound = false; + Resource resource = null; for (RESTDispatcher dispatcher : ApiUtils.getDispatchers()) { - Resource resource = dispatcher.findResource(synCtx, resources.values()); + resource = dispatcher.findResource(synCtx, resources.values()); if (resource != null) { resourceFound = true; String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD); @@ -495,7 +482,13 @@ public void process(MessageContext synCtx) { if (!resourceFound) { handleResourceNotFound(synCtx); } else if (!matchingMethodFound) { - handleMethodNotAllowed(synCtx); + String method = (String) synCtx.getProperty(RESTConstants.REST_METHOD); + if (RESTConstants.METHOD_OPTIONS.equals(method)) { + initWirelogHolder(synCtx, resource); + resource.process(synCtx); + } else { + 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); @@ -504,6 +497,23 @@ public void process(MessageContext synCtx) { } } + private static void initWirelogHolder(MessageContext synCtx, Resource resource) { + 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); + } + private void handleMethodNotAllowed(MessageContext synCtx) { auditDebug("Method not allowed for the request: " + synCtx.getMessageID()); diff --git a/modules/core/src/main/java/org/apache/synapse/api/Resource.java b/modules/core/src/main/java/org/apache/synapse/api/Resource.java index 96c7806214..74c2f72279 100644 --- a/modules/core/src/main/java/org/apache/synapse/api/Resource.java +++ b/modules/core/src/main/java/org/apache/synapse/api/Resource.java @@ -168,9 +168,7 @@ public String[] getMethods() { * @return true if support false otherwise. */ public boolean hasMatchingMethod(String method) { - if (RESTConstants.METHOD_OPTIONS.equals(method)) { - return true; // OPTIONS requests are always welcome - } else if (!methods.isEmpty()) { + if (!methods.isEmpty()) { if (!methods.contains(method)) { if (log.isDebugEnabled()) { log.debug("HTTP method does not match"); @@ -251,9 +249,7 @@ boolean canProcess(MessageContext synCtx) { String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD); synCtx.setProperty(RESTConstants.REST_METHOD, method); - if (RESTConstants.METHOD_OPTIONS.equals(method)) { - return true; // OPTIONS requests are always welcome - } else if (!methods.isEmpty()) { + if (!methods.isEmpty()) { if (!methods.contains(method)) { if (log.isDebugEnabled()) { log.debug("HTTP method does not match");