@PreDestroy public void stop() { server.ifPresent(Throwing.consumer(JettyHttpServer::stop).sneakyThrow()); }
@Override public void configure(HierarchicalConfiguration config) throws ConfigurationException { server.ifPresent(Throwing.consumer(JettyHttpServer::start).sneakyThrow()); }
public void updateReferences(Map<String, String> headers, MailboxSession session) throws MailboxException { Optional<String> inReplyToId = Optional.ofNullable(headers.get(RFC2822Headers.IN_REPLY_TO)); Optional<String> forwardedId = Optional.ofNullable(headers.get(X_FORWARDED_ID_HEADER)); inReplyToId.ifPresent(Throwing.consumer((String id) -> updateAnswered(id, session)).sneakyThrow()); forwardedId.ifPresent(Throwing.consumer((String id) -> updateForwarded(id, session)).sneakyThrow()); }
private void doStop() { try { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); registeredKeys.forEach(Throwing.consumer(key -> mBeanServer.unregisterMBean(new ObjectName(key)))); registeredKeys.clear(); jmxConnectorServer.stop(); restrictingRMISocketFactory.getSockets() .forEach(Throwing.consumer(ServerSocket::close) .sneakyThrow()); } catch (Exception e) { throw new RuntimeException(e); } }
protected void addConfiguredDomains(List<Domain> domains) { domains.stream() .filter(Throwing.predicate((Domain domain) -> !containsDomainInternal(domain)).sneakyThrow()) .forEach(Throwing.consumer(this::addDomain).sneakyThrow()); }
@Override public void removeAll() throws MessagingException { ImmutableList.copyOf(list()) .forEach(Throwing.<MailKey>consumer(this::remove).sneakyThrow()); } }
@Override public void setGlobalMaxMessageCount(SerializableQuotaValue<QuotaCount> maxGlobalMessageCount) throws MailboxException { maxGlobalMessageCount.toValue(QuotaCount::count, QuotaCount.unlimited()) .ifPresent(Throwing.consumer(maxQuotaManager::setGlobalMaxMessage).sneakyThrow()); }
@Override public void setGlobalMaxStorage(SerializableQuotaValue<QuotaSize> maxGlobalSize) throws MailboxException { maxGlobalSize.toValue(QuotaSize::size, QuotaSize.unlimited()) .ifPresent(Throwing.consumer(maxQuotaManager::setGlobalMaxStorage).sneakyThrow()); } }
public void deleteMail(MailRepositoryPath path, MailKey mailKey) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { getRepositories(path) .forEach(Throwing.consumer((MailRepository repository) -> repository.remove(mailKey)).sneakyThrow()); }
@Override public void service(Mail mail) throws MessagingException { if (mail.getAttribute(attribute) == null) { return; } try { getCalendarMap(mail) .values() .stream() .findAny() .ifPresent(Throwing.<Calendar>consumer(calendar -> writeToHeaders(calendar, mail)) .sneakyThrow()); } catch (ClassCastException e) { LOGGER.error("Received a mail with {} not being an ICAL object for mail {}", attribute, mail.getName(), e); } }
private void store(Mail mail, MailRepositoryUrl url) throws MessagingException { try { Optional<MailRepository> mailRepository = retrieveRepository(url); if (!mailRepository.isPresent()) { LOGGER.warn("'{}' mail repository does not exist and will not be created. Mail {} will not be stored in it.", url, mail.getName()); } ThrowingConsumer<MailRepository> storingConsumer = repository -> repository.store(mail); mailRepository.ifPresent(Throwing.consumer(storingConsumer).sneakyThrow()); } catch (MailRepositoryStore.MailRepositoryStoreException e) { throw new MessagingException("Error while selecting url " + url, e); } }
@Override public void setMaxMessageCount(String quotaRoot, SerializableQuotaValue<QuotaCount> maxMessageCount) throws MailboxException { maxMessageCount.toValue(QuotaCount::count, QuotaCount.unlimited()) .ifPresent( Throwing.consumer( (QuotaCount value) -> maxQuotaManager.setMaxMessage(quotaRootResolver.fromString(quotaRoot), value)) .sneakyThrow()); }
@Override public void service(Mail mail) throws MessagingException { MimeMessage message = mail.getMessage(); // Invoke SpamAssassin connection and scan the message SpamAssassinInvoker sa = new SpamAssassinInvoker(metricFactory, spamdHost, spamdPort); mail.getRecipients() .forEach( Throwing.consumer((MailAddress recipient) -> querySpamAssassin(mail, message, sa, recipient)) .sneakyThrow()); }
@Override public void setMaxStorage(String quotaRoot, SerializableQuotaValue<QuotaSize> maxSize) throws MailboxException { maxSize.toValue(QuotaSize::size, QuotaSize.unlimited()) .ifPresent( Throwing.consumer( (QuotaSize value) -> maxQuotaManager.setMaxStorage(quotaRootResolver.fromString(quotaRoot), value)) .sneakyThrow()); }
public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { // Copying a mime message fails when the body is empty // Copying manually the headers is the best alternative... MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); // Avoid future body reads to fail return result; } }
public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { // Copying a mime message fails when the body is empty // Copying manually the headers is the best alternative... MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); // Avoid future body reads to fail return result; } }
@Override public Client get() { TransportClient transportClient = TransportClient.builder().build(); ConsumerChainer<Host> consumer = Throwing.consumer(host -> transportClient .addTransportAddress( new InetSocketTransportAddress( InetAddress.getByName(host.getHostName()), host.getPort()))); hosts.forEach(consumer.sneakyThrow()); return transportClient; } }
@Override public void deleteScript(User user, ScriptName name) throws ScriptNotFoundException, IsActiveException, StorageException { transactionRunner.runAndHandleException(Throwing.<EntityManager>consumer(entityManager -> { Optional<JPASieveScript> sieveScript = findSieveScript(user, name, entityManager); if (!sieveScript.isPresent()) { rollbackTransactionIfActive(entityManager.getTransaction()); throw new ScriptNotFoundException("Unable to find script " + name.getValue() + " for user " + user.asString()); } JPASieveScript sieveScriptToRemove = sieveScript.get(); if (sieveScriptToRemove.isActive()) { rollbackTransactionIfActive(entityManager.getTransaction()); throw new IsActiveException("Unable to delete active script " + name.getValue() + " for user " + user.asString()); } entityManager.remove(sieveScriptToRemove); }).sneakyThrow(), throwStorageException("Unable to delete script " + name.getValue() + " for user " + user.asString())); }
@Override public void setActive(User user, ScriptName name) throws ScriptNotFoundException, StorageException { transactionRunner.runAndHandleException(Throwing.<EntityManager>consumer(entityManager -> { try { if (SieveRepository.NO_SCRIPT_NAME.equals(name)) { switchOffActiveScript(user, entityManager); } else { setActiveScript(user, name, entityManager); } } catch (StorageException | ScriptNotFoundException e) { rollbackTransactionIfActive(entityManager.getTransaction()); throw e; } }).sneakyThrow(), throwStorageException("Unable to set active script " + name.getValue() + " for user " + user.asString())); }
@Override public void putScript(User user, ScriptName name, ScriptContent content) throws StorageException, QuotaExceededException { transactionRunner.runAndHandleException(Throwing.<EntityManager>consumer(entityManager -> { try { haveSpace(user, name, content.length()); JPASieveScript jpaSieveScript = JPASieveScript.builder() .username(user.asString()) .scriptName(name.getValue()) .scriptContent(content) .build(); entityManager.persist(jpaSieveScript); } catch (QuotaExceededException | StorageException e) { rollbackTransactionIfActive(entityManager.getTransaction()); throw e; } }).sneakyThrow(), throwStorageException("Unable to put script for user " + user.asString())); }