added debug in downloadImageFromUrl and date of email to scanned Gmail results
This commit is contained in:
@@ -10,6 +10,7 @@ public class EmailMessage {
|
|||||||
private String messageId;
|
private String messageId;
|
||||||
private String subject;
|
private String subject;
|
||||||
private String historyId;
|
private String historyId;
|
||||||
|
private String date;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||||
List<QRCodeByContentId> qrCodeByContentId;
|
List<QRCodeByContentId> qrCodeByContentId;
|
||||||
@@ -17,10 +18,11 @@ public class EmailMessage {
|
|||||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||||
List<QRCodeByURL> qrCodeByURL;
|
List<QRCodeByURL> qrCodeByURL;
|
||||||
|
|
||||||
public EmailMessage(String messageId, String subject, String historyId) {
|
public EmailMessage(String messageId, String subject, String historyId, String date) {
|
||||||
this.messageId = messageId;
|
this.messageId = messageId;
|
||||||
this.subject = subject;
|
this.subject = subject;
|
||||||
this.historyId = historyId;
|
this.historyId = historyId;
|
||||||
|
this.date = date;
|
||||||
this.qrCodeByContentId = new ArrayList<>();
|
this.qrCodeByContentId = new ArrayList<>();
|
||||||
this.qrCodeByURL = new ArrayList<>();
|
this.qrCodeByURL = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ public class GmailService {
|
|||||||
private static final Logger logger = LoggerFactory.getLogger(GmailService.class);
|
private static final Logger logger = LoggerFactory.getLogger(GmailService.class);
|
||||||
private static final HttpTransport httpTransport = new NetHttpTransport();
|
private static final HttpTransport httpTransport = new NetHttpTransport();
|
||||||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||||
|
private static final long MAX_RESULTS = 100L;
|
||||||
|
|
||||||
private Gmail getGmailService(String accessToken) {
|
private Gmail getGmailService(String accessToken) {
|
||||||
Credential userCredentials = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
|
Credential userCredentials = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
|
||||||
@@ -54,8 +55,7 @@ public class GmailService {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long MAX_RESULTS = 100L;
|
// Scan all emails in the user's inbox.
|
||||||
|
|
||||||
public ScannedGmailResponseDto getEmail(String accessToken) throws IOException, InterruptedException {
|
public ScannedGmailResponseDto getEmail(String accessToken) throws IOException, InterruptedException {
|
||||||
Gmail service = getGmailService(accessToken);
|
Gmail service = getGmailService(accessToken);
|
||||||
logger.info("Gmail service initialized: {}", service);
|
logger.info("Gmail service initialized: {}", service);
|
||||||
@@ -63,12 +63,12 @@ public class GmailService {
|
|||||||
List<EmailMessage> emailMessagesList = new ArrayList<>();
|
List<EmailMessage> emailMessagesList = new ArrayList<>();
|
||||||
String userId = "me";
|
String userId = "me";
|
||||||
String nextPageToken = null;
|
String nextPageToken = null;
|
||||||
|
// Fetching email messages with page token and setting max results, Default value is 100.
|
||||||
do {
|
do {
|
||||||
ListMessagesResponse listResponse = fetchMessages(service, userId, nextPageToken);
|
ListMessagesResponse listResponse = fetchMessages(service, userId, nextPageToken);
|
||||||
List<Message> messages = listResponse.getMessages();
|
List<Message> messages = listResponse.getMessages();
|
||||||
nextPageToken = listResponse.getNextPageToken();
|
nextPageToken = listResponse.getNextPageToken();
|
||||||
|
// Iterate all the messages and add to emailMessagesList only if it has a valid QR code.
|
||||||
for (Message message : messages) {
|
for (Message message : messages) {
|
||||||
EmailMessage emailMessage = processMessage(service, userId, message);
|
EmailMessage emailMessage = processMessage(service, userId, message);
|
||||||
if (emailMessage != null) {
|
if (emailMessage != null) {
|
||||||
@@ -79,15 +79,15 @@ public class GmailService {
|
|||||||
|
|
||||||
return new ScannedGmailResponseDto(emailMessagesList);
|
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 {
|
private ListMessagesResponse fetchMessages(Gmail service, String userId, String pageToken) throws IOException {
|
||||||
return service.users().messages().list(userId)
|
return service.users().messages().list(userId)
|
||||||
.setPageToken(pageToken)
|
.setPageToken(pageToken)
|
||||||
.setMaxResults(MAX_RESULTS)
|
.setMaxResults(MAX_RESULTS)
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
// 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, InterruptedException {
|
private EmailMessage processMessage(Gmail service, String userId, Message message) throws IOException {
|
||||||
message = service.users().messages().get(userId, message.getId()).setFormat("full").execute();
|
message = service.users().messages().get(userId, message.getId()).setFormat("full").execute();
|
||||||
List<MessagePart> parts = message.getPayload().getParts();
|
List<MessagePart> parts = message.getPayload().getParts();
|
||||||
Set<String> attachmentIds = new HashSet<>();
|
Set<String> attachmentIds = new HashSet<>();
|
||||||
@@ -98,19 +98,20 @@ public class GmailService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String subject = getSubject(message);
|
String subject = getHeader(message, "Subject");
|
||||||
|
String emailDate = getHeader(message, "Date");
|
||||||
logger.info("Email Subject: {}", subject);
|
logger.info("Email Subject: {}", subject);
|
||||||
logger.info("Message ID: {}", message.getId());
|
logger.info("Message ID: {}", message.getId());
|
||||||
logger.info("History ID: {}", message.getHistoryId());
|
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);
|
processAttachments(service, message.getId(), parts, attachmentIds, emailMessage);
|
||||||
processImageUrls(imageUrls, emailMessage);
|
processImageUrls(imageUrls, emailMessage);
|
||||||
|
|
||||||
return emailMessage.hasQRCodes() ? emailMessage : null;
|
return emailMessage.hasQRCodes() ? emailMessage : null;
|
||||||
}
|
}
|
||||||
|
// Process all the attachments.
|
||||||
private void processAttachments(Gmail service, String messageId, List<MessagePart> parts, Set<String> attachmentIds, EmailMessage emailMessage) throws IOException {
|
private void processAttachments(Gmail service, String messageId, List<MessagePart> parts, Set<String> attachmentIds, EmailMessage emailMessage) throws IOException {
|
||||||
for (String attachmentId : attachmentIds) {
|
for (String attachmentId : attachmentIds) {
|
||||||
Optional<String> attachment = findAttachmentIdByCid(parts, attachmentId);
|
Optional<String> attachment = findAttachmentIdByCid(parts, attachmentId);
|
||||||
@@ -122,8 +123,8 @@ public class GmailService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Process all the image URLs.
|
||||||
private void processImageUrls(Set<String> imageUrls, EmailMessage emailMessage) throws IOException {
|
private void processImageUrls(Set<String> imageUrls, EmailMessage emailMessage) {
|
||||||
for (String imageUrl : imageUrls) {
|
for (String imageUrl : imageUrls) {
|
||||||
List<String> qrCodeValue = scanQRCodeFromUrl(imageUrl);
|
List<String> qrCodeValue = scanQRCodeFromUrl(imageUrl);
|
||||||
if (!qrCodeValue.isEmpty()) {
|
if (!qrCodeValue.isEmpty()) {
|
||||||
@@ -131,21 +132,22 @@ public class GmailService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Find the header with the given name.
|
||||||
private String getSubject(Message message) {
|
private String getHeader(Message message, String name) {
|
||||||
return message.getPayload().getHeaders().stream()
|
return message.getPayload().getHeaders().stream()
|
||||||
.filter(header -> "Subject".equals(header.getName()))
|
.filter(header -> name.equalsIgnoreCase(header.getName()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.map(MessagePartHeader::getValue)
|
.map(MessagePartHeader::getValue)
|
||||||
.orElse("No Subject");
|
.orElse("No " + name);
|
||||||
}
|
}
|
||||||
|
// Find the attachment ID in the given message part.
|
||||||
private Optional<String> findAttachmentIdByCid(List<MessagePart> parts, String cid) {
|
private Optional<String> findAttachmentIdByCid(List<MessagePart> parts, String cid) {
|
||||||
return parts.stream()
|
return parts.stream()
|
||||||
.flatMap(part -> Stream.concat(findAttachmentIdInCurrentPart(part, cid).stream(), Optional.ofNullable(part.getParts())
|
.flatMap(part -> Stream.concat(findAttachmentIdInCurrentPart(part, cid).stream(), Optional.ofNullable(part.getParts())
|
||||||
.flatMap(subParts -> findAttachmentIdByCid(subParts, cid)).stream()))
|
.flatMap(subParts -> findAttachmentIdByCid(subParts, cid)).stream()))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
}
|
}
|
||||||
|
// Find the attachment ID in the message subpart.
|
||||||
private Optional<String> findAttachmentIdInCurrentPart(MessagePart part, String cid) {
|
private Optional<String> findAttachmentIdInCurrentPart(MessagePart part, String cid) {
|
||||||
return Optional.ofNullable(part.getHeaders())
|
return Optional.ofNullable(part.getHeaders())
|
||||||
.flatMap(headers -> headers.stream()
|
.flatMap(headers -> headers.stream()
|
||||||
@@ -153,7 +155,7 @@ public class GmailService {
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
.map(header -> part.getBody().getAttachmentId()));
|
.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) {
|
private boolean isContentIdHeader(MessagePartHeader header, String cid) {
|
||||||
return "Content-ID".equalsIgnoreCase(header.getName()) && header.getValue().contains(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);
|
logger.info("Downloading image from URL: {}", imageUrl);
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI(imageUrl))
|
.uri(new URI(imageUrl))
|
||||||
|
.header("User-Agent", "Mozilla/5.0")
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@@ -207,13 +210,18 @@ public class GmailService {
|
|||||||
logger.warn("Failed to download image. HTTP response code: {}", response.statusCode());
|
logger.warn("Failed to download image. HTTP response code: {}", response.statusCode());
|
||||||
}
|
}
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
logger.warn("Invalid URL: {} -> {}", imageUrl, e.getMessage());
|
logger.error("Invalid URL: {} -> {}", imageUrl, e.getMessage());
|
||||||
} catch (HttpTimeoutException e) {
|
} 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) {
|
} catch (ConnectException e) {
|
||||||
logger.warn("Failed to connect to URL: {} -> {}", imageUrl, e.getMessage());
|
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());
|
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();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -245,16 +253,14 @@ public class GmailService {
|
|||||||
qrCodeValues.add(result.getText());
|
qrCodeValues.add(result.getText());
|
||||||
logger.info("Detected QR code: {}", result.getText());
|
logger.info("Detected QR code: {}", result.getText());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
logger.info("No QR codes found in the image");
|
|
||||||
}
|
}
|
||||||
} catch (NotFoundException e) {
|
} catch (NotFoundException e) {
|
||||||
logger.info("No QR codes found in the image");
|
// No QR codes found
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error decoding QR codes", e);
|
logger.error("Error decoding QR codes", e);
|
||||||
}
|
}
|
||||||
|
if (!qrCodeValues.isEmpty())
|
||||||
logger.info("Total QR codes found: {}", qrCodeValues.size());
|
logger.info("Total QR codes found: {}", qrCodeValues.size());
|
||||||
return qrCodeValues;
|
return qrCodeValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user