STIX-Java icon indicating copy to clipboard operation
STIX-Java copied to clipboard

create a ralation ship object

Open hadesgrid opened this issue 4 years ago • 2 comments

Hello Stephen

I am tryind to create a ralation ship object

Relationship usesRel = Relationship.builder()
	                .relationshipType("uses")
	                .created(Instant.now())
	                .sourceRef(AttackPattern.builder()
	                        .name("Some Attack Pattern 1")
	                        .build())
	                .targetRef(Malware.builder()
	                        .name("dog")
	                        .addLabels("worm")
	                        .build())
	                .build();

the Relationship class is


ackage io.digitalstate.stix.sro.objects;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

import io.digitalstate.stix.bundle.Bundle;
import io.digitalstate.stix.sdo.DomainObject;

@JsonDeserialize(builder = Relationship.Builder.class)
public class Relationship {
	
	private String relationshipType;
	private String description;
	private DomainObject sourceRef;
	private DomainObject targetRef;
	
	private Relationship(String relationshipType,	String description,	DomainObject sourceRef,	DomainObject targetRef) {
		this.relationshipType=relationshipType;
		this.description=description;
		this.sourceRef=sourceRef;
		this.targetRef=targetRef;
		
	}
	
	@JsonPOJOBuilder
	public static class Builder{
		String relationshipType1;
		String description;
		DomainObject sourceRef;
		DomainObject targetRef;
		
		Builder setRelationshipType (String relationshipType) {
            this.relationshipType1 = relationshipType;
            return this;
        }
		
		Builder setDescription (String description) {
            this.description = description;
            return this;
        }
		
		Builder setSourceRef (DomainObject sourceRef) {
            this.sourceRef = sourceRef;
            return this;
        }
		
		Builder setTargetRef (DomainObject targetRef) {
            this.targetRef = targetRef;
            return this;
        }
		
		public Relationship build() {
            return new Relationship(relationshipType1,description,sourceRef,targetRef);
        }
	}

}

my question is ¿how do you implement the method builder() in the Relationship class? many thanks in advance

hadesgrid avatar Aug 18 '21 09:08 hadesgrid

What is the error you are receiving?

StephenOTT avatar Aug 18 '21 14:08 StephenOTT

Hello Stephen, Thank you againg for you help. I am trying to figure out how to construct the missing classes form the repository, so that I can serilize and deserialize a stix json

for this example :


Relationship usesRel = Relationship.builder()
	                .relationshipType("uses")
	                .created(Instant.now())
	                .sourceRef(AttackPattern.builder() //The method sourceRef(DomainObject) in the type Relationship.Builder is not 
                                                                                  //applicable for the arguments (AttackPattern)
	                        .name("Some Attack Pattern 1")
	                        .build())
	                .targetRef(Malware.builder()
	                        .name("dog")
	                        .addLabels("worm")
	                        .build())
	                .build();

I need the following missing classes: Relationship AttackPattern Malware

This is the way that I have constructed them.

Relationship


@JsonDeserialize(builder = Relationship.Builder.class)
public class Relationship {
	

	public static Builder builder() {
	    return new Builder();
	}
	
	private Instant created;
	private String relationshipType;
	private String description;	
	private DomainObject sourceRef;
	private DomainObject targetRef;
	
	
private Relationship(Instant created ,String relationshipType,	String description,	DomainObject sourceRef,	DomainObject targetRef) {
		this.created=created;
		this.relationshipType=relationshipType;
		this.description=description;
		this.sourceRef=sourceRef;
		this.targetRef=targetRef;
		
	}
	
	@JsonPOJOBuilder
	public static class Builder{
		Instant created;
		String relationshipType;
		String description;
		DomainObject sourceRef;
		DomainObject targetRef;
		
		public Builder relationshipType (String relationshipType) {
            this.relationshipType = relationshipType;
            return this;
        }
		
		public Builder created (Instant instant) {
            this.created = instant;
            return this;
        }
		
		
		public Builder description (String description) {
            this.description = description;
            return this;
        }
		
		public Builder sourceRef (DomainObject sourceRef) {
            this.sourceRef = sourceRef;
            return this;
        }
		
		public Builder targetRef (DomainObject targetRef) {
            this.targetRef = targetRef;
            return this;
        }
		
		public Relationship build() {
            return new Relationship(created,relationshipType,description,sourceRef,targetRef);
        }

	}

	@Override
	public String toString() {
		return "Relationship [created=" + created + ", relationshipType=" + relationshipType + ", description="
				+ description + ", sourceRef=" + sourceRef + ", targetRef=" + targetRef + "]";
	}
			
