Skip to content

Commit

Permalink
Merge pull request #55 from FishHooks/attachments
Browse files Browse the repository at this point in the history
Added ability to send and receive attachments
  • Loading branch information
brianjmiller authored Aug 19, 2016
2 parents 0fc6101 + e56dea1 commit ba8eaaf
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 61 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@
<artifactId>jetty-client</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
</dependencies>

<properties>
Expand Down
50 changes: 41 additions & 9 deletions src/main/java/com/rusticisoftware/tincan/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.rusticisoftware.tincan.http.HTTPPart;
import com.rusticisoftware.tincan.json.JSONBase;
import com.rusticisoftware.tincan.json.Mapper;
import org.apache.commons.codec.binary.Hex;

import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -43,44 +49,62 @@ public class Attachment extends JSONBase {
private Integer length;
private String sha2;
private URL fileUrl;

public Attachment(JsonNode jsonNode) throws URISyntaxException, MalformedURLException {
private byte[] content;

public Attachment(JsonNode jsonNode) throws URISyntaxException, MalformedURLException, IOException, NoSuchAlgorithmException {
this(jsonNode, null);
}

public Attachment(JsonNode jsonNode, byte[] content) throws URISyntaxException, MalformedURLException, IOException, NoSuchAlgorithmException {
JsonNode usageTypeNode = jsonNode.path("usageType");
if (! usageTypeNode.isMissingNode()) {
this.setUsageType(new URI(usageTypeNode.textValue()));
}

JsonNode displayNode = jsonNode.path("display");
if (! displayNode.isMissingNode()) {
this.setDisplay(new LanguageMap(displayNode));
}

JsonNode descriptionNode = jsonNode.path("description");
if (! descriptionNode.isMissingNode()) {
this.setDescription(new LanguageMap(descriptionNode));
}

JsonNode contentTypeNode = jsonNode.path("contentType");
if (! contentTypeNode.isMissingNode()) {
this.setContentType(contentTypeNode.textValue());
}

JsonNode lengthNode = jsonNode.path("length");
if (! lengthNode.isMissingNode()) {
this.setLength(lengthNode.intValue());
}

JsonNode sha2Node = jsonNode.path("sha2");
if (! sha2Node.isMissingNode()) {
this.setSha2(sha2Node.textValue());
}

JsonNode fileUrlNode = jsonNode.path("fileUrl");
if (! fileUrlNode.isMissingNode()) {
this.setFileUrl(new URL(fileUrlNode.textValue()));
}

if (content != null) {
this.setContent(content);
}
}

public void setContent(byte[] content) throws NoSuchAlgorithmException {
this.content = Arrays.copyOf(content, content.length);
this.setLength(content.length);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(content);
byte[] hash = digest.digest();
this.setSha2(new String(Hex.encodeHex(hash)));
}

@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
ObjectNode node = Mapper.getInstance().createObjectNode();
Expand All @@ -107,4 +131,12 @@ public ObjectNode toJSONNode(TCAPIVersion version) {
}
return node;
}

public HTTPPart getPart() {
HTTPPart part = new HTTPPart();
part.setContent(content);
part.setContentType(contentType);
part.setSha2(sha2);
return part;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/rusticisoftware/tincan/LRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface LRS {
StatementsResultLRSResponse saveStatements(List<Statement> statements);
StatementLRSResponse retrieveStatement(String id);
StatementLRSResponse retrieveVoidedStatement(String id);
StatementLRSResponse retrieveStatement(String id, boolean attachments);
StatementLRSResponse retrieveVoidedStatement(String id, boolean attachments);
StatementsResultLRSResponse queryStatements(StatementsQueryInterface query);
StatementsResultLRSResponse moreStatements(String moreURL);

Expand Down
Loading

0 comments on commit ba8eaaf

Please sign in to comment.