From 1abf90d412888edee55dc1fdad707257247a66c2 Mon Sep 17 00:00:00 2001 From: Dakshina Ranmal <76484102+dakshina99@users.noreply.github.com> Date: Mon, 9 Dec 2024 20:25:40 +0530 Subject: [PATCH] Revert "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, 23 insertions(+), 29 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 2c59db3b47..5d7ed3cf0b 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 @@ -456,8 +456,22 @@ public void process(MessageContext synCtx) { if (resource != null) { if (synCtx.getEnvironment().isDebuggerEnabled()) { if (!synCtx.isResponse()) { - initWirelogHolder(synCtx, 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); } + } resource.process(synCtx); return; @@ -468,9 +482,8 @@ 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 = dispatcher.findResource(synCtx, resources.values()); + Resource resource = dispatcher.findResource(synCtx, resources.values()); if (resource != null) { resourceFound = true; String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD); @@ -481,13 +494,7 @@ public void process(MessageContext synCtx) { if (!resourceFound) { handleResourceNotFound(synCtx); } else if (!matchingMethodFound) { - String method = (String) synCtx.getProperty(RESTConstants.REST_METHOD); - if (RESTConstants.METHOD_OPTIONS.equals(method)) { - initWirelogHolder(synCtx, resource); - resource.process(synCtx); - } else { - handleMethodNotAllowed(synCtx); - } + 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); @@ -496,23 +503,6 @@ 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 74c2f72279..96c7806214 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,7 +168,9 @@ public String[] getMethods() { * @return true if support false otherwise. */ public boolean hasMatchingMethod(String method) { - if (!methods.isEmpty()) { + if (RESTConstants.METHOD_OPTIONS.equals(method)) { + return true; // OPTIONS requests are always welcome + } else if (!methods.isEmpty()) { if (!methods.contains(method)) { if (log.isDebugEnabled()) { log.debug("HTTP method does not match"); @@ -249,7 +251,9 @@ boolean canProcess(MessageContext synCtx) { String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD); synCtx.setProperty(RESTConstants.REST_METHOD, method); - if (!methods.isEmpty()) { + if (RESTConstants.METHOD_OPTIONS.equals(method)) { + return true; // OPTIONS requests are always welcome + } else if (!methods.isEmpty()) { if (!methods.contains(method)) { if (log.isDebugEnabled()) { log.debug("HTTP method does not match");