diff --git a/src/main/java/net/interfax/rest/client/InterFAX.java b/src/main/java/net/interfax/rest/client/InterFAX.java index 05d2f29..87209b4 100644 --- a/src/main/java/net/interfax/rest/client/InterFAX.java +++ b/src/main/java/net/interfax/rest/client/InterFAX.java @@ -87,6 +87,37 @@ public APIResponse sendFax(final String faxNumber, final File[] filesToSendAsFax, final Optional options) throws IOException, URISyntaxException; + /** + * Send a combination of files and pre-uploaded/HTTP-hosted document urls as a single fax + * + * @param faxNumber number to fax to + * @param filesToSendAsFax array of files to send as fax + * @param urlsOfDocs array of urls of documents to send as fax + * @return {@link APIResponse} + * @throws IOException + * @see https://www.interfax.net/en/dev/rest/reference/2918 + */ + public APIResponse sendFax(final String faxNumber, + final File[] filesToSendAsFax, + final String[] urlsOfDocs) throws IOException, URISyntaxException; + + /** + * Send a combination of files and pre-uploaded/HTTP-hosted document urls as a single fax, with additional + * {@link SendFaxOptions} + * + * @param faxNumber number to fax to + * @param filesToSendAsFax array of files to send as fax + * @param urlsOfDocs array of urls of documents to send as fax + * @param options {@link SendFaxOptions} to use when sending the fax + * @return {@link APIResponse} + * @throws IOException + * @see https://www.interfax.net/en/dev/rest/reference/2918 + */ + public APIResponse sendFax(final String faxNumber, + final File[] filesToSendAsFax, + final String[] urlsOfDocs, + final Optional options) throws IOException, URISyntaxException; + /** * Send an array of input streams as a fax with additional {@link SendFaxOptions} * diff --git a/src/main/java/net/interfax/rest/client/impl/DefaultInterFAXClient.java b/src/main/java/net/interfax/rest/client/impl/DefaultInterFAXClient.java index 3d8630c..baa6429 100644 --- a/src/main/java/net/interfax/rest/client/impl/DefaultInterFAXClient.java +++ b/src/main/java/net/interfax/rest/client/impl/DefaultInterFAXClient.java @@ -21,6 +21,7 @@ import org.glassfish.jersey.client.RequestEntityProcessing; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.glassfish.jersey.jackson.JacksonFeature; +import org.glassfish.jersey.media.multipart.BodyPart; import org.glassfish.jersey.media.multipart.MultiPart; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; @@ -123,6 +124,38 @@ public APIResponse sendFax(final String faxNumber, return sendMultiPartFax(faxNumber, multiPart, options); } + @Override + public APIResponse sendFax(final String faxNumber, final File[] filesToSendAsFax, final String[] urlsOfDocs) + throws IOException, URISyntaxException { + + return sendFax(faxNumber, filesToSendAsFax, urlsOfDocs, Optional.empty()); + } + + @Override + public APIResponse sendFax(final String faxNumber, + final File[] filesToSendAsFax, + final String[] urlsOfDocs, + final Optional options) throws IOException, URISyntaxException { + + MultiPart multiPart = new MultiPart(); + int count = 1; + for (File file : filesToSendAsFax) { + String contentType = tika.detect(file); + String entityName = "file"+count++; + FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(entityName, file, MediaType.valueOf(contentType)); + multiPart.bodyPart(fileDataBodyPart); + } + + for (String urlOfDoc : urlsOfDocs) { + BodyPart urlBodyPart = new BodyPart("", MediaType.TEXT_PLAIN_TYPE); + urlBodyPart.getHeaders().add("Content-Location", urlOfDoc); + urlBodyPart.getHeaders().add("Content-Length", "0"); + multiPart.bodyPart(urlBodyPart); + } + + return sendMultiPartFax(faxNumber, multiPart, options); + } + @Override public APIResponse sendFax(String faxNumber, InputStream[] streamsToSendAsFax, diff --git a/src/test/java/net/interfax/rest/client/impl/DefaultInterFAXClientTest.java b/src/test/java/net/interfax/rest/client/impl/DefaultInterFAXClientTest.java index c928b4d..8c1c42f 100644 --- a/src/test/java/net/interfax/rest/client/impl/DefaultInterFAXClientTest.java +++ b/src/test/java/net/interfax/rest/client/impl/DefaultInterFAXClientTest.java @@ -96,6 +96,39 @@ public void testSendMultipleFilesAsFaxWithOptions() throws Exception { } + @Test + public void testSendMixOfFilesAndUrlsAsFax() throws Exception { + + String absoluteFilePath = this.getClass().getClassLoader().getResource("test.pdf").getFile(); + File file = new File(absoluteFilePath); + + File[] files = {file}; + String[] urlsOfDocs = {"https://rest.interfax.net/outbound/documents/mixed-test-doc"}; + + InterFAX interFAX = new DefaultInterFAXClient(); + APIResponse apiResponse = interFAX.sendFax(faxNumber, files, urlsOfDocs); + Assert.assertEquals("[https://rest.interfax.net/outbound/faxes/667457901]", apiResponse.getHeaders().get("Location").toString()); + Assert.assertEquals(201, apiResponse.getStatusCode()); + } + + @Test + public void testSendMixOfFilesAndUrlsAsFaxWithOptions() throws Exception { + + String absoluteFilePath = this.getClass().getClassLoader().getResource("test.pdf").getFile(); + File file = new File(absoluteFilePath); + + File[] files = {file}; + String[] urlsOfDocs = {"https://rest.interfax.net/outbound/documents/mixed-test-doc"}; + + SendFaxOptions sendFaxOptions = new SendFaxOptions(); + sendFaxOptions.setPageSize(Optional.of("a4")); + + InterFAX interFAX = new DefaultInterFAXClient(); + APIResponse apiResponse = interFAX.sendFax(faxNumber, files, urlsOfDocs, Optional.of(sendFaxOptions)); + Assert.assertEquals("[https://rest.interfax.net/outbound/faxes/667457902]", apiResponse.getHeaders().get("Location").toString()); + Assert.assertEquals(201, apiResponse.getStatusCode()); + } + @Test public void testSendMultipleInputStreamsAsFax() throws Exception { diff --git a/src/test/resources/__files/body-outbound-faxes-mixed-files-and-urls-with-options.json b/src/test/resources/__files/body-outbound-faxes-mixed-files-and-urls-with-options.json new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/__files/body-outbound-faxes-mixed-files-and-urls.json b/src/test/resources/__files/body-outbound-faxes-mixed-files-and-urls.json new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/mappings/mapping-outbound-faxes-mixed-files-and-urls-with-options.json b/src/test/resources/mappings/mapping-outbound-faxes-mixed-files-and-urls-with-options.json new file mode 100644 index 0000000..8c561f9 --- /dev/null +++ b/src/test/resources/mappings/mapping-outbound-faxes-mixed-files-and-urls-with-options.json @@ -0,0 +1,31 @@ +{ + "uuid" : "c7b2f7c1-3a4e-4e9f-ad2b-7b3c9f0d1e22", + "request" : { + "url" : "/outbound/faxes?faxNumber=%2B442084978672&pageSize=a4", + "headers": { + "Authorization": { + "equalTo" : "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" + }, + "Content-Type": { + "matches" : "multipart/mixed.*" + } + }, + "method" : "POST", + "bodyPatterns" : [ { + "matches" : "(?s).*test\\.pdf.*Content-Location: https://rest\\.interfax\\.net/outbound/documents/mixed-test-doc.*" + } ] + }, + "response" : { + "status" : 201, + "bodyFileName" : "body-outbound-faxes-mixed-files-and-urls-with-options.json", + "headers" : { + "Cache-Control" : "private", + "Content-Type" : "text/json", + "Location" : "https://rest.interfax.net/outbound/faxes/667457902", + "Access-Control-Allow-Origin" : "*", + "Access-Control-Allow-Methods" : "*", + "Access-Control-Allow-Headers" : "Content-Type, Access-Control-Allow-Headers, Access-Control-Request-Method, Authorization, X-Requested-With, Origin, Accept, Accept-Encoding, Accept-Language, Connection, Host, Referer, User-Agent, Content-Disposition", + "Date" : "Fri, 30 Sep 2016 13:33:45 GMT" + } + } +} diff --git a/src/test/resources/mappings/mapping-outbound-faxes-mixed-files-and-urls.json b/src/test/resources/mappings/mapping-outbound-faxes-mixed-files-and-urls.json new file mode 100644 index 0000000..d724fa8 --- /dev/null +++ b/src/test/resources/mappings/mapping-outbound-faxes-mixed-files-and-urls.json @@ -0,0 +1,31 @@ +{ + "uuid" : "b6a1e6b0-2f3d-4d8e-9c1a-6a2b8f9c0d11", + "request" : { + "url" : "/outbound/faxes?faxNumber=%2B442084978672", + "headers": { + "Authorization": { + "equalTo" : "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" + }, + "Content-Type": { + "matches" : "multipart/mixed.*" + } + }, + "method" : "POST", + "bodyPatterns" : [ { + "matches" : "(?s).*test\\.pdf.*Content-Location: https://rest\\.interfax\\.net/outbound/documents/mixed-test-doc.*" + } ] + }, + "response" : { + "status" : 201, + "bodyFileName" : "body-outbound-faxes-mixed-files-and-urls.json", + "headers" : { + "Cache-Control" : "private", + "Content-Type" : "text/json", + "Location" : "https://rest.interfax.net/outbound/faxes/667457901", + "Access-Control-Allow-Origin" : "*", + "Access-Control-Allow-Methods" : "*", + "Access-Control-Allow-Headers" : "Content-Type, Access-Control-Allow-Headers, Access-Control-Request-Method, Authorization, X-Requested-With, Origin, Accept, Accept-Encoding, Accept-Language, Connection, Host, Referer, User-Agent, Content-Disposition", + "Date" : "Fri, 30 Sep 2016 13:33:45 GMT" + } + } +}