-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from lunasoft/feature/storage
Feature/storage
- Loading branch information
Showing
18 changed files
with
969 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package Services.Storage; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
import Exceptions.AuthException; | ||
import Exceptions.GeneralException; | ||
import Services.SWService; | ||
import Utils.Requests.Storage.StorageOptionsRequest; | ||
import Utils.Requests.Storage.StorageRequest; | ||
import Utils.Responses.IResponse; | ||
|
||
public class SWStorageService extends SWService { | ||
/** | ||
* Constructor de la clase. | ||
* | ||
* @param user correo o usuario de SW | ||
* @param password password de SW. | ||
* @param URI url base | ||
* @param URIAPI base de la API | ||
* @param proxyHost ip o dominio de proxy (null si no se utiliza) | ||
* @param proxyPort número de puerto de proxy (cualquier valor si proxy es | ||
* ull) | ||
* @throws AuthException exception en caso de error. | ||
*/ | ||
public SWStorageService(String user, String password, String URI, String URIAPI, String proxyHost, int proxyPort) | ||
throws AuthException { | ||
super(user, password, URI, URIAPI, proxyHost, proxyPort); | ||
} | ||
|
||
/** | ||
* Constructor de la clase. | ||
* | ||
* @param token token infinito de SW. | ||
* @param URIAPI url base de la API | ||
* @param proxyHost ip o dominio de proxy (null si no se utiliza) | ||
* @param proxyPort número de puerto de proxy (cualquier valor si proxy es | ||
* ull) | ||
*/ | ||
public SWStorageService(String token, String URIAPI, String proxyHost, int proxyPort) { | ||
super(token, URIAPI, proxyHost, proxyPort); | ||
} | ||
|
||
// Metodo para recuperar información de un XML timbrado con SW | ||
public IResponse getXml(UUID uuid) throws AuthException, GeneralException, IOException { | ||
StorageOptionsRequest settings = new StorageOptionsRequest(getToken(), uuid, getURIAPI(), getProxyHost(), | ||
getProxyPort()); | ||
StorageRequest req = new StorageRequest(); | ||
req.sendRequest(settings); | ||
return req.sendRequest(settings); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/Utils/Requests/Storage/StorageOptionsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package Utils.Requests.Storage; | ||
|
||
import java.util.UUID; | ||
import Utils.Constants; | ||
import Utils.Requests.IRequest; | ||
|
||
public class StorageOptionsRequest extends IRequest { | ||
private UUID uuid; | ||
|
||
public StorageOptionsRequest(String token, UUID uuid, String URIAPI, String proxyHost, int proxyPort) { | ||
super(token, URIAPI + Constants.STORAGE_PATH, proxyHost, proxyPort); | ||
this.uuid = uuid; | ||
} | ||
|
||
public UUID getUuid() { | ||
return uuid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package Utils.Requests.Storage; | ||
|
||
import java.io.IOException; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonSyntaxException; | ||
import Exceptions.AuthException; | ||
import Exceptions.GeneralException; | ||
import Utils.Helpers.RequestHelper; | ||
import Utils.Requests.IRequest; | ||
import Utils.Requests.IRequestor; | ||
import Utils.Responses.IResponse; | ||
import Utils.Responses.Storage.StorageData; | ||
import Utils.Responses.Storage.StorageResponse; | ||
|
||
public class StorageRequest implements IRequestor { | ||
StorageData dataXml; | ||
|
||
public IResponse sendRequest(IRequest request) | ||
throws GeneralException, AuthException, GeneralException, IOException { | ||
try { | ||
CloseableHttpClient client = HttpClients.createDefault(); | ||
HttpGet httpget = new HttpGet(request.URI + ((StorageOptionsRequest) request).getUuid()); | ||
httpget.setHeader("Authorization", "bearer " + request.Token); | ||
RequestHelper.setTimeOut(request.options, 3500); | ||
RequestHelper.setProxy(request.options, request.proxyHost, request.proxyPort); | ||
httpget.setConfig(request.options.build()); | ||
CloseableHttpResponse responseB = client.execute(httpget); | ||
HttpEntity entity = responseB.getEntity(); | ||
String responseString = EntityUtils.toString(entity, "UTF-8"); | ||
int status = responseB.getStatusLine().getStatusCode(); | ||
client.close(); | ||
responseB.close(); | ||
|
||
if (!responseString.isEmpty() && status < 500) { | ||
JSONObject body = new JSONObject(responseString); | ||
if (status == 200) { | ||
dataXml = deserialize(responseString, StorageData.class); | ||
StorageResponse response = new StorageResponse(status, body.getString("status"), "OK", "OK", | ||
dataXml); | ||
if (response.getData() == null || response.getData().getData().getRecords().size() <= 0) { | ||
response.setHttpStatusCode(400); | ||
response.setStatus("error"); | ||
response.setMessage("Bad request."); | ||
response.setMessageDetail("El UUID no ha sido encontrado"); | ||
} | ||
return response; | ||
} else { | ||
StorageResponse response = new StorageResponse(status, body.getString("status"), "OK", "OK", | ||
dataXml); | ||
return new StorageResponse(status, "error", response.getMessage(), response.getMessageDetail()); | ||
} | ||
} else { | ||
return new StorageResponse(status, "error", responseB.getStatusLine().getReasonPhrase(), | ||
responseString); | ||
} | ||
} catch (JSONException e) { | ||
throw new GeneralException(500, e.getMessage()); | ||
} | ||
|
||
} | ||
|
||
public <T> T deserialize(String json, Class<T> contentClass) throws JsonSyntaxException, IOException { | ||
if (json == null || json.isEmpty()) { | ||
throw new IOException("No se obtuvo respuesta para el request hecho."); | ||
} | ||
Gson gson = new GsonBuilder().create(); | ||
return gson.fromJson(json, contentClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,79 @@ | ||
package Utils.Responses; | ||
|
||
public abstract class IResponse { | ||
public int HttpStatusCode ; | ||
public int HttpStatusCode; | ||
public String Status; | ||
public String message = null; | ||
public String messageDetail = null; | ||
|
||
//Status error | ||
// Status error | ||
public IResponse(int httpStatusCode, String status, String message, String messageDetail) { | ||
HttpStatusCode = httpStatusCode; | ||
Status = status; | ||
this.message = message; | ||
this.messageDetail = messageDetail; | ||
} | ||
//Status success general | ||
|
||
// Status success general | ||
public IResponse(int httpStatusCode, String status) { | ||
HttpStatusCode = httpStatusCode; | ||
Status = status; | ||
} | ||
|
||
/** | ||
* Obtiene el status de la solicitud. | ||
* Solo puede ser "success" o "error". | ||
* En caso de "success" puede consultar el campo "data". | ||
* En caso de "error", consultar "getMessage" y "getMessageDetail". | ||
* | ||
* @return String | ||
*/ | ||
public String getStatus() { | ||
return this.Status; | ||
} | ||
|
||
public void setStatus(String value) { | ||
this.Status = value; | ||
} | ||
|
||
/** | ||
* En caso de requerirlo se puede modificar el Codigo de respuesta Http.. | ||
* | ||
* @return int | ||
*/ | ||
public void setHttpStatusCode(int value) { | ||
this.HttpStatusCode = value; | ||
} | ||
|
||
/** | ||
* Obtiene el mensaje de error obtenido. | ||
* <b>Nota:</b> Este valor generalmente solo existe cunado la solicitud | ||
* termino con status "error". | ||
* | ||
* @return String | ||
*/ | ||
public String getMessage() { | ||
return this.message; | ||
} | ||
|
||
public void setMessage(String value) { | ||
this.message = value; | ||
} | ||
|
||
/** | ||
* Obtiene los detalles acerca del mensaje de error. | ||
* Útil cuando el mensaje de error no es muy claro. | ||
* <b>Nota:</b> Este valor generalmente solo existe cunado la solicitud | ||
* termino con status "error". | ||
* <b>Nota:</b> Puede ser null. | ||
* | ||
* @return String | ||
*/ | ||
public String getMessageDetail() { | ||
return this.messageDetail; | ||
} | ||
|
||
public void setMessageDetail(String value) { | ||
this.messageDetail = value; | ||
} | ||
} |
Oops, something went wrong.