Skip to content

Commit

Permalink
Merge branch 'feature-attachments'
Browse files Browse the repository at this point in the history
  • Loading branch information
cternes committed Oct 25, 2017
2 parents 22b6212 + aeb10ae commit cfcb289
Show file tree
Hide file tree
Showing 28 changed files with 1,065 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException;
import de.slackspace.openkeepass.parser.KeePassDatabaseXmlParser;
import de.slackspace.openkeepass.parser.SimpleXmlParser;
import de.slackspace.openkeepass.processor.BinaryEnricher;
import de.slackspace.openkeepass.processor.DecryptionStrategy;
import de.slackspace.openkeepass.processor.IconEnricher;
import de.slackspace.openkeepass.processor.ProtectedValueProcessor;
Expand Down Expand Up @@ -58,7 +59,8 @@ private KeePassFile parseDatabase(byte[] decompressed, ProtectedStringCrypto pro
KeePassFile unprocessedKeepassFile = keePassDatabaseXmlParser.fromXml(new ByteArrayInputStream(decompressed));
new ProtectedValueProcessor().processProtectedValues(new DecryptionStrategy(protectedStringCrypto), unprocessedKeepassFile);

return new IconEnricher().enrichNodesWithIconData(unprocessedKeepassFile);
KeePassFile iconEnrichedKeepassFile = new IconEnricher().enrichNodesWithIconData(unprocessedKeepassFile);
return new BinaryEnricher().enrichNodesWithBinaryData(iconEnrichedKeepassFile);
}

private ProtectedStringCrypto getProtectedStringCrypto() {
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/de/slackspace/openkeepass/domain/Attachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package de.slackspace.openkeepass.domain;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
* Represents an attachment of an {@link Entry}.
*
*/
@Root(strict = false, name = "Binary")
public class Attachment implements KeePassFileElement {

@Element(name = "Key")
private String key;

@Element(name = "Value")
private AttachmentValue attachmentValue;

private transient byte[] data;

Attachment() {
}

public Attachment(String key, int ref) {
this.key = key;
this.attachmentValue = new AttachmentValue(ref);
}

public Attachment(String key, int ref, byte[] data) {
this.key = key;
this.attachmentValue = new AttachmentValue(ref);
this.data = data;
}

public String getKey() {
return key;
}

public int getRef() {
return attachmentValue.getRef();
}

public byte[] getData() {
return data;
}

@Override
public String toString() {
return "Property [key=" + key + ", attachmentValue=" + attachmentValue + "]";
}

@Override
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((attachmentValue == null) ? 0 : attachmentValue.hashCode());
return result;
}

@Override
public final boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Attachment))
return false;
Attachment other = (Attachment) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (attachmentValue == null) {
if (other.attachmentValue != null)
return false;
} else if (!attachmentValue.equals(other.attachmentValue))
return false;
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.slackspace.openkeepass.domain;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

/**
* Represents the value part of an attachment.
*
*/
@Root(name = "Value", strict = false)
public class AttachmentValue {

@Attribute(name = "Ref")
private int ref;

AttachmentValue() {
}

public AttachmentValue(int ref) {
this.ref = ref;
}

public int getRef() {
return ref;
}

@Override
public String toString() {
return "AttachmentValue [ref=" + ref + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ref;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AttachmentValue other = (AttachmentValue) obj;
return ref == other.ref;
}

}
76 changes: 76 additions & 0 deletions src/main/java/de/slackspace/openkeepass/domain/Binaries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package de.slackspace.openkeepass.domain;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
* Represents a list of binary items in the metadata of a KeePass file.
*
*/
@Root(name = "Binaries", strict = false)
public class Binaries {

@ElementList(name = "Binary", inline = true, required = false)
private List<Binary> binaryList = new ArrayList<Binary>();

Binaries() {
}

public Binaries(BinariesContract binariesContract) {
this.binaryList = binariesContract.getBinaries();
}

/**
* Returns all binary items found inside the database.
*
* @return a list of attachments
*/
public List<Binary> getBinaries() {
return binaryList;
}

/**
* Retrieves a binary item based on its id.
*
* @param id the id which should be searched
* @return the binary item if found, null otherwise
*/
public Binary getBinaryById(int id) {
for (Binary binary : binaryList) {
if (binary.getId() == id) {
return binary;
}
}

return null;
}

@Override
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((binaryList == null) ? 0 : binaryList.hashCode());
return result;
}

@Override
public final boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Binaries))
return false;
Binaries other = (Binaries) obj;
if (binaryList == null) {
if (other.binaryList != null)
return false;
} else if (!binaryList.equals(other.binaryList))
return false;
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.slackspace.openkeepass.domain;

import java.util.ArrayList;
import java.util.List;

/**
* A builder to create {@link Binaries} objects.
*
*/
public class BinariesBuilder implements BinariesContract {

List<Binary> binaries = new ArrayList<Binary>();

public BinariesBuilder() {
// default no-args constructor
}

public BinariesBuilder(Binaries binaries) {
this.binaries = binaries.getBinaries();
}

public BinariesBuilder binaries(List<Binary> binaries) {
this.binaries = binaries;
return this;
}

public BinariesBuilder addBinary(Binary binary) {
binaries.add(binary);
return this;
}

/**
* Builds a new binary item list with the values from the builder.
*
* @return a new Binaries object
*/
public Binaries build() {
return new Binaries(this);
}

@Override
public List<Binary> getBinaries() {
return binaries;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.slackspace.openkeepass.domain;

import java.util.List;

public interface BinariesContract {

List<Binary> getBinaries();
}
Loading

0 comments on commit cfcb289

Please sign in to comment.