View Javadoc
1   package com.meltmedia.jgroups.aws;
2   
3   import com.amazonaws.AmazonServiceException;
4   import com.amazonaws.Request;
5   import com.amazonaws.handlers.RequestHandler;
6   import com.amazonaws.transform.Unmarshaller;
7   import com.amazonaws.util.TimingInfo;
8   import org.jgroups.logging.Log;
9   import org.jgroups.logging.LogFactory;
10  import org.w3c.dom.Node;
11  
12  import javax.xml.transform.Result;
13  import javax.xml.transform.Source;
14  import javax.xml.transform.Transformer;
15  import javax.xml.transform.TransformerFactory;
16  import java.io.StringWriter;
17  
18  /**
19   * This class will log the request along with the response from the AWS ec2 service on fault only.
20   *
21   * @author John McEntire
22   */
23  @SuppressWarnings("deprecation")
24  class AWSFaultLogger implements Unmarshaller<AmazonServiceException, Node>, RequestHandler {
25    private static Log log = LogFactory.getLog(AWS_PING.class);
26    private final ThreadLocal<Request<?>> request = new ThreadLocal<>();
27  
28    @Override
29    public AmazonServiceException unmarshall(Node node) throws Exception {
30      try {
31        final TransformerFactory tfactory = TransformerFactory.newInstance();
32        final Transformer xform = tfactory.newTransformer();
33        final Source src = new javax.xml.transform.dom.DOMSource(node);
34        final StringWriter writer = new StringWriter();
35        final Result result = new javax.xml.transform.stream.StreamResult(writer);
36  
37        xform.transform(src, result);
38        log.error("AWS Exception: [%s] For request [%s]", writer, request.get());
39      } catch (Throwable t) {
40        log.debug("Failed to log xml soap fault message.", t);
41      } finally {
42        request.remove();
43      }
44      return null;
45    }
46  
47    @Override
48    public void afterError(Request<?> request, Exception e) {
49      this.request.remove();
50    }
51  
52    @Override
53    public void afterResponse(Request<?> request, Object obj, TimingInfo timing) {
54      this.request.remove();
55    }
56  
57    @Override
58    public void beforeRequest(Request<?> request) {
59      this.request.set(request);
60    }
61  }