Skip to content

Commit ba8eaaf

Browse files
authored
Merge pull request #55 from FishHooks/attachments
Added ability to send and receive attachments
2 parents 0fc6101 + e56dea1 commit ba8eaaf

File tree

15 files changed

+659
-61
lines changed

15 files changed

+659
-61
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@
163163
<artifactId>jetty-client</artifactId>
164164
<version>9.3.6.v20151106</version>
165165
</dependency>
166+
<dependency>
167+
<groupId>org.eclipse.jetty</groupId>
168+
<artifactId>jetty-servlet</artifactId>
169+
<version>9.3.6.v20151106</version>
170+
</dependency>
166171
</dependencies>
167172

168173
<properties>

src/main/java/com/rusticisoftware/tincan/Attachment.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919
import java.net.URI;
2020
import java.net.URISyntaxException;
2121
import java.net.URL;
22+
import java.security.MessageDigest;
23+
import java.security.NoSuchAlgorithmException;
24+
import java.util.Arrays;
25+
import java.io.IOException;
2226

2327
import com.fasterxml.jackson.databind.JsonNode;
2428
import com.fasterxml.jackson.databind.node.ObjectNode;
29+
import com.rusticisoftware.tincan.http.HTTPPart;
2530
import com.rusticisoftware.tincan.json.JSONBase;
2631
import com.rusticisoftware.tincan.json.Mapper;
32+
import org.apache.commons.codec.binary.Hex;
2733

2834
import lombok.Data;
2935
import lombok.EqualsAndHashCode;
@@ -43,44 +49,62 @@ public class Attachment extends JSONBase {
4349
private Integer length;
4450
private String sha2;
4551
private URL fileUrl;
46-
47-
public Attachment(JsonNode jsonNode) throws URISyntaxException, MalformedURLException {
52+
private byte[] content;
53+
54+
public Attachment(JsonNode jsonNode) throws URISyntaxException, MalformedURLException, IOException, NoSuchAlgorithmException {
55+
this(jsonNode, null);
56+
}
57+
58+
public Attachment(JsonNode jsonNode, byte[] content) throws URISyntaxException, MalformedURLException, IOException, NoSuchAlgorithmException {
4859
JsonNode usageTypeNode = jsonNode.path("usageType");
4960
if (! usageTypeNode.isMissingNode()) {
5061
this.setUsageType(new URI(usageTypeNode.textValue()));
5162
}
52-
63+
5364
JsonNode displayNode = jsonNode.path("display");
5465
if (! displayNode.isMissingNode()) {
5566
this.setDisplay(new LanguageMap(displayNode));
5667
}
57-
68+
5869
JsonNode descriptionNode = jsonNode.path("description");
5970
if (! descriptionNode.isMissingNode()) {
6071
this.setDescription(new LanguageMap(descriptionNode));
6172
}
62-
73+
6374
JsonNode contentTypeNode = jsonNode.path("contentType");
6475
if (! contentTypeNode.isMissingNode()) {
6576
this.setContentType(contentTypeNode.textValue());
6677
}
67-
78+
6879
JsonNode lengthNode = jsonNode.path("length");
6980
if (! lengthNode.isMissingNode()) {
7081
this.setLength(lengthNode.intValue());
7182
}
72-
83+
7384
JsonNode sha2Node = jsonNode.path("sha2");
7485
if (! sha2Node.isMissingNode()) {
7586
this.setSha2(sha2Node.textValue());
7687
}
77-
88+
7889
JsonNode fileUrlNode = jsonNode.path("fileUrl");
7990
if (! fileUrlNode.isMissingNode()) {
8091
this.setFileUrl(new URL(fileUrlNode.textValue()));
8192
}
93+
94+
if (content != null) {
95+
this.setContent(content);
96+
}
97+
}
98+
99+
public void setContent(byte[] content) throws NoSuchAlgorithmException {
100+
this.content = Arrays.copyOf(content, content.length);
101+
this.setLength(content.length);
102+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
103+
digest.update(content);
104+
byte[] hash = digest.digest();
105+
this.setSha2(new String(Hex.encodeHex(hash)));
82106
}
83-
107+
84108
@Override
85109
public ObjectNode toJSONNode(TCAPIVersion version) {
86110
ObjectNode node = Mapper.getInstance().createObjectNode();
@@ -107,4 +131,12 @@ public ObjectNode toJSONNode(TCAPIVersion version) {
107131
}
108132
return node;
109133
}
134+
135+
public HTTPPart getPart() {
136+
HTTPPart part = new HTTPPart();
137+
part.setContent(content);
138+
part.setContentType(contentType);
139+
part.setSha2(sha2);
140+
return part;
141+
}
110142
}

src/main/java/com/rusticisoftware/tincan/LRS.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public interface LRS {
3131
StatementsResultLRSResponse saveStatements(List<Statement> statements);
3232
StatementLRSResponse retrieveStatement(String id);
3333
StatementLRSResponse retrieveVoidedStatement(String id);
34+
StatementLRSResponse retrieveStatement(String id, boolean attachments);
35+
StatementLRSResponse retrieveVoidedStatement(String id, boolean attachments);
3436
StatementsResultLRSResponse queryStatements(StatementsQueryInterface query);
3537
StatementsResultLRSResponse moreStatements(String moreURL);
3638

0 commit comments

Comments
 (0)