Skip to content

Commit

Permalink
Merge pull request #21 from lunasoft/release/sw-sdk-java_0.0.3.3
Browse files Browse the repository at this point in the history
Release/sw sdk java 0.0.3.3
  • Loading branch information
rbarusta authored Jul 20, 2017
2 parents a45003b + 8be0bdd commit 5056879
Show file tree
Hide file tree
Showing 38 changed files with 1,131 additions and 700 deletions.
274 changes: 219 additions & 55 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SW-JAVA.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>mx.com.sw.services</groupId>
<artifactId>SW-JAVA</artifactId>
<version>0.0.2.2</version>
<version>0.0.3.3</version>

<packaging>jar</packaging>

Expand Down Expand Up @@ -37,6 +37,20 @@
<picUrl>https://media.licdn.com/media/AAEAAQAAAAAAAAYGAAAAJDY3NWU0NTkxLWMxOTctNGQ2NC1hMWYwLWQ4NzM4ZjI1OTE5ZQ.jpg</picUrl>
</properties>
</developer>
<developer>
<id>guadalupe.alvarado</id>
<name>Maria Guadalupe Alvarado</name>
<email>guadalupe.alvarado@sw.com.mx</email>
<url>https://github.com/lupineka</url>
<organization>Smarter Web</organization>
<organizationUrl>https://sw.com.mx/</organizationUrl>
<roles>
<role>developer</role>
</roles>
<properties>
<picUrl>https://media.licdn.com/media/AAEAAQAAAAAAAAYGAAAAJDY3NWU0NTkxLWMxOTctNGQ2NC1hMWYwLWQ4NzM4ZjI1OTE5ZQ.jpg</picUrl>
</properties>
</developer>
</developers>
<name>${project.groupId}:${project.artifactId}</name>
<description>SW SDK JAVA</description>
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/Services/Authentication/SWAuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import Exceptions.AuthException;
import Exceptions.GeneralException;
import Services.SWService;
import Utils.Constants;
import Utils.Requests.Authentication.AuthOptionsRequest;
import Utils.Requests.Authentication.AuthRequest;
import Utils.Requests.Authentication.AuthRequestDummy;
import Utils.Requests.Authentication.AuthSoapRequest;
import Utils.Responses.IResponse;

