/** * Try to determine what address from the to address list should be used as main TO address for * the different EmailReceiver's. In case the references header is null we try to find a * matching email access point. * * @param recipients The recipients from the TO address list * @param references The references header * @return A hopefully valid email address as a string */ private String toaddress(final Address[] recipients, String references) { String result = ""; // No recipients found return n/a if (recipients == null || recipients.length == 0) return "n/a"; if (!hasStreamflowReference(references)) { Organizations.Data organizations = module .unitOfWorkFactory() .currentUnitOfWork() .get(Organizations.Data.class, OrganizationsEntity.ORGANIZATIONS_ID); EmailAccessPoints.Data emailAccessPoints = (EmailAccessPoints.Data) organizations.organization().get(); Iterable<EmailAccessPoint> possibleAccesspoints = Iterables.filter( new Specification<EmailAccessPoint>() { public boolean satisfiedBy(final EmailAccessPoint accessPoint) { return Iterables.matchesAny( new Specification<Address>() { public boolean satisfiedBy(Address address) { return ((InternetAddress) address) .getAddress() .equalsIgnoreCase(accessPoint.getDescription()); } }, Arrays.asList(recipients)); } }, emailAccessPoints.emailAccessPoints().toList()); if (Iterables.count(possibleAccesspoints) > 0) result = Iterables.first(possibleAccesspoints).getDescription(); else result = ((InternetAddress) recipients[0]).getAddress(); } else { result = ((InternetAddress) recipients[0]).getAddress(); } return Strings.empty(result) ? "n/a" : result.toLowerCase(); }
public void activate() throws Exception { circuitBreaker = descriptor.metaInfo(CircuitBreaker.class); logger = LoggerFactory.getLogger(ReceiveMailService.class); if (config.configuration().enabled().get()) { UnitOfWork uow = module .unitOfWorkFactory() .newUnitOfWork(newUsecase("Create Streamflow support structure")); RoleMap.newCurrentRoleMap(); RoleMap.current().set(uow.get(UserAuthentication.class, UserEntity.ADMINISTRATOR_USERNAME)); RoleMap.current().set(new UserPrincipal(UserEntity.ADMINISTRATOR_USERNAME)); Organizations.Data orgs = uow.get(OrganizationsEntity.class, OrganizationsEntity.ORGANIZATIONS_ID); OrganizationEntity org = (OrganizationEntity) orgs.organization().get(); // check for the existance of support structure for mails that cannot be parsed RoleMap.current().set(org.getAdministratorRole()); OrganizationalUnit ou = null; Project project = null; CaseType caseType = null; try { try { ou = org.getOrganizationalUnitByName( systemDefaults.config().configuration().supportOrganizationName().get()); } catch (IllegalArgumentException iae) { ou = org.createOrganizationalUnit( systemDefaults.config().configuration().supportOrganizationName().get()); } try { project = ou.getProjectByName( systemDefaults.config().configuration().supportProjectName().get()); } catch (IllegalArgumentException iae) { project = ou.createProject( systemDefaults.config().configuration().supportProjectName().get()); } try { caseType = project.getCaseTypeByName( systemDefaults .config() .configuration() .supportCaseTypeForIncomingEmailName() .get()); } catch (IllegalArgumentException iae) { caseType = ou.createCaseType( systemDefaults .config() .configuration() .supportCaseTypeForIncomingEmailName() .get()); project.addSelectedCaseType(caseType); project.addMember(RoleMap.current().get(Member.class)); } } finally { uow.complete(); RoleMap.clearCurrentRoleMap(); } // Authenticator authenticator = new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( config.configuration().user().get(), config.configuration().password().get()); } }; props = new Properties(); String protocol = config.configuration().protocol().get(); props.put("mail." + protocol + ".host", config.configuration().host().get()); props.put("mail.transport.protocol", protocol); props.put("mail." + protocol + ".auth", "true"); props.put("mail." + protocol + ".port", config.configuration().port().get()); if (config.configuration().useSSL().get()) { props.setProperty( "mail." + protocol + ".socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail." + protocol + ".socketFactory.fallback", "false"); props.setProperty( "mail." + protocol + ".socketFactory.port", "" + config.configuration().port().get()); } url = new URLName( protocol, config.configuration().host().get(), config.configuration().port().get(), "", config.configuration().user().get(), config.configuration().password().get()); circuitBreaker.addVetoableChangeListener(this); circuitBreaker.turnOn(); long sleep = config.configuration().sleepPeriod().get(); logger.info( "Starting scheduled mail receiver thread. Checking every: " + (sleep == 0 ? 10 : sleep) + " min"); receiverExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("ReceiveMail")); receiverExecutor.scheduleWithFixedDelay( this, 0, (sleep == 0 ? 10 : sleep), TimeUnit.MINUTES); } }