From 274e904f45d9404ff5926eddc8ec32e63e8a36af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lu=C3=ADs=20Felipe=20Pereira?=
<12111087@W1975424.portoalegre.pucrsnet.br>
Date: Thu, 17 Nov 2016 00:16:30 -0200
Subject: [PATCH] =?UTF-8?q?Envio=20de=20SMS=20para=20valida=C3=A7=C3=A3o?=
=?UTF-8?q?=20de=20telefone=20conclu=C3=ADdo.=20Melhoria=20no=20tratamento?=
=?UTF-8?q?=20do=20retorno=20do=20envio=20do=20SMS.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../javaee7/petclinic/entities/Owner.java | 3 -
.../petclinic/web/OwnerController.java | 117 +++++++++++++++---
src/main/webapp/editOwner.xhtml | 1 +
src/main/webapp/newOwner.xhtml | 21 ++--
4 files changed, 110 insertions(+), 32 deletions(-)
diff --git a/src/main/java/org/woehlke/javaee7/petclinic/entities/Owner.java b/src/main/java/org/woehlke/javaee7/petclinic/entities/Owner.java
index 7c659ec..393fc17 100644
--- a/src/main/java/org/woehlke/javaee7/petclinic/entities/Owner.java
+++ b/src/main/java/org/woehlke/javaee7/petclinic/entities/Owner.java
@@ -24,9 +24,6 @@
import javax.validation.constraints.Digits;
import java.util.*;
-import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonReader;
/**
* Created with IntelliJ IDEA.
diff --git a/src/main/java/org/woehlke/javaee7/petclinic/web/OwnerController.java b/src/main/java/org/woehlke/javaee7/petclinic/web/OwnerController.java
index 196d619..a009ea1 100644
--- a/src/main/java/org/woehlke/javaee7/petclinic/web/OwnerController.java
+++ b/src/main/java/org/woehlke/javaee7/petclinic/web/OwnerController.java
@@ -1,7 +1,9 @@
package org.woehlke.javaee7.petclinic.web;
+import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import org.woehlke.javaee7.petclinic.dao.OwnerDao;
import org.woehlke.javaee7.petclinic.dao.PetDao;
import org.woehlke.javaee7.petclinic.dao.PetTypeDao;
@@ -21,15 +23,14 @@
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;
-import javax.faces.application.FacesMessage;
-import javax.faces.context.FacesContext;
import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonReader;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
/**
* Created with IntelliJ IDEA.
@@ -76,8 +77,6 @@ public class OwnerController implements Serializable {
private String insertCode;
- private String zipMessage;
-
public String getInsertCode() {
return insertCode;
}
@@ -85,6 +84,18 @@ public String getInsertCode() {
public void setInsertCode(String insertCode) {
this.insertCode = insertCode;
}
+
+ private String smsMessage;
+
+ public String getSmsMessage() {
+ return smsMessage;
+ }
+
+ public void setSmsMessage(String smsMessage) {
+ this.smsMessage = smsMessage;
+ }
+
+ private String zipMessage;
public String getZipMessage() {
return zipMessage;
@@ -310,34 +321,101 @@ public void searchZipcode() throws MalformedURLException, IOException {
public void sendSMS() throws MalformedURLException, IOException, UnsupportedEncodingException {
this.owner.setValidatedPhone("False");
+ setSmsMessage("");
+
+ DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+ Date today = new Date();
+ String date = df.format(today);
String phone = owner.getTelephone();
Random r = new Random();
int number = r.nextInt(100000);
codeSMS = String.format("%05d", number);
+
String Credential = URLEncoder.encode("C80B675E120C10BB03AAB71095D221C526127A8E", "UTF-8");
String Token = URLEncoder.encode("b3fA08", "UTF-8");
String Mobile = URLEncoder.encode("55" + phone, "UTF-8");
- String Msg = URLEncoder.encode("Seu código de validação é: " + codeSMS, "UTF-8");
- Msg = URLEncoder.encode(Msg, "UTF-8");
+ String Msg = URLEncoder.encode("Seu código de validação solicitado em " + date + " é: " + codeSMS, "UTF-8");
String connection
- = "https://www.mpgateway.com/v_3_00/sms/smspush/enviasms.aspx?CREDENCIAL="
+ = "http://www.mpgateway.com/v_3_00/sms/smspush/enviasms.aspx?CREDENCIAL="
+ Credential + "&TOKEN=" + Token + "&PRINCIPAL_USER=NA&AUX_USER=NA&MOBILE=" + Mobile
+ "&SEND_PROJECT=N&MESSAGE=" + Msg;
+
URL url = new URL(connection);
+
+ setSmsMessage("Sending SMS");
+
InputStream input = url.openStream();
+ BufferedReader br = new BufferedReader(new InputStreamReader(input));
- setInsertCode("Sending SMS");
-
- byte[] b = new byte[4];
- input.read(b, 0, b.length);
-
- String RetornoMPG = new String(b);
+ String retorno = "";
+ String linha;
+ while ((linha = br.readLine()) != null) {
+ retorno += linha;
+ }
+
+ retorno = retorno.trim();
+
+ if (retorno.length() > 3) {
+ retorno = retorno.substring(0, 3);
+ }
+
+ switch (retorno) {
+ case "000":
+ retorno += " Mensagem enviada com sucesso";
+ break;
+ case "X01":
+ retorno += " Um ou mais parâmetros com erro";
+ break;
+ case "X02":
+ retorno += " Um ou mais parâmetros com erro";
+ break;
+ case "001":
+ retorno += " Credencial inválida";
+ break;
+ case "005":
+ retorno += " MOBILE com formato inválido";
+ break;
+ case "007":
+ retorno += " SEND_PROJECT com formato inválido";
+ break;
+ case "008":
+ retorno += " MESSAGE ou MESSAGE + NOME_PROJETO com mais de 160 posições ou SMS concatenado com mais de 1000 posições";
+ break;
+ case "009":
+ retorno += " Créditos insuficientes em conta";
+ break;
+ case "010":
+ retorno += " Gateway SMS da conta bloqueado";
+ break;
+ case "012":
+ retorno += " MOBILE correto, porém com crítica";
+ break;
+ case "013":
+ retorno += " Conteúdo da mensagem inválido ou vazio";
+ break;
+ case "015":
+ retorno += " País de destino sem cobertura";
+ break;
+ case "016":
+ retorno += " MOBILE com código de área inválido";
+ break;
+ case "018":
+ retorno += " MOBILE se encontra em lista negra";
+ break;
+ case "019":
+ retorno += " TOKEN inválido";
+ break;
+ case "022":
+ retorno += " Conta atingiu o limite de envio do dia";
+ break;
+ }
+
if (!this.owner.getValidatedPhone().equals("True")) {
- setInsertCode(RetornoMPG);
+ setSmsMessage(retorno);
}
- //Documentação
+ //Documentação
//http://www.mobipronto.com/pt-br/SMS-MT-API/documentacao-sms-mt-api-http-get-v3-00
}
@@ -345,13 +423,14 @@ public void validatePhone() {
if (codeSMS != null) {
if (codeSMS.equals(insertCode)) {
this.owner.setValidatedPhone("True");
+ setSmsMessage("Successfully validated");
} else {
- this.owner.setValidatedPhone("Invalid code");
+ setSmsMessage("Invalid code");
}
}
else
{
- setInsertCode("Send SMS code before");
+ setSmsMessage("Send SMS code before");
}
}
}
diff --git a/src/main/webapp/editOwner.xhtml b/src/main/webapp/editOwner.xhtml
index e176474..789f6d6 100644
--- a/src/main/webapp/editOwner.xhtml
+++ b/src/main/webapp/editOwner.xhtml
@@ -37,6 +37,7 @@
value="#{ownerController.insertCode}"/>
+
diff --git a/src/main/webapp/newOwner.xhtml b/src/main/webapp/newOwner.xhtml
index a35c94e..b55a532 100644
--- a/src/main/webapp/newOwner.xhtml
+++ b/src/main/webapp/newOwner.xhtml
@@ -21,24 +21,25 @@
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
+
+
@@ -76,4 +77,4 @@
-
+
\ No newline at end of file