This commit is contained in:
LORENZO\pacio 2024-10-23 14:38:20 +02:00
parent f9f653d8f5
commit 951028e0a8
13 changed files with 494 additions and 81 deletions

113
.gitignore vendored
View File

@ -1,88 +1,39 @@
# ---> Java
# Compiled class file
*.class
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
# Log file
*.log
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
# ---> Eclipse
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
### Eclipse ###
.apt_generated
.classpath
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.project
.settings
.springBeans
.sts4-cache
# Code Recommenders
.recommenders/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
# Annotation Processing
.apt_generated/
.apt_generated_test/
# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
/bin/

3
pi_mailupclient.log Normal file
View File

@ -0,0 +1,3 @@
===============>2023-09-15 17:14:02.826 -365
POST RESULT: HTTP/1.1 200 OK 1100

64
pom.xml Normal file
View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.poloinformatico</groupId>
<artifactId>MailUpClient</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.1.jre8</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,192 @@
package com.poloinformatico.MailupClient;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.poloinformatico.MailupClient.obj.Recipient;
import com.poloinformatico.MailupClient.obj.RecipientField;
import com.poloinformatico.MailupClient.obj.RecipientsResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.Objects;
public class ApiClient {
private static String _urlPostToken = "https://services.mailup.com/Authorization/OAuth/Token/";
private static String _token, _idGroup, _urlRecipients;
private static int _datediff = 0;
private static HttpClient _httpclient;
private static final String _fileout = "pi_mailupclient.log";
public static String start(String[] args) throws Exception {
String res = null;
try {
_datediff = Integer.parseInt(args[0]) * -1;
} catch (Exception e) {
_datediff = 0;
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(_fileout, true));) {
writer.append(System.lineSeparator() + "===============>" + new Timestamp(System.currentTimeMillis()) + " " + _datediff);
_httpclient = HttpClients.createDefault();
_idGroup = String.valueOf(1114);
_urlRecipients = "https://services.mailup.com/API/v1.1/Rest/ConsoleService.svc/Console/Group/" + _idGroup + "/Recipients";
ArrayList<Recipient> recs = readDatabase();
if (recs.size() > 0) {
getToken();
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(recs);
HttpResponse resp = doPost(_urlRecipients, new StringEntity(json, "UTF-8"));
String sResult = "POST RESULT: " + resp.getStatusLine() + " " + EntityUtils.toString(resp.getEntity());
writer.append(System.lineSeparator() + sResult);
}
}
return res;
}
public static boolean getToken() throws Exception {
boolean res = false;
HttpResponse response = null;
HttpPost req = new HttpPost(_urlPostToken);
req.addHeader("Content-Type", "application/x-www-form-urlencoded");
req.addHeader("Authorization", "Basic YjkzODE2MjctZjRkYi00MzA2LTljNTYtY2Q0ZjllNDAzMmExOjk3ODMxYTdiLTk2MWQtNDk2MC1iYzkxLTMyODM2NTRlMmRiOA==");
StringEntity params = new StringEntity("grant_type=password&username=m59820&password=g4st0n3!");
req.setEntity(params);
String responseBodyString = EntityUtils.toString(_httpclient.execute(req).getEntity());
JsonNode jsonNode = new ObjectMapper().readTree(responseBodyString);
_token = _token == null ? jsonNode.get("access_token").asText() : jsonNode.get("refresh_token").asText();
return res;
}
//attenzione, il body deve avere encoding UTF-8
public static HttpResponse doPost(String uri, StringEntity body) throws Exception {
HttpPost req = new HttpPost(uri);
req.addHeader("Authorization", "Bearer " + _token);
req.addHeader("Content-Type", "application/json; charset=utf-8");
body.setContentType("application/json");
//body.setContentEncoding("UTF-8");
req.setEntity(body);
HttpResponse resp = _httpclient.execute(req);
return resp;
}
public static HttpResponse doGet(String uri) throws Exception {
HttpGet req = new HttpGet(uri);
req.addHeader("Authorization", "Bearer " + _token);
HttpResponse response = _httpclient.execute(req);
return response;
}
public static String parseResponse(HttpResponse httpResponse) throws Exception {
String res = null;
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response);
res = jsonNode.asText();
return res;
}
public static ArrayList<Recipient> readDatabase() throws Exception {
Recipient rec;
ArrayList<Recipient> recs = new ArrayList<>();
int cnt_err = 0;
String sql = "SELECT ANPARIVA,AN_EMAIL,ANDESCRI,ANCODICE,ANLOCALI,AN___CAP,ANPROVIN,ANCATCOM,ANCLACL2,";
sql += "case when anclacl3 like '%BM%' or anclacl3 like '%BZ%' or anclacl3 like '%99%' or anclacl3 like '%CONC%' or anclacl3 like '%VISUR%' or anclacl3 like '%1SL%' or anclacl3 like '%2SL%' or anclacl3 like '%BL%' or anclacl3 like '%CAUSA%' or anclacl3 like '%DI%' or anclacl3 like '%ESEC%' or anclacl3 like '%LEG%' or anclacl3 like '%PDR%' or anclacl3 like '%BV%' then 1 else 0 end as blocco ";
sql += " from POLOICONTI ";
sql += " where AN_EMAIL is not null and ANDESCRI is not null and ANTIPCON='C' and GETDATE() <= DATEDIFF(DAY,?,UTDV) or GETDATE() <= DATEDIFF(DAY,?,UTDC)";
try (Connection conn = Db.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql);) {
stmt.setInt(1, _datediff);
stmt.setInt(2, _datediff);
try (ResultSet rs = stmt.executeQuery();) {
while (rs.next()) {
try {
rec = new Recipient();
rec.email = rs.getString("AN_EMAIL").trim();
rec.name = rs.getString("ANDESCRI").trim();
rec.fields.add(new RecipientField(3, Objects.toString(rs.getString("ANDESCRI"), "").trim()));
rec.fields.add(new RecipientField(4, Objects.toString(rs.getString("ANLOCALI"), "").trim()));
rec.fields.add(new RecipientField(5, Objects.toString(rs.getString("ANPROVIN"), "").trim()));
rec.fields.add(new RecipientField(6, Objects.toString(rs.getString("AN___CAP"), "").trim()));
rec.fields.add(new RecipientField(12, Objects.toString(rs.getString("ANCODICE"), "").trim()));
rec.fields.add(new RecipientField(16, Objects.toString(rs.getString("ANCATCOM"), "").trim()));
rec.fields.add(new RecipientField(17, Objects.toString(rs.getString("ANCLACL2"), "").trim()));
recs.add(rec);
} catch (Exception e) {
cnt_err++;
continue;
}
}
}
}
System.out.println("Errori database: " + cnt_err);
return recs;
}
}

