Skip to content

Commit

Permalink
Introduced Apache Freemaker as the template engine instead of Apache …
Browse files Browse the repository at this point in the history
…Velocity
  • Loading branch information
rnavagamuwa committed Mar 29, 2019
1 parent ebfdba7 commit 2a8f10b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
4 changes: 2 additions & 2 deletions sample/src/main/resources/xacmlConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"Attribute": [
{
"AttributeId": "urn:oasis:names:tc:xacml:1.0:action:action-id",
"Value": "$action-id"
"Value": "${actionid}"
}
]
},
"Resource": {
"Attribute": [
{
"AttributeId": "urn:oasis:names:tc:xacml:1.0:resource:resource-id",
"Value": "$resource-id"
"Value": "${resourceid}"
}
]
}
Expand Down
12 changes: 5 additions & 7 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
<version>${org.json.version}</version>
</dependency>

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>${org.apache.velocity.version}</version>
</dependency>

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
Expand All @@ -64,10 +58,14 @@
<artifactId>spring-ws-core</artifactId>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>

</dependencies>
<properties>
<commons-io.version>2.6</commons-io.version>
<org.json.version>20180813</org.json.version>
<org.apache.velocity.version>1.7</org.apache.velocity.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package org.wso2.spring.security.abac.util;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.json.JSONObject;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.wso2.spring.security.abac.cache.Cache;
import org.wso2.spring.security.abac.cache.EhCacheManager;
import org.wso2.spring.security.abac.exception.AttributeEvaluatorException;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
* @author Randika Navagamuwa
Expand Down Expand Up @@ -44,26 +45,29 @@ public String createAuthRequest(String policyName, String jsonKeyValuePairs) {
return cachedRequest;
}

VelocityContext vc = generateVelocityData(jsonKeyValuePairs);

String xacmlRequest;
try {
try (StringWriter out = new StringWriter()) {

Configuration cfg = new Configuration(new Version("2.3.23"));

cfg.setClassForTemplateLoading(this.getClass(), "/");
cfg.setDefaultEncoding("UTF-8");

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
ResourceUtils.getFile("classpath:" + ATTRIBUTE_CONFIG_FILE_NAME).getParent());
Template template = velocityEngine.getTemplate(ATTRIBUTE_CONFIG_FILE_NAME);
velocityEngine.init();
Template template = cfg.getTemplate(ATTRIBUTE_CONFIG_FILE_NAME);

StringWriter writer = new StringWriter();
template.merge(vc, writer);
Map<String, Object> templateData = generateFreemakeTemplateData(jsonKeyValuePairs);

xacmlRequest = new JSONObject(writer.toString()).get(policyName).toString();
template.process(templateData, out);
xacmlRequest = new JSONObject(out.toString()).get(policyName).toString();

} catch (IOException e) {
out.flush();


} catch (IOException | TemplateException e) {

throw new AttributeEvaluatorException("Failed to build the XACML Json request for policy with name : " +
policyName, e);

}

if (xacmlRequest == null || xacmlRequest.isEmpty()) {
Expand All @@ -75,8 +79,7 @@ public String createAuthRequest(String policyName, String jsonKeyValuePairs) {
return this.requestBuilderCache.putIfAbsent(key, xacmlRequest);
}

private VelocityContext generateVelocityData(String jsonKeyValuePairs) {

private Map<String, Object> generateFreemakeTemplateData(String jsonKeyValuePairs) {
JSONObject jsonObject = new JSONObject(jsonKeyValuePairs.trim());

Iterator<String> keys = jsonObject.keys();
Expand All @@ -85,15 +88,15 @@ private VelocityContext generateVelocityData(String jsonKeyValuePairs) {
RequestContextHolder.currentRequestAttributes();
HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();

VelocityContext velocityContext = new VelocityContext();
Map<String, Object> templateData = new HashMap<>();

while (keys.hasNext()) {
String key = keys.next();
String value = jsonObject.get(key).toString();
velocityContext.put(key, httpServletRequest.getHeader(value));
templateData.put(key, httpServletRequest.getHeader(value));
}

return velocityContext;
return templateData;
}

}

0 comments on commit 2a8f10b

Please sign in to comment.