Skip to content

Commit

Permalink
Merge pull request #2114 from Kirishikesan/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirishikesan authored Nov 2, 2023
2 parents b00d235 + 4b2bdae commit 343fca8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
44 changes: 27 additions & 17 deletions modules/core/src/main/java/org/apache/synapse/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 343fca8

Please sign in to comment.