james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [2/7] james-project git commit: JAMES-2177 Logging Runtime exception with context in JMAP
Date Wed, 11 Oct 2017 03:09:30 GMT
JAMES-2177 Logging Runtime exception with context in JMAP

Before, RuntimeException were logged by Jetty, outside of the scope of the MDC, hence we were
loosing context


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/11621472
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/11621472
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/11621472

Branch: refs/heads/master
Commit: 11621472a359330ecfbb39126a89f079371d8ede
Parents: fb40bd9
Author: benwa <btellier@linagora.com>
Authored: Tue Oct 10 10:16:20 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Wed Oct 11 10:08:40 2017 +0700

----------------------------------------------------------------------
 .../apache/james/metrics/api/MetricFactory.java |  4 ++
 .../james/metrics/api/NoopMetricFactory.java    |  7 +++
 .../dropwizard/DropWizardMetricFactory.java     | 12 +++++
 .../java/org/apache/james/util/MDCBuilder.java  | 15 ++++++
 .../james/jmap/methods/GetMailboxesMethod.java  | 39 ++++++--------
 .../jmap/methods/GetMessageListMethod.java      | 56 +++++++++-----------
 .../james/jmap/methods/GetMessagesMethod.java   | 36 +++++--------
 .../jmap/methods/GetVacationResponseMethod.java | 28 ++++------
 .../james/jmap/methods/SetMailboxesMethod.java  | 34 +++++-------
 .../SetMessagesDestructionProcessor.java        | 17 +++---
 .../james/jmap/methods/SetMessagesMethod.java   | 36 +++++--------
 .../jmap/methods/SetVacationResponseMethod.java | 51 ++++++++----------
 12 files changed, 159 insertions(+), 176 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/MetricFactory.java
----------------------------------------------------------------------
diff --git a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/MetricFactory.java
b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/MetricFactory.java
index 2115197..10937b0 100644
--- a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/MetricFactory.java
+++ b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/MetricFactory.java
@@ -19,9 +19,13 @@
 
 package org.apache.james.metrics.api;
 
