Skip to content

Commit

Permalink
Merge branch 'wso2:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
GihanAyesh authored Feb 8, 2024
2 parents b4dffa1 + 6666e7e commit bcbad36
Show file tree
Hide file tree
Showing 66 changed files with 1,110 additions and 375 deletions.
2 changes: 1 addition & 1 deletion modules/commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.synapse</groupId>
<artifactId>Apache-Synapse</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<parent>
<groupId>org.apache.synapse</groupId>
<artifactId>Apache-Synapse</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,18 @@ private String decodeString(String value) {
}
}

private void processUrlTemplate(MessageContext synCtx) throws VariableExpansionException {
protected void processUrlTemplate(MessageContext synCtx) throws VariableExpansionException {

String evaluatedUri = resolveUrlTemplate(synCtx);
if (evaluatedUri != null) {
synCtx.setTo(new EndpointReference(evaluatedUri));
if (super.getDefinition() != null) {
synCtx.setProperty(EndpointDefinition.DYNAMIC_URL_VALUE, evaluatedUri);
}
}
}

protected String resolveUrlTemplate(MessageContext synCtx) throws VariableExpansionException {
Map<String, Object> variables = new HashMap<String, Object>();

/*The properties with uri.var.* are only considered for Outbound REST Endpoints*/
Expand All @@ -162,10 +173,10 @@ private void processUrlTemplate(MessageContext synCtx) throws VariableExpansionE
if (objProperty != null) {
if (objProperty instanceof String) {
variables.put(propertyKey.toString(),
decodeString((String) synCtx.getProperty(propertyKey.toString())));
decodeString((String) synCtx.getProperty(propertyKey.toString())));
} else {
variables.put(propertyKey.toString(),
decodeString(String.valueOf(synCtx.getProperty(propertyKey.toString()))));
decodeString(String.valueOf(synCtx.getProperty(propertyKey.toString()))));
}
}
}
Expand Down Expand Up @@ -224,14 +235,14 @@ private void processUrlTemplate(MessageContext synCtx) throws VariableExpansionE
(propertyKey.toString().startsWith(RESTConstants.REST_URI_VARIABLE_PREFIX)
|| propertyKey.toString().startsWith(RESTConstants.REST_QUERY_PARAM_PREFIX))) {
Object objProperty =
synCtx.getProperty(propertyKey.toString());
synCtx.getProperty(propertyKey.toString());
if (objProperty != null) {
if (objProperty instanceof String) {
variables.put(propertyKey.toString(),
(String) synCtx.getProperty(propertyKey.toString()));
(String) synCtx.getProperty(propertyKey.toString()));
} else {
variables.put(propertyKey.toString(),
(String) String.valueOf(synCtx.getProperty(propertyKey.toString())));
(String) String.valueOf(synCtx.getProperty(propertyKey.toString())));
}
}
}
Expand Down Expand Up @@ -282,14 +293,7 @@ private void processUrlTemplate(MessageContext synCtx) throws VariableExpansionE
}
}
}


if (evaluatedUri != null) {
synCtx.setTo(new EndpointReference(evaluatedUri));
if (super.getDefinition() != null) {
synCtx.setProperty(EndpointDefinition.DYNAMIC_URL_VALUE, evaluatedUri);
}
}
return evaluatedUri;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.apache.synapse.endpoints;

