| .. | .. |
|---|
| 17 | 17 | import javax.inject.Singleton; |
|---|
| 18 | 18 | |
|---|
| 19 | 19 | import net.curisit.securis.SeCurisException; |
|---|
| 20 | +import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 21 | +import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 20 | 22 | |
|---|
| 21 | 23 | import org.apache.commons.io.IOUtils; |
|---|
| 22 | 24 | import org.apache.http.HttpResponse; |
|---|
| 23 | 25 | import org.apache.http.auth.AuthScope; |
|---|
| 24 | 26 | import org.apache.http.auth.UsernamePasswordCredentials; |
|---|
| 25 | 27 | import org.apache.http.client.CredentialsProvider; |
|---|
| 28 | +import org.apache.http.client.HttpClient; |
|---|
| 26 | 29 | import org.apache.http.client.methods.HttpPost; |
|---|
| 27 | 30 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|---|
| 28 | 31 | import org.apache.http.entity.ContentType; |
|---|
| .. | .. |
|---|
| 30 | 33 | import org.apache.http.entity.mime.MultipartEntityBuilder; |
|---|
| 31 | 34 | import org.apache.http.entity.mime.content.FileBody; |
|---|
| 32 | 35 | import org.apache.http.impl.client.BasicCredentialsProvider; |
|---|
| 33 | | -import org.apache.http.impl.client.CloseableHttpClient; |
|---|
| 34 | 36 | import org.apache.http.impl.client.HttpClientBuilder; |
|---|
| 35 | 37 | import org.apache.http.ssl.SSLContextBuilder; |
|---|
| 36 | 38 | import org.apache.http.ssl.TrustStrategy; |
|---|
| .. | .. |
|---|
| 49 | 51 | private static final Logger LOG = LogManager.getLogger(EmailManager.class); |
|---|
| 50 | 52 | |
|---|
| 51 | 53 | private final String serverUrl; |
|---|
| 52 | | - private final CloseableHttpClient httpClient; |
|---|
| 54 | + private final HttpClientBuilder httpClientBuilder; |
|---|
| 53 | 55 | |
|---|
| 54 | 56 | /** |
|---|
| 55 | 57 | * |
|---|
| .. | .. |
|---|
| 61 | 63 | throw new SeCurisException("Please, add '" + Config.KEYS.MAILGUN_DOMAIN + "' parameter to config file"); |
|---|
| 62 | 64 | } |
|---|
| 63 | 65 | serverUrl = String.format("https://api.mailgun.net/v2/%s/messages", domain); |
|---|
| 64 | | - httpClient = createHttpClient(); |
|---|
| 66 | + httpClientBuilder = createHttpClient(); |
|---|
| 65 | 67 | |
|---|
| 66 | 68 | } |
|---|
| 67 | 69 | |
|---|
| 68 | | - private CloseableHttpClient createHttpClient() throws SeCurisException { |
|---|
| 70 | + private HttpClientBuilder createHttpClient() throws SeCurisException { |
|---|
| 69 | 71 | SSLContextBuilder builder = new SSLContextBuilder(); |
|---|
| 70 | 72 | SSLConnectionSocketFactory sslsf = null; |
|---|
| 71 | 73 | try { |
|---|
| .. | .. |
|---|
| 84 | 86 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("api", Config.get(Config.KEYS.MAILGUN_API_KEY)); |
|---|
| 85 | 87 | provider.setCredentials(AuthScope.ANY, credentials); |
|---|
| 86 | 88 | |
|---|
| 87 | | - return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setSSLSocketFactory(sslsf).build(); |
|---|
| 89 | + return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setSSLSocketFactory(sslsf); |
|---|
| 88 | 90 | } |
|---|
| 89 | 91 | |
|---|
| 90 | 92 | /** |
|---|
| .. | .. |
|---|
| 98 | 100 | * @throws SeCurisException |
|---|
| 99 | 101 | * @throws UnsupportedEncodingException |
|---|
| 100 | 102 | */ |
|---|
| 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 { |
|---|
| 102 | 104 | HttpPost postRequest = new HttpPost(serverUrl); |
|---|
| 103 | 105 | |
|---|
| 104 | 106 | MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
|---|
| .. | .. |
|---|
| 120 | 122 | |
|---|
| 121 | 123 | postRequest.setEntity(builder.build()); |
|---|
| 122 | 124 | HttpResponse response; |
|---|
| 125 | + HttpClient httpClient = httpClientBuilder.build(); |
|---|
| 123 | 126 | try { |
|---|
| 124 | 127 | response = httpClient.execute(postRequest); |
|---|
| 125 | 128 | |
|---|
| .. | .. |
|---|
| 130 | 133 | |
|---|
| 131 | 134 | LOG.debug("Response mail read OK: {}", responseBean); |
|---|
| 132 | 135 | } 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()); |
|---|
| 134 | 137 | } |
|---|
| 135 | 138 | } catch (IOException e) { |
|---|
| 136 | 139 | LOG.error("Error sending email", e); |
|---|
| 137 | | - throw new SeCurisException("Error sending email"); |
|---|
| 140 | + throw new SeCurisServiceException(ErrorCodes.UNEXPECTED_ERROR, "Error sending email"); |
|---|
| 138 | 141 | } |
|---|
| 139 | 142 | } |
|---|
| 140 | 143 | |
|---|
| .. | .. |
|---|
| 160 | 163 | EmailManager.this.sendEmail(subject, body, to, cc, file); |
|---|
| 161 | 164 | callback.success(); |
|---|
| 162 | 165 | } 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) { |
|---|
| 165 | 168 | callback.error(e); |
|---|
| 166 | 169 | } |
|---|
| 167 | 170 | |
|---|
| .. | .. |
|---|
| 173 | 176 | public static interface EmailCallback { |
|---|
| 174 | 177 | public void success(); |
|---|
| 175 | 178 | |
|---|
| 176 | | - public void error(SeCurisException e); |
|---|
| 179 | + public void error(SeCurisServiceException e); |
|---|
| 177 | 180 | } |
|---|
| 178 | 181 | |
|---|
| 179 | 182 | public static void main(String[] args) throws SeCurisException, UnsupportedEncodingException { |
|---|
| .. | .. |
|---|
| 190 | 193 | } |
|---|
| 191 | 194 | |
|---|
| 192 | 195 | @Override |
|---|
| 193 | | - public void error(SeCurisException e) { |
|---|
| 196 | + public void error(SeCurisServiceException e) { |
|---|
| 194 | 197 | LOG.error("Error: {} !!!", e); |
|---|
| 195 | 198 | } |
|---|
| 196 | 199 | }); |
|---|