+import java.util.function.Supplier;
+
 public interface MetricFactory {
 
     Metric generate(String name);
 
     TimeMetric timer(String name);
+
+    <T> T withMetric(String name, Supplier<T> operation);
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/NoopMetricFactory.java
----------------------------------------------------------------------
diff --git a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/NoopMetricFactory.java
b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/NoopMetricFactory.java
index 6b76cad..5afacd4 100644
--- a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/NoopMetricFactory.java
+++ b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/NoopMetricFactory.java
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.james.metrics.api;
 
+import java.util.function.Supplier;
+
 public class NoopMetricFactory implements MetricFactory {
 
     @Override
@@ -55,4 +57,9 @@ public class NoopMetricFactory implements MetricFactory {
             return 0;
         }
     }
+
+    @Override
+    public <T> T withMetric(String name, Supplier<T> operation) {
+        return operation.get();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
----------------------------------------------------------------------
diff --git a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
index 55da0dc..eafb860 100644
--- a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
+++ b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
@@ -19,6 +19,8 @@
 
 package org.apache.james.metrics.dropwizard;
 
+import java.util.function.Supplier;
+
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
@@ -53,6 +55,16 @@ public class DropWizardMetricFactory implements MetricFactory {
         return new DropWizardTimeMetric(name, metricRegistry.timer(name).time());
     }
 
+    @Override
+    public <T> T withMetric(String name, Supplier<T> operation) {
+        TimeMetric timer = timer(name);
+        try {
+            return operation.get();
+        } finally {
+            timer.stopAndPublish();
+        }
+    }
+
     @PostConstruct
     public void start() throws ConfigurationException {
         jmxReporter.start();

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
index bf2d334..14aefd1 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
@@ -32,11 +32,26 @@ import org.slf4j.MDC;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Supplier;
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
 public class MDCBuilder {
 
+    public static <T> T withMdc(MDCBuilder mdcBuilder, Supplier<T> answerSupplier)
{
+        try (Closeable closeable = mdcBuilder.build()) {
+            try {
+                return answerSupplier.get();
+            } catch (RuntimeException e) {
+                LOGGER.error("Got error, logging its context", e);
+                throw e;
+            }
+        } catch (IOException e) {
+            throw Throwables.propagate(e);
+        }
+    }
+
     public static final String HOST = "host";
     public static final String IP = "ip";
     public static final String PROTOCOL = "protocol";

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
index b4cad15..556adec 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Optional;
@@ -42,7 +40,6 @@ import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxMetaData;
 import org.apache.james.mailbox.model.search.MailboxQuery;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 import org.apache.james.util.OptionalUtils;
 
@@ -83,25 +80,23 @@ public class GetMailboxesMethod implements Method {
     public Stream<JmapResponse> process(JmapRequest request, ClientId clientId, MailboxSession
mailboxSession) {
         Preconditions.checkArgument(request instanceof GetMailboxesRequest);
         GetMailboxesRequest mailboxesRequest = (GetMailboxesRequest) request;
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "GET_MAILBOXES")
-                     .addContext("accountId", mailboxesRequest.getAccountId())
-                     .addContext("mailboxIds", mailboxesRequest.getIds())
-                     .addContext("properties", mailboxesRequest.getProperties())
-                     .build()) {
-            return Stream.of(
-                    JmapResponse.builder().clientId(clientId)
-                    .response(getMailboxesResponse(mailboxesRequest, mailboxSession))
-                    .properties(mailboxesRequest.getProperties().map(this::ensureContainsId))
-                    .responseName(RESPONSE_NAME)
-                    .build());
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "GET_MAILBOXES")
+                    .addContext("accountId", mailboxesRequest.getAccountId())
+                    .addContext("mailboxIds", mailboxesRequest.getIds())
+                    .addContext("properties", mailboxesRequest.getProperties()),
+                () -> process(clientId, mailboxSession, mailboxesRequest)));
+    }
+
+    private Stream<JmapResponse> process(ClientId clientId, MailboxSession mailboxSession,
GetMailboxesRequest mailboxesRequest) {
+        return Stream.of(
+            JmapResponse.builder().clientId(clientId)
+                .response(getMailboxesResponse(mailboxesRequest, mailboxSession))
+                .properties(mailboxesRequest.getProperties().map(this::ensureContainsId))
+                .responseName(RESPONSE_NAME)
+                .build());
     }
 
     private Set<MailboxProperty> ensureContainsId(Set<MailboxProperty> input)
{

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
index a128fcc..1958e1f 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
@@ -46,7 +44,6 @@ import org.apache.james.mailbox.model.MailboxId.Factory;
 import org.apache.james.mailbox.model.MultimailboxesSearchQuery;
 import org.apache.james.mailbox.model.SearchQuery;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 
 import com.github.steveash.guavate.Guavate;
@@ -93,35 +90,34 @@ public class GetMessageListMethod implements Method {
     @Override
     public Stream<JmapResponse> process(JmapRequest request, ClientId clientId, MailboxSession
mailboxSession) {
         Preconditions.checkArgument(request instanceof GetMessageListRequest);
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
-        
+
         GetMessageListRequest messageListRequest = (GetMessageListRequest) request;
+
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "GET_MESSAGE_LIST")
+                    .addContext("accountId", messageListRequest.getAccountId())
+                    .addContext("limit", messageListRequest.getLimit())
+                    .addContext("anchor", messageListRequest.getAnchor())
+                    .addContext("offset", messageListRequest.getAnchorOffset())
+                    .addContext("properties", messageListRequest.getFetchMessageProperties())
+                    .addContext("position", messageListRequest.getPosition())
+                    .addContext("filters", messageListRequest.getFilter())
+                    .addContext("sorts", messageListRequest.getSort())
+                    .addContext("isFetchMessage", messageListRequest.isFetchMessages())
+                    .addContext("isCollapseThread", messageListRequest.isCollapseThreads()),
+                () -> process(clientId, mailboxSession, messageListRequest)));
+    }
+
+    private Stream<JmapResponse> process(ClientId clientId, MailboxSession mailboxSession,
GetMessageListRequest messageListRequest) {
         GetMessageListResponse messageListResponse = getMessageListResponse(messageListRequest,
mailboxSession);
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "GET_MESSAGE_LIST")
-                     .addContext("accountId", messageListRequest.getAccountId())
-                     .addContext("limit", messageListRequest.getLimit())
-                     .addContext("anchor", messageListRequest.getAnchor())
-                     .addContext("offset", messageListRequest.getAnchorOffset())
-                     .addContext("properties", messageListRequest.getFetchMessageProperties())
-                     .addContext("position", messageListRequest.getPosition())
-                     .addContext("filters", messageListRequest.getFilter())
-                     .addContext("sorts", messageListRequest.getSort())
-                     .addContext("isFetchMessage", messageListRequest.isFetchMessages())
-                     .addContext("isCollapseThread", messageListRequest.isCollapseThreads())
-                     .build()) {
-            Stream<JmapResponse> jmapResponse = Stream.of(JmapResponse.builder().clientId(clientId)
-                    .response(messageListResponse)
-                    .responseName(RESPONSE_NAME)
-                    .build());
-            return Stream.concat(jmapResponse,
-                    processGetMessages(messageListRequest, messageListResponse, clientId,
mailboxSession));
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+        Stream<JmapResponse> jmapResponse = Stream.of(JmapResponse.builder().clientId(clientId)
+            .response(messageListResponse)
+            .responseName(RESPONSE_NAME)
+            .build());
+        return Stream.concat(jmapResponse,
+            processGetMessages(messageListRequest, messageListResponse, clientId, mailboxSession));
     }
 
     private GetMessageListResponse getMessageListResponse(GetMessageListRequest messageListRequest,
MailboxSession mailboxSession) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
index aa4a439..e1e5fc9 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
@@ -46,7 +44,6 @@ import org.apache.james.mailbox.model.FetchGroupImpl;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MessageResult;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -97,25 +94,20 @@ public class GetMessagesMethod implements Method {
 
         GetMessagesRequest getMessagesRequest = (GetMessagesRequest) request;
         MessageProperties outputProperties = getMessagesRequest.getProperties().toOutputProperties();
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "GET_MESSAGES")
-                     .addContext("accountId", getMessagesRequest.getAccountId())
-                     .addContext("ids", getMessagesRequest.getIds())
-                     .addContext("properties", getMessagesRequest.getProperties())
-                     .build()) {
-            return Stream.of(JmapResponse.builder().clientId(clientId)
-                            .response(getMessagesResponse(mailboxSession, getMessagesRequest))
-                            .responseName(RESPONSE_NAME)
-                            .properties(outputProperties.getOptionalMessageProperties())
-                            .filterProvider(buildOptionalHeadersFilteringFilterProvider(outputProperties))
-                            .build());
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "GET_MESSAGES")
+                    .addContext("accountId", getMessagesRequest.getAccountId())
+                    .addContext("ids", getMessagesRequest.getIds())
+                    .addContext("properties", getMessagesRequest.getProperties()),
+                () -> Stream.of(JmapResponse.builder().clientId(clientId)
+                    .response(getMessagesResponse(mailboxSession, getMessagesRequest))
+                    .responseName(RESPONSE_NAME)
+                    .properties(outputProperties.getOptionalMessageProperties())
+                    .filterProvider(buildOptionalHeadersFilteringFilterProvider(outputProperties))
+                    .build())));
     }
 
     private Optional<SimpleFilterProvider> buildOptionalHeadersFilteringFilterProvider(MessageProperties
properties) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java
index 6653976..941bed8 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.stream.Stream;
 
 import javax.inject.Inject;
