From 1493d0f7be4ccf14631ba3e0b31cf63ca7fcf32c Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Tue, 02 Dec 2014 17:57:50 +0000
Subject: [PATCH] #396 fix - Many issues fixed: Block/Unblock commands, Upload request data, Send second email, ...

---
 securis/src/main/java/net/curisit/securis/utils/EmailManager.java |   27 +++++++++++++++------------
 1 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/utils/EmailManager.java b/securis/src/main/java/net/curisit/securis/utils/EmailManager.java
index e6b686a..faf2389 100644
--- a/securis/src/main/java/net/curisit/securis/utils/EmailManager.java
+++ b/securis/src/main/java/net/curisit/securis/utils/EmailManager.java
@@ -17,12 +17,15 @@
 import javax.inject.Singleton;
 
 import net.curisit.securis.SeCurisException;
+import net.curisit.securis.services.exception.SeCurisServiceException;
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.entity.ContentType;
@@ -30,7 +33,6 @@
 import org.apache.http.entity.mime.MultipartEntityBuilder;
 import org.apache.http.entity.mime.content.FileBody;
 import org.apache.http.impl.client.BasicCredentialsProvider;
-import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.ssl.SSLContextBuilder;
 import org.apache.http.ssl.TrustStrategy;
@@ -49,7 +51,7 @@
     private static final Logger LOG = LogManager.getLogger(EmailManager.class);
 
     private final String serverUrl;
-    private final CloseableHttpClient httpClient;
+    private final HttpClientBuilder httpClientBuilder;
 
     /**
      * 
@@ -61,11 +63,11 @@
             throw new SeCurisException("Please, add '" + Config.KEYS.MAILGUN_DOMAIN + "' parameter to config file");
         }
         serverUrl = String.format("https://api.mailgun.net/v2/%s/messages", domain);
-        httpClient = createHttpClient();
+        httpClientBuilder = createHttpClient();
 
     }
 
-    private CloseableHttpClient createHttpClient() throws SeCurisException {
+    private HttpClientBuilder createHttpClient() throws SeCurisException {
         SSLContextBuilder builder = new SSLContextBuilder();
         SSLConnectionSocketFactory sslsf = null;
         try {
@@ -84,7 +86,7 @@
         UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("api", Config.get(Config.KEYS.MAILGUN_API_KEY));
         provider.setCredentials(AuthScope.ANY, credentials);
 
-        return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setSSLSocketFactory(sslsf).build();
+        return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setSSLSocketFactory(sslsf);
     }
 
     /**
@@ -98,7 +100,7 @@
      * @throws SeCurisException
      * @throws UnsupportedEncodingException
      */
-    public void sendEmail(String subject, String body, String to, String cc, File file) throws SeCurisException, UnsupportedEncodingException {
+    public void sendEmail(String subject, String body, String to, String cc, File file) throws SeCurisServiceException, UnsupportedEncodingException {
         HttpPost postRequest = new HttpPost(serverUrl);
 
         MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@@ -120,6 +122,7 @@
 
         postRequest.setEntity(builder.build());
         HttpResponse response;
+        HttpClient httpClient = httpClientBuilder.build();
         try {
             response = httpClient.execute(postRequest);
 
@@ -130,11 +133,11 @@
 
                 LOG.debug("Response mail read OK: {}", responseBean);
             } else {
-                throw new SeCurisException("Error sending email, response estatus: " + response.getStatusLine());
+                throw new SeCurisServiceException(ErrorCodes.UNEXPECTED_ERROR, "Error sending email, response estatus: " + response.getStatusLine());
             }
         } catch (IOException e) {
             LOG.error("Error sending email", e);
-            throw new SeCurisException("Error sending email");
+            throw new SeCurisServiceException(ErrorCodes.UNEXPECTED_ERROR, "Error sending email");
         }
     }
 
@@ -160,8 +163,8 @@
                     EmailManager.this.sendEmail(subject, body, to, cc, file);
                     callback.success();
                 } catch (UnsupportedEncodingException e) {
-                    callback.error(new SeCurisException("Error sending email", e));
-                } catch (SeCurisException e) {
+                    callback.error(new SeCurisServiceException("Error sending email: " + e));
+                } catch (SeCurisServiceException e) {
                     callback.error(e);
                 }
 
@@ -173,7 +176,7 @@
     public static interface EmailCallback {
         public void success();
 
-        public void error(SeCurisException e);
+        public void error(SeCurisServiceException e);
     }
 
     public static void main(String[] args) throws SeCurisException, UnsupportedEncodingException {
@@ -190,7 +193,7 @@
                     }
 
                     @Override
-                    public void error(SeCurisException e) {
+                    public void error(SeCurisServiceException e) {
                         LOG.error("Error: {} !!!", e);
                     }
                 });

--
Gitblit v1.3.2