Skip to content

Commit

Permalink
Merge branch 'bradsdavis-static-ip-addresses'
Browse files Browse the repository at this point in the history
  • Loading branch information
jsight committed Feb 4, 2015
2 parents c76972b + fc7105a commit 35398e1
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 24 deletions.
Expand Up @@ -46,8 +46,7 @@ public Class<? extends RulePhase> getPhase()
@Override
public Configuration getConfiguration(GraphContext context)
{
ConditionBuilder applicationProjectModelsFound = Query
.fromType(WindupConfigurationModel.class);
ConditionBuilder applicationProjectModelsFound = Query.fromType(WindupConfigurationModel.class);

AbstractIterationOperation<WindupConfigurationModel> addReport = new AbstractIterationOperation<WindupConfigurationModel>()
{
Expand Down
@@ -0,0 +1,103 @@
package org.jboss.windup.rules.apps.java.ip;

import java.util.HashMap;
import java.util.Map;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.WindupRuleProvider;
import org.jboss.windup.config.operation.GraphOperation;
import org.jboss.windup.config.phase.ReportGeneration;
import org.jboss.windup.config.phase.RulePhase;
import org.jboss.windup.config.query.Query;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.model.ProjectModel;
import org.jboss.windup.graph.model.WindupConfigurationModel;
import org.jboss.windup.graph.model.WindupVertexFrame;
import org.jboss.windup.graph.service.GraphService;
import org.jboss.windup.graph.service.WindupConfigurationService;
import org.jboss.windup.reporting.model.ApplicationReportModel;
import org.jboss.windup.reporting.model.TemplateType;
import org.jboss.windup.reporting.model.WindupVertexListModel;
import org.jboss.windup.reporting.service.ApplicationReportService;
import org.jboss.windup.reporting.service.ReportService;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.context.EvaluationContext;

/**
* Finds files that contain potential static IP addresses, determined by regular expression.
*
* @author <a href="mailto:bradsdavis@gmail.com">Brad Davis</a>
*/
public class CreateStaticIPAddressReportRuleProvider extends WindupRuleProvider
{
private static final String TITLE = "Static IP Addresses";
public static final String TEMPLATE_REPORT = "/reports/templates/static_ip_addresses.ftl";

@Override
public Class<? extends RulePhase> getPhase()
{
return ReportGeneration.class;
}

@Override
public Configuration getConfiguration(final GraphContext graphContext)
{

return ConfigurationBuilder
.begin()
.addRule()
// when a IP Location Model exists...
.when(Query.fromType(StaticIPLocationModel.class))
// perform the write of this report once (GraphOperation)...
.perform(new GraphOperation()
{

@Override
public void perform(GraphRewrite event, EvaluationContext context)
{
// configuration of current execution
WindupConfigurationModel configurationModel = WindupConfigurationService.getConfigurationModel(event.getGraphContext());

// reference to input project model
ProjectModel projectModel = configurationModel.getInputPath().getProjectModel();
createIPReport(event.getGraphContext(), projectModel);
}
});
}

private ApplicationReportModel createIPReport(GraphContext context, ProjectModel rootProjectModel)
{
ApplicationReportService applicationReportService = new ApplicationReportService(context);

// create a reference in the graph to the static ip location report.
ApplicationReportModel applicationReport = applicationReportService.create();

applicationReport.setReportPriority(600);
applicationReport.setReportName(TITLE);
applicationReport.setTemplatePath(TEMPLATE_REPORT);
applicationReport.setDisplayInApplicationReportIndex(true);
applicationReport.setTemplateType(TemplateType.FREEMARKER);
applicationReport.setProjectModel(rootProjectModel);

// find all IPLocationModels
GraphService<StaticIPLocationModel> ipLocationModelService = new GraphService<StaticIPLocationModel>(context, StaticIPLocationModel.class);
Iterable<StaticIPLocationModel> results = ipLocationModelService.findAll();

Map<String, WindupVertexFrame> relatedData = new HashMap<>(1);
WindupVertexListModel staticIPList = new GraphService<WindupVertexListModel>(context, WindupVertexListModel.class).create();
for (StaticIPLocationModel location : results)
{
staticIPList.addItem(location);
}
relatedData.put("staticIPLocations", staticIPList);
applicationReport.setRelatedResource(relatedData);

// performs methods on the graph to create a unique file name.
ReportService reportService = new ReportService(context);

// uses project model's name for the report name.
reportService.setUniqueFilename(applicationReport, "static_ips" + rootProjectModel.getName(), "html");
return applicationReport;
}
}
@@ -0,0 +1,45 @@
package org.jboss.windup.rules.apps.java.ip;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.WindupRuleProvider;
import org.jboss.windup.config.operation.ruleelement.AbstractIterationOperation;
import org.jboss.windup.config.phase.MigrationRules;
import org.jboss.windup.config.phase.RulePhase;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.service.GraphService;
import org.jboss.windup.rules.files.condition.FileContent;
import org.jboss.windup.rules.files.model.FileLocationModel;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.context.EvaluationContext;

/**
* Finds files that contain potential static IP addresses, determined by regular
* expression.
*
* @author <a href="mailto:bradsdavis@gmail.com">Brad Davis</a>
*/
public class DiscoverStaticIPAddressRuleProvider extends WindupRuleProvider {
@Override
public Class<? extends RulePhase> getPhase() {
return MigrationRules.class;
}

@Override
public Configuration getConfiguration(GraphContext context) {

return ConfigurationBuilder
.begin()
.addRule()
.when(FileContent.matches("{ip}").inFilesNamed("{*}.{type}"))
.perform(new AbstractIterationOperation<FileLocationModel>() {
public void perform(GraphRewrite event, EvaluationContext context, FileLocationModel payload) {
//for all file location models that match the regular expression in the where clause, add the IP Location Model to the graph
GraphService.addTypeToModel(event.getGraphContext(), payload, StaticIPLocationModel.class);
};
})
.where("ip").matches("\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b")
.where("type").matches("java|properties|xml");
}

}
@@ -0,0 +1,15 @@
package org.jboss.windup.rules.apps.java.ip;

import org.jboss.windup.rules.files.model.FileLocationModel;

import com.tinkerpop.frames.modules.typedgraph.TypeValue;

/**
* Contains a Java package name
*
*/
@TypeValue(StaticIPLocationModel.TYPE)
public interface StaticIPLocationModel extends FileLocationModel
{
String TYPE = "StaticIPLocationModel";
}
Expand Up @@ -93,19 +93,15 @@ private IgnoredFilesReportModel createIgnoredFilesReport(GraphContext context,
GraphService<IgnoredFileModel> ignoredFilesModelService = new GraphService<IgnoredFileModel>(context,
IgnoredFileModel.class);
Iterable<IgnoredFileModel> allIgnoredFiles = ignoredFilesModelService.findAll();
List<IgnoredFileModel> returnIgnoredFiles = new ArrayList<>();
for (IgnoredFileModel file : allIgnoredFiles)
{
List<String> allProjectPaths = getAllFatherProjectPaths(file.getProjectModel());
if (allProjectPaths.contains(rootProjectModel.getRootFileModel().getFilePath()))
{
returnIgnoredFiles.add(file);
ignoredFilesReportModel.addIgnoredFile(file);
}
}
for (IgnoredFileModel ignored : returnIgnoredFiles)
{
ignoredFilesReportModel.addIgnoredFile(ignored);
}

for (IgnoredFileRegexModel ignoreRegexModel : javaCfg.getIgnoredFileRegexes())
{
ignoredFilesReportModel.addFileRegex(ignoreRegexModel);
Expand Down
@@ -0,0 +1,87 @@
<!DOCTYPE html>
<html lang="en">

<#assign applicationReportIndexModel = reportModel.applicationReportIndexModel>

<#macro tagRenderer tag>
<span class="label label-${tag.level.name()?lower_case}"><#nested/></span>
</#macro>

<#macro staticIpFileRenderer reportModel>
<div class="panel panel-primary">
<table class="table table-striped table-bordered" id="staticIPTable">
<tr>
<th>Path</th>
<th>Location</th>
<th>IP Address</th>
</tr>

<#list reportModel.relatedResources.staticIPLocations.list.iterator() as staticIpRef>
<tr>
<td> <#if staticIpRef.file.prettyPath?has_content> ${staticIpRef.file.prettyPath} </#if> </td>
<td> <#if staticIpRef.lineNumber?has_content>Line Number ${staticIpRef.lineNumber}, </#if><#if staticIpRef.columnNumber?has_content>Column Number ${staticIpRef.columnNumber} </#if> </td>
<td> <#if staticIpRef.sourceSnippit?has_content> ${staticIpRef.sourceSnippit} </#if> </td>
</tr>
</#list>
</table>
</div>

</#macro>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>${reportModel.projectModel.name} - Static IP Address Files</title>
<link href="resources/css/bootstrap.min.css" rel="stylesheet">
<link href="resources/css/windup.css" rel="stylesheet" media="screen">
<link href="resources/css/windup.java.css" rel="stylesheet" media="screen">
</head>
<body role="document">

<!-- Fixed navbar -->
<div class="navbar-fixed-top windup-bar" role="navigation">
<div class="container theme-showcase" role="main">
<img src="resources/img/windup-logo.png" class="logo"/>
</div>
</div>

<div class="container" role="main">
<div class="row">
<div class="page-header page-header-no-border">
<h1>Files Containing Static IPs for <span class="slash">/</span><small style="margin-left: 20px; font-weight: 100;">${reportModel.projectModel.name}</small></h1>
<div class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ol class="breadcrumb top-menu">
<li><a href="../index.html">All Applications</a></li>
<#include "include/breadcrumbs.ftl">
</ol>

</div><!-- /.nav-collapse -->
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav">
<#include "include/navbar.ftl">
</ul>
</div><!-- /.nav-collapse -->
</div>
</div>
</div>

<div class="container theme-showcase" role="main">
<@staticIpFileRenderer reportModel />
</div>
</div> <!-- /container -->


<script src="resources/js/jquery-1.10.1.min.js"></script>
<script src="resources/libraries/flot/jquery.flot.min.js"></script>
<script src="resources/libraries/flot/jquery.flot.pie.min.js"></script>
<script src="resources/js/bootstrap.min.js"></script>
</body>
</html>
@@ -1,7 +1,5 @@
package org.jboss.windup.testutil.html;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

Expand Down Expand Up @@ -46,7 +44,6 @@ public boolean checkBeanInReport(EJBType ejbType, String beanName, String classN
{
throw new CheckFailedException("Unable to find ejb beans table element");
}
List<WebElement> rowElements = element.findElements(By.xpath(".//tr"));
return checkValueInTable(element, beanName, className);
}
}
Expand Up @@ -38,22 +38,30 @@ protected WebDriver getDriver()
/**
* Checks that the table contains a row with the given first two columns
*/
boolean checkValueInTable(WebElement element, String column1Expected, String column2Expected)
boolean checkValueInTable(WebElement element, String... columnValues)
{
List<WebElement> rowElements = element.findElements(By.xpath(".//tr"));
boolean foundExpectedResult = false;
for (WebElement rowElement : rowElements)
{
List<WebElement> td1Elements = rowElement.findElements(By.xpath(".//td[position() = 1]"));
List<WebElement> td2Elements = rowElement.findElements(By.xpath(".//td[position() = 2]"));
if (td1Elements.size() != 1 || td2Elements.size() != 1)
boolean rowMatches = true;
for (int i = 0; i < columnValues.length; i++)
{
continue;
String expectedValue = columnValues[i];
List<WebElement> tdElements = rowElement.findElements(By.xpath(".//td[position() = " + (i + 1) + "]"));
if (tdElements.size() != 1)
{
rowMatches = false;
break;
}
String actualValue = tdElements.get(0).getText().trim();
if (!actualValue.equals(expectedValue))
{
rowMatches = false;
break;
}
}

String column1 = td1Elements.get(0).getText().trim();
String column2 = td2Elements.get(0).getText().trim();
if (column1.equals(column1Expected) && column2.equals(column2Expected))
if (rowMatches)
{
foundExpectedResult = true;
break;
Expand Down
@@ -1,7 +1,5 @@
package org.jboss.windup.testutil.html;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

Expand All @@ -14,7 +12,7 @@
public class TestSpringBeanReportUtil extends TestReportUtil
{
/**
* Checks that a Spring Bean is listed with the given name and classname
* Checks that for the given filename, location, and IP
*/
public boolean checkSpringBeanInReport(String beanName, String className)
{
Expand All @@ -23,7 +21,6 @@ public boolean checkSpringBeanInReport(String beanName, String className)
{
throw new CheckFailedException("Unable to spring beans table element");
}
List<WebElement> rowElements = element.findElements(By.xpath(".//tr"));
return checkValueInTable(element, beanName, className);
}
}
@@ -0,0 +1,24 @@
package org.jboss.windup.testutil.html;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**
* Tests the contents of the static ip report
*/
public class TestStaticIPReportUtil extends TestReportUtil
{

/**
* Checks that a sis listed with the given filename, location, and IP
*/
public boolean checkStaticIPInReport(String filename, String locationInFile, String ip)
{
WebElement element = getDriver().findElement(By.id("staticIPTable"));
if (element == null)
{
throw new CheckFailedException("Unable to find static IP table element");
}
return checkValueInTable(element, filename, locationInFile, ip);
}
}

0 comments on commit 35398e1

Please sign in to comment.