@@ -34,12 +32,10 @@ import org.apache.james.jmap.model.GetVacationResponse;
 import org.apache.james.jmap.model.VacationResponse;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 import org.apache.james.util.date.ZonedDateTimeProvider;
 
 import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
 
 public class GetVacationResponseMethod implements Method {
 
@@ -74,21 +70,15 @@ public class GetVacationResponseMethod implements Method {
         Preconditions.checkNotNull(mailboxSession);
         Preconditions.checkArgument(request instanceof GetVacationRequest);
 
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "VACATION")
-                     .build()) {
-            return Stream.of(JmapResponse.builder()
-                .clientId(clientId)
-                .responseName(RESPONSE_NAME)
-                .response(process(mailboxSession))
-                .build());
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "VACATION"),
+                () -> Stream.of(JmapResponse.builder()
+                    .clientId(clientId)
+                    .responseName(RESPONSE_NAME)
+                    .response(process(mailboxSession))
+                    .build())));
     }
 
     private GetVacationResponse process(MailboxSession mailboxSession) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMailboxesMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMailboxesMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMailboxesMethod.java
index f73f788..133cc1b 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMailboxesMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMailboxesMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.Set;
 import java.util.stream.Stream;
 
@@ -31,12 +29,10 @@ import org.apache.james.jmap.model.SetMailboxesRequest;
 import org.apache.james.jmap.model.SetMailboxesResponse;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
 
 public class SetMailboxesMethod implements Method {
 
@@ -68,26 +64,20 @@ public class SetMailboxesMethod implements Method {
         Preconditions.checkNotNull(clientId);
         Preconditions.checkNotNull(mailboxSession);
         Preconditions.checkArgument(request instanceof SetMailboxesRequest);
-        
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
+
         SetMailboxesRequest setMailboxesRequest = (SetMailboxesRequest) request;
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "SET_MAILBOXES")
-                     .addContext("create", setMailboxesRequest.getCreate())
-                     .addContext("update", setMailboxesRequest.getUpdate())
-                     .addContext("destroy", setMailboxesRequest.getDestroy())
-                     .build()) {
-            return Stream.of(
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "SET_MAILBOXES")
+                    .addContext("create", setMailboxesRequest.getCreate())
+                    .addContext("update", setMailboxesRequest.getUpdate())
+                    .addContext("destroy", setMailboxesRequest.getDestroy()),
+                () -> Stream.of(
                     JmapResponse.builder().clientId(clientId)
-                    .response(setMailboxesResponse(setMailboxesRequest, mailboxSession))
-                    .responseName(RESPONSE_NAME)
-                    .build());
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+                        .response(setMailboxesResponse(setMailboxesRequest, mailboxSession))
+                        .responseName(RESPONSE_NAME)
+                        .build())));
     }
 
     private SetMailboxesResponse setMailboxesResponse(SetMailboxesRequest request, MailboxSession
mailboxSession) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
index 844319e..7cb1454 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
@@ -62,15 +62,14 @@ public class SetMessagesDestructionProcessor implements SetMessagesProcessor
{
 
     @Override
     public SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession)
{
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMessageDestructionProcessor");
-
-        SetMessagesResponse result = request.getDestroy().stream()
-            .map(delete(mailboxSession))
-            .reduce(SetMessagesResponse.builder(), SetMessagesResponse.Builder::accumulator,
SetMessagesResponse.Builder::combiner)
-            .build();
-
-        timeMetric.stopAndPublish();
-        return result;
+        return TimeMetric.withMetric(
+            metricFactory.timer(JMAP_PREFIX + "SetMessageDestructionProcessor"),
+            () -> request.getDestroy().stream()
+                .map(delete(mailboxSession))
+                .reduce(SetMessagesResponse.builder(),
+                    SetMessagesResponse.Builder::accumulator,
+                    SetMessagesResponse.Builder::combiner)
+                .build());
     }
 
     private Function<? super MessageId, SetMessagesResponse> delete(MailboxSession
mailboxSession) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesMethod.java
index def3929..508e5ff 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.Set;
 import java.util.stream.Stream;
 