	public String toJsonString() {
		try {
			
			 JsonNode response = StixParsers.getJsonMapper().valueToTree(this);
		      ObjectNode responseNode = (ObjectNode) response;
		        
			//ObjectMapper mapper = new ObjectMapper();
			//String realtion_s= mapper.writeValueAsString(this);
			//ObjectNode json = (ObjectNode) mapper.readTree(realtion_s);
			//json.remove("field");
			return responseNode.toString();
		}catch(Exception e) {
		}
		return null;
	}	
}

//// AttackPattern class

@JsonDeserialize(builder = AttackPattern.Builder.class)
public class AttackPattern {
	
	public static Builder builder() {
	    return new Builder();
	}
	
	private String name;
	private String description;
	private Set<KillChainPhaseType>  killChainPhases;
	
	private  AttackPattern(String name ,	 String  description  ,		  Set<KillChainPhaseType> killChainPhases) {
		this.name = name;
		this.description = description;
		this.killChainPhases = killChainPhases;		
	}
		
	@JsonPOJOBuilder
	public static class Builder
	  {
		 String name;
		 String description;
		 Set<KillChainPhaseType>  killChainPhases;
    	
    	public Builder name(String name) {
            this.name = name;
            return this;
        }
    	
    	public Builder description(String description) {
            this.description = description;
            return this;
        }
    	
    	public Builder killChainPhases(Set<KillChainPhaseType> killChainPhases) {
            this.killChainPhases = killChainPhases;
            return this;
        }
    	
    	public AttackPattern build() {
            return new AttackPattern(name,description,killChainPhases);
        }
	  }

	public String toJsonString() {
		try {
			
			 JsonNode response = StixParsers.getJsonMapper().valueToTree(this);
		      ObjectNode responseNode = (ObjectNode) response;
		        
			//ObjectMapper mapper = new ObjectMapper();
			//String realtion_s= mapper.writeValueAsString(this);
			//ObjectNode json = (ObjectNode) mapper.readTree(realtion_s);
			//json.remove("field");
			return responseNode.toString();
		}catch(Exception e) {
		}
		return null;
	}	
	
	@Override
	public String toString() {
		return "AttackPattern [name=" + name + ", description=" + description + ", killChainPhases=" + killChainPhases
				+ "]";
	}
}

Malware class


@JsonDeserialize(builder = Malware.Builder.class)
public class Malware {
	
	public static Builder builder() {
	    return new Builder();
	}
	private String labels;
	private String  name;
	private String description;
	private  Set<KillChainPhaseType>  killChainPhases;
	
	public Malware(String labels, String name, String description, Set<KillChainPhaseType> killChainPhases) {
		super();
		this.labels = labels;
		this.name = name;
		this.description = description;
		this.killChainPhases = killChainPhases;
	}
	
	@JsonPOJOBuilder
	public static class Builder
	  {
		String labels;
		String  name;
		String description;
		Set<KillChainPhaseType>  killChainPhases;
		
		public Builder addLabels(String labels) {
            this.labels = labels;
            return this;
        }
    	
		public Builder name(String name) {
            this.name = name;
            return this;
        }
    	
		public Builder description(String description) {
            this.description = description;
            return this;
        }
    	
    	
		public Builder killChainPhases( Set<KillChainPhaseType> killChainPhases) {
            this.killChainPhases = killChainPhases;
            return this;
        }
    	
    	public Malware build() {
            return new Malware(labels,name,description,killChainPhases);
        }
	  }
	
	public String toJsonString() {
		try {
			
			 JsonNode response = StixParsers.getJsonMapper().valueToTree(this);
		      ObjectNode responseNode = (ObjectNode) response;
		        
			//ObjectMapper mapper = new ObjectMapper();
			//String realtion_s= mapper.writeValueAsString(this);
			//ObjectNode json = (ObjectNode) mapper.readTree(realtion_s);
			//json.remove("field");
			return responseNode.toString();
		}catch(Exception e) {
		}
		return null;
	}

	@Override
	public String toString() {
		return "Malware [labels=" + labels + ", name=" + name + ", description=" + description + ", killChainPhases="
				+ killChainPhases + "]";
	}	
	
	
	
}

When i calling



Relationship usesRel = Relationship.builder()
	                .relationshipType("uses")
	                .created(Instant.now())
	                .sourceRef(AttackPattern.builder() 
	                        .name("Some Attack Pattern 1")
	                        .build())
	                .targetRef(Malware.builder()
	                        .name("dog")
	                        .addLabels("worm")
	                        .build())
	                .build();

the line .sourceRef(AttackPattern.builder()

is showing the error The method sourceRef(DomainObject) in the type Relationship.Builder is not applicable for the arguments (AttackPattern)

so it does with this line

.targetRef(Malware.builder()

what I am doing wrong? how can I construct the json?

many thanks in advance

hadesgrid avatar Aug 18 '21 15:08 hadesgrid