Browse code

BM-14760 Fix: try/catch

Thomas Fricker authored on 14/05/2019 07:12:42
Showing 1 changed files
... ...
@@ -768,7 +768,6 @@ public class MailIndexService implements IMailIndexService {
768 768
 		return ret;
769 769
 	}
770 770
 
771
-	@SuppressWarnings({ "unchecked" })
772 771
 	@Override
773 772
 	public SearchResult searchItems(String dirEntryUid, MailboxFolderSearchQuery searchQuery) {
774 773
 		SearchQuery query = searchQuery.query;
... ...
@@ -809,43 +808,12 @@ public class MailIndexService implements IMailIndexService {
809 808
 		List<MessageSearchResult> results = new ArrayList<>();
810 809
 
811 810
 		for (SearchHit sh : searchHits.getHits()) {
812
-			Integer itemId = (Integer) sh.field("itemId").getValue();
813
-			Map<String, Object> source = sh.getSourceAsMap();
814
-			String folderUid = ((String) source.get("id")).split(":")[0];
815
-			String contUid = "mbox_records_" + folderUid;
816
-			String subject = (String) source.get("subject");
817
-			int size = (int) source.get("size");
818
-			ZonedDateTime date = ZonedDateTime.parse((String) source.get("date"));
819
-			Date messageDate = Date.from(date.toInstant());
820
-			List<String> flags = (List<String>) source.get("is");
821
-			boolean seen = flags.contains("seen");
822
-			boolean flagged = flags.contains("flagged");
823
-
824
-			Map<String, String> headers = (Map<String, String>) source.get("headers");
825
-
826
-			Mbox to = Mbox.create("unknown", "unknown");
827
-			MailboxList addrList = LenientAddressBuilder.DEFAULT
828
-					.parseAddressList(Optional.ofNullable(headers.get("to")).orElse("")).flatten();
829
-			if (!addrList.isEmpty()) {
830
-				org.apache.james.mime4j.dom.address.Mailbox mboxFrom = addrList.get(0);
831
-				to = Mbox.create(mboxFrom.getName(), mboxFrom.getAddress());
811
+			try {
812
+				MessageSearchResult msr = createSearchResult(sh);
813
+				results.add(msr);
814
+			} catch (Exception e) {
815
+				logger.warn("Cannot create result object", e);
832 816
 			}
833
-
834
-			Mbox from = Mbox.create("unknown", "unknown");
835
-			org.apache.james.mime4j.dom.address.Mailbox mboxFrom = LenientAddressBuilder.DEFAULT
836
-					.parseMailbox(headers.get("from"));
837
-			String routingType = getRoutingType(mboxFrom.getDomain(), mboxFrom.getAddress());
838
-			from = Mbox.create(mboxFrom.getName(), mboxFrom.getAddress(), routingType);
839
-
840
-			boolean hasAttachment = !((List<String>) source.get("has")).isEmpty();
841
-
842
-			String preview = (String) source.get("preview");
843
-
844
-			MessageSearchResult msr = new MessageSearchResult(contUid, itemId, subject, size, "IPM.Note", messageDate,
845
-					from, to, seen, flagged, hasAttachment, preview);
846
-			logger.info("matching result:{} in folder:{}", msr.subject, folderUid);
847
-			results.add(msr);
848
-
849 817
 		}
850 818
 
851 819
 		SearchResult result = new SearchResult();
... ...
@@ -856,6 +824,46 @@ public class MailIndexService implements IMailIndexService {
856 824
 		return result;
857 825
 	}
858 826
 
827
+	@SuppressWarnings({ "unchecked" })
828
+	private MessageSearchResult createSearchResult(SearchHit sh) {
829
+		Integer itemId = (Integer) sh.field("itemId").getValue();
830
+		Map<String, Object> source = sh.getSourceAsMap();
831
+		String folderUid = ((String) source.get("id")).split(":")[0];
832
+		String contUid = "mbox_records_" + folderUid;
833
+		String subject = (String) source.get("subject");
834
+		logger.info("matching result:{} -> {} in folder:{}", itemId, subject, folderUid);
835
+		int size = (int) source.get("size");
836
+		ZonedDateTime date = ZonedDateTime.parse((String) source.get("date"));
837
+		Date messageDate = Date.from(date.toInstant());
838
+		List<String> flags = (List<String>) source.get("is");
839
+		boolean seen = flags.contains("seen");
840
+		boolean flagged = flags.contains("flagged");
841
+
842
+		Map<String, String> headers = (Map<String, String>) source.get("headers");
843
+
844
+		Mbox to = Mbox.create("unknown", "unknown");
845
+		MailboxList addrList = LenientAddressBuilder.DEFAULT
846
+				.parseAddressList(Optional.ofNullable(headers.get("to")).orElse("")).flatten();
847
+		if (!addrList.isEmpty()) {
848
+			org.apache.james.mime4j.dom.address.Mailbox mboxFrom = addrList.get(0);
849
+			to = Mbox.create(mboxFrom.getName(), mboxFrom.getAddress());
850
+		}
851
+
852
+		Mbox from = Mbox.create("unknown", "unknown");
853
+		org.apache.james.mime4j.dom.address.Mailbox mboxFrom = LenientAddressBuilder.DEFAULT
854
+				.parseMailbox(headers.get("from"));
855
+		String routingType = getRoutingType(mboxFrom.getDomain(), mboxFrom.getAddress());
856
+		from = Mbox.create(mboxFrom.getName(), mboxFrom.getAddress(), routingType);
857
+
858
+		boolean hasAttachment = !((List<String>) source.get("has")).isEmpty();
859
+
860
+		String preview = (String) source.get("preview");
861
+
862
+		MessageSearchResult msr = new MessageSearchResult(contUid, itemId, subject, size, "IPM.Note", messageDate, from,
863
+				to, seen, flagged, hasAttachment, preview);
864
+		return msr;
865
+	}
866
+
859 867
 	private String getRoutingType(String domain, String address) {
860 868
 		try {
861 869
 			IDirectory dir = ServerSideServiceProvider.getProvider(SecurityContext.SYSTEM).instance(IDirectory.class,