@@ -31,12 +29,10 @@ import org.apache.james.jmap.model.SetMessagesRequest;
 import org.apache.james.jmap.model.SetMessagesResponse;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
 
 public class SetMessagesMethod implements Method {
 
@@ -64,27 +60,21 @@ public class SetMessagesMethod implements Method {
 
     public Stream<JmapResponse> process(JmapRequest request, ClientId clientId, MailboxSession
mailboxSession) {
         Preconditions.checkArgument(request instanceof SetMessagesRequest);
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
         SetMessagesRequest setMessagesRequest = (SetMessagesRequest) request;
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "SET_MESSAGES")
-                     .addContext("accountId", setMessagesRequest.getAccountId())
-                     .addContext("create", setMessagesRequest.getCreate())
-                     .addContext("destroy", setMessagesRequest.getDestroy())
-                     .addContext("ifInState", setMessagesRequest.getIfInState())
-                     .build()) {
-            Stream<JmapResponse> responses = Stream.of(
+
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "SET_MESSAGES")
+                    .addContext("accountId", setMessagesRequest.getAccountId())
+                    .addContext("create", setMessagesRequest.getCreate())
+                    .addContext("destroy", setMessagesRequest.getDestroy())
+                    .addContext("ifInState", setMessagesRequest.getIfInState()),
+                () ->  Stream.of(
                     JmapResponse.builder().clientId(clientId)
-                    .response(setMessagesResponse(setMessagesRequest, mailboxSession))
-                    .responseName(RESPONSE_NAME)
-                    .build());
-            return responses;
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+                        .response(setMessagesResponse(setMessagesRequest, mailboxSession))
+                        .responseName(RESPONSE_NAME)
+                        .build())));
     }
 
     private SetMessagesResponse setMessagesResponse(SetMessagesRequest request, MailboxSession
mailboxSession) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/11621472/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
index ffe30e9..245a1b1 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
@@ -19,8 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import java.io.Closeable;
-import java.io.IOException;
 import java.util.stream.Stream;
 
 import javax.inject.Inject;
