rsanchez
2014-12-02 1493d0f7be4ccf14631ba3e0b31cf63ca7fcf32c
securis/src/main/java/net/curisit/securis/utils/EmailManager.java
....@@ -17,12 +17,15 @@
1717 import javax.inject.Singleton;
1818
1919 import net.curisit.securis.SeCurisException;
20
+import net.curisit.securis.services.exception.SeCurisServiceException;
21
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
2022
2123 import org.apache.commons.io.IOUtils;
2224 import org.apache.http.HttpResponse;
2325 import org.apache.http.auth.AuthScope;
2426 import org.apache.http.auth.UsernamePasswordCredentials;
2527 import org.apache.http.client.CredentialsProvider;
28
+import org.apache.http.client.HttpClient;
2629 import org.apache.http.client.methods.HttpPost;
2730 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2831 import org.apache.http.entity.ContentType;
....@@ -30,7 +33,6 @@
3033 import org.apache.http.entity.mime.MultipartEntityBuilder;
3134 import org.apache.http.entity.mime.content.FileBody;
3235 import org.apache.http.impl.client.BasicCredentialsProvider;
33
-import org.apache.http.impl.client.CloseableHttpClient;
3436 import org.apache.http.impl.client.HttpClientBuilder;
3537 import org.apache.http.ssl.SSLContextBuilder;
3638 import org.apache.http.ssl.TrustStrategy;
....@@ -49,7 +51,7 @@
4951 private static final Logger LOG = LogManager.getLogger(EmailManager.class);
5052
5153 private final String serverUrl;
52
- private final CloseableHttpClient httpClient;
54
+ private final HttpClientBuilder httpClientBuilder;
5355
5456 /**
5557 *
....@@ -61,11 +63,11 @@
6163 throw new SeCurisException("Please, add '" + Config.KEYS.MAILGUN_DOMAIN + "' parameter to config file");
6264 }
6365 serverUrl = String.format("https://api.mailgun.net/v2/%s/messages", domain);
64
- httpClient = createHttpClient();
66
+ httpClientBuilder = createHttpClient();
6567
6668 }
6769
68
- private CloseableHttpClient createHttpClient() throws SeCurisException {
70
+ private HttpClientBuilder createHttpClient() throws SeCurisException {
6971 SSLContextBuilder builder = new SSLContextBuilder();
7072 SSLConnectionSocketFactory sslsf = null;
7173 try {
....@@ -84,7 +86,7 @@
8486 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("api", Config.get(Config.KEYS.MAILGUN_API_KEY));
8587 provider.setCredentials(AuthScope.ANY, credentials);
8688
87
- return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setSSLSocketFactory(sslsf).build();
89
+ return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setSSLSocketFactory(sslsf);
8890 }
8991
9092 /**
....@@ -98,7 +100,7 @@
98100 * @throws SeCurisException
99101 * @throws UnsupportedEncodingException
100102 */
101
- public void sendEmail(String subject, String body, String to, String cc, File file) throws SeCurisException, UnsupportedEncodingException {
103
+ public void sendEmail(String subject, String body, String to, String cc, File file) throws SeCurisServiceException, UnsupportedEncodingException {
102104 HttpPost postRequest = new HttpPost(serverUrl);
103105
104106 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
....@@ -120,6 +122,7 @@
120122
121123 postRequest.setEntity(builder.build());
122124 HttpResponse response;
125
+ HttpClient httpClient = httpClientBuilder.build();
123126 try {
124127 response = httpClient.execute(postRequest);
125128
....@@ -130,11 +133,11 @@
130133
131134 LOG.debug("Response mail read OK: {}", responseBean);
132135 } else {
133
- throw new SeCurisException("Error sending email, response estatus: " + response.getStatusLine());
136
+ throw new SeCurisServiceException(ErrorCodes.UNEXPECTED_ERROR, "Error sending email, response estatus: " + response.getStatusLine());
134137 }
135138 } catch (IOException e) {
136139 LOG.error("Error sending email", e);
137
- throw new SeCurisException("Error sending email");
140
+ throw new SeCurisServiceException(ErrorCodes.UNEXPECTED_ERROR, "Error sending email");
138141 }
139142 }
140143
....@@ -160,8 +163,8 @@
160163 EmailManager.this.sendEmail(subject, body, to, cc, file);
161164 callback.success();
162165 } catch (UnsupportedEncodingException e) {
163
- callback.error(new SeCurisException("Error sending email", e));
164
- } catch (SeCurisException e) {
166
+ callback.error(new SeCurisServiceException("Error sending email: " + e));
167
+ } catch (SeCurisServiceException e) {
165168 callback.error(e);
166169 }
167170
....@@ -173,7 +176,7 @@
173176 public static interface EmailCallback {
174177 public void success();
175178
176
- public void error(SeCurisException e);
179
+ public void error(SeCurisServiceException e);
177180 }
178181
179182 public static void main(String[] args) throws SeCurisException, UnsupportedEncodingException {
....@@ -190,7 +193,7 @@
190193 }
191194
192195 @Override
193
- public void error(SeCurisException e) {
196
+ public void error(SeCurisServiceException e) {
194197 LOG.error("Error: {} !!!", e);
195198 }
196199 });