From 0fde70a4b66e699a35e978dc9c7ced8ed5a670d2 Mon Sep 17 00:00:00 2001 From: heyethereum Date: Sun, 28 Jul 2024 10:59:45 +0800 Subject: [PATCH] added debug in downloadImageFromUrl and date of email to scanned Gmail results --- .../safeqr/app/gmail/model/EmailMessage.java | 4 +- .../app/gmail/service/GmailService.java | 58 ++++++++++--------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/safeqr/app/gmail/model/EmailMessage.java b/src/main/java/com/safeqr/app/gmail/model/EmailMessage.java index 5dfb87d..79a13eb 100644 --- a/src/main/java/com/safeqr/app/gmail/model/EmailMessage.java +++ b/src/main/java/com/safeqr/app/gmail/model/EmailMessage.java @@ -10,6 +10,7 @@ public class EmailMessage { private String messageId; private String subject; private String historyId; + private String date; @JsonInclude(JsonInclude.Include.NON_EMPTY) List qrCodeByContentId; @@ -17,10 +18,11 @@ public class EmailMessage { @JsonInclude(JsonInclude.Include.NON_EMPTY) List qrCodeByURL; - public EmailMessage(String messageId, String subject, String historyId) { + public EmailMessage(String messageId, String subject, String historyId, String date) { this.messageId = messageId; this.subject = subject; this.historyId = historyId; + this.date = date; this.qrCodeByContentId = new ArrayList<>(); this.qrCodeByURL = new ArrayList<>(); } diff --git a/src/main/java/com/safeqr/app/gmail/service/GmailService.java b/src/main/java/com/safeqr/app/gmail/service/GmailService.java index c3b8599..676535d 100644 --- a/src/main/java/com/safeqr/app/gmail/service/GmailService.java +++ b/src/main/java/com/safeqr/app/gmail/service/GmailService.java @@ -46,6 +46,7 @@ public class GmailService { private static final Logger logger = LoggerFactory.getLogger(GmailService.class); private static final HttpTransport httpTransport = new NetHttpTransport(); private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); + private static final long MAX_RESULTS = 100L; private Gmail getGmailService(String accessToken) { Credential userCredentials = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken); @@ -54,8 +55,7 @@ public class GmailService { .build(); } - private static final long MAX_RESULTS = 100L; - + // Scan all emails in the user's inbox. public ScannedGmailResponseDto getEmail(String accessToken) throws IOException, InterruptedException { Gmail service = getGmailService(accessToken); logger.info("Gmail service initialized: {}", service); @@ -63,12 +63,12 @@ public class GmailService { List emailMessagesList = new ArrayList<>(); String userId = "me"; String nextPageToken = null; - + // Fetching email messages with page token and setting max results, Default value is 100. do { ListMessagesResponse listResponse = fetchMessages(service, userId, nextPageToken); List messages = listResponse.getMessages(); nextPageToken = listResponse.getNextPageToken(); - + // Iterate all the messages and add to emailMessagesList only if it has a valid QR code. for (Message message : messages) { EmailMessage emailMessage = processMessage(service, userId, message); if (emailMessage != null) { @@ -79,15 +79,15 @@ public class GmailService { return new ScannedGmailResponseDto(emailMessagesList); } - + // Fetching email messages with page token and setting max results private ListMessagesResponse fetchMessages(Gmail service, String userId, String pageToken) throws IOException { return service.users().messages().list(userId) .setPageToken(pageToken) .setMaxResults(MAX_RESULTS) .execute(); } - - private EmailMessage processMessage(Gmail service, String userId, Message message) throws IOException, InterruptedException { + // Processing email message and returning EmailMessage object if it has a valid QR code. + private EmailMessage processMessage(Gmail service, String userId, Message message) throws IOException { message = service.users().messages().get(userId, message.getId()).setFormat("full").execute(); List parts = message.getPayload().getParts(); Set attachmentIds = new HashSet<>(); @@ -98,19 +98,20 @@ public class GmailService { return null; } - String subject = getSubject(message); + String subject = getHeader(message, "Subject"); + String emailDate = getHeader(message, "Date"); logger.info("Email Subject: {}", subject); logger.info("Message ID: {}", message.getId()); logger.info("History ID: {}", message.getHistoryId()); - EmailMessage emailMessage = new EmailMessage(message.getId(), subject, String.valueOf(message.getHistoryId())); + EmailMessage emailMessage = new EmailMessage(message.getId(), subject, String.valueOf(message.getHistoryId()), emailDate); processAttachments(service, message.getId(), parts, attachmentIds, emailMessage); processImageUrls(imageUrls, emailMessage); return emailMessage.hasQRCodes() ? emailMessage : null; } - + // Process all the attachments. private void processAttachments(Gmail service, String messageId, List parts, Set attachmentIds, EmailMessage emailMessage) throws IOException { for (String attachmentId : attachmentIds) { Optional attachment = findAttachmentIdByCid(parts, attachmentId); @@ -122,8 +123,8 @@ public class GmailService { } } } - - private void processImageUrls(Set imageUrls, EmailMessage emailMessage) throws IOException { + // Process all the image URLs. + private void processImageUrls(Set imageUrls, EmailMessage emailMessage) { for (String imageUrl : imageUrls) { List qrCodeValue = scanQRCodeFromUrl(imageUrl); if (!qrCodeValue.isEmpty()) { @@ -131,21 +132,22 @@ public class GmailService { } } } - - private String getSubject(Message message) { + // Find the header with the given name. + private String getHeader(Message message, String name) { return message.getPayload().getHeaders().stream() - .filter(header -> "Subject".equals(header.getName())) + .filter(header -> name.equalsIgnoreCase(header.getName())) .findFirst() .map(MessagePartHeader::getValue) - .orElse("No Subject"); + .orElse("No " + name); } + // Find the attachment ID in the given message part. private Optional findAttachmentIdByCid(List parts, String cid) { return parts.stream() .flatMap(part -> Stream.concat(findAttachmentIdInCurrentPart(part, cid).stream(), Optional.ofNullable(part.getParts()) .flatMap(subParts -> findAttachmentIdByCid(subParts, cid)).stream())) .findFirst(); } - + // Find the attachment ID in the message subpart. private Optional findAttachmentIdInCurrentPart(MessagePart part, String cid) { return Optional.ofNullable(part.getHeaders()) .flatMap(headers -> headers.stream() @@ -153,7 +155,7 @@ public class GmailService { .findFirst() .map(header -> part.getBody().getAttachmentId())); } - + // Check if the header is a Content-ID header with the given CID. private boolean isContentIdHeader(MessagePartHeader header, String cid) { return "Content-ID".equalsIgnoreCase(header.getName()) && header.getValue().contains(cid); } @@ -195,6 +197,7 @@ public class GmailService { logger.info("Downloading image from URL: {}", imageUrl); HttpRequest request = HttpRequest.newBuilder() .uri(new URI(imageUrl)) + .header("User-Agent", "Mozilla/5.0") .GET() .build(); @@ -207,13 +210,18 @@ public class GmailService { logger.warn("Failed to download image. HTTP response code: {}", response.statusCode()); } } catch (URISyntaxException e) { - logger.warn("Invalid URL: {} -> {}", imageUrl, e.getMessage()); + logger.error("Invalid URL: {} -> {}", imageUrl, e.getMessage()); } catch (HttpTimeoutException e) { - logger.warn("Request timed out for URL: {} -> {}", imageUrl, e.getMessage()); + logger.error("Request timed out for URL: {} -> {}", imageUrl, e.getMessage()); } catch (ConnectException e) { logger.warn("Failed to connect to URL: {} -> {}", imageUrl, e.getMessage()); - } catch (IOException | InterruptedException e) { + } catch (IOException e) { logger.warn("Error downloading image from URL: {} -> {}", imageUrl, e.getMessage()); + if (Thread.currentThread().isInterrupted()) { + logger.warn("Thread was interrupted during IO operation for URL: {}", imageUrl); + } + } catch (InterruptedException e) { + logger.warn("Thread was interrupted during HTTP request for URL: {} -> {}", imageUrl, e.getMessage()); Thread.currentThread().interrupt(); } return null; @@ -245,16 +253,14 @@ public class GmailService { qrCodeValues.add(result.getText()); logger.info("Detected QR code: {}", result.getText()); } - } else { - logger.info("No QR codes found in the image"); } } catch (NotFoundException e) { - logger.info("No QR codes found in the image"); + // No QR codes found } catch (Exception e) { logger.error("Error decoding QR codes", e); } - - logger.info("Total QR codes found: {}", qrCodeValues.size()); + if (!qrCodeValues.isEmpty()) + logger.info("Total QR codes found: {}", qrCodeValues.size()); return qrCodeValues; }