Regalamiunsorriso/decompiled-libs/www/acxent-face-1.0.0/it/acxent/face/rest/FaceRestSvlt.java

368 lines
17 KiB
Java

package it.acxent.face.rest;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import it.acxent.db.ApplParm;
import it.acxent.db.ApplParmFull;
import it.acxent.db.CRAdapter;
import it.acxent.db.DBAdapter;
import it.acxent.db.ResParm;
import it.acxent.face.Evento;
import it.acxent.face.Selfie;
import it.acxent.face.Users;
import it.acxent.rest._AblRestAdapter;
import it.acxent.util.Debug;
import it.acxent.util.Timer;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/rest/*"})
public class FaceRestSvlt extends _AblRestAdapter {
private static Debug debugHelper = new Debug();
private static final boolean debug = false;
private Cache<String, Object> cache;
private static final long serialVersionUID = -8266400675249237394L;
public void __templateChiamata(HttpServletRequest req, HttpServletResponse res) {
ApplParmFull apFull = getApFull(req);
boolean debug = false;
String parametroTest = getRequestParameter(req, "id");
if (debug)
System.out.println(" parm id: " + parametroTest + " apiParms: " +
getApiParms() + " json post: " + getApiJSONObject().toString(4));
try {
long l_id_user = getRequestLongParameter(req, "id_user");
boolean authOk = checkBasicAuth(req, res);
if (!authOk)
sendHtmlMsgResponse(req, res, "testina di vitello, _elencofoto");
} catch (Exception e) {
e.printStackTrace();
sendHtmlMsgResponse(req, res, "testina di vitello,_elencofoto eccezione: " + e.getMessage());
}
}
protected DBAdapter getBean(HttpServletRequest req) {
return null;
}
protected CRAdapter getBeanCR(HttpServletRequest req) {
return null;
}
public static void main(String[] args) {
String hostname = "localhost";
String db = "cc";
ApplParmFull ap = new ApplParmFull(new ApplParm(14, "//" + hostname + ":1433;databaseName=" + db + ";user=sa;password=T3st000!", null, null, 1, 10, 300));
}
protected boolean checkBasicAuth(HttpServletRequest req, HttpServletResponse res) {
return true;
}
protected String getApiBaseUrl() {
return "/fr/face/";
}
protected Users getUsersByBasicAuth(HttpServletRequest req, HttpServletResponse res) {
String[] auth = getBasicAuthorizationHeaders(req, res);
Users bean = new Users(getApFull());
if (auth != null)
bean.findLogonUtente(auth[0], auth[1], req.getRemoteAddr());
return bean;
}
private Cache<String, Object> getCache() {
if (this.cache == null)
this.cache = CacheBuilder.newBuilder().maximumSize(100L).expireAfterAccess(100L, TimeUnit.MINUTES).build();
return this.cache;
}
public void _scoringSelfie_POST(HttpServletRequest req, HttpServletResponse res) {
boolean debug = false;
Timer timer = null;
timer = new Timer();
timer.start();
String result = "";
String json_data_path = null;
try {
JsonObject jo = getApiJsonObject();
DBAdapter.printDebug(debug, "************ SCORING SELFIE POST CHIAMATA IN ARRIVO. *********************");
System.out.println("json in arrivo");
System.out.println(DBAdapter.gsonToString(jo));
System.out.println("*-------------- FINE ---------------------------*");
JsonObject joRes = new JsonObject();
JsonArray jNoImageA = new JsonArray();
long codeGara = jo.get("id_gara").getAsLong();
boolean inviaNotifiche = jo.get("sendNotifiche").getAsBoolean();
Evento evento = new Evento(getApFull());
evento.findByCodeGara(codeGara);
if (evento.getId_evento() == 0L) {
DBAdapter.printDebug(debug, "Errore evento.getId_evento()==0. codeGara: " + codeGara);
joRes.addProperty("success", Boolean.valueOf(false));
joRes.addProperty("msg", "Codice gara non corrisponde a nessun evento!!!");
sendJsonMsgResponse(req, res, joRes.toString());
} else {
String fileAlternativo;
DBAdapter.printDebug(debug, "trovato evento id_evento: " + evento.getId_evento() + " codgara: " + codeGara);
String md5 = jo.get("md5").getAsString();
if (jo.has("originalPath")) {
fileAlternativo = jo.get("originalPath").getAsString();
if (!new File(fileAlternativo).exists())
fileAlternativo = "";
} else {
fileAlternativo = "";
}
Selfie selfie = new Selfie(getApFull());
DBAdapter.printDebug(debug, " 1 SELFIE find by md5 " + md5 + " id_evento " + evento.getId_evento() + " .......");
selfie.findByMd5Evento(md5, evento.getId_evento());
ResParm rp = new ResParm(true);
if (selfie.getId_selfie() == 0L) {
DBAdapter.printDebug(debug, "_scoringSelfie_POST 1 SELFIE NON TROVATO");
selfie.setMd5(md5);
selfie.setId_evento(evento.getId_evento());
selfie.setFileAlternativo(fileAlternativo);
rp = selfie.save();
}
DBAdapter.printDebug(debug, "_scoringSelfie_POST salvato selfie id " + selfie.getId_selfie());
if (!rp.getStatus())
DBAdapter.printDebug(true, "_scoringSelfie_POST ************* ERRORE SALVATAGGIO SELFIE***************: " +
rp.getMsg());
if (!selfie.isSelfieImageExist()) {
DBAdapter.printDebug(debug, "_scoringSelfie_POST 1 IMMAGINE NON ESISTE");
if (jo.has("image")) {
String imageContent = jo.get("image").getAsJsonObject()
.get("content").getAsString();
DBAdapter.printDebug(debug, "_scoringSelfie_POST 1 attributo image trovato. scrivo " +
selfie.getFullFileNameSelfie());
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
String currentImage = selfie.getFullFileNameSelfie();
File file = new File(currentImage);
if (!file.exists()) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(decodedBytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null)
fos.close();
}
}
} else {
DBAdapter.printDebug(debug, "_scoringSelfie_POST 1 RICHIEDO IMMAGINE");
JsonObject jMd5 = new JsonObject();
jMd5.addProperty("md5", md5);
jNoImageA.add((JsonElement)jMd5);
joRes.addProperty("success", Boolean.valueOf(false));
joRes.addProperty("msg", "Manda immagine selfie");
joRes.add("noImg", (JsonElement)jMd5);
}
}
DBAdapter.printDebug(debug, "_scoringSelfie_POST 1 RICONTROLLO SE C'e' L'IMMAGINE " + selfie.getRealSelfieImgPath());
if (selfie.isSelfieImageExist()) {
DBAdapter.printDebug(debug, "_scoringSelfie_POST 2 IMMAGINE ESISTE");
if (selfie.getFlgStatus() == 99L) {
joRes.addProperty("success", Boolean.valueOf(true));
joRes.addProperty("msg", "Tutto Ok amico");
JsonObject response = selfie.getJsonResponseByEvento(evento.getId_evento());
joRes.add("response", (JsonElement)response);
joRes.addProperty("status", Long.valueOf(selfie.getFlgStatus()));
DBAdapter.printDebug(debug, "_scoringSelfie_POST STATUS_ELABORAZIONE_CONCLUSA.\n############### SU RESPONSE DOVREI AVERE LE FOTO ############## -->\n" +
DBAdapter.gsonToString(response));
} else if (selfie.getFlgStatus() == 1L) {
joRes.addProperty("success", Boolean.valueOf(true));
joRes.addProperty("msg", "In Elaborazione");
joRes.addProperty("status", Long.valueOf(selfie.getFlgStatus()));
joRes.add("response", (JsonElement)selfie.getJsonResponseByEvento(evento.getId_evento()));
DBAdapter.printDebug(debug, "_scoringSelfie_POST STATUS_IN_ELABORAZIONE:\n" + joRes.toString());
} else {
DBAdapter.printDebug(debug, "_scoringSelfie_POST addCodaElaborazione.......");
joRes.addProperty("success", Boolean.valueOf(true));
joRes.addProperty("msg", "Avvio Elaborazione");
joRes.addProperty("status", Long.valueOf(1L));
joRes.add("response", (JsonElement)selfie.getJsonResponseByEvento(evento.getId_evento()));
Selfie.addCodaElaborazioneAsync(selfie, inviaNotifiche);
}
} else if (joRes.size() == 0) {
joRes.addProperty("success", Boolean.valueOf(false));
joRes.addProperty("msg", "ERRORE!!! impossibile salvare l'immagine selfie!!!!");
DBAdapter.printDebug(debug, "_scoringSelfie_POST ERRORE!!! IMPOSSIBILE SALVARE IMMAGINE. NON CI DEVO MAI ARRIVARE. imgfull: " +
selfie.getFullFileNameSelfie() + " real: " + selfie.getRealSelfieImgPath());
}
int numResponses = -1;
if (debug)
if (joRes.has("response")) {
JsonObject response = joRes.getAsJsonObject("response");
if (response.has("labels") && response.get("labels").isJsonArray()) {
JsonArray labels = response.getAsJsonArray("labels");
DBAdapter.printDebug(debug, "************ _scoringSelfie_POST RISPOSTA AL CLIENT ===> Numero di elementi in labels: " +
labels.size());
numResponses = labels.size();
} else {
DBAdapter.printDebug(debug, "************ _scoringSelfie_POST RISPOSTA AL CLIENT ===> La chiave 'labels' non e' presente o non e' un array.\n" +
DBAdapter.gsonToString(joRes));
}
} else {
DBAdapter.printDebug(debug, "************ _scoringSelfie_POST RISPOSTA AL CLIENT ===> La chiave 'response' non e' presente.\n" +
DBAdapter.gsonToString(joRes));
}
timer.stop();
if (joRes.has("responses"))
numResponses = joRes.get("responses").getAsJsonArray().size();
DBAdapter.printDebug(debug, "************ _scoringSelfie_POST =================> Resp size: " + numResponses + " ms " +
timer.getDurataMilliSec());
sendJsonMsgResponse(req, res, joRes.toString());
}
} catch (Exception e) {
e.printStackTrace();
JsonObject joRes = new JsonObject();
joRes.addProperty("success", "false");
joRes.addProperty("msg", "Ritorno: " + result + "\nEccezione!! " + e.getMessage());
DBAdapter.printDebug("************ _scoringSelfie_POST ECCEZIONE!!!! json_data_path: " + json_data_path + "\nRISPOSTA AL CLIENT *********************");
System.out.println(DBAdapter.gsonToString(joRes));
System.out.println("*-------------- FINE ---------------------------*");
sendJsonMsgResponse(req, res, joRes.toString());
}
}
@Deprecated
public void _scoringSelfie_POSTOLD(HttpServletRequest req, HttpServletResponse res) {
boolean debug = true;
Timer timer = null;
timer = new Timer();
timer.start();
String result = "";
String json_data_path = null;
try {
JsonObject jo = getApiJsonObject();
DBAdapter.printDebug(debug, "************ *********************");
DBAdapter.printDebug(debug, "json in arrivo");
DBAdapter.printDebug(debug, DBAdapter.gsonToString(jo));
DBAdapter.printDebug(debug, "*-------------- FINE ---------------------------*");
JsonObject joRes = new JsonObject();
JsonArray jNoImageA = new JsonArray();
long codeGara = jo.get("id_gara").getAsLong();
Evento evento = new Evento(getApFull());
evento.findByCodeGara(codeGara);
if (evento.getId_evento() == 0L) {
joRes.addProperty("success", Boolean.valueOf(false));
joRes.addProperty("msg", "Codice gara non corrisponde a nessun evento!!!");
sendJsonMsgResponse(req, res, joRes.toString());
} else {
String fileAlternativo;
String md5 = jo.get("md5").getAsString();
if (jo.has("originalPath")) {
fileAlternativo = jo.get("originalPath").getAsString();
if (!new File(fileAlternativo).exists())
fileAlternativo = "";
} else {
fileAlternativo = "";
}
Selfie selfie = new Selfie(getApFull());
DBAdapter.printDebug(debug, " 1 SELFIE find by md5 " + md5);
selfie.findByMd5(md5);
if (selfie.getId_selfie() == 0L) {
DBAdapter.printDebug(debug, " 1 SELFIE NON TROVATO");
selfie.setMd5(md5);
selfie.setId_evento(evento.getId_evento());
selfie.setFileAlternativo(fileAlternativo);
selfie.save();
}
if (!selfie.isSelfieImageExist()) {
DBAdapter.printDebug(debug, "1 IMMAGINE NON ESISTE");
if (jo.has("image")) {
String imageContent = jo.get("image").getAsJsonObject()
.get("content").getAsString();
DBAdapter.printDebug(debug, "1 attributo image trovato");
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
String currentImage = selfie.getRealSelfieImgPath();
File file = new File(currentImage);
if (!file.exists()) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(decodedBytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null)
fos.close();
}
}
} else {
DBAdapter.printDebug(debug, "1 RICHIEDO IMMAGINE");
JsonObject jMd5 = new JsonObject();
jMd5.addProperty("md5", md5);
jNoImageA.add((JsonElement)jMd5);
joRes.addProperty("success", Boolean.valueOf(false));
joRes.addProperty("msg", "Manda immagine selfie");
joRes.add("noImg", (JsonElement)jMd5);
}
}
DBAdapter.printDebug(debug, "1 RICONTROLLO SE C'è L'IMMAGINE");
if (selfie.isSelfieImageExist()) {
DBAdapter.printDebug(debug, "2 IMMAGINE ESISTE");
if (selfie.getFlgStatus() == 99L) {
joRes.addProperty("success", Boolean.valueOf(true));
joRes.addProperty("msg", "Tutto Ok amico");
joRes.add("response", (JsonElement)selfie.getJsonResponseByEvento(evento.getId_evento()));
DBAdapter.printDebug(debug, "STATUS_ELABORAZIONE_CONCLUSA:\n" + joRes.toString());
} else if (selfie.getFlgStatus() == 1L) {
joRes.addProperty("success", Boolean.valueOf(true));
joRes.addProperty("msg", "In Elaborazione");
joRes.add("response", (JsonElement)selfie.getJsonResponseByEvento(evento.getId_evento()));
DBAdapter.printDebug(debug, " STATUS_IN_ELABORAZIONE:\n" + joRes.toString());
} else {
DBAdapter.printDebug(debug, " addCodaElaborazione.......");
joRes.addProperty("success", Boolean.valueOf(true));
joRes.addProperty("msg", "Avvio Elaborazione");
joRes.add("response", (JsonElement)selfie.getJsonResponseByEvento(evento.getId_evento()));
Selfie.addCodaElaborazione(selfie, false);
}
} else if (joRes.size() == 0) {
joRes.addProperty("success", Boolean.valueOf(false));
joRes.addProperty("msg", "ERRORE!!! impossibile salvare l'immagine selfie!!!!");
DBAdapter.printDebug(debug, "ERRORE!!! IMPOSSIBILE SALVARE IMMAGINE. NON CI DEVO MAI ARRIVARE");
}
DBAdapter.printDebug(debug, "************ RISPOSTA AL CLIENT *********************");
DBAdapter.printDebug(debug, DBAdapter.gsonToString(joRes));
timer.stop();
int numResponses = -1;
if (joRes.has("responses"))
numResponses = joRes.get("responses").getAsJsonArray().size();
System.out.println(String.valueOf(DBAdapter.getNow()) + " .. Resp size: " + String.valueOf(DBAdapter.getNow()) + " ms " + numResponses);
sendJsonMsgResponse(req, res, joRes.toString());
}
} catch (Exception e) {
e.printStackTrace();
JsonObject joRes = new JsonObject();
joRes.addProperty("success", "false");
joRes.addProperty("msg", "Ritorno: " + result + "\nEccezione!! " + e.getMessage());
DBAdapter.printDebug("************ ECCEZIONE!!!! json_data_path: " + json_data_path + "\nRISPOSTA AL CLIENT *********************");
System.out.println(DBAdapter.gsonToString(joRes));
System.out.println("*-------------- FINE ---------------------------*");
sendJsonMsgResponse(req, res, joRes.toString());
}
}
protected Class<? extends _AblRestAdapter> getConcreteApiClass() {
return null;
}
}