WIP: Spring-Upgrade auf 2.2.0
[facebook-errors] / src / main / java / de / juplo / facebook / errors / GraphApiErrorResponseErrorHandler.java
1 package de.juplo.facebook.errors;
2
3
4 import java.io.ByteArrayInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.nio.charset.Charset;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.http.HttpHeaders;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.http.client.ClientHttpResponse;
13 import org.springframework.util.FileCopyUtils;
14 import org.springframework.web.client.DefaultResponseErrorHandler;
15 import org.springframework.web.client.ResponseErrorHandler;
16
17
18
19 /**
20  * Error-Handler for error-messages from the Facebook Graph-API.
21  * <p>
22  * This error-handler handels responses withe the HTTP-status code
23  * {@code 400 BAD REQUEST}. It tries to extract and parse the error-message
24  * from the HTTP-body. Successfully extracted and parsed messages are mapped
25  * to a hierarchy of exceptions, that reflects the hierarchy of the error-
26  * codes and -types.
27  * <p>
28  * If the HTTP-status-code of the response is not {@code 400 BAD REQUEST} or
29  * the HTTP-body could not be extracted or parsed, this error-handler
30  * delegates the handling to its parent.
31  *
32  * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
33  * @see <a href="http://fbdevwiki.com/wiki/Error_codes">Inofficial Wiki For Facebook-Developers</a>
34  * @author Kai Moritz
35  */
36 public class GraphApiErrorResponseErrorHandler implements ResponseErrorHandler
37 {
38   private final static Logger LOG =
39       LoggerFactory.getLogger(GraphApiErrorResponseErrorHandler.class);
40
41   private final ResponseErrorHandler parent;
42
43
44   public GraphApiErrorResponseErrorHandler(ResponseErrorHandler errorHandler)
45   {
46     this.parent =
47         errorHandler == null
48             ? new DefaultResponseErrorHandler()
49             : errorHandler;
50   }
51
52
53   @Override
54   public boolean hasError(ClientHttpResponse response) throws IOException
55   {
56     return
57         HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())
58         || this.parent.hasError(response);
59   }
60
61   @Override
62   public void handleError(final ClientHttpResponse response) throws IOException
63   {
64     GraphApiErrorResponseErrorHandler.handleError(parent, response);
65   }
66
67   public static void handleError(
68       final ResponseErrorHandler parent,
69       final ClientHttpResponse response
70       )
71       throws
72         IOException
73   {
74     if (response.getBody() == null)
75     {
76       // There is no body to interpret in the HTTP-message
77       LOG.warn("Could not convert the response into an exception, because there is no message-body.");
78       parent.handleError(response);
79       return;
80     }
81
82     final byte[] body = FileCopyUtils.copyToByteArray(response.getBody());
83     GraphApiException error;
84
85     try
86     {
87       error = GraphApiException.create(response.getStatusCode(), response.getHeaders(), body);
88       if (LOG.isInfoEnabled())
89         LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8")));
90     }
91     catch (Exception e)
92     {
93       // The body of the HTTP-message could not be parsed.
94       // Let the parent error-handler try to handle the response.
95
96       LOG.warn(
97           "Could not convert the response into an exception, " +
98           "because the body is unparsable: error={}, body={}",
99           e.toString(),
100           new String(body, Charset.forName("UTF-8"))
101           );
102
103       // To do so, we have to wrap the original response to fill in
104       // the buffered body, if needed
105       ClientHttpResponse buffered = new ClientHttpResponse()
106       {
107         @Override
108         public HttpStatus getStatusCode() throws IOException
109         {
110           return response.getStatusCode();
111         }
112
113         @Override
114         public synchronized InputStream getBody() throws IOException
115         {
116           return new ByteArrayInputStream(body);
117         }
118
119         @Override
120         public HttpHeaders getHeaders()
121         {
122           return response.getHeaders();
123         }
124
125         @Override
126         public String getStatusText() throws IOException
127         {
128           return response.getStatusText();
129         }
130
131         @Override
132         public void close()
133         {
134           response.close();
135         }
136
137         @Override
138         public int getRawStatusCode() throws IOException
139         {
140           return response.getRawStatusCode();
141         }
142       };
143
144       parent.handleError(buffered);
145       return;
146     }
147
148     throw error;
149   }
150 }