From 5d9867b30dca2100766f2ff68e61f3e3895db233 Mon Sep 17 00:00:00 2001 From: Rich Barusta Date: Tue, 18 Apr 2017 11:43:07 -0500 Subject: [PATCH 1/5] MDA-5656 --- .idea/artifacts/SW_JAVA.xml | 7 + .idea/compiler.xml | 2 +- .idea/workspace.xml | 628 +++++++++--------- META-INF/MANIFEST.MF | 2 + SW-JAVA.iml | 5 +- pom.xml | 21 +- ...alException.java => GeneralException.java} | 4 +- .../SWAuthenticationService.java | 4 +- .../java/Services/Stamp/SWStampService.java | 63 +- .../Requests/Authentication/AuthRequest.java | 10 +- .../Authentication/AuthRequestDummy.java | 4 +- .../Authentication/AuthSoapRequest.java | 12 +- src/main/java/Utils/Requests/IRequestor.java | 5 +- .../Utils/Requests/Stamp/StampRequest.java | 21 +- .../Requests/Stamp/StampRequestDummy.java | 4 +- .../java/Utils/Responses/JSendFactory.java | 6 +- src/test/java/Tests/33.xml | 136 ---- .../SWAuthenticationServiceTest.java | 4 +- .../java/Tests/Stamp/SWStampServiceTest.java | 73 +- src/test/java/Tests/Utils.java | 209 ++---- src/test/resources/33-elbueno.xml | 1 + 21 files changed, 554 insertions(+), 667 deletions(-) create mode 100644 META-INF/MANIFEST.MF rename src/main/java/Exceptions/{GenaralException.java => GeneralException.java} (82%) delete mode 100644 src/test/java/Tests/33.xml create mode 100644 src/test/resources/33-elbueno.xml diff --git a/.idea/artifacts/SW_JAVA.xml b/.idea/artifacts/SW_JAVA.xml index dc85ad1..03df7f4 100644 --- a/.idea/artifacts/SW_JAVA.xml +++ b/.idea/artifacts/SW_JAVA.xml @@ -3,6 +3,13 @@ $PROJECT_DIR$/out/artifacts/SW_JAVA + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index cfbdd82..915d950 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 73d55e8..6b2406a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,12 +7,29 @@ - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - @@ -837,314 +840,323 @@ - + + - - + + - - + - - - + + - + - - + - - - + + - + - - - + + - + - - - - - + - + - - - + + - + - - - - - + - - - + + - + - - - + + - + - - - + + - + - - - + + - + - - - + + - + - - - + + - + - - - - - - + + - + - - - + + - + - - - + + - - - - - + + + + + + + + + + + + - + - - + - + + - - - - - - + + - + + + - - - - - - + + - + - - - + + - + - - - + + - + - - + + - + - - - + + + + + - + - - - + + + + + - + - - + + - - + - + - - + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + - - + + + + - + - - + + - - - - + + + + + + + - - - - - - - - - - - + + + + + + - + - - + + - + - - - + + + + + - + - - + + - + - - + + @@ -1156,8 +1168,8 @@ - + SW-JAVA diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..59499bc --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/SW-JAVA.iml b/SW-JAVA.iml index cb05983..218f835 100644 --- a/SW-JAVA.iml +++ b/SW-JAVA.iml @@ -7,11 +7,12 @@ + - + @@ -40,7 +41,7 @@ - + diff --git a/pom.xml b/pom.xml index 5de00e1..27037f8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,8 @@ mx.com.sw.services SW-JAVA - 1.0.0 + 1.0.1 + jar @@ -50,6 +51,24 @@ src/main/java src/test/java + + org.apache.maven.plugins + maven-assembly-plugin + + + create-my-bundle + package + + single + + + + jar-with-dependencies + + + + + org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/Exceptions/GenaralException.java b/src/main/java/Exceptions/GeneralException.java similarity index 82% rename from src/main/java/Exceptions/GenaralException.java rename to src/main/java/Exceptions/GeneralException.java index 562fb94..3c9e4c8 100644 --- a/src/main/java/Exceptions/GenaralException.java +++ b/src/main/java/Exceptions/GeneralException.java @@ -1,6 +1,6 @@ package Exceptions; -public class GenaralException extends Exception { +public class GeneralException extends Exception { public int HttpStatusCode; public String ErrorMSG; @@ -20,7 +20,7 @@ public void setErrorMSG(String errorMSG) { ErrorMSG = errorMSG; } - public GenaralException(int httpStatusCode, String errorMSG) { + public GeneralException(int httpStatusCode, String errorMSG) { super(httpStatusCode+" ----> "+errorMSG); HttpStatusCode = httpStatusCode; ErrorMSG = errorMSG; diff --git a/src/main/java/Services/Authentication/SWAuthenticationService.java b/src/main/java/Services/Authentication/SWAuthenticationService.java index 2a956d2..d80ebd3 100644 --- a/src/main/java/Services/Authentication/SWAuthenticationService.java +++ b/src/main/java/Services/Authentication/SWAuthenticationService.java @@ -1,7 +1,7 @@ package Services.Authentication; import Exceptions.AuthException; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Services.SWService; import Utils.Constants; import Utils.Requests.Authentication.AuthOptionsRequest; @@ -16,7 +16,7 @@ public SWAuthenticationService(String user, String password, String URI) { } - public IResponse Token() throws GenaralException, AuthException { + public IResponse Token() throws GeneralException, AuthException { AuthOptionsRequest settings = new AuthOptionsRequest(getURI(),getUser(),getPassword()); String dum = settings.URI.split("-")[0]; diff --git a/src/main/java/Services/Stamp/SWStampService.java b/src/main/java/Services/Stamp/SWStampService.java index dcfc71f..86a363b 100644 --- a/src/main/java/Services/Stamp/SWStampService.java +++ b/src/main/java/Services/Stamp/SWStampService.java @@ -1,22 +1,22 @@ package Services.Stamp; import Exceptions.AuthException; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Services.SWService; import Utils.Constants; -import Utils.EncoderHelper; import Utils.Requests.Authentication.AuthOptionsRequest; import Utils.Requests.Authentication.AuthRequest; -import Utils.Requests.Authentication.AuthSoapRequest; import Utils.Requests.Stamp.StampOptionsRequest; import Utils.Requests.Stamp.StampRequest; import Utils.Requests.Stamp.StampRequestDummy; -import Utils.Responses.AuthResponse; + import Utils.Responses.IResponse; +import org.json.JSONObject; + + +import java.nio.charset.Charset; -import java.io.*; -import java.net.MalformedURLException; public class SWStampService extends SWService { @@ -28,7 +28,7 @@ public SWStampService(String token, String URI) { super(token, URI); } - public IResponse Stamp(String xml, String version) throws AuthException, GenaralException { + public IResponse Stamp(String xml, String version) throws AuthException, GeneralException { @@ -41,12 +41,12 @@ public IResponse Stamp(String xml, String version) throws AuthException, Genaral } //CUSTOMER HASN'T TOKEN, BUT HAS USER AND PASSWORD --> AUTH,GENERATE TOKEN AND SET TOKEN IN GLOBAL SETTINGS - // Temporaly changes to soap - AuthOptionsRequest settings = new AuthOptionsRequest(Constants.BASE_PATH+Constants.AUTH_PATH,getUser(),getPassword()); - AuthSoapRequest req = new AuthSoapRequest(); - IResponse res = req.sendRequest(settings); + AuthOptionsRequest settings = new AuthOptionsRequest(Constants.BASE_PATH,getUser(),getPassword()); + AuthRequest req = new AuthRequest(); + IResponse res = req.sendRequest(settings); if (res.HttpStatusCode==200){ - setToken(res.Data); + JSONObject obj = new JSONObject(res.Data); + setToken(obj.getString("token")); } else{ //CUSTOMER HASN'T TOKEN, AND USER AND PASSWORD ARE BAD--> WE CANT' DO ANYTHING --> THROW EXCEPTION @@ -68,22 +68,11 @@ public IResponse Stamp(String xml, String version) throws AuthException, Genaral } - public IResponse Stamp(File xmlFile, String version) throws AuthException, GenaralException { + public IResponse Stamp(byte[] xmlFile, String version) throws AuthException, GeneralException { //BINARY XML - try{ - String xmlProcess = ""; - String strLine; - BufferedReader reader = new BufferedReader(new FileReader(xmlFile)); - while ((strLine = reader.readLine()) != null) { - // Print the content on the console - xmlProcess+=strLine; - } - - reader.close(); - - + String xmlProcess = new String(xmlFile,Charset.forName("UTF-8")); if (getToken()==null){ @@ -94,11 +83,12 @@ public IResponse Stamp(File xmlFile, String version) throws AuthException, Genar } //CUSTOMER HASN'T TOKEN, BUT HAS USER AND PASSWORD --> TRY AUTH,GENERATE TOKEN AND SET TOKEN IN GLOBAL SETTINGS - AuthOptionsRequest settings = new AuthOptionsRequest(Constants.BASE_PATH+Constants.AUTH_PATH,getUser(),getPassword()); + AuthOptionsRequest settings = new AuthOptionsRequest(Constants.BASE_PATH,getUser(),getPassword()); AuthRequest req = new AuthRequest(); - AuthResponse res = (AuthResponse) req.sendRequest(settings); + IResponse res = req.sendRequest(settings); if (res.HttpStatusCode==200){ - setToken(res.Data); + JSONObject obj = new JSONObject(res.Data); + setToken(obj.getString("token")); } else{ //CUSTOMER HASN'T TOKEN, AND USER AND PASSWORD ARE BAD--> WE CANT' DO ANYTHING --> THROW EXCEPTION @@ -112,20 +102,13 @@ public IResponse Stamp(File xmlFile, String version) throws AuthException, Genar StampOptionsRequest settings = new StampOptionsRequest(getToken(),getURI(),xmlProcess,version); - String dum = settings.URI.split("-")[0]; - System.out.println(dum); - if (dum.equalsIgnoreCase("d")){ - StampRequestDummy req = new StampRequestDummy(); - return req.sendRequest(settings); - } + String dum = settings.URI.split("-")[0]; + if (dum.equalsIgnoreCase("d")){ + StampRequestDummy req = new StampRequestDummy(); + return req.sendRequest(settings); + } StampRequest req = new StampRequest(); return req.sendRequest(settings); - }catch (FileNotFoundException e) { - throw new GenaralException(500,"ERROR AL LEER ARCHIVO"); - } catch (IOException e) { - throw new GenaralException(500,"ERROR AL LEER ARCHIVO"); - } - } } diff --git a/src/main/java/Utils/Requests/Authentication/AuthRequest.java b/src/main/java/Utils/Requests/Authentication/AuthRequest.java index 9d33f3e..692e57f 100644 --- a/src/main/java/Utils/Requests/Authentication/AuthRequest.java +++ b/src/main/java/Utils/Requests/Authentication/AuthRequest.java @@ -1,6 +1,6 @@ package Utils.Requests.Authentication; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Utils.Requests.IRequest; import Utils.Requests.IRequestor; import Utils.Responses.AuthResponse; @@ -20,10 +20,10 @@ public class AuthRequest implements IRequestor { @Override - public IResponse sendRequest(IRequest request) throws GenaralException { + public IResponse sendRequest(IRequest request) throws GeneralException { if (request.URI.isEmpty()){ - throw new GenaralException(500,"URL VACIA"); + throw new GeneralException(500,"URL VACIA"); } try { @@ -42,10 +42,10 @@ public IResponse sendRequest(IRequest request) throws GenaralException { } catch (UnirestException e) { - throw new GenaralException(500,"SERVIDOR INACTIVO"); + throw new GeneralException(500,"SERVIDOR INACTIVO"); } catch(JSONException e){ - throw new GenaralException(500,e.getMessage()); + throw new GeneralException(500,e.getMessage()); } diff --git a/src/main/java/Utils/Requests/Authentication/AuthRequestDummy.java b/src/main/java/Utils/Requests/Authentication/AuthRequestDummy.java index c631f3e..3431be6 100644 --- a/src/main/java/Utils/Requests/Authentication/AuthRequestDummy.java +++ b/src/main/java/Utils/Requests/Authentication/AuthRequestDummy.java @@ -1,6 +1,6 @@ package Utils.Requests.Authentication; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Utils.Requests.IRequest; import Utils.Requests.IRequestor; import Utils.Responses.IResponse; @@ -11,7 +11,7 @@ public class AuthRequestDummy implements IRequestor { @Override - public IResponse sendRequest(IRequest request) throws GenaralException { + public IResponse sendRequest(IRequest request) throws GeneralException { String dummy_case = request.URI.split("-")[1].toString(); String dcase = dummy_case.split("/")[0].toString(); int idcae = 0; diff --git a/src/main/java/Utils/Requests/Authentication/AuthSoapRequest.java b/src/main/java/Utils/Requests/Authentication/AuthSoapRequest.java index f41001a..19a7eaa 100644 --- a/src/main/java/Utils/Requests/Authentication/AuthSoapRequest.java +++ b/src/main/java/Utils/Requests/Authentication/AuthSoapRequest.java @@ -1,7 +1,7 @@ package Utils.Requests.Authentication; import Exceptions.AuthException; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Utils.Constants; import Utils.Requests.IRequest; import Utils.Requests.IRequestor; @@ -22,7 +22,7 @@ public class AuthSoapRequest implements IRequestor { @Override - public IResponse sendRequest(IRequest request) throws GenaralException, AuthException { + public IResponse sendRequest(IRequest request) throws GeneralException, AuthException { try { String body = Constants.auth_soap_envelope(request.User, request.Password); HttpResponse response = Unirest.post("http://pruebascfdi.smartweb.com.mx/Autenticacion/wsAutenticacion.asmx?WSDL=") @@ -55,13 +55,13 @@ public IResponse sendRequest(IRequest request) throws GenaralException, AuthExce ); } catch (UnirestException e) { - throw new GenaralException(500,e.getMessage()); + throw new GeneralException(500,e.getMessage()); } catch (ParserConfigurationException e) { - throw new GenaralException(500,e.getMessage()); + throw new GeneralException(500,e.getMessage()); } catch (SAXException e) { - throw new GenaralException(500,e.getMessage()); + throw new GeneralException(500,e.getMessage()); } catch (IOException e) { - throw new GenaralException(500,e.getMessage()); + throw new GeneralException(500,e.getMessage()); } } diff --git a/src/main/java/Utils/Requests/IRequestor.java b/src/main/java/Utils/Requests/IRequestor.java index 80cabfa..875b050 100644 --- a/src/main/java/Utils/Requests/IRequestor.java +++ b/src/main/java/Utils/Requests/IRequestor.java @@ -1,11 +1,12 @@ package Utils.Requests; import Exceptions.AuthException; -import Exceptions.GenaralException; +import Exceptions.GeneralException; +import Exceptions.GeneralException; import Utils.Requests.IRequest; import Utils.Responses.IResponse; public interface IRequestor { - public IResponse sendRequest(IRequest request) throws GenaralException, AuthException; + public IResponse sendRequest(IRequest request) throws GeneralException, AuthException, GeneralException; } diff --git a/src/main/java/Utils/Requests/Stamp/StampRequest.java b/src/main/java/Utils/Requests/Stamp/StampRequest.java index 0980949..2974ff3 100644 --- a/src/main/java/Utils/Requests/Stamp/StampRequest.java +++ b/src/main/java/Utils/Requests/Stamp/StampRequest.java @@ -1,12 +1,11 @@ package Utils.Requests.Stamp; import Exceptions.AuthException; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Utils.Requests.IRequest; import Utils.Requests.IRequestor; import Utils.Responses.IResponse; import Utils.Responses.JSendFactory; -import Utils.Responses.StampResponse; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; @@ -19,16 +18,20 @@ public class StampRequest implements IRequestor { - public IResponse sendRequest(IRequest request) throws GenaralException, AuthException { + public IResponse sendRequest(IRequest request) throws GeneralException, AuthException { try { String xmlStr = ((StampOptionsRequest) request).getXml(); + File tempFile = File.createTempFile("tmp-", ".xml"); - BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile)); + //BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile)); + BufferedWriter bw = new BufferedWriter + (new OutputStreamWriter(new FileOutputStream(tempFile),"UTF-8")); bw.write(xmlStr); bw.close(); + tempFile.deleteOnExit(); HttpResponse response = Unirest.post(request.URI) @@ -49,16 +52,16 @@ public IResponse sendRequest(IRequest request) throws GenaralException, AuthExce } catch (UnirestException e) { - throw new GenaralException(404,"HOST DESCONOCIDO"); + throw new GeneralException(404,"HOST DESCONOCIDO"); } catch (JSONException e){ - throw new GenaralException(500,e.getMessage()); + throw new GeneralException(500,e.getMessage()); } catch(java.net.MalformedURLException e){ - throw new GenaralException(404,"HOST DESCONOCIDO"); + throw new GeneralException(404,"HOST DESCONOCIDO"); } catch (IOException e) { - e.printStackTrace(); - throw new GenaralException(404,e.getCause().getMessage()); + + throw new GeneralException(404,e.getCause().getMessage()); } diff --git a/src/main/java/Utils/Requests/Stamp/StampRequestDummy.java b/src/main/java/Utils/Requests/Stamp/StampRequestDummy.java index e06029e..c6cbb70 100644 --- a/src/main/java/Utils/Requests/Stamp/StampRequestDummy.java +++ b/src/main/java/Utils/Requests/Stamp/StampRequestDummy.java @@ -1,6 +1,6 @@ package Utils.Requests.Stamp; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Utils.Requests.IRequest; import Utils.Requests.IRequestor; import Utils.Responses.IResponse; @@ -10,7 +10,7 @@ public class StampRequestDummy implements IRequestor{ @Override - public IResponse sendRequest(IRequest request) throws GenaralException { + public IResponse sendRequest(IRequest request) throws GeneralException { String dummy_case = request.URI.split("-")[1].toString(); String dcase = dummy_case.split("/")[0].toString(); diff --git a/src/main/java/Utils/Responses/JSendFactory.java b/src/main/java/Utils/Responses/JSendFactory.java index df98225..15dfb97 100644 --- a/src/main/java/Utils/Responses/JSendFactory.java +++ b/src/main/java/Utils/Responses/JSendFactory.java @@ -1,9 +1,9 @@ package Utils.Responses; -import Exceptions.GenaralException; +import Exceptions.GeneralException; public class JSendFactory { - public static IJSend response(String status,String msg, int code) throws GenaralException { + public static IJSend response(String status,String msg, int code) throws GeneralException { int idcae = 0; if (status.equalsIgnoreCase("success")){ @@ -22,7 +22,7 @@ else if (status.equalsIgnoreCase("error")){ case 2: return new FailureResponse(code, msg, status); case 3: - throw new GenaralException(code,msg); + throw new GeneralException(code,msg); default: return new FailureResponse(code, msg, status); diff --git a/src/test/java/Tests/33.xml b/src/test/java/Tests/33.xml deleted file mode 100644 index 02d3458..0000000 --- a/src/test/java/Tests/33.xml +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/test/java/Tests/Authentication/SWAuthenticationServiceTest.java b/src/test/java/Tests/Authentication/SWAuthenticationServiceTest.java index 084dad1..d845554 100644 --- a/src/test/java/Tests/Authentication/SWAuthenticationServiceTest.java +++ b/src/test/java/Tests/Authentication/SWAuthenticationServiceTest.java @@ -1,6 +1,6 @@ package Tests.Authentication; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Services.Authentication.SWAuthenticationService; import Utils.Responses.IResponse; import junit.framework.TestCase; @@ -50,7 +50,7 @@ public void testTokenERROR_RESPONSE() throws Exception { System.out.println(response.HttpStatusCode); System.out.println(response.Data); } - catch (GenaralException e){ + catch (GeneralException e){ Assert.assertNotNull("some bad happend", e); } } diff --git a/src/test/java/Tests/Stamp/SWStampServiceTest.java b/src/test/java/Tests/Stamp/SWStampServiceTest.java index fba78f6..30ae172 100644 --- a/src/test/java/Tests/Stamp/SWStampServiceTest.java +++ b/src/test/java/Tests/Stamp/SWStampServiceTest.java @@ -1,6 +1,6 @@ package Tests.Stamp; -import Exceptions.GenaralException; +import Exceptions.GeneralException; import Services.Authentication.SWAuthenticationService; import Services.Stamp.SWStampService; import Tests.Utils; @@ -10,11 +10,19 @@ import org.json.JSONObject; import org.junit.Assert; +import java.io.*; + +import java.nio.charset.Charset; +import java.text.Normalizer; +import java.util.Date; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class SWStampServiceTest extends TestCase { - public void testStampREAL_XML_STRING_USER_PASSWORD_AUTH() throws Exception { + /* public void testStampREAL_XML_STRING_USER_PASSWORD_AUTH() throws Exception { SWStampService api = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); IResponse response = null; response = api.Stamp(Utils.dummy_xml_string,"v1"); @@ -23,17 +31,68 @@ public void testStampREAL_XML_STRING_USER_PASSWORD_AUTH() throws Exception { System.out.println(response.Data); } + public void testStampREAL_XML_BYTES_USER_PASSWORD_AUTH() throws Exception { + + SWStampService api = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); + IResponse response = null; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("33-elbueno.xml").getFile()); + byte[] b = Utils.fileToBytes(file); + IResponse res = api.Stamp(b,"v1"); + System.out.println(res.Data); + + } + + public void testStamppattern() throws Exception { + ClassLoader classLoader = getClass().getClassLoader(); + File fileDir = new File(classLoader.getResource("33-elbueno.xml").getFile()); + try { + + + BufferedReader in = new BufferedReader( + new InputStreamReader(new FileInputStream(fileDir), "UTF-8")); + + String str; + + while ((str = in.readLine()) != null) { + PrintStream out = new PrintStream(System.out, true, "UTF-8"); + + + out.println(str); + + + } + + in.close(); + } + catch (UnsupportedEncodingException e) + { + System.out.println(e.getMessage()); + } + catch (IOException e) + { + System.out.println(e.getMessage()); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + + } + public void testStampREAL_XML_STRING_TOKEN_AUTH() throws Exception { SWAuthenticationService auth = new SWAuthenticationService("demo","123456789","http://services.test.sw.com.mx"); JSONObject obj = new JSONObject(auth.Token().Data); String tkn = obj.getString("token"); SWStampService api = new SWStampService(tkn,"http://services.test.sw.com.mx"); IResponse response = null; + Date date1 = new Date(); response = api.Stamp(Utils.dummy_xml_string,"v1"); - System.out.println(response.Status); - System.out.println(response.HttpStatusCode); + Date date2 = new Date(); + System.out.println(Utils.getDateDiff(date1,date2, TimeUnit.MILLISECONDS)); System.out.println(response.Data); - } + }*/ public void testStampREAL_XML_STRING_EMPTY_PARAMS() throws Exception { @@ -101,7 +160,7 @@ public void testStampTOKEN_EXPIRES_NOT_USER_NOT_PASSWORD() throws Exception { } } - public void testStampRenewToken() throws Exception { + /* public void testStampRenewToken() throws Exception { SWStampService api = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); IResponse response = null; @@ -161,6 +220,6 @@ public void testStampDUMMY_ERROR() throws Exception { Assert.assertNotNull("some bad happend", e); } - } + }*/ } \ No newline at end of file diff --git a/src/test/java/Tests/Utils.java b/src/test/java/Tests/Utils.java index ffa62f2..2cc59a2 100644 --- a/src/test/java/Tests/Utils.java +++ b/src/test/java/Tests/Utils.java @@ -1,145 +1,80 @@ package Tests; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.Normalizer; +import java.util.Date; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Created by asalvio on 16/02/2017. */ public class Utils { public static String dummy_token = "T2lYQ0t4L0RHVkR4dHZ5Nkk1VHNEakZ3Y0J4Nk9GODZuRyt4cE1wVm5tbXB3YVZxTHdOdHAwVXY2NTdJb1hkREtXTzE3dk9pMmdMdkFDR2xFWFVPUTQyWFhnTUxGYjdKdG8xQTZWVjFrUDNiOTVrRkhiOGk3RHladHdMaEM0cS8rcklzaUhJOGozWjN0K2h6R3gwQzF0c0g5aGNBYUt6N2srR3VoMUw3amtvPQ.T2lYQ0t4L0RHVkR4dHZ5Nkk1VHNEakZ3Y0J4Nk9GODZuRyt4cE1wVm5tbFlVcU92YUJTZWlHU3pER1kySnlXRTF4alNUS0ZWcUlVS0NhelhqaXdnWTRncklVSWVvZlFZMWNyUjVxYUFxMWFxcStUL1IzdGpHRTJqdS9Zakw2UGRiMTFPRlV3a2kyOWI5WUZHWk85ODJtU0M2UlJEUkFTVXhYTDNKZVdhOXIySE1tUVlFdm1jN3kvRStBQlpLRi9NeWJrd0R3clhpYWJrVUMwV0Mwd3FhUXdpUFF5NW5PN3J5cklMb0FETHlxVFRtRW16UW5ZVjAwUjdCa2g0Yk1iTExCeXJkVDRhMGMxOUZ1YWlIUWRRVC8yalFTNUczZXdvWlF0cSt2UW0waFZKY2gyaW5jeElydXN3clNPUDNvU1J2dm9weHBTSlZYNU9aaGsvalpQMUxyQ0IvSUh1bHYxaFMxa2xmb3ZIaHlqQlpZZGRmVlpuRDBHdHNweStrZmNPcWZjWktlcm5IZVFhelhRUDBQWXpHS0JmMGhURm9OTU5KUEJLeEZMeGpnR3hpOWFoakcvaXRVb2RSSVFFSVF2OFd5UExVT3JiSFo5RmcxOGJUWTlQSnJPdzAxdU5NdVRwWlR1azRQY0FQdTg9.YA-QpZRGEYqNpQz5Un_eFwY20-JijfDKJvX6sC5z-XE"; - public static String dummy_xml_string = "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - ""; -} + public static String dummy_xml_string = ""; + public static String remove2(String input) { + // Descomposición canónica + String normalized = Normalizer.normalize(input, Normalizer.Form.NFD); + // Nos quedamos únicamente con los caracteres ASCII + Pattern pattern = Pattern.compile("\\p{ASCII}+"); + return pattern.matcher(normalized).replaceAll(""); + } + + public static boolean isNoAlphaNumeric(String s) { + return s.matches("[ \\w]+"); + } + + public static byte[] fileToBytes(File file) { + byte[] bytesArray = new byte[(int) file.length()]; + + FileInputStream fis = null; + try { + fis = new FileInputStream(file); + fis.read(bytesArray); //read file into bytes[] + fis.close(); + + return bytesArray; + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + + } + + public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { + long diffInMillies = date2.getTime() - date1.getTime(); + return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); + } + + public static String removeBadChars(String s) { + if (s == null) return null; + StringBuilder sb = new StringBuilder(); + for(int i = 0 ; i < s.length() ; i++){ + if (Character.isHighSurrogate(s.charAt(i))) continue; + sb.append(s.charAt(i)); + } + return sb.toString(); + } + + public static StringBuffer removeUTFCharacters(String data){ + Pattern p = Pattern.compile("\\\\u(\\p{XDigit}{4})"); + Matcher m = p.matcher(data); + StringBuffer buf = new StringBuffer(data.length()); + while (m.find()) { + String ch = String.valueOf((char) Integer.parseInt(m.group(1), 16)); + m.appendReplacement(buf, Matcher.quoteReplacement(ch)); + } + m.appendTail(buf); + return buf; + } + + + } diff --git a/src/test/resources/33-elbueno.xml b/src/test/resources/33-elbueno.xml new file mode 100644 index 0000000..73ece88 --- /dev/null +++ b/src/test/resources/33-elbueno.xml @@ -0,0 +1 @@ + \ No newline at end of file From f8d2d7866e64510b488a8ddfa0f55b0a16d8bf91 Mon Sep 17 00:00:00 2001 From: Rich Barusta Date: Tue, 18 Apr 2017 12:34:53 -0500 Subject: [PATCH 2/5] MDA-5656 change readme --- .idea/workspace.xml | 78 +++++++++++++++++++-------------------------- README.md | 25 +++++++++++---- 2 files changed, 50 insertions(+), 53 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6b2406a..091b5fb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,25 +7,7 @@ - - - - - - - - - - - - - - - - - - - + @@ -46,8 +28,8 @@ - - + + @@ -143,7 +125,6 @@ @@ -173,10 +155,10 @@ - @@ -728,8 +710,8 @@ - - + + @@ -754,8 +736,8 @@ - - + + @@ -960,21 +942,6 @@ - - - - - - - - - - - - - - - @@ -1046,7 +1013,7 @@ - + @@ -1153,10 +1120,29 @@ + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 06a96d3..59b4e52 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,11 @@ Java 1.6 o superior ##Dependencias * [Unirest](http://unirest.io/java.html) +* [org.json](http://www.json.org/java) +* [httpclient 4.3.6](http://hc.apache.org/downloads.cgi) +* [httpmime 4.3.6](http://hc.apache.org/downloads.cgi) +* [httpasyncclient 4.0.2](http://hc.apache.org/downloads.cgi) + ##Instalación Descargas el modulo mediante Maven: @@ -22,18 +27,24 @@ Descargas el modulo mediante Maven: mx.com.sw.services SW-JAVA - 1.0.0 + 1.0.1 ``` -##Instalación manual -``` -git clone https://github.com/lunasoft/sw-sdk-java.git -cd sw-sdk-java/ -mvn clean package +##Instalación manual (sin Maven) +Descargar el modulo directamente de los siguientes links: +* [Relase Github](https://github.com/lunasoft/sw-sdk-java/releases) +* [Maven](https://oss.sonatype.org/service/local/repositories/releases/content/mx/com/sw/services/SW-JAVA/1.0.0/SW-JAVA-1.0.0.jar) +Asi como instalar manualmente cada una de las dependencias: +* [Unirest](http://unirest.io/java.html) +* [org.json](http://www.json.org/java) +* [httpclient 4.3.6](http://hc.apache.org/downloads.cgi) +* [httpmime 4.3.6](http://hc.apache.org/downloads.cgi) +* [httpasyncclient 4.0.2](http://hc.apache.org/downloads.cgi) + +Alternativamente tambien se cuenta con un archivo JAR en el que se incluyen todas las dependencias, este se encuentra en la carpeta [Relase Github](https://github.com/lunasoft/sw-sdk-java/releases), con el sufijo _"jar-with-dependencies.jar"_ -``` ##Uso rapido... From e30a828ef638f95382086acfdcd3909215b307d9 Mon Sep 17 00:00:00 2001 From: Rich Barusta Date: Tue, 18 Apr 2017 12:37:21 -0500 Subject: [PATCH 3/5] MDA-5656 add readme file --- .idea/workspace.xml | 42 +++++++++++++++++++++++++++++------------- README.md | 2 ++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 091b5fb..b8c537d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -25,7 +25,7 @@ - + @@ -35,6 +35,22 @@ + + + + + + + + + + + + + + + + @@ -1120,14 +1136,22 @@ + + + + + + + + - - + + - - + + @@ -1139,14 +1163,6 @@ - - - - - - - - diff --git a/README.md b/README.md index 59b4e52..1c3447a 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Java 1.6 o superior ##Instalación + Descargas el modulo mediante Maven: ```html @@ -33,6 +34,7 @@ Descargas el modulo mediante Maven: ``` ##Instalación manual (sin Maven) Descargar el modulo directamente de los siguientes links: + * [Relase Github](https://github.com/lunasoft/sw-sdk-java/releases) * [Maven](https://oss.sonatype.org/service/local/repositories/releases/content/mx/com/sw/services/SW-JAVA/1.0.0/SW-JAVA-1.0.0.jar) Asi como instalar manualmente cada una de las dependencias: From f2d2fd0f8b1e4baab5e527b0d93c06d9a858a632 Mon Sep 17 00:00:00 2001 From: Rich Barusta Date: Tue, 2 May 2017 11:58:10 -0500 Subject: [PATCH 4/5] SDT-386 --- .idea/workspace.xml | 311 +++++++++++------- pom.xml | 2 +- .../Utils/Requests/Stamp/StampRequest.java | 2 +- .../java/Tests/Stamp/SWStampServiceTest.java | 11 +- src/test/java/Tests/Utils.java | 2 +- 5 files changed, 201 insertions(+), 127 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b8c537d..8a17d43 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,7 +7,11 @@ - + + + + + @@ -28,25 +32,19 @@ - - + + - - - - - - - - - - - - + + + + + + @@ -54,18 +52,18 @@ - - + + - + - - + + @@ -146,7 +144,6 @@ @@ -194,21 +192,6 @@ - - - - - - - - - - - @@ -238,6 +221,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -278,7 +364,7 @@ - + @@ -357,7 +443,7 @@ - + - + @@ -685,12 +771,6 @@ - - - - - - @@ -712,6 +792,12 @@ + + + + + + @@ -727,7 +813,7 @@ - + @@ -739,7 +825,6 @@ - @@ -750,15 +835,16 @@ - - - + + + + @@ -1004,23 +1090,10 @@ - - - - - - - - - - - - - @@ -1038,16 +1111,6 @@ - - - - - - - - - - @@ -1058,9 +1121,6 @@ - - - @@ -1068,9 +1128,6 @@ - - - @@ -1078,9 +1135,6 @@ - - - @@ -1096,9 +1150,6 @@ - - - @@ -1106,7 +1157,6 @@ - @@ -1114,52 +1164,67 @@ - - + + + + + + + + + + + + + + + + + + + + - - - - - + + + - + - - + + - - + + - - - - - - - - - - - + + + + + - - - + + + + + + + + diff --git a/pom.xml b/pom.xml index 27037f8..37be55a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ mx.com.sw.services SW-JAVA - 1.0.1 + 0.0.1.2 jar diff --git a/src/main/java/Utils/Requests/Stamp/StampRequest.java b/src/main/java/Utils/Requests/Stamp/StampRequest.java index 2974ff3..439087c 100644 --- a/src/main/java/Utils/Requests/Stamp/StampRequest.java +++ b/src/main/java/Utils/Requests/Stamp/StampRequest.java @@ -35,7 +35,7 @@ public IResponse sendRequest(IRequest request) throws GeneralException, AuthExce tempFile.deleteOnExit(); HttpResponse response = Unirest.post(request.URI) - .header("Authorization",request.Token) + .header("Authorization","bearer "+request.Token) .field("xml",tempFile).asJson(); if (response.getStatus()==403){ diff --git a/src/test/java/Tests/Stamp/SWStampServiceTest.java b/src/test/java/Tests/Stamp/SWStampServiceTest.java index 30ae172..37840db 100644 --- a/src/test/java/Tests/Stamp/SWStampServiceTest.java +++ b/src/test/java/Tests/Stamp/SWStampServiceTest.java @@ -22,7 +22,7 @@ public class SWStampServiceTest extends TestCase { - /* public void testStampREAL_XML_STRING_USER_PASSWORD_AUTH() throws Exception { + public void testStampREAL_XML_STRING_USER_PASSWORD_AUTH_V1() throws Exception { SWStampService api = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); IResponse response = null; response = api.Stamp(Utils.dummy_xml_string,"v1"); @@ -31,6 +31,15 @@ public class SWStampServiceTest extends TestCase { System.out.println(response.Data); } + public void testStampREAL_XML_STRING_USER_PASSWORD_AUTH_V2() throws Exception { + SWStampService api = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); + IResponse response = null; + response = api.Stamp(Utils.dummy_xml_string,"v2"); + System.out.println(response.Status); + System.out.println(response.HttpStatusCode); + System.out.println(response.Data); + } +/* public void testStampREAL_XML_BYTES_USER_PASSWORD_AUTH() throws Exception { SWStampService api = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); diff --git a/src/test/java/Tests/Utils.java b/src/test/java/Tests/Utils.java index 2cc59a2..ff79149 100644 --- a/src/test/java/Tests/Utils.java +++ b/src/test/java/Tests/Utils.java @@ -16,7 +16,7 @@ public class Utils { public static String dummy_token = "T2lYQ0t4L0RHVkR4dHZ5Nkk1VHNEakZ3Y0J4Nk9GODZuRyt4cE1wVm5tbXB3YVZxTHdOdHAwVXY2NTdJb1hkREtXTzE3dk9pMmdMdkFDR2xFWFVPUTQyWFhnTUxGYjdKdG8xQTZWVjFrUDNiOTVrRkhiOGk3RHladHdMaEM0cS8rcklzaUhJOGozWjN0K2h6R3gwQzF0c0g5aGNBYUt6N2srR3VoMUw3amtvPQ.T2lYQ0t4L0RHVkR4dHZ5Nkk1VHNEakZ3Y0J4Nk9GODZuRyt4cE1wVm5tbFlVcU92YUJTZWlHU3pER1kySnlXRTF4alNUS0ZWcUlVS0NhelhqaXdnWTRncklVSWVvZlFZMWNyUjVxYUFxMWFxcStUL1IzdGpHRTJqdS9Zakw2UGRiMTFPRlV3a2kyOWI5WUZHWk85ODJtU0M2UlJEUkFTVXhYTDNKZVdhOXIySE1tUVlFdm1jN3kvRStBQlpLRi9NeWJrd0R3clhpYWJrVUMwV0Mwd3FhUXdpUFF5NW5PN3J5cklMb0FETHlxVFRtRW16UW5ZVjAwUjdCa2g0Yk1iTExCeXJkVDRhMGMxOUZ1YWlIUWRRVC8yalFTNUczZXdvWlF0cSt2UW0waFZKY2gyaW5jeElydXN3clNPUDNvU1J2dm9weHBTSlZYNU9aaGsvalpQMUxyQ0IvSUh1bHYxaFMxa2xmb3ZIaHlqQlpZZGRmVlpuRDBHdHNweStrZmNPcWZjWktlcm5IZVFhelhRUDBQWXpHS0JmMGhURm9OTU5KUEJLeEZMeGpnR3hpOWFoakcvaXRVb2RSSVFFSVF2OFd5UExVT3JiSFo5RmcxOGJUWTlQSnJPdzAxdU5NdVRwWlR1azRQY0FQdTg9.YA-QpZRGEYqNpQz5Un_eFwY20-JijfDKJvX6sC5z-XE"; - public static String dummy_xml_string = ""; + public static String dummy_xml_string = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; public static String remove2(String input) { // Descomposición canónica String normalized = Normalizer.normalize(input, Normalizer.Form.NFD); From 9f627cd33bc0eb729f0b4492c7b46922a642a223 Mon Sep 17 00:00:00 2001 From: Rich Barusta Date: Tue, 2 May 2017 12:06:11 -0500 Subject: [PATCH 5/5] SDT-386 change readme --- .idea/workspace.xml | 76 ++++++++++++++++++++--------------- README.md | 98 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 32 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8a17d43..4399b03 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,11 +7,7 @@ - - - - - + @@ -33,12 +29,28 @@ - + + + + + + + + + + + + + + + + + @@ -59,11 +71,11 @@ - + - + @@ -153,11 +165,11 @@ @@ -813,7 +825,7 @@ - + @@ -837,7 +849,7 @@ - + @@ -1167,25 +1179,6 @@ - - - - - - - - - - - - - - - - - - - @@ -1205,7 +1198,7 @@ - + @@ -1221,13 +1214,32 @@ - + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 1c3447a..d6eb017 100644 --- a/README.md +++ b/README.md @@ -172,4 +172,102 @@ TimbrarV1 Recibe el contenido de un XML ya emitido (sellado) en formato String } +``` + + + +##Timbrar CFDI V2 +TimbrarV2 Recibe el contenido de un XML ya emitido (sellado) en formato String , posteriormente si la factura y el token son correctos devuelve el CFDI ya tibrado, en caso contrario lanza una excepción. + +##Timbrar XML en formato string utilizando usuario y contraseña +```java + try{ + //Es preferible inicializar el objeto con el usuario y password de nuestra cuenta, en caso contrario se puede incluir solamente el token de acceso + //Se especifica el base path, esto para consumir el api de pruebas o productivo + SWStampService sdk = new SWStampService("demo","123456789","http://services.test.sw.com.mx"); + //Se inicializa un objeto response, que obtendra la respuesta del api + IResponse response = null; + //Se asigna el resultado de la respuesta a dicho objeto + //Se ejecuta el metodo "Stamp", que timbrara nuestro comprobante posteriormente sellado, asi como la versión del servicio de timbrado, + //puede ver mas de estas versiones en el apartado "Versiones de timbrado" + response = sdk.Stamp(stringXML,"v2"); + //El objeto response tendra así los atributos: + // Status: estado de la petición procesada, puede ser : "success", "fail", "error" + // HttpStatusCode: Codigo de respuesta HTTP del servidor: eg. 200, 400, 500 + // Data: Cuerpo de la respuesta que arroja el servidor + //En este caso arrojara el complemento timbre: {"tfd":""} + System.out.println(response.Status); + System.out.println(response.HttpStatusCode); + System.out.println(response.Data); + } + catch(Exception e){ + //En caso de obtener estatus "fail", "error" + //Se generara una excepción + System.out.println(e.getMessage()); + } + + +``` + +##Timbrar XML en formato string utilizando token + +```java + try{ + //Es preferible inicializar el objeto con el usuario y password de nuestra cuenta, en caso contrario se puede incluir solamente el token de acceso + //Se especifica el base path, esto para consumir el api de pruebas o productivo + SWStampService sdk = new SWStampService("T2lYQ0t4L0R....","http://services.test.sw.com.mx"); + //Se inicializa un objeto response, que obtendra la respuesta del api + IResponse response = null; + //Se asigna el resultado de la respuesta a dicho objeto + //Se ejecuta el metodo "Stamp", que timbrara nuestro comprobante posteriormente sellado, asi como la versión del servicio de timbrado, + //puede ver mas de estas versiones en el apartado "Versiones de timbrado" + response = sdk.Stamp(stringXML,"v2"); + //El objeto response tendra así los atributos: + // Status: estado de la petición procesada, puede ser : "success", "fail", "error" + // HttpStatusCode: Codigo de respuesta HTTP del servidor: eg. 200, 400, 500 + // Data: Cuerpo de la respuesta que arroja el servidor + //En este caso arrojara el complemento timbre: {"tfd":""} + System.out.println(response.Status); + System.out.println(response.HttpStatusCode); + System.out.println(response.Data); + } + catch(Exception e){ + //En caso de obtener estatus "fail", "error" + //Se generara una excepción + System.out.println(e.getMessage()); + } + + +``` + +##Timbrar XML en File type utilizando token + +```java + try{ + //Es preferible inicializar el objeto con el usuario y password de nuestra cuenta, en caso contrario se puede incluir solamente el token de acceso + //Se especifica el base path, esto para consumir el api de pruebas o productivo + SWStampService sdk = new SWStampService("T2lYQ0t4L0R....","http://services.test.sw.com.mx"); + //Se inicializa un objeto response, que obtendra la respuesta del api + IResponse response = null; + //Se asigna el resultado de la respuesta a dicho objeto + //Se ejecuta el metodo "Stamp", que timbrara nuestro comprobante posteriormente sellado, asi como la versión del servicio de timbrado, + //puede ver mas de estas versiones en el apartado "Versiones de timbrado" + File fileXML = new File('xfdi.xml'); + response = sdk.Stamp(fileXML,"v2"); + //El objeto response tendra así los atributos: + // Status: estado de la petición procesada, puede ser : "success", "fail", "error" + // HttpStatusCode: Codigo de respuesta HTTP del servidor: eg. 200, 400, 500 + // Data: Cuerpo de la respuesta que arroja el servidor + //En este caso arrojara el complemento timbre: {"tfd":""} + System.out.println(response.Status); + System.out.println(response.HttpStatusCode); + System.out.println(response.Data); + } + catch(Exception e){ + //En caso de obtener estatus "fail", "error" + //Se generara una excepción + System.out.println(e.getMessage()); + } + + ``` \ No newline at end of file