[ADÐ] NUEVO CORREO TRISMEGISTO.LOGISTICA AGREGADO

parent b21c5c8b
......@@ -33,7 +33,7 @@ public class DriveApi {
response.put("status", false);
response.put("message", "No se pudo subir el archivo," +
" existe multiples razones por las cuales esto puede ocurrir," +
" por favor verifique que el archivo no sea muy grande o que el formato sea correcto");
" por favor verifique que el archivo no sea muy grande , que el formato sea correcto o se tenga permiso en la carpeta");
return ResponseEntity.badRequest().body(response.toMap());
} else {
response.put("status", true);
......
......@@ -25,7 +25,7 @@
public ResponseEntity<?> full(@ModelAttribute GmailRequest request) {
try {
MimeMessage message = emailService.htmlMessage(request);
JSONObject response = emailService.send(message);
JSONObject response = emailService.send(message,request.getFrom());
return ResponseEntity.ok(response.toMap());
} catch (Exception e) {
return ResponseEntity.internalServerError().body(new JSONObject().put("message", e.getMessage()).put("status", false).toMap());
......
......@@ -25,12 +25,13 @@ import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import javax.annotation.PostConstruct;
import javax.mail.Address;
@Configuration
public class GoogleConfig {
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String OAUTH2 = "/oAuth2.json";
// private static final String OAUTH2 = "/oAuth2_planilla.json";
private static final String CREDENTIALS_FOLDER_PATH = "/tokens";
private final String APPLICATION_NAME = "FullService Application";
private String refresh_token_gmail = null;
......@@ -40,22 +41,11 @@ public class GoogleConfig {
@PostConstruct
public void getGoogleCredentials() {
try {
InputStream in = GoogleConfig.class.getResourceAsStream(CREDENTIALS_FOLDER_PATH + OAUTH2);
if (in == null)
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FOLDER_PATH + OAUTH2);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
client_id = clientSecrets.getDetails().getClientId();
client_secret = clientSecrets.getDetails().getClientSecret();
refresh_token_gmail = clientSecrets.getDetails().get("gmail_refresh_token").toString();
refresh_token_drive = clientSecrets.getDetails().get("drive_refresh_token").toString();
}catch (IOException e) {
System.out.println("Error: " + e);
}
}
public Drive getDrive() {
try {
getCredentials("/oAuth2_planilla.json");
NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Credential authorize = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(JSON_FACTORY)
......@@ -71,8 +61,18 @@ public class GoogleConfig {
}
}
public Gmail getGmail() {
public Gmail getGmail(String from) {
try {
switch (from) {
case "trismegisto.planilla@sacooliveros.edu.pe":
getCredentials("/oAuth2_planilla.json");
break;
case "trismegisto.logistica@sacooliveros.edu.pe":
getCredentials("/oAuth2_logistica.json");
break;
}
NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Credential authorize = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(JSON_FACTORY)
......@@ -88,6 +88,27 @@ public class GoogleConfig {
}
}
private void getCredentials(String Path) {
try {
InputStream in = GoogleConfig.class.getResourceAsStream(CREDENTIALS_FOLDER_PATH + Path);
if (in == null)
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FOLDER_PATH + Path);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
client_id = clientSecrets.getDetails().getClientId();
client_secret = clientSecrets.getDetails().getClientSecret();
if (clientSecrets.getDetails().get("gmail_refresh_token") != null){
refresh_token_gmail = clientSecrets.getDetails().get("gmail_refresh_token").toString();
}
if (clientSecrets.getDetails().get("drive_refresh_token") != null){
refresh_token_drive = clientSecrets.getDetails().get("drive_refresh_token").toString();
}
} catch (IOException e) {
System.out.println("Error: " + e);
}
}
public String getAccessToken(String refresh_token) {
try {
......
......@@ -4,6 +4,8 @@ package web.multitask.trismegistoservices.services.google;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import com.google.api.client.util.Base64;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;
......@@ -93,14 +95,14 @@ package web.multitask.trismegistoservices.services.google;
}
}
public JSONObject send(MimeMessage email) {
public JSONObject send(MimeMessage email,String from) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
email.writeTo(baos);
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
System.out.println(googleConfig.getGmail().users().messages().send("me", message).execute());
System.out.println(googleConfig.getGmail(from).users().messages().send("me", message).execute());
return new JSONObject().put("message", "Email enviado correctamente").put("status", true);
} catch (Exception e) {
System.out.println(e.getMessage());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment