com.google.api.services.gmail.model.ListMessagesResponse Java Examples

The following examples show how to use com.google.api.services.gmail.model.ListMessagesResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleMailExporterTest.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a response with a single message
 */
private void setUpSingleMessageResponse() throws IOException {
  messageListResponse = new ListMessagesResponse()
      .setMessages(Collections.singletonList(INITIAL_MESSAGE));
  when(messageListRequest.execute()).thenReturn(messageListResponse);
  when(get.execute()).thenReturn(FULL_MESSAGE);
}
 
Example #2
Source File: BaseGmailProvider.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
private List<String> getDataFromApi(String query) throws IOException {
    List<String> messageList = new ArrayList<>();
    String user = "me";
    ListMessagesResponse response = mService.users().messages().list(user).setQ(query).execute();
    int total = 1;
    String deliverTo = "";
    String from = "";
    String subject = "";
    String content = "";
    long timestamp = 0;
    if (response.getMessages() != null) {
        for(int i = response.getMessages().size()-1;i>=0;i--){
            Message item = response.getMessages().get(i);
            if (total > mMaxResult) {
                break;
            }
            Message message = mService.users().messages().get(user, item.getId()).setFormat("full").execute();
            List<MessagePart> messageParts = message.getPayload().getParts();
            List<MessagePartHeader> headers = message.getPayload().getHeaders();

            if (!headers.isEmpty()) {
                for (MessagePartHeader header : headers) {
                    String name = header.getName();
                    switch (name) {
                        case "From":
                            from = header.getValue();
                            break;
                        case "To":
                            deliverTo = header.getValue();
                            break;
                        case "Subject":
                            subject = header.getValue();
                            break;
                        case "Date":
                            String date = header.getValue();
                            if(date.contains(","))
                                date = date.substring(date.indexOf(",") + 2,date.length());;
                            String timestampFormat = "dd MMM yyyy HH:mm:ss Z";
                            timestamp = TimeUtils.fromFormattedString(timestampFormat,date) / 1000;
                            break;
                    }
                }
            }
            if (messageParts != null && !messageParts.isEmpty()) {
                byte[] bytes = Base64.decodeBase64(messageParts.get(0).getBody().getData());
                if (bytes != null) {
                    String mailText = new String(bytes);
                    if (!mailText.isEmpty()) {
                        total++;
                        content = mailText;
                        messageList.add(mailText);
                    }
                }
            }
            if(mLastEmailTime < timestamp) mLastEmailTime = timestamp;
            this.output(new Email(content, AppUtils.APP_PACKAGE_GMAIL, from, deliverTo, subject, timestamp));
        }
    }

    //Reset the value for from and to
    mBegin = 0;
    mEnd = 0;
    return messageList;
}
 
Example #3
Source File: GoogleMailIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
private static boolean idInList(String testEmailId, ListMessagesResponse listOfMessages) {
    Assert.assertNotNull("list result", listOfMessages);
    List<Message> messages = listOfMessages.getMessages();
    if (messages != null) {
        for (Message m : listOfMessages.getMessages()) {
            if (testEmailId.equals(m.getId())) {
                return true;
            }
        }
    }
    return false;
}
 
Example #4
Source File: GmailDownloader.java    From shortyz with GNU General Public License v3.0 4 votes vote down vote up
@Override
public File download(Date date) {
    LOGGER.fine("==starting gmail download....");
    try {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        Date after = calendar.getTime();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        Date before = calendar.getTime();
        String query = "after:"+dateFormat.format(after)+" AND before:"+dateFormat.format(before)+" AND has:attachment AND (filename:.puz OR filename:.jpz)";
        LOGGER.info("Running query: "+query);
        ListMessagesResponse response = gmailService.users()
                .messages()
                .list("me")
                .setQ(query)
                .execute();
        LOGGER.fine("==Found "+ neverNull(response.getMessages()).size()+" messages.");
        for(Message message : neverNull(response.getMessages())){
            String singleFilename = "";

            Message fetched = gmailService.users().messages().get("me", message.getId())
                    .execute();
            LOGGER.fine("==Fetched message "+fetched.getId());
            HashMap<String, MessagePart> toDownload = new HashMap<>();
            scanParts(fetched.getPayload().getParts(), toDownload);
            for(Map.Entry<String, MessagePart> entry : toDownload.entrySet()){
                LOGGER.info("==Reading : "+singleFilename);
                String source = getSender(fetched.getPayload().getHeaders());
                @SuppressWarnings("deprecation") String filename = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" +
                        source+"-"+singleFilename.replaceAll(" ", "") + ".puz";
                File destination = new File(CROSSWORDS, filename);
                if(!destination.exists()){
                    byte[] data = entry.getValue().getBody().getAttachmentId() != null ?
                            gmailService.users().messages().attachments().get("me", message.getId(), entry.getValue().getBody().getAttachmentId()).execute().decodeData()
                            : entry.getValue().getBody().decodeData();
                    Puzzle puzzle = singleFilename.endsWith("jpz") ?
                            JPZIO.readPuzzle(new ByteArrayInputStream(data))
                            : IO.loadNative(new DataInputStream(new ByteArrayInputStream(data)));
                    puzzle.setDate(date);
                    puzzle.setSource(source);
                    puzzle.setSourceUrl("gmail://" + fetched.getId());
                    puzzle.setUpdatable(false);
                    date = getSentDate(fetched.getPayload().getHeaders());
                    LOGGER.info("Downloaded "+filename);
                    IO.save(puzzle, destination);
                }
            }


        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}