@@ -36,11 +34,9 @@ import org.apache.james.jmap.model.SetVacationResponse;
 import org.apache.james.jmap.model.VacationResponse;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.util.MDCBuilder;
 
 import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
 
 public class SetVacationResponseMethod implements Method {
 
@@ -80,33 +76,30 @@ public class SetVacationResponseMethod implements Method {
         Preconditions.checkArgument(request instanceof SetVacationRequest);
         SetVacationRequest setVacationRequest = (SetVacationRequest) request;
 
-        TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + METHOD_NAME.getName());
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(MDCBuilder.ACTION, "SET_VACATION")
-                     .addContext("update", setVacationRequest.getUpdate())
-                     .build()) {
-            if (!setVacationRequest.isValid()) {
-                return Stream.of(JmapResponse
-                    .builder()
-                    .clientId(clientId)
-                    .error(ErrorResponse.builder()
-                        .type(INVALID_ARGUMENTS1)
-                        .description(INVALID_ARGUMENT_DESCRIPTION)
-                        .build())
-                    .build());
-            }
-    
-            return process(clientId,
-                AccountId.fromString(mailboxSession.getUser().getUserName()),
-                setVacationRequest.getUpdate().get(Vacation.ID));
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        } finally {
-            timeMetric.stopAndPublish();
-        }
+        return metricFactory.withMetric(JMAP_PREFIX + METHOD_NAME.getName(),
+            () -> MDCBuilder.withMdc(
+                MDCBuilder.create()
+                    .addContext(MDCBuilder.ACTION, "SET_VACATION")
+                    .addContext("update", setVacationRequest.getUpdate()),
+                () -> process(clientId, mailboxSession, setVacationRequest)));
     }
 
+    private Stream<JmapResponse> process(ClientId clientId, MailboxSession mailboxSession,
SetVacationRequest setVacationRequest) {
+        if (!setVacationRequest.isValid()) {
+            return Stream.of(JmapResponse
+                .builder()
+                .clientId(clientId)
+                .error(ErrorResponse.builder()
+                    .type(INVALID_ARGUMENTS1)
+                    .description(INVALID_ARGUMENT_DESCRIPTION)
+                    .build())
+                .build());
+        }
+
+        return process(clientId,
+            AccountId.fromString(mailboxSession.getUser().getUserName()),
+            setVacationRequest.getUpdate().get(Vacation.ID));
+    }
 
 
     private Stream<JmapResponse> process(ClientId clientId, AccountId accountId, VacationResponse
vacationResponse) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message