Skip to content

Commit

Permalink
MDA-11050
Browse files Browse the repository at this point in the history
se agrega soporte para concurrencia y proxy para autenticacion y cancelacion
  • Loading branch information
Rich Barusta committed Dec 27, 2017
1 parent ab33b19 commit 71904ac
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 72 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Alternativamente tambien se cuenta con un archivo JAR en el que se incluyen toda
//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");
//Si deseas utilizar un proxy customizado, debes agregar dos parametros mas
//Se inicializa un objeto response, que obtendra la respuesta del api
SuccessV1Response response = null;
//Se asigna el resultado de la respuesta a dicho objeto
Expand Down
2 changes: 1 addition & 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.3.4</version>
<version>0.0.3.6</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ public SWAuthenticationService(String user, String password, String URI) {
super(user, password, URI);
}

public SWAuthenticationService(String user, String password, String URI, String hostProxy, String portProxy) {
super(user, password, hostProxy, portProxy, URI);
}


public IResponse Token() throws GeneralException, AuthException {
AuthOptionsRequest settings = new AuthOptionsRequest(getURI(),getUser(),getPassword());
AuthOptionsRequest settings;
if(getProxyHost() != null){

settings = new AuthOptionsRequest(getURI(),getUser(),getPassword(),getProxyHost(),getPortHost());
}else{
settings = new AuthOptionsRequest(getURI(),getUser(),getPassword());
}



AuthRequest req = new AuthRequest();
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/Services/Cancelation/SWCancelationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public SWCancelationService(String user, String password, String URI) {
public SWCancelationService(String token, String URI) {
super(token, URI);
}

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

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


public IResponse Cancelation(String uuid, String password_csd, String rfc, String b64Cer, String b64Key) throws AuthException, GeneralException {
Expand All @@ -30,8 +38,13 @@ public IResponse Cancelation(String uuid, String password_csd, String rfc, Strin
}

//MAKE CANCELATION PROCESS, CUSTOMER ALREADY HAS TOKEN
CancelationOptionsRequest settings;
if(getProxyHost() != null){
settings = new CancelationOptionsRequest(getToken(),getURI(),uuid, password_csd, rfc, b64Cer, b64Key, getProxyHost(),getPortHost());
}else{
settings = new CancelationOptionsRequest(getToken(),getURI(),uuid, password_csd, rfc, b64Cer, b64Key);
}

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

CancelationRequest req = new CancelationRequest();
return req.sendRequest(settings);
Expand All @@ -45,8 +58,13 @@ public IResponse Cancelation(String xml) throws AuthException, GeneralException
}

//MAKE CANCELATION PROCESS, CUSTOMER ALREADY HAS TOKEN
CancelationOptionsRequest settings;
if(getProxyHost() != null){
settings = new CancelationOptionsRequest(getToken(),getURI(),xml,getProxyHost(),getPortHost());
}else{
settings = new CancelationOptionsRequest(getToken(),getURI(),xml);
}

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

CancelationRequest req = new CancelationRequest();
return req.sendRequest(settings, true);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Services/SWService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public abstract class SWService {
private String Password = null;
private String ProxyHost = null;

public SWService(String user,String password, String proxyHost, String portHost, String URI) {
User = user;
Password = password;
ProxyHost = proxyHost;
PortHost = portHost;
this.URI = URI;
}

public SWService(String token, String proxyHost, String portHost, String URI) {
Token = token;
ProxyHost = proxyHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,33 @@
import Utils.Requests.IRequest;

public class AuthOptionsRequest extends IRequest {
private String ProxyHost = null;
private String PortHost = null;

public void setProxyHost(String proxyHost) {
ProxyHost = proxyHost;
}

public void setPortHost(String portHost) {
PortHost = portHost;
}

public AuthOptionsRequest(String URI, String user, String password) {
super(URI+ Constants.AUTH_PATH, user, password);
}

public String getProxyHost() {
return ProxyHost;
}

public String getPortHost() {
return PortHost;
}

public AuthOptionsRequest(String URI, String user, String password, String hostProxy, String portProxy) {

super(URI+ Constants.AUTH_PATH, user, password);
this.setPortHost(portProxy);
this.setProxyHost(hostProxy);
}
}
65 changes: 48 additions & 17 deletions src/main/java/Utils/Requests/Authentication/AuthRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@
import Exceptions.GeneralException;
import Utils.Requests.IRequest;
import Utils.Requests.IRequestor;
import Utils.Requests.Stamp.StampOptionsRequest;
import Utils.Responses.*;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Scanner;

public class AuthRequest implements IRequestor {

Expand All @@ -26,36 +37,56 @@ public IResponse sendRequest(IRequest request) throws GeneralException, AuthExce

String messageDetail = "";
try {
String hostProxy = ((AuthOptionsRequest) request).getProxyHost();
String portProxy = ((AuthOptionsRequest) request).getPortHost();

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httppost = new HttpPost(request.URI);
httppost.setHeader("user", request.User);
httppost.setHeader("password", request.Password);
if( hostProxy !=null && portProxy != null){
HttpHost proxy = new HttpHost(hostProxy, Integer.parseInt(portProxy), request.URI.split(":")[0]);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
httppost.setConfig(config);
}
CloseableHttpResponse responseB = client.execute(httppost);

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");
}
InputStream inputStream = responseB.getEntity().getContent();

if(response.getStatus()==200){
JSONObject data = body.getJSONObject("data");
return new SuccessAuthResponse(response.getStatus(),body.getString("status"),data.getString("token"),"OK","OK");
}
else{
return new SuccessAuthResponse(response.getStatus(),body.getString("status"),"",body.getString("message"),messageDetail);
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String responseString = s.hasNext() ? s.next() : "";

int statusE = responseB.getStatusLine().getStatusCode();
client.close();

if(!responseString.isEmpty()) {
JSONObject body = new JSONObject(responseString);
if(statusE==200){
JSONObject data = body.getJSONObject("data");
return new SuccessAuthResponse(statusE,body.getString("status"),data.getString("token"),"OK","OK");
}
else{
if(!body.isNull("messageDetail")){
messageDetail = body.getString("messageDetail");
}
return new SuccessAuthResponse(statusE,body.getString("status"),"",body.getString("message"),messageDetail);
}
}
else{
return new SuccessAuthResponse(response.getStatus(),"error","",response.getStatusText(),response.getStatusText());
return new SuccessAuthResponse(statusE,"error","",responseB.getStatusLine().getReasonPhrase(),responseB.getStatusLine().getReasonPhrase());

}

} catch (UnirestException e) {

throw new GeneralException(500,"SERVIDOR INACTIVO");
}
catch(JSONException e){
throw new GeneralException(500,e.getMessage());
} catch (ClientProtocolException e) {
throw new GeneralException(500,e.getMessage());
} catch (IOException e) {
throw new GeneralException(500,e.getMessage());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class CancelationOptionsRequest extends IRequest{
private String b64key;

private String xml;
private String ProxyHost;
private String PortHost;


public CancelationOptionsRequest(String token, String URI, String uuid, String password_csd, String rfc, String b64Cer, String b64Key) {
Expand All @@ -22,12 +24,40 @@ public CancelationOptionsRequest(String token, String URI, String uuid, String p
this.b64Cer = b64Cer;
this.b64key = b64Key;
}

public CancelationOptionsRequest(String token, String URI, String uuid, String password_csd, String rfc, String b64Cer, String b64Key, String proxyHost, String portHst) {
super(token, URI+ Constants.CANCELATION_CSD_PATH);
this.uuid = uuid;
this.password_csd = password_csd;
this.rfc = rfc;
this.b64Cer = b64Cer;
this.b64key = b64Key;
ProxyHost = proxyHost;
PortHost = portHst;
}

public CancelationOptionsRequest(String token, String URI, String xml) {
super(token, URI+ Constants.CANCELATION_XML_PATH);
this.xml = xml;
}

public String getProxyHost() {
return ProxyHost;
}

public String getPortHost() {
return PortHost;
}

public CancelationOptionsRequest(String token, String URI, String xml, String proxyHost, String portHst) {
super(token, URI+ Constants.CANCELATION_XML_PATH);
this.xml = xml;
ProxyHost = proxyHost;
PortHost = portHst;


}

public String getUuid() {
return uuid;
}
Expand Down
Loading

0 comments on commit 71904ac

Please sign in to comment.