Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
Signed-off-by: mehab <[email protected]>
  • Loading branch information
mehab committed Sep 28, 2023
1 parent 3d35066 commit a207295
Show file tree
Hide file tree
Showing 18 changed files with 336 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.FetchStatus;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.persistence.QueryManager;

import java.time.Instant;
import java.util.Date;

public abstract class AbstractMetaHandler implements Handler {

ComponentProjection componentProjection;
QueryManager queryManager;
KafkaEventDispatcher kafkaEventDispatcher;


public static IntegrityMetaComponent createIntegrityMetaComponent(Component component) {
IntegrityMetaComponent integrityMetaComponent1 = new IntegrityMetaComponent();
integrityMetaComponent1.setStatus(FetchStatus.IN_PROGRESS);
integrityMetaComponent1.setPurl(component.getPurl().toString());
integrityMetaComponent1.setLastFetch(Date.from(Instant.now()));
return integrityMetaComponent1;
}

public static IntegrityMetaComponent createIntegrityMetaComponent(ComponentProjection component) {
IntegrityMetaComponent integrityMetaComponent1 = new IntegrityMetaComponent();
integrityMetaComponent1.setStatus(FetchStatus.IN_PROGRESS);
integrityMetaComponent1.setPurl(component.purl());
integrityMetaComponent1.setLastFetch(Date.from(Instant.now()));
return integrityMetaComponent1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.dependencytrack.componentmeta;

public record ComponentProjection(String purlCoordinates, Boolean internal, String purl) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.persistence.QueryManager;

public class ComposerMetaHandler extends AbstractMetaHandler{
public ComposerMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.persistence.QueryManager;

public class CpanMetaHandler extends AbstractMetaHandler {

public CpanMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.persistence.QueryManager;

public class GemMetaHandler extends AbstractMetaHandler{
public GemMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
}
19 changes: 19 additions & 0 deletions src/main/java/org/dependencytrack/componentmeta/GoMetaHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.persistence.QueryManager;

public class GoMetaHandler extends AbstractMetaHandler {

public GoMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/dependencytrack/componentmeta/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.dependencytrack.componentmeta;

public interface Handler {
void handle();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.persistence.QueryManager;

public class HexMetaHandler extends AbstractMetaHandler {
public HexMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.model.FetchStatus;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.persistence.QueryManager;

import java.time.Instant;
import java.util.Date;

public class MavenMetaHandler extends AbstractMetaHandler {


public MavenMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
IntegrityMetaComponent integrityMetaComponent = queryManager.getIntegrityMetaComponent(componentProjection.purl());
if (integrityMetaComponent != null) {
if (integrityMetaComponent.getStatus() == null || (integrityMetaComponent.getStatus() == FetchStatus.IN_PROGRESS && Date.from(Instant.now()).getTime()-integrityMetaComponent.getLastFetch().getTime()>60 * 60 * 1000L)) {
integrityMetaComponent.setLastFetch(Date.from(Instant.now()));
queryManager.updateIntegrityMetaComponent(integrityMetaComponent);
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true));
} else {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
} else {
queryManager.createIntegrityMetaComponent(AbstractMetaHandler.createIntegrityMetaComponent(componentProjection));
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.model.FetchStatus;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.persistence.QueryManager;

import java.time.Instant;
import java.util.Date;

public class NpmMetaHandler extends AbstractMetaHandler {

public NpmMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
IntegrityMetaComponent integrityMetaComponent = queryManager.getIntegrityMetaComponent(componentProjection.purl());
if (integrityMetaComponent != null) {
if (integrityMetaComponent.getStatus() == null || (integrityMetaComponent.getStatus() == FetchStatus.IN_PROGRESS && Date.from(Instant.now()).getTime()-integrityMetaComponent.getLastFetch().getTime()>60 * 60 * 1000L)) {
integrityMetaComponent.setLastFetch(Date.from(Instant.now()));
queryManager.updateIntegrityMetaComponent(integrityMetaComponent);
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true));
} else {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
} else {
queryManager.createIntegrityMetaComponent(AbstractMetaHandler.createIntegrityMetaComponent(componentProjection));
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.persistence.QueryManager;

public class NugetMetaHandler extends AbstractMetaHandler{
public NugetMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.dependencytrack.componentmeta;

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.model.FetchStatus;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.persistence.QueryManager;

import java.time.Instant;
import java.util.Date;

public class PypiMetaHandler extends AbstractMetaHandler {
public PypiMetaHandler(ComponentProjection component, QueryManager qm, KafkaEventDispatcher kafkaEventDispatcher) {
this.componentProjection = component;
this.queryManager = qm;
this.kafkaEventDispatcher = kafkaEventDispatcher;
}

@Override
public void handle() {
IntegrityMetaComponent integrityMetaComponent = queryManager.getIntegrityMetaComponent(componentProjection.purl());
if (integrityMetaComponent != null) {
if (integrityMetaComponent.getStatus() == null || (integrityMetaComponent.getStatus() == FetchStatus.IN_PROGRESS && Date.from(Instant.now()).getTime()-integrityMetaComponent.getLastFetch().getTime()>60 * 60 * 1000L)) {
integrityMetaComponent.setLastFetch(Date.from(Instant.now()));
queryManager.updateIntegrityMetaComponent(integrityMetaComponent);
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true));
} else {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false));
}
} else {
queryManager.createIntegrityMetaComponent(AbstractMetaHandler.createIntegrityMetaComponent(componentProjection));
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true));
}
}
}
3 changes: 0 additions & 3 deletions src/main/java/org/dependencytrack/model/FetchStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public enum FetchStatus {
//request processed successfully
PROCESSED,

//an attempt was made to fetch hash info but request timed out
TIMED_OUT,

//fetching information for this component is in progress
IN_PROGRESS,

Expand Down
Loading

0 comments on commit a207295

Please sign in to comment.