import com.damnhandy.uri.template.VariableExpansionException;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.endpoints.auth.AuthHandler;
Expand Down Expand Up @@ -46,6 +48,7 @@ public OAuthConfiguredHTTPEndpoint(AuthHandler authHandler) {
public void send(MessageContext synCtx) {

try {
setResolvedUrlTemplate(synCtx);
oAuthHandler.setAuthHeader(synCtx);

// If this a blocking call, add 401 as a non error http status code
Expand Down Expand Up @@ -77,17 +80,22 @@ public void send(MessageContext synCtx) {
*/
public MessageContext retryCallWithNewToken(MessageContext synCtx) {
// remove the existing token from the cache so that a new token is generated
oAuthHandler.removeTokenFromCache();
// set RETRIED_ON_OAUTH_FAILURE property to true
synCtx.setProperty(AuthConstants.RETRIED_ON_OAUTH_FAILURE, true);
send(synCtx);
try {
// set RETRIED_ON_OAUTH_FAILURE property to true
synCtx.setProperty(AuthConstants.RETRIED_ON_OAUTH_FAILURE, true);
oAuthHandler.removeTokenFromCache(synCtx);
send(synCtx);
} catch (AuthException e) {
handleError(synCtx,
"Error removing access token for oauth configured http endpoint " + this.getName(), e);
}
return synCtx;
}

@Override
public void destroy() {

oAuthHandler.removeTokenFromCache();
oAuthHandler.removeTokensFromCache();
super.destroy();
}

Expand All @@ -109,4 +117,21 @@ private void handleError(MessageContext synCtx, String message, Exception except
log.error(errorMsg);
informFailure(synCtx, SynapseConstants.ENDPOINT_AUTH_FAILURE, errorMsg);
}

private void setResolvedUrlTemplate(MessageContext messageContext) {
String resolvedUrl = resolveUrlTemplate(messageContext);
if (resolvedUrl != null) {
messageContext.setTo(new EndpointReference(resolvedUrl));
if (super.getDefinition() != null) {
messageContext.setProperty(EndpointDefinition.DYNAMIC_URL_VALUE, resolvedUrl);
}
}
}

@Override
protected void processUrlTemplate(MessageContext synCtx) throws VariableExpansionException {
// Since we set the resolved URL in the OAuthConfiguredHTTPEndpoint.send method
// return the processUrlTemplate to skip re-resolving at HTTPEndpoint.
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.synapse.endpoints.auth.AuthConstants;
import org.apache.synapse.endpoints.auth.AuthException;

import java.util.Objects;

/**
* This class is used to handle Authorization code grant oauth.
*/
Expand Down Expand Up @@ -74,6 +76,15 @@ protected OMElement serializeSpecificOAuthConfigs(OMFactory omFactory) {
return authCode;
}

@Override
protected int getHash(MessageContext messageContext) throws AuthException {
return Objects.hash(messageContext.getTo().getAddress(), OAuthUtils.resolveExpression(getTokenUrl(), messageContext),
OAuthUtils.resolveExpression(getClientId(), messageContext), OAuthUtils.resolveExpression(getClientSecret(),
messageContext), OAuthUtils.resolveExpression(getRefreshToken(), messageContext),
getRequestParametersAsString(messageContext), getResolvedCustomHeadersMap(getCustomHeadersMap(),
messageContext));
}

/**
* Return the refresh token secret relevant to the Authorization Code Handler.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.synapse.endpoints.auth.AuthConstants;
import org.apache.synapse.endpoints.auth.AuthException;

import java.util.Objects;

/**
* This class is used to handle Client Credentials grant oauth.
*/
Expand Down Expand Up @@ -60,4 +62,12 @@ protected OMElement serializeSpecificOAuthConfigs(OMFactory omFactory) {

return omFactory.createOMElement(AuthConstants.CLIENT_CREDENTIALS, SynapseConstants.SYNAPSE_OMNAMESPACE);
}

@Override
protected int getHash(MessageContext messageContext) throws AuthException {
return Objects.hash(messageContext.getTo().getAddress(), OAuthUtils.resolveExpression(getTokenUrl(), messageContext),
OAuthUtils.resolveExpression(getClientId(), messageContext), OAuthUtils.resolveExpression(getClientSecret(),
messageContext), getRequestParametersAsString(messageContext),
getResolvedCustomHeadersMap(getCustomHeadersMap(), messageContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
public abstract class OAuthHandler implements AuthHandler {

private final String id;

private final String tokenApiUrl;
private final String clientId;
private final String clientSecret;
Expand Down Expand Up @@ -89,7 +88,7 @@ public void setAuthHeader(MessageContext messageContext) throws AuthException {
private String getToken(final MessageContext messageContext) throws AuthException {

try {
return TokenCache.getInstance().getToken(id, new Callable<String>() {
return TokenCache.getInstance().getToken(getId(messageContext), new Callable<String>() {
@Override
public String call() throws AuthException, IOException {
return OAuthClient.generateToken(OAuthUtils.resolveExpression(tokenApiUrl, messageContext),
Expand Down Expand Up @@ -129,12 +128,20 @@ public int compare(String o1, String o2) {
}
}

/**
* Method to remove the token from the cache when the token is invalid.
*/
public void removeTokenFromCache(MessageContext messageContext) throws AuthException {

TokenCache.getInstance().removeToken(getId(messageContext));
}

/**
* Method to remove the token from the cache when the endpoint is destroyed.
*/
public void removeTokenFromCache() {
public void removeTokensFromCache() {

TokenCache.getInstance().removeToken(id);
TokenCache.getInstance().removeTokens(id.concat("_"));
}

/**
Expand Down Expand Up @@ -313,7 +320,7 @@ public void setCustomHeaders(Map<String, String> customHeadersMap) {
* @param messageContext Message Context of the request which will be used to resolve dynamic expressions
* @return Map<String, String> Resolved custom headers
*/
private Map<String, String> getResolvedCustomHeadersMap(Map<String, String> customHeadersMap,
protected Map<String, String> getResolvedCustomHeadersMap(Map<String, String> customHeadersMap,
MessageContext messageContext) throws AuthException {

Map<String, String> resolvedCustomHeadersMap = null;
Expand All @@ -339,4 +346,9 @@ public int getSocketTimeout() {
return socketTimeout;
}

protected abstract int getHash(MessageContext messageContext) throws AuthException;

private String getId(MessageContext messageContext) throws AuthException {
return id.concat("_").concat(String.valueOf(getHash(messageContext)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.synapse.endpoints.auth.AuthConstants;
import org.apache.synapse.endpoints.auth.AuthException;

import java.util.Objects;

/**
* This class is used to handle Password Credentials grant oauth.
*/
Expand Down Expand Up @@ -78,6 +80,16 @@ protected OMElement serializeSpecificOAuthConfigs(OMFactory omFactory) {
return passwordCredentials;
}

@Override
protected int getHash(MessageContext messageContext) throws AuthException {
return Objects.hash(messageContext.getTo().getAddress(), OAuthUtils.resolveExpression(getTokenUrl(), messageContext),
OAuthUtils.resolveExpression(getClientId(), messageContext), OAuthUtils.resolveExpression(getClientSecret(),
messageContext), OAuthUtils.resolveExpression(getUsername(), messageContext),
OAuthUtils.resolveExpression(getPassword(), messageContext),
getRequestParametersAsString(messageContext), getResolvedCustomHeadersMap(getCustomHeadersMap(),
messageContext));
}

public String getUsername() {

return username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public String getToken(String id, Callable<String> callable) throws ExecutionExc
}

/**
* This method is called to remove the token from the cache when the endpoint is destroyed
* This method is called to remove the token from the cache when the token is invalid
*
* @param id id of the endpoint
*/
Expand All @@ -91,4 +91,12 @@ public void removeToken(String id) {
tokenMap.invalidate(id);
}

/**
* This method is called to remove the tokens from the cache when the endpoint is destroyed
*
* @param oauthHandlerId id of the OAuth handler bounded to the endpoint
*/
public void removeTokens(String oauthHandlerId) {
tokenMap.asMap().entrySet().removeIf(entry -> entry.getKey().startsWith(oauthHandlerId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,33 @@ private static String replaceValue(MessageContext messageContext, String text, b
Matcher matcher = EXPRESSION_PATTERN.matcher(text);
while (matcher.find()) {
String matchSeq = matcher.group();
String surroundedString;
try {
surroundedString = text.substring(text.indexOf(matchSeq) - 1, text.indexOf(matchSeq) + matchSeq
.length() + 1);
} catch (IndexOutOfBoundsException e) {
// catch index out of bound exception when the expression is at the beginning or end of the text
surroundedString = StringUtils.EMPTY;
}
String value = getDynamicValue(messageContext, matchSeq.substring(1, matchSeq.length() - 1));
if (value == null) {
value = StringUtils.EMPTY;
}
// If the string is neither XML or JSON, it is considered a String and must be wrapped in double quotes
// If it is an empty string returned from a json-eval expression it must be wrapped in double quotes
if (isInline && ((value.isEmpty() && matchSeq.contains(EXPRESSION_JSON_EVAL))
|| (!isValidXML(value) && !isValidJson(value)))) {
|| (!isValidXML(value) && !isValidJson(value) && !isSurroundedByQuotes(surroundedString)))) {
value = "\"" + value + "\"";
}
text = text.replace(matchSeq, value);
}
return text;
}

private static boolean isSurroundedByQuotes(String text) {

return text.startsWith("\"") && text.endsWith("\"");
}
/**
* Replaces Dynamic Values represented by expressions inside json-eval.
*
Expand Down
2 changes: 1 addition & 1 deletion modules/coverage-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.synapse</groupId>
<artifactId>Apache-Synapse</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.synapse</groupId>
<artifactId>Apache-Synapse</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/experimental/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.synapse</groupId>
<artifactId>Apache-Synapse</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.synapse</groupId>
<artifactId>Apache-Synapse</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<parent>
<artifactId>synapse-features</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<groupId>org.apache.synapse</groupId>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<parent>
<artifactId>synapse-features</artifactId>
<version>4.0.0-wso2v72-SNAPSHOT</version>
<version>4.0.0-wso2v79-SNAPSHOT</version>
<groupId>org.apache.synapse</groupId>
</parent>

Expand Down
Loading

0 comments on commit bcbad36

Please sign in to comment.