1919import java .net .URI ;
2020import java .net .URISyntaxException ;
2121import java .net .URL ;
22+ import java .security .MessageDigest ;
23+ import java .security .NoSuchAlgorithmException ;
24+ import java .util .Arrays ;
25+ import java .io .IOException ;
2226
2327import com .fasterxml .jackson .databind .JsonNode ;
2428import com .fasterxml .jackson .databind .node .ObjectNode ;
29+ import com .rusticisoftware .tincan .http .HTTPPart ;
2530import com .rusticisoftware .tincan .json .JSONBase ;
2631import com .rusticisoftware .tincan .json .Mapper ;
32+ import org .apache .commons .codec .binary .Hex ;
2733
2834import lombok .Data ;
2935import 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}
0 commit comments