public class SWAuthenticationService extends SWService {
Expand All @@ -19,14 +16,7 @@ public SWAuthenticationService(String user, String password, String URI) {
public IResponse Token() throws GeneralException, AuthException {
AuthOptionsRequest settings = new AuthOptionsRequest(getURI(),getUser(),getPassword());

String dum = settings.URI.split("-")[0];



if (dum.equalsIgnoreCase("d")){
AuthRequestDummy req = new AuthRequestDummy();
return req.sendRequest(settings);
}
AuthRequest req = new AuthRequest();
return req.sendRequest(settings);

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/Services/BalanceAccount/SWBalanceAccountService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Services.BalanceAccount;

//@author: Lupita Alvarado

import Exceptions.AuthException;
import Exceptions.GeneralException;
import Services.SWService;
import Utils.Constants;
import Utils.Requests.Authentication.AuthOptionsRequest;
import Utils.Requests.Authentication.AuthRequest;
import Utils.Requests.BalanceAccount.BalanceAcctOptionsRequest;
import Utils.Requests.BalanceAccount.BalanceAcctRequest;
import Utils.Responses.IResponse;


public class SWBalanceAccountService extends SWService {

public SWBalanceAccountService(String user, String password, String URI) {
super(user, password, URI);
}

public SWBalanceAccountService(String token, String URI) {
super(token, URI);
}

public IResponse GetBalanceAccount() throws AuthException, GeneralException {

if (getToken()==null){

generateToken();
}

//MAKE GET BALANCE ACCOUNT PROCESS, CUSTOMER ALREADY HAS TOKEN

BalanceAcctOptionsRequest settings = new BalanceAcctOptionsRequest(getToken(),getURI());

BalanceAcctRequest req = new BalanceAcctRequest();
return req.sendRequest(settings);

}
}
47 changes: 45 additions & 2 deletions src/main/java/Services/Cancelation/SWCancelationService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
package Services.Cancelation;

import Exceptions.AuthException;
import Exceptions.GeneralException;
import Services.SWService;
import Utils.Constants;
import Utils.Requests.Authentication.AuthOptionsRequest;
import Utils.Requests.Authentication.AuthRequest;
import Utils.Requests.Cancelation.CancelationOptionsRequest;
import Utils.Requests.Cancelation.CancelationRequest;
import Utils.Responses.IResponse;



public class SWCancelationService extends SWService {
protected SWCancelationService(String user, String password, String URI) {

public SWCancelationService(String user, String password, String URI) {
super(user, password, URI);
}

protected SWCancelationService(String token, String URI) {
public SWCancelationService(String token, String URI) {
super(token, URI);
}


public IResponse Cancelation(String uuid, String password_csd, String rfc, String b64Cer, String b64Key) throws AuthException, GeneralException {

if (getToken()==null){
generateToken();
}

//MAKE CANCELATION PROCESS, CUSTOMER ALREADY HAS TOKEN

CancelationOptionsRequest settings = new CancelationOptionsRequest(getToken(),getURI(),uuid, password_csd, rfc, b64Cer, b64Key);

CancelationRequest req = new CancelationRequest();
return req.sendRequest(settings);

}

public IResponse Cancelation(String xml) throws AuthException, GeneralException {

if (getToken()==null){
generateToken();
}

//MAKE CANCELATION PROCESS, CUSTOMER ALREADY HAS TOKEN

CancelationOptionsRequest settings = new CancelationOptionsRequest(getToken(),getURI(),xml);

CancelationRequest req = new CancelationRequest();
return req.sendRequest(settings, true);

}

}
27 changes: 27 additions & 0 deletions src/main/java/Services/SWService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package Services;

import Exceptions.AuthException;
import Exceptions.GeneralException;
import Utils.Constants;
import Utils.Requests.Authentication.AuthOptionsRequest;
import Utils.Requests.Authentication.AuthRequest;
import Utils.Responses.IResponse;
import Utils.Responses.SuccessAuthResponse;

public abstract class SWService {
private String Token = null;
private String User = null;
Expand Down Expand Up @@ -46,4 +54,23 @@ public void setUser(String user) {
public void setPassword(String password) {
Password = password;
}

public void generateToken() throws AuthException, GeneralException {

if (User == null || Password == null) {
//CUSTOMER HASN'T TOKEN, USER AND PASSWORD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(400, "no existen elementos de autenticación");
}

//CUSTOMER HASN'T TOKEN, BUT HAS USER AND PASSWORD --> AUTH,GENERATE TOKEN AND SET TOKEN IN GLOBAL SETTINGS
AuthOptionsRequest settings = new AuthOptionsRequest(URI, getUser(), getPassword());
AuthRequest req = new AuthRequest();
SuccessAuthResponse res = (SuccessAuthResponse) req.sendRequest(settings);
if (res.HttpStatusCode == 200) {
setToken(res.token);
} else {
//CUSTOMER HASN'T TOKEN, AND USER AND PASSWORD ARE BAD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(res.HttpStatusCode, res.message);
}
}
}
86 changes: 4 additions & 82 deletions src/main/java/Services/Stamp/SWStampService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import Exceptions.AuthException;
import Exceptions.GeneralException;
import Services.SWService;
import Utils.Constants;
import Utils.Requests.Authentication.AuthOptionsRequest;
import Utils.Requests.Authentication.AuthRequest;
import Utils.Requests.Stamp.StampOptionsRequest;
import Utils.Requests.Stamp.StampRequest;
import Utils.Requests.Stamp.StampRequestDummy;

import Utils.Responses.IResponse;
import org.json.JSONObject;


import java.nio.charset.Charset;
Expand All @@ -33,27 +28,7 @@ public IResponse Stamp(String xml, String version) throws AuthException, General


if (getToken()==null){

if (getUser()==null || getPassword()==null){
//CUSTOMER HASN'T TOKEN, USER AND PASSWORD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(500,"no existen elementos de autenticación");

}

//CUSTOMER HASN'T TOKEN, BUT HAS USER AND PASSWORD --> AUTH,GENERATE TOKEN AND SET TOKEN IN GLOBAL 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.token);
}
else{
//CUSTOMER HASN'T TOKEN, AND USER AND PASSWORD ARE BAD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(res.HttpStatusCode,res.Data);
}


generateToken();
}
//MAKE STAMP PROCESS, CUSTOMER ALREADY HAS TOKEN

Expand All @@ -70,26 +45,7 @@ public IResponse Stamp(String xml, String version, boolean isb64) throws AuthExc


if (getToken()==null){

if (getUser()==null || getPassword()==null){
//CUSTOMER HASN'T TOKEN, USER AND PASSWORD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(500,"no existen elementos de autenticación");

}

//CUSTOMER HASN'T TOKEN, BUT HAS USER AND PASSWORD --> AUTH,GENERATE TOKEN AND SET TOKEN IN GLOBAL 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.token);
}
else{
//CUSTOMER HASN'T TOKEN, AND USER AND PASSWORD ARE BAD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(res.HttpStatusCode,res.Data);
}

generateToken();

}
//MAKE STAMP PROCESS, CUSTOMER ALREADY HAS TOKEN
Expand All @@ -116,24 +72,7 @@ public IResponse Stamp(byte[] xmlFile, String version, boolean isb64) throws Aut

if (getToken()==null){

if (getUser()==null || getPassword()==null){
//CUSTOMER HASN'T TOKEN, USER AND PASSWORD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(500,"no existen elementos de autenticación");

}

//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,getUser(),getPassword());
AuthRequest req = new AuthRequest();
IResponse res = req.sendRequest(settings);
if (res.HttpStatusCode==200){
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
throw new AuthException(res.HttpStatusCode,res.Data);
}
generateToken();


}
Expand All @@ -155,24 +94,7 @@ public IResponse Stamp(byte[] xmlFile, String version) throws AuthException, Gen

if (getToken()==null){

if (getUser()==null || getPassword()==null){
//CUSTOMER HASN'T TOKEN, USER AND PASSWORD--> WE CANT' DO ANYTHING --> THROW EXCEPTION
throw new AuthException(500,"no existen elementos de autenticación");

}

//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,getUser(),getPassword());
AuthRequest req = new AuthRequest();
IResponse res = req.sendRequest(settings);
if (res.HttpStatusCode==200){
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
throw new AuthException(res.HttpStatusCode,res.Data);
}
generateToken();


}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/Utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public class Constants {
public static String BASE_PATH = "http://services.test.sw.com.mx";
public static String AUTH_PATH = "/security/authenticate";
public static String STAMP_PATH = "/cfdi33/stamp/";
public static String CANCELATION_CSD_PATH = "/cfdi33/cancel/csd";
public static String CANCELATION_XML_PATH = "/cfdi33/cancel/xml";
public static String BALANCE_ACCOUNT_PATH = "/account/balance/";

public static String auth_soap_envelope(String user, String pwd){
return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:suf=\"http://sufacturacion.com/\">\n" +
" <soapenv:Header/>\n" +
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/Utils/Requests/Authentication/AuthRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@ public IResponse sendRequest(IRequest request) throws GeneralException, AuthExce
if (request.URI.isEmpty()){
throw new GeneralException(500,"URL VACIA");
}

String messageDetail = "";
try {

HttpResponse<JsonNode> response = Unirest.post(request.URI)
.header("user",request.User)
.header("password",request.Password).asJson();
if(!response.getBody().toString().isEmpty()) {
JSONObject body = new JSONObject(response.getBody().toString());
if(!body.isNull("messageDetail")){
messageDetail = body.getString("messageDetail");
}

if(response.getStatus()==200){
JSONObject data = body.getJSONObject("data");
return new SuccessAuthResponse(response.getStatus(),body.getString("status"),data.getString("token"),true);
return new SuccessAuthResponse(response.getStatus(),body.getString("status"),data.getString("token"),"OK","OK");
}
else{
return new BadResponse(response.getStatus(),body.getString("status"),body.getString("message"),body.getString("messageDetail"));
return new SuccessAuthResponse(response.getStatus(),body.getString("status"),"",body.getString("message"),messageDetail);

}
}
else{
return new BadResponse(response.getStatus(),"error",response.getStatusText(),response.getStatusText());
}





return new SuccessAuthResponse(response.getStatus(),"error","",response.getStatusText(),response.getStatusText());

}

} catch (UnirestException e) {

Expand Down
Loading

0 comments on commit 5056879

Please sign in to comment.