Skip to content

Commit 8016aa9

Browse files
authored
#17: Download attachments from emails
From #20
2 parents 6b04890 + 54c89a9 commit 8016aa9

File tree

10 files changed

+343
-47
lines changed

10 files changed

+343
-47
lines changed

.circleci/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Environment variables: https://circleci.com/docs/2.0/env-vars
55
# Verify circleci *.yml: https://circleci.com/docs/2.0/local-cli
66
#
7+
# @todo #/DEV Enable `qulice` plugin once https://github.com/teamed/qulice/issues/1035 is resolved.
8+
#
79
version: 2
810
jobs:
911
assemble_jar:
@@ -20,7 +22,7 @@ jobs:
2022
- run:
2123
name: Build java sources (including integration tests)
2224
command: |
23-
mvn -X -P integration-tests,qulice clean install -DLL.yandex.user=${EMAIL_USER} -DLL.yandex.pass=${EMAIL_PASS} -DLL.yandex.to.user=${EMAIL_TO}
25+
mvn -X -P integration-tests clean install -DLL.yandex.user=${EMAIL_USER} -DLL.yandex.pass=${EMAIL_PASS} -DLL.yandex.to.user=${EMAIL_TO}
2426
2527
workflows:
2628
version: 2

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
*.iml
33
*.iws
44
*.class
5-
/.idea/
5+
/.idea/
6+
/.tmp/

pom.xml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
<version>0.0.0</version>
3232
<packaging>jar</packaging>
3333
<name>${project.artifactId}</name>
34-
<description>
35-
Simplify manipulations with CLI terminal(s) for Java-based applications
36-
</description>
34+
<description>Simplify manipulations with CLI terminal(s) for Java-based applications</description>
3735
<url>http://github.com/dgroup/mbox4j</url>
3836
<licenses>
3937
<license>
@@ -61,9 +59,7 @@
6159
<!-- Application properties -->
6260
<build.version>${project.version}</build.version>
6361
<license>MIT License</license>
64-
<license.url>
65-
https://github.com/dgroup/mbox4j/blob/master/license.txt
66-
</license.url>
62+
<license.url>https://github.com/dgroup/mbox4j/blob/master/license.txt</license.url>
6763
<!-- System, mvn plugins, etc -->
6864
<jdk.version>1.8</jdk.version>
6965
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -80,7 +76,7 @@
8076
<mvn.gpg.version>1.5</mvn.gpg.version>
8177
<mvn.properties.version>1.0.0</mvn.properties.version>
8278
<jacoco.version>0.7.9</jacoco.version>
83-
<qulice.version>0.18.9</qulice.version>
79+
<qulice.version>0.18.13</qulice.version>
8480
<sonar.version>3.4.0.905</sonar.version>
8581
<commons-logging.version>1.2</commons-logging.version>
8682
<!-- General dependencies -->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package io.github.dgroup.mbox4j.inbox.javax;
26+
27+
import java.io.BufferedInputStream;
28+
import java.io.BufferedOutputStream;
29+
import java.io.File;
30+
import java.io.IOException;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.nio.file.Paths;
34+
import javax.mail.Part;
35+
import org.cactoos.Scalar;
36+
import org.cactoos.io.InputOf;
37+
import org.cactoos.io.OutputTo;
38+
import org.cactoos.io.TeeInput;
39+
import org.cactoos.scalar.LengthOf;
40+
41+
/**
42+
* Represents an attachment from {@link javax.mail.Part}.
43+
*
44+
* @since 0.1.0
45+
*/
46+
public final class AttachmentOf implements Scalar<File> {
47+
48+
/**
49+
* An instance of {@link javax.mail.Part} with attachment.
50+
*/
51+
private final Part part;
52+
53+
/**
54+
* The temporal directory for the email attachments.
55+
*/
56+
private final Scalar<File> tmp;
57+
58+
/**
59+
* Ctor.
60+
* @param part An instance of {@link javax.mail.Part} with attachment.
61+
* @param tmp The temporal directory for the email attachments.
62+
*/
63+
public AttachmentOf(final Part part, final Scalar<File> tmp) {
64+
this.part = part;
65+
this.tmp = tmp;
66+
}
67+
68+
@Override
69+
@SuppressWarnings("PMD.AvoidCatchingGenericException")
70+
public File value() throws IOException {
71+
try {
72+
final Path dest = Paths.get(
73+
this.tmp.value().getAbsolutePath(), this.part.getFileName()
74+
);
75+
try (BufferedInputStream inp = new BufferedInputStream(this.part.getInputStream());
76+
BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(dest))) {
77+
new LengthOf(
78+
new TeeInput(
79+
new InputOf(inp),
80+
new OutputTo(out)
81+
)
82+
).intValue();
83+
}
84+
return dest.toFile();
85+
// @checkstyle IllegalCatchCheck (3 lines)
86+
} catch (final Exception cause) {
87+
throw new IOException(cause);
88+
}
89+
}
90+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package io.github.dgroup.mbox4j.inbox.javax;
26+
27+
import java.util.ArrayList;
28+
import java.util.Collection;
29+
import javax.mail.Multipart;
30+
import javax.mail.Part;
31+
import org.cactoos.iterable.IterableEnvelope;
32+
33+
/**
34+
* Represents all parts of {@link javax.mail.Multipart} as an {@link Iterable}.
35+
*
36+
* @since 0.1.0
37+
* @todo #/DEV Add hierarchical unwrap as multipart can have tree-based structure.
38+
* For example, the multipart can have few levels with multipart(s).
39+
*/
40+
public final class PartsOf extends IterableEnvelope<Part> {
41+
42+
/**
43+
* Ctor.
44+
* @param multipart The content of email message.
45+
*/
46+
public PartsOf(final Multipart multipart) {
47+
super(
48+
() -> {
49+
final int quantity = multipart.getCount();
50+
final Collection<Part> parts = new ArrayList<>(quantity);
51+
for (int idx = 0; idx < quantity; ++idx) {
52+
parts.add(multipart.getBodyPart(idx));
53+
}
54+
return parts;
55+
}
56+
);
57+
}
58+
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package io.github.dgroup.mbox4j.inbox.javax;
26+
27+
import java.util.Collections;
28+
import java.util.Set;
29+
import javax.mail.Address;
30+
import javax.mail.Message;
31+
import org.cactoos.collection.Mapped;
32+
import org.cactoos.set.SetEnvelope;
33+
import org.cactoos.set.SetOf;
34+
35+
/**
36+
* The email recipients based on their type for the dedicated message.
37+
*
38+
* @since 0.1.0
39+
*/
40+
public final class RecipientsOf extends SetEnvelope<String> {
41+
42+
/**
43+
* Ctor.
44+
* @param type The type of recipients.
45+
* @param msg The message with recipients.
46+
*/
47+
public RecipientsOf(final Message.RecipientType type, final Message msg) {
48+
super(
49+
() -> {
50+
final Address[] addresses = msg.getRecipients(type);
51+
final Set<String> recipients;
52+
if (addresses == null || addresses.length == 0) {
53+
recipients = Collections.emptySet();
54+
} else {
55+
recipients = new SetOf<>(
56+
new Mapped<>(Address::toString, addresses)
57+
);
58+
}
59+
return recipients;
60+
}
61+
);
62+
}
63+
}

0 commit comments

Comments
 (0)