-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#17: Download attachments from emails
From #20
- Loading branch information
Showing
10 changed files
with
343 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
*.iml | ||
*.iws | ||
*.class | ||
/.idea/ | ||
/.idea/ | ||
/.tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/io/github/dgroup/mbox4j/inbox/javax/AttachmentOf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Yurii Dubinka | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom | ||
* the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | ||
* OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package io.github.dgroup.mbox4j.inbox.javax; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import javax.mail.Part; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.io.InputOf; | ||
import org.cactoos.io.OutputTo; | ||
import org.cactoos.io.TeeInput; | ||
import org.cactoos.scalar.LengthOf; | ||
|
||
/** | ||
* Represents an attachment from {@link javax.mail.Part}. | ||
* | ||
* @since 0.1.0 | ||
*/ | ||
public final class AttachmentOf implements Scalar<File> { | ||
|
||
/** | ||
* An instance of {@link javax.mail.Part} with attachment. | ||
*/ | ||
private final Part part; | ||
|
||
/** | ||
* The temporal directory for the email attachments. | ||
*/ | ||
private final Scalar<File> tmp; | ||
|
||
/** | ||
* Ctor. | ||
* @param part An instance of {@link javax.mail.Part} with attachment. | ||
* @param tmp The temporal directory for the email attachments. | ||
*/ | ||
public AttachmentOf(final Part part, final Scalar<File> tmp) { | ||
this.part = part; | ||
this.tmp = tmp; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("PMD.AvoidCatchingGenericException") | ||
public File value() throws IOException { | ||
try { | ||
final Path dest = Paths.get( | ||
this.tmp.value().getAbsolutePath(), this.part.getFileName() | ||
); | ||
try (BufferedInputStream inp = new BufferedInputStream(this.part.getInputStream()); | ||
BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(dest))) { | ||
new LengthOf( | ||
new TeeInput( | ||
new InputOf(inp), | ||
new OutputTo(out) | ||
) | ||
).intValue(); | ||
} | ||
return dest.toFile(); | ||
// @checkstyle IllegalCatchCheck (3 lines) | ||
} catch (final Exception cause) { | ||
throw new IOException(cause); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/io/github/dgroup/mbox4j/inbox/javax/PartsOf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Yurii Dubinka | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom | ||
* the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | ||
* OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package io.github.dgroup.mbox4j.inbox.javax; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import javax.mail.Multipart; | ||
import javax.mail.Part; | ||
import org.cactoos.iterable.IterableEnvelope; | ||
|
||
/** | ||
* Represents all parts of {@link javax.mail.Multipart} as an {@link Iterable}. | ||
* | ||
* @since 0.1.0 | ||
* @todo #/DEV Add hierarchical unwrap as multipart can have tree-based structure. | ||
* For example, the multipart can have few levels with multipart(s). | ||
*/ | ||
public final class PartsOf extends IterableEnvelope<Part> { | ||
|
||
/** | ||
* Ctor. | ||
* @param multipart The content of email message. | ||
*/ | ||
public PartsOf(final Multipart multipart) { | ||
super( | ||
() -> { | ||
final int quantity = multipart.getCount(); | ||
final Collection<Part> parts = new ArrayList<>(quantity); | ||
for (int idx = 0; idx < quantity; ++idx) { | ||
parts.add(multipart.getBodyPart(idx)); | ||
} | ||
return parts; | ||
} | ||
); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/io/github/dgroup/mbox4j/inbox/javax/RecipientsOf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Yurii Dubinka | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom | ||
* the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | ||
* OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package io.github.dgroup.mbox4j.inbox.javax; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import javax.mail.Address; | ||
import javax.mail.Message; | ||
import org.cactoos.collection.Mapped; | ||
import org.cactoos.set.SetEnvelope; | ||
import org.cactoos.set.SetOf; | ||
|
||
/** | ||
* The email recipients based on their type for the dedicated message. | ||
* | ||
* @since 0.1.0 | ||
*/ | ||
public final class RecipientsOf extends SetEnvelope<String> { | ||
|
||
/** | ||
* Ctor. | ||
* @param type The type of recipients. | ||
* @param msg The message with recipients. | ||
*/ | ||
public RecipientsOf(final Message.RecipientType type, final Message msg) { | ||
super( | ||
() -> { | ||
final Address[] addresses = msg.getRecipients(type); | ||
final Set<String> recipients; | ||
if (addresses == null || addresses.length == 0) { | ||
recipients = Collections.emptySet(); | ||
} else { | ||
recipients = new SetOf<>( | ||
new Mapped<>(Address::toString, addresses) | ||
); | ||
} | ||
return recipients; | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.
8016aa9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
DEV-c3045043
disappeared fromsrc/main/java/io/github/dgroup/mbox4j/inbox/javax/ToMsg.java
, that's why I closed #17. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.8016aa9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
DEV-d8406873
discovered insrc/main/java/io/github/dgroup/mbox4j/inbox/javax/PartsOf.java
and submitted as #21. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.8016aa9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
DEV-630ec172
discovered in.circleci/config.yml
and submitted as #22. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.