Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import org.apache.axis2.addressing.EndpointReference;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.endpoints.auth.AuthHandler;
import org.apache.synapse.endpoints.auth.oauth.MessageCache;
import org.apache.synapse.endpoints.auth.AuthConstants;
import org.apache.synapse.endpoints.auth.AuthException;
import org.apache.synapse.endpoints.auth.oauth.OAuthHandler;
import org.apache.synapse.endpoints.auth.oauth.OAuthUtils;
import org.apache.synapse.transport.passthru.PassThroughConstants;
import org.apache.synapse.transport.passthru.util.RelayUtils;
import org.apache.synapse.util.MessageHelper;

/**
Expand Down Expand Up @@ -56,22 +59,45 @@ public void send(MessageContext synCtx) {
OAuthUtils.append401HTTPSC(synCtx);
}

// Clone the original MessageContext and save it to do a retry after a token refresh
MessageContext clonedMessageContext = MessageHelper.cloneMessageContext(synCtx);
MessageCache.getInstance().addMessageContext(synCtx.getMessageID(), clonedMessageContext);
boolean isMessageBuildingDisabled = Boolean.parseBoolean(synCtx.getEnvironment().getSynapseConfiguration().
getProperty(AuthConstants.OAUTH_RETRY_MESSAGE_BUILDING_DISABLED));
if (!isMessageBuildingDisabled) {
// Clone the original MessageContext and save it to do a retry after a token refresh
MessageContext cloneMessageContext = MessageHelper.cloneMessageContext(synCtx);
MessageCache.getInstance().addMessageContext(synCtx.getMessageID(), cloneMessageContext);
}

super.send(synCtx);

} catch (AuthException e) {
consumeInputOnException(synCtx);
handleError(synCtx,
"Could not generate access token for oauth configured http endpoint " + this.getName() + ".", e);
} catch (AxisFault axisFault) {
consumeInputOnException(synCtx);
handleError(synCtx,
"Error cloning the message context for oauth configured http endpoint " + this.getName() + ".",
axisFault);
}
}

/**
* This method will consume the input stream in case of an exception
*
* @param synCtx Synapse message context.
*/
private void consumeInputOnException(MessageContext synCtx) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).
getAxis2MessageContext();
axis2MessageContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);

try {
RelayUtils.discardRequestMessage(axis2MessageContext);
} catch (AxisFault axisFault) {
log.error("Exception while consuming the input stream on Om Exception", axisFault);
}
}

/**
* This method is called when we need to retry a call to the resource with a new token
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,8 @@ public class AuthConstants {
public static final String OAUTH_TOKEN_ENDPOINT_TRUST_STORE_TYPE = "synapse.endpoint.http.oauth.token.endpoint.trust.store.type";
public static final String OAUTH_TOKEN_ENDPOINT_TRUST_STORE_PASSWORD = "synapse.endpoint.http.oauth.token.endpoint.trust.store.password";

//Disable message building for oauth secured endpoint token generation
public static final String OAUTH_RETRY_MESSAGE_BUILDING_DISABLED = "synapse.endpoint.http.oauth.token.retry.message.building.disabled";

public static final String HTTPS_PROTOCOL = "https";
}
Loading