View File

@ -0,0 +1,15 @@
package com.poloinformatico.MailupClient;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Db {
public static Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;databaseName=AHR_PI;";
String username = "sa";
String password = "p0l01nf.";
return DriverManager.getConnection(url, username, password);
}
}

View File

@ -0,0 +1,15 @@
package com.poloinformatico.MailupClient;
public class Main {
public static void main(String[] args) {
try {
ApiClient.start(args);
} catch (Exception e) {
System.out.println("eccezione esterna: " + e.getMessage());
}
}
}

View File

@ -0,0 +1,10 @@
package com.poloinformatico.MailupClient.obj;
import com.fasterxml.jackson.annotation.JsonProperty;
public class FieldRecipientsResponse {
@JsonProperty("Description")
public String description;
@JsonProperty("Id")
public int id;
@JsonProperty("Value")
public String value;
}

View File

@ -0,0 +1,18 @@
package com.poloinformatico.MailupClient.obj;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
public class ItemRecipientsResponse {
@JsonProperty("Fields")
public ArrayList<FieldRecipientsResponse> fieldRecipientsResponses;
@JsonProperty("Name")
public Object name;
public int idRecipient;
@JsonProperty("Email")
public String email;
@JsonProperty("MobileNumber")
public String mobileNumber;
@JsonProperty("MobilePrefix")
public String mobilePrefix;
}

View File

@ -0,0 +1,18 @@
package com.poloinformatico.MailupClient.obj;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
public class Recipient {
@JsonProperty("Email")
public String email;
@JsonProperty("Fields")
public ArrayList<RecipientField> fields = new ArrayList<>();
/*@JsonProperty("MobileNumber")
public String mobileNumber;
@JsonProperty("MobilePrefix")
public String mobilePrefix;*/
@JsonProperty("Name")
public String name;
}

View File

@ -0,0 +1,16 @@
package com.poloinformatico.MailupClient.obj;
import com.fasterxml.jackson.annotation.JsonProperty;
public class RecipientField {
/*@JsonProperty("Description")
public String description;*/
public RecipientField(int id, String value) {
this.id = id;
this.value = value;
}
@JsonProperty("Id")
public int id;
@JsonProperty("Value")
public String value;
}

View File

@ -0,0 +1,19 @@
package com.poloinformatico.MailupClient.obj;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
public class RecipientsResponse {
@JsonProperty("IsPaginated")
public boolean isPaginated;
@JsonProperty("Items")
public ArrayList<ItemRecipientsResponse> itemRecipientsResponses;
@JsonProperty("PageNumber")
public int pageNumber;
@JsonProperty("PageSize")
public int pageSize;
@JsonProperty("Skipped")
public int skipped;
@JsonProperty("TotalElementsCount")
public int totalElementsCount;
}

View File

@ -0,0 +1,89 @@
{
"IsPaginated": true,
"Items": [
{
"Description": "compleanno",
"Id": 27
},
{
"Description": "idtuttiprodottiacquistati",
"Id": 26
},
{
"Description": "totalefatturatoultimi30gg",
"Id": 25
},
{
"Description": "totalefatturatoultimi12mesi",
"Id": 24
},
{
"Description": "totalefatturato",
"Id": 23
},
{
"Description": "idcarrelloabbandonato",
"Id": 22
},
{
"Description": "totalecarrelloabbandonato",
"Id": 21
},
{
"Description": "datacarrelloabbandonato",
"Id": 20
},
{
"Description": "idultimoordinespedito",
"Id": 19
},
{
"Description": "dataultimoordinespedito",
"Id": 18
},
{
"Description": "idcategorieultimoordine",
"Id": 17
},
{
"Description": "idprodottiultimoordine",
"Id": 16
},
{
"Description": "totaleultimoordine",
"Id": 15
},
{
"Description": "dataultimoordine",
"Id": 14
},
{
"Description": "idultimoordine",
"Id": 13
},
{
"Description": "idcliente",
"Id": 12
},
{
"Description": "telefono",
"Id": 11
},
{
"Description": "fax",
"Id": 10
},
{
"Description": "indirizzo",
"Id": 9
},
{
"Description": "paese",
"Id": 8
}
],
"PageNumber": 0,
"PageSize": 20,
"Skipped": 0,
"TotalElementsCount": 27
}

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.poloinformatico.MailupClient.Main