Skip to content

Commit

Permalink
Merge pull request #2226 from kalaiyarasiganeshalingam/master
Browse files Browse the repository at this point in the history
Add config resolver
  • Loading branch information
kalaiyarasiganeshalingam authored Nov 20, 2024
2 parents 8efead3 + 890bc23 commit bb3dc07
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.commons.property;

import java.util.concurrent.ConcurrentHashMap;

/**
* Property Holder is the class used to store the properties in the map
*/
public class PropertyHolder {

private ConcurrentHashMap<String, String> properties;

private static class PropertyLoaderHelper {
private static final PropertyHolder INSTANCE = new PropertyHolder();
}

private PropertyHolder() {}

public static PropertyHolder getInstance() {
return PropertyLoaderHelper.INSTANCE;
}

public void setProperty(String key, String value) {
getProperties().put(key, value);
}

public String getPropertyValue(String key) {
return getProperties().get(key);
}

public Boolean hasKey(String key) {
return getProperties().containsKey(key);
}

public ConcurrentHashMap<String, String> getProperties() {
if (properties == null) {
this.properties = new ConcurrentHashMap<>();
}
return this.properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.commons.resolvers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.commons.property.PropertyHolder;

/**
* Config Resolver can be used to resolve configurable property variables in the synapse config.
*/
public class ConfigResolver implements Resolver {

private static final Log LOG = LogFactory.getLog(FilePropertyResolver.class);

private String input;

@Override
public void setVariable(String input) {
this.input = input;
}

@Override
public String resolve() {
String propertyValue = PropertyHolder.getInstance().getPropertyValue(this.input);
if (propertyValue == null) {
throw new ResolverException("Parameter key: " + input + " could not be found");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Resolving parameter key: "+ input + " value: " + propertyValue);
}
return propertyValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public class ResolverFactory {

private static final Log LOG = LogFactory.getLog(ResolverFactory.class);
private static final int RESOLVER_INDEX = 2;
private static ResolverFactory resolverFactory = new ResolverFactory();
private static final ResolverFactory resolverFactory = new ResolverFactory();
private final Pattern rePattern = Pattern.compile("(\\$)([_a-zA-Z0-9]+):([_a-zA-Z0-9]+)");
private static final String SYSTEM_VARIABLE_PREFIX = "$SYSTEM";
private static final String FILE_PROPERTY_VARIABLE_PREFIX = "$FILE";
private static final String CUSTOM_PROPERTY_VARIABLE_PREFIX = "$CUSTOM_";
private static final String CONFIGURABLE_VARIABLE_PREFIX = "$config:";

private Map<String, Class<? extends Resolver>> resolverMap = new HashMap<>();
private final Map<String, Class<? extends Resolver>> resolverMap = new HashMap<>();

/**
* This function return an object of Resolver factory
Expand All @@ -61,58 +62,25 @@ public Resolver getResolver(String input) {
if (input == null) {
return null;
}

if (input.startsWith(SYSTEM_VARIABLE_PREFIX)) {
Matcher matcher = rePattern.matcher(input);
Resolver resolverObject = null;
Matcher matcher = rePattern.matcher(input);
if (input.startsWith(CONFIGURABLE_VARIABLE_PREFIX)) {
if (matcher.find()) {
Class<? extends Resolver> resolverClass = resolverMap.get(matcher.group(RESOLVER_INDEX).toLowerCase());
if (resolverClass != null) {
try {
resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be found");
}
} else {
throw new ResolverException("Resolver could not be found");
}
return getResolver(matcher.group(RESOLVER_INDEX).toLowerCase(), matcher);
}
} else if(input.startsWith(FILE_PROPERTY_VARIABLE_PREFIX)){
Matcher matcher = rePattern.matcher(input);
Resolver resolverObject = null;
if (matcher.find()){
Class<? extends Resolver> resolverClass = resolverMap.get(matcher.group(RESOLVER_INDEX).toLowerCase());
if (resolverClass != null) {
try {
resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be initialized", e);
}
} else {
throw new ResolverException("Resolver could not be found");
}
} else if (input.startsWith(SYSTEM_VARIABLE_PREFIX)) {
if (matcher.find()) {
return getResolver(matcher.group(RESOLVER_INDEX).toLowerCase(), matcher);
}
} else if(input.startsWith(FILE_PROPERTY_VARIABLE_PREFIX)) {
if (matcher.find()) {
return getResolver(matcher.group(RESOLVER_INDEX).toLowerCase(), matcher);
}
} else if(input.startsWith(CUSTOM_PROPERTY_VARIABLE_PREFIX)) {
Matcher matcher = rePattern.matcher(input);
Resolver resolverObject = null;
if (matcher.find()){
String nameWithPlaceholder = matcher.group(RESOLVER_INDEX).toLowerCase();
String className = nameWithPlaceholder.substring(CUSTOM_PROPERTY_VARIABLE_PREFIX.length() - 1);
Class<? extends Resolver> resolverClass = resolverMap.get(className);
if (resolverClass != null) {
try {
resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be initialized", e);
}
} else {
throw new ResolverException("Resolver could not be found");
if (matcher.find()) {
return getResolver(className, matcher);
}
}
}
Expand All @@ -126,6 +94,7 @@ public Resolver getResolver(String input) {
private void registerResolvers() {
resolverMap.put("system", SystemResolver.class);
resolverMap.put("file", FilePropertyResolver.class);
resolverMap.put("config", ConfigResolver.class);
}

private void registerExterns() {
Expand All @@ -147,4 +116,19 @@ private void registerExterns() {
}
}
}

private Resolver getResolver(String className, Matcher matcher) {
Class<? extends Resolver> resolverClass = resolverMap.get(className);
if (resolverClass != null) {
try {
Resolver resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be initialized", e);
}
} else {
throw new ResolverException("Resolver could not be found");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import org.apache.synapse.commons.SynapseCommonsException;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -101,7 +102,7 @@ private void loadPropertiesFile() throws SynapseCommonsException {
File file = new File(propertiesFilePath);
if (file.exists()) {
if (file.lastModified() > lastModifiedTimestamp) {
try (InputStream in = new FileInputStream(propertiesFilePath)) {
try (InputStream in = Files.newInputStream(Paths.get(propertiesFilePath))) {
Properties rawProps = new Properties();
Map<String, String> tempPropertyMap = new HashMap<>();
rawProps.load(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public int compare(String o1, String o2) {
org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS,
headersMap);
}
}else if(XMLConfigConstants.SCOPE_OPERATION.equals(scope)
} else if(XMLConfigConstants.SCOPE_OPERATION.equals(scope)
&& synCtx instanceof Axis2MessageContext){
//Setting Transport Headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
Expand Down Expand Up @@ -504,7 +504,7 @@ private String getMatchedValue(String value, SynapseLog synLog) {
matchedValue = matcher.group(group);
} else {
if (synLog.isTraceOrDebugEnabled()) {
String msg = "Failed to get a match for regx : " +
String msg = "Failed to get a match for regex : " +
pattern.toString() + " with the property value :" +
value + " for group :" + group;
synLog.traceOrDebug(msg);
Expand All @@ -515,7 +515,7 @@ private String getMatchedValue(String value, SynapseLog synLog) {

} else {
if (synLog.isTraceOrDebugEnabled()) {
String msg = "Unable to find a match for regx : " +
String msg = "Unable to find a match for regex : " +
pattern.toString() + " with the property value :" + value;
synLog.traceOrDebug(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.synapse.util.synapse.expression.ast;

import org.apache.synapse.SynapseConstants;
import org.apache.synapse.commons.property.PropertyHolder;
import org.apache.synapse.commons.resolvers.ResolverException;
import org.apache.synapse.util.synapse.expression.context.EvaluationContext;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;

Expand Down Expand Up @@ -60,6 +62,12 @@ public ExpressionResult evaluate(EvaluationContext context) {
Object value;
if (Type.HEADER.equals(type)) {
value = context.getHeader(name);
} else if (Type.CONFIG.equals(type)) {
try {
value = PropertyHolder.getInstance().getPropertyValue(name);
} catch (ResolverException e) {
throw new EvaluationException("The value of the key:[" + name + "] is null");
}
} else {
if (SynapseConstants.URI_PARAM.equals(scope)) {
value = context.getProperty("uri.var." + name, SynapseConstants.SYNAPSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ private SSLContext createSSLContext(OMElement keyStoreElt, OMElement trustStoreE
if (log.isDebugEnabled()) {
log.debug(name + " Loading Trust Keystore from : " + location);
}

SslSenderTrustStoreHolder.getInstance().setLocation(location);
SslSenderTrustStoreHolder.getInstance().setPassword(passwordElement.getText());
SslSenderTrustStoreHolder.getInstance().setType(type);
trustStore.load(fis, storePassword.toCharArray());
TrustManagerFactory trustManagerfactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.transport.nhttp.config;

/**
* The SSL Sender TrustStore Holder class to store the client trust store's configurable details.
*/
public class SslSenderTrustStoreHolder {

private static volatile SslSenderTrustStoreHolder instance;

private SslSenderTrustStoreHolder() {}

private String location;
private String password;
private String type;

public static SslSenderTrustStoreHolder getInstance() {

if (instance == null) {
synchronized (TrustStoreHolder.class) {
if (instance == null) {
instance = new SslSenderTrustStoreHolder();
}
}
}
return instance;
}

public void setLocation(String location) {
this.location = location;
}

public String getLocation() {
return this.location;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return this.password;
}

public void setType(String type) {
this.type = type;
}

public String getType() {
return this.type;
}
}

0 comments on commit bb3dc07

Please sign in to comment.