Browse Source

Remove sendgrid API library and its usage

pull/61/head
Kristijan Mitrovic 4 years ago
parent
commit
6b237a2640
2 changed files with 27 additions and 39 deletions
  1. +0
    -6
      bubble-server/pom.xml
  2. +27
    -33
      bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java

+ 0
- 6
bubble-server/pom.xml View File

@@ -275,12 +275,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>

<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.6.5</version>
</dependency>
</dependencies> </dependencies>


<build> <build>


+ 27
- 33
bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java View File

@@ -6,26 +6,28 @@ import bubble.model.account.Account;
import bubble.model.cloud.CloudCredentials; import bubble.model.cloud.CloudCredentials;
import bubble.model.cloud.CloudService; import bubble.model.cloud.CloudService;
import bubble.server.BubbleConfiguration; import bubble.server.BubbleConfiguration;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import lombok.*; import lombok.*;
import org.cobbzilla.util.http.HttpMethods;
import org.cobbzilla.util.http.HttpRequestBean;
import org.cobbzilla.util.http.HttpResponseBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;


import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;


import static bubble.cloud.storage.StorageCryptStream.MIN_DISTINCT_LENGTH; import static bubble.cloud.storage.StorageCryptStream.MIN_DISTINCT_LENGTH;
import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH; import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.daemon.ZillaRuntime.die;
import static org.cobbzilla.util.json.JsonUtil.EMPTY_JSON;
import static org.cobbzilla.util.http.HttpContentTypes.APPLICATION_JSON;
import static org.cobbzilla.util.http.HttpUtil.getResponse;
import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.json.JsonUtil.json;
import static org.cobbzilla.util.security.CryptoUtil.generatePassword; import static org.cobbzilla.util.security.CryptoUtil.generatePassword;
import static org.cobbzilla.util.string.StringUtil.repeat; import static org.cobbzilla.util.string.StringUtil.repeat;


public class SendgridSmtpEmailDriver extends SmtpEmailDriver { public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
public static final String SG_API_BASE = "https://api.sendgrid.com/v3/";


private static final String PARAM_PARENT_SERVICE = "parentService"; private static final String PARAM_PARENT_SERVICE = "parentService";


@@ -49,26 +51,12 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
return super.setupDelegatedCloudService(configuration, parentService, delegatedService); return super.setupDelegatedCloudService(configuration, parentService, delegatedService);
} }


final SendGrid sg = new SendGrid(parentCredentials.getParam(PARAM_PASSWORD));
final Account accountWithDelegate = accountDAO.findByUuid(delegatedService.getAccount()); final Account accountWithDelegate = accountDAO.findByUuid(delegatedService.getAccount());
final String user = sgUsername(delegatedService); final String user = sgUsername(delegatedService);
String password = generatePassword(MIN_KEY_LENGTH, MIN_DISTINCT_LENGTH); String password = generatePassword(MIN_KEY_LENGTH, MIN_DISTINCT_LENGTH);
final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password, final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password,
new String[]{}); new String[]{});
final Request req = new Request();
req.setMethod(Method.POST);
req.setEndpoint("subusers");
req.setBody(json(data));

final Response res;
try {
res = sg.api(req);
} catch (IOException e) {
return die("Cannot create SendGrid Subuser", e);
}
if (res.getStatusCode() != HTTP_OK) {
return die("Wrong response when creating SendGrid Subuser: " + res.getStatusCode() + " : " + res.getBody());
}
doRequest("subusers", HttpMethods.POST, json(data));


delegatedService.setDelegated(null).setTemplate(false); delegatedService.setDelegated(null).setTemplate(false);
delegatedService.getCredentials() delegatedService.getCredentials()
@@ -86,24 +74,29 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
final CloudService parentService = serviceDAO.findByUuid(parentServiceUuid); final CloudService parentService = serviceDAO.findByUuid(parentServiceUuid);
if (parentService == null) return; if (parentService == null) return;


final SendGrid sg = new SendGrid(parentService.getCredentials().getParam(PARAM_PASSWORD));
final String sgUserToDelete = sgUsername(service); final String sgUserToDelete = sgUsername(service);
doRequest("subusers/" + sgUserToDelete, HttpMethods.DELETE, null);
}


final Request req = new Request();
req.setMethod(Method.DELETE);
req.setEndpoint("subusers/" + sgUserToDelete);
req.setBody(EMPTY_JSON);
private String doRequest(@NonNull final String uri, @NonNull final String method, @Nullable final String json) {
final HttpRequestBean request = new HttpRequestBean();
request.setMethod(method)
.setUri(SG_API_BASE + uri)
.setEntity(json)
.setHeader(CONTENT_TYPE, APPLICATION_JSON)
.setHeader(AUTHORIZATION, "Bearer " + getCredentials().getParam(PARAM_PASSWORD));


final Response res;
final HttpResponseBean response;
try { try {
res = sg.api(req);
response = getResponse(request);
} catch (IOException e) { } catch (IOException e) {
die("Cannot delete SendGrid Subuser " + sgUserToDelete, e);
return;
return die("doGet(" + uri + "): " + e.getMessage(), e);
} }
if (res.getStatusCode() != HTTP_NO_CONTENT) {
die("Wrong response when creating SendGrid Subuser: " + res.getStatusCode() + " : " + res.getBody());
if (!response.isOk()) {
return die("doPost(" + uri + "): HTTP " + response.getStatus() + " : " + response.getEntityString());
} }

return response.getEntityString();
} }


@AllArgsConstructor @NoArgsConstructor @AllArgsConstructor @NoArgsConstructor
@@ -113,4 +106,5 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
@Getter @Setter private String password; @Getter @Setter private String password;
@Getter @Setter private String[] ips; @Getter @Setter private String[] ips;
} }

} }

Loading…
Cancel
Save