From 90ab2ddaebb6056b8b5d4306a614ade5d2f1b56f Mon Sep 17 00:00:00 2001 From: Guobiao Mo Date: Tue, 8 May 2018 14:31:26 -0700 Subject: Fixed issue in extracting package name from rule Issue-ID: HOLMES-133 Change-Id: I4db09e952e1e9dcba3d2d0398b26b4229aefef20 Signed-off-by: Guobiao Mo --- .../holmes/engine/resources/EngineResources.java | 26 ++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'engine-d/src/main/java/org/onap') diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java index b9875da..64d727e 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java @@ -24,6 +24,9 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; @@ -51,7 +54,8 @@ import org.onap.holmes.engine.response.CorrelationRuleResponse; @Produces(MediaType.APPLICATION_JSON) @Slf4j public class EngineResources { - + private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*"); + @Inject DroolsEngine droolsEngine; @@ -70,6 +74,10 @@ public class EngineResources { Locale locale = LanguageUtil.getLocale(httpRequest); try { String packageName = getPackageName(deployRuleRequest.getContent()); + if(packageName == null) { + throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent()); + } + DmaapService.loopControlNames .put(packageName, deployRuleRequest.getLoopControlName()); String packageNameRet = droolsEngine.deployRule(deployRuleRequest, locale); @@ -79,8 +87,8 @@ public class EngineResources { DmaapService.loopControlNames .put(packageNameRet, deployRuleRequest.getLoopControlName()); } - log.info("Rule deployed. Package name: " + packageName); - crResponse.setPackageName(packageName); + log.info("Rule deployed. Package name: " + packageNameRet); + crResponse.setPackageName(packageNameRet); } catch (CorrelationException correlationException) { log.error(correlationException.getMessage(), correlationException); @@ -131,10 +139,14 @@ public class EngineResources { } return true; } - + private String getPackageName(String contents){ - String ret = contents.trim(); - ret = ret.substring(7, ret.indexOf("import")).trim(); - return ret.endsWith(";") ? ret.substring(0, ret.length() - 1) : ret; + Matcher m = packagePattern.matcher(contents); + + if (m.find( )) { + return m.group(1); + }else { + return null; + } } } -- cgit 1.2.3-korg