Skip to content

Commit

Permalink
#17: Download attachments from emails
Browse files Browse the repository at this point in the history
From #20
  • Loading branch information
dgroup committed Mar 31, 2019
2 parents 6b04890 + 54c89a9 commit 8016aa9
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 47 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Environment variables: https://circleci.com/docs/2.0/env-vars
# Verify circleci *.yml: https://circleci.com/docs/2.0/local-cli
#
# @todo #/DEV Enable `qulice` plugin once https://github.com/teamed/qulice/issues/1035 is resolved.
#
version: 2
jobs:
assemble_jar:
Expand All @@ -20,7 +22,7 @@ jobs:
- run:
name: Build java sources (including integration tests)
command: |
mvn -X -P integration-tests,qulice clean install -DLL.yandex.user=${EMAIL_USER} -DLL.yandex.pass=${EMAIL_PASS} -DLL.yandex.to.user=${EMAIL_TO}
mvn -X -P integration-tests clean install -DLL.yandex.user=${EMAIL_USER} -DLL.yandex.pass=${EMAIL_PASS} -DLL.yandex.to.user=${EMAIL_TO}
workflows:
version: 2
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.iml
*.iws
*.class
/.idea/
/.idea/
/.tmp/
10 changes: 3 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
<version>0.0.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>
Simplify manipulations with CLI terminal(s) for Java-based applications
</description>
<description>Simplify manipulations with CLI terminal(s) for Java-based applications</description>
<url>http://github.com/dgroup/mbox4j</url>
<licenses>
<license>
Expand Down Expand Up @@ -61,9 +59,7 @@
<!-- Application properties -->
<build.version>${project.version}</build.version>
<license>MIT License</license>
<license.url>
https://github.com/dgroup/mbox4j/blob/master/license.txt
</license.url>
<license.url>https://github.com/dgroup/mbox4j/blob/master/license.txt</license.url>
<!-- System, mvn plugins, etc -->
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -80,7 +76,7 @@
<mvn.gpg.version>1.5</mvn.gpg.version>
<mvn.properties.version>1.0.0</mvn.properties.version>
<jacoco.version>0.7.9</jacoco.version>
<qulice.version>0.18.9</qulice.version>
<qulice.version>0.18.13</qulice.version>
<sonar.version>3.4.0.905</sonar.version>
<commons-logging.version>1.2</commons-logging.version>
<!-- General dependencies -->
Expand Down
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 src/main/java/io/github/dgroup/mbox4j/inbox/javax/PartsOf.java
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;
}
);
}

}
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;
}
);
}
}
Loading

3 comments on commit 8016aa9

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 8016aa9 Mar 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-c3045043 disappeared from src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 8016aa9 Mar 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-d8406873 discovered in src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 8016aa9 Mar 31, 2019

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.

Please sign in to comment.