PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/no/digipost/api/client/representations/Document.java

https://github.com/digipost/digipost-api-client-java
Java | 238 lines | 183 code | 37 blank | 18 comment | 21 complexity | 4a8514252585a4ec4eae73d1f3174c07 MD5 | raw file
  1. /**
  2. * Copyright (C) Posten Norge AS
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package no.digipost.api.client.representations;
  17. import no.digipost.api.datatypes.DataType;
  18. import javax.xml.bind.annotation.XmlAccessType;
  19. import javax.xml.bind.annotation.XmlAccessorType;
  20. import javax.xml.bind.annotation.XmlAttribute;
  21. import javax.xml.bind.annotation.XmlElement;
  22. import javax.xml.bind.annotation.XmlType;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Objects;
  26. import java.util.Optional;
  27. import java.util.UUID;
  28. import java.util.stream.Stream;
  29. import static java.util.Optional.ofNullable;
  30. import static java.util.stream.Collectors.collectingAndThen;
  31. import static java.util.stream.Collectors.joining;
  32. import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
  33. import static org.apache.commons.lang3.StringUtils.join;
  34. @XmlAccessorType(XmlAccessType.FIELD)
  35. @XmlType(name = "document", propOrder = {
  36. "uuid",
  37. "subject",
  38. "digipostFileType",
  39. "opened",
  40. "openingReceipt",
  41. "smsNotification",
  42. "emailNotification",
  43. "authenticationLevel",
  44. "sensitivityLevel",
  45. "encrypted",
  46. "contentHash",
  47. "links",
  48. "dataType"
  49. })
  50. public class Document extends Representation {
  51. @XmlElement(name = "uuid", required = true)
  52. public final UUID uuid;
  53. @XmlElement(name = "subject", required = true)
  54. public final String subject;
  55. @XmlElement(name = "file-type", required = true)
  56. protected String digipostFileType;
  57. @XmlElement(nillable = false)
  58. protected Boolean opened;
  59. @XmlElement(name = "opening-receipt")
  60. public final String openingReceipt;
  61. @XmlElement(name = "sms-notification")
  62. public final SmsNotification smsNotification;
  63. @XmlElement(name = "email-notification")
  64. public final EmailNotification emailNotification;
  65. @XmlElement(name = "authentication-level")
  66. public final AuthenticationLevel authenticationLevel;
  67. @XmlElement(name = "sensitivity-level")
  68. public final SensitivityLevel sensitivityLevel;
  69. @XmlElement(name = "encrypted")
  70. protected EncryptionInfo encrypted;
  71. @XmlElement(name = "content-hash", nillable = false)
  72. protected ContentHash contentHash;
  73. @XmlElement(name="data-type")
  74. protected DataTypeHolder dataType;
  75. @XmlElement(name = "link")
  76. protected List<Link> getLinks() {
  77. return links;
  78. }
  79. @XmlAttribute(name = "technical-type")
  80. private String technicalType;
  81. public Document() { this(null, null, null); }
  82. /**
  83. * Constructor for just the required fields of a document.
  84. */
  85. public Document(UUID uuid, String subject, FileType fileType) {
  86. this(uuid, subject, fileType, null, null, null, null, null, null, null, (String[]) null);
  87. }
  88. public Document(UUID uuid, String subject, FileType fileType, DataType data) {
  89. this(uuid, subject, fileType, null, null, null, null, null, null, data, (String[]) null);
  90. }
  91. public Document(UUID uuid, String subject, FileType fileType, String openingReceipt,
  92. SmsNotification smsNotification, EmailNotification emailNotification,
  93. AuthenticationLevel authenticationLevel,
  94. SensitivityLevel sensitivityLevel) {
  95. this(uuid, subject, fileType, openingReceipt, smsNotification, emailNotification, authenticationLevel, sensitivityLevel, null, null, (String[]) null);
  96. }
  97. public Document(UUID uuid, String subject, FileType fileType, String openingReceipt,
  98. SmsNotification smsNotification, EmailNotification emailNotification,
  99. AuthenticationLevel authenticationLevel,
  100. SensitivityLevel sensitivityLevel, Boolean opened, DataType data, String... technicalType) {
  101. this.uuid = uuid;
  102. this.subject = subject;
  103. this.digipostFileType = Objects.toString(fileType, null);
  104. this.openingReceipt = defaultIfBlank(openingReceipt, null);
  105. this.opened = Boolean.TRUE.equals(opened) ? true : null;
  106. this.smsNotification = smsNotification;
  107. this.emailNotification = emailNotification;
  108. this.authenticationLevel = authenticationLevel;
  109. this.sensitivityLevel = sensitivityLevel;
  110. this.technicalType = parseTechnicalTypes(technicalType);
  111. this.dataType = data != null ? new DataTypeHolder(data) : null;
  112. this.validate();
  113. }
  114. static String parseTechnicalTypes(String... technicalTypes){
  115. if(technicalTypes == null || technicalTypes.length == 0) {
  116. return null;
  117. }
  118. return Stream.of(technicalTypes)
  119. .filter(s -> Objects.nonNull(s) && !s.isEmpty())
  120. .map(String::trim)
  121. .collect(collectingAndThen(joining(","), joined -> joined.isEmpty() ? null : joined));
  122. }
  123. public Document copyDocumentAndSetDigipostFileTypeToPdf(){
  124. Document newDoc = new Document(this.uuid, this.subject, new FileType("pdf"), this.openingReceipt, this.smsNotification, this.emailNotification,
  125. this.authenticationLevel, this.sensitivityLevel, this.opened, this.dataType != null ? this.dataType.get() : null, this.getTechnicalType());
  126. newDoc.encrypted = this.encrypted == null ? null : this.encrypted.copy();
  127. newDoc.setContentHash(this.contentHash);
  128. return newDoc;
  129. }
  130. private void validate() {
  131. List<String> errors = new ArrayList<>();
  132. if (openingReceipt != null && opened != null) {
  133. errors.add("Both openingReceipt and opened was set");
  134. }
  135. if (!errors.isEmpty()) {
  136. throw new IllegalStateException(
  137. errors.size() + " errors when instantiating " + Document.class.getSimpleName() +
  138. "\n - " + join(errors, "\n - "));
  139. }
  140. }
  141. public static Document technicalAttachment(FileType fileType, String... type) {
  142. Document document = new Document(UUID.randomUUID(), null, fileType);
  143. document.technicalType = parseTechnicalTypes(type);
  144. return document;
  145. }
  146. public void setContentHash(ContentHash contentHash){
  147. this.contentHash = contentHash;
  148. }
  149. public void setDigipostFileType(FileType fileType) {
  150. this.digipostFileType = fileType.toString();
  151. }
  152. public String getDigipostFileType() {
  153. return digipostFileType;
  154. }
  155. public boolean is(FileType fileType) {
  156. return fileType.equals(new FileType(digipostFileType));
  157. }
  158. public Document encrypt() {
  159. if (this.encrypted != null) {
  160. throw new IllegalStateException("Document already set to encrypted, are you calling encrypt() twice?");
  161. }
  162. this.encrypted = new EncryptionInfo();
  163. return this;
  164. }
  165. public boolean willBeEncrypted() {
  166. return encrypted != null;
  167. }
  168. public EncryptionInfo getEncrypted() {
  169. return encrypted;
  170. }
  171. public Link getAddContentLink() {
  172. return getLinkByRelationName(Relation.ADD_CONTENT);
  173. }
  174. public AddDataLink getAddDataLink() {
  175. return new AddDataLink(getLinkByRelationName(Relation.ADD_DATA).getUri().getPath());
  176. }
  177. public Link getEncryptionKeyLink() {
  178. return getLinkByRelationName(Relation.GET_ENCRYPTION_KEY);
  179. }
  180. public String[] getTechnicalType() {
  181. return technicalType != null ? technicalType.split(",") : null;
  182. }
  183. public boolean isOpened() {
  184. return opened != null && opened;
  185. }
  186. public Optional<DataType> getDataType() {
  187. return Optional.ofNullable(dataType).map(DataTypeHolder::get);
  188. }
  189. @Override
  190. public String toString() {
  191. return getClass().getSimpleName() + " with uuid '" + uuid + "'" +
  192. ofNullable(technicalType).map(t -> ", technicalType '" + t + "'").orElse("") +
  193. (willBeEncrypted() ? ofNullable(subject).map(s -> ", subject '" + s + "'").orElse(", no subject") : ", encrypted");
  194. }
  195. public void setNumberOfEncryptedPages(int pages) {
  196. if (this.encrypted == null) {
  197. throw new IllegalStateException("Tried setting number of encrypted pages, but document is not set to be encrypted. Have you called Document.encrypt()?");
  198. }
  199. this.encrypted.setNumberOfPages(pages);
  200. }
  201. }