Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Commit

Permalink
Refatorando projeto, limpando e otimizando o código.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoxkk0 committed Sep 13, 2020
1 parent d2c2f71 commit 62f54c5
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 201 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ repositories {
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.jsoup:jsoup:1.13.1'
}
Binary file not shown.
Binary file not shown.
41 changes: 11 additions & 30 deletions src/main/java/br/com/brunoxkk0/RastreioAPI.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
package br.com.brunoxkk0;

import br.com.brunoxkk0.core.Rastreio;
import br.com.brunoxkk0.core.SRO;
import br.com.brunoxkk0.utils.InvalidRequestException;
import br.com.brunoxkk0.utils.InvalidSroException;
import br.com.brunoxkk0.utils.RastreioParser;
import br.com.brunoxkk0.utils.WebHelper;
import br.com.brunoxkk0.utils.RequestFactory;
import br.com.brunoxkk0.utils.RequestProcessor;

public class RastreioAPI {

private String sro;
private String events;
private String currentStatus;
private RastreioAPI(){}

public RastreioAPI(String sro) throws InvalidSroException {
if(!parseSro(sro)){
throw new InvalidSroException();
}
public static Rastreio getRastreio(String SRO) throws InvalidSroException, InvalidRequestException {

this.sro = sro;
SRO sro = new SRO(SRO);

WebHelper webHelper = new WebHelper();
RastreioParser rastreioParser = new RastreioParser(webHelper.post("https://www2.correios.com.br/sistemas/rastreamento/resultado_semcontent.cfm","objetos="+sro, "null"));
RequestFactory requestFactory = new RequestFactory();
RequestProcessor requestProcessor = new RequestProcessor(requestFactory.post(sro));

currentStatus = rastreioParser.getCurrentStatus();
events = rastreioParser.convert();
return new Rastreio(sro, requestProcessor.getEvents());
}

private boolean parseSro(String sro){
String patten = "([A-Z]){2}([0-9]){9}([A-Z]){2}";
return sro.matches(patten);
}

public String getCurrentStatus() {
return currentStatus;
}

public String getEvents() {
return events;
}

public String getSro() {
return sro;
}
}
28 changes: 28 additions & 0 deletions src/main/java/br/com/brunoxkk0/core/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package br.com.brunoxkk0.core;

public class Event {

private final String where;
private final String action;

public Event(String where, String action){
this.where = where;
this.action = action;
}

public String getAction() {
return action;
}

public String getWhere() {
return where;
}

@Override
public String toString() {
return "Event{" +
"where='" + where + '\'' +
", action='" + action + '\'' +
'}';
}
}
34 changes: 34 additions & 0 deletions src/main/java/br/com/brunoxkk0/core/Rastreio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package br.com.brunoxkk0.core;

import java.util.LinkedHashSet;

public class Rastreio {

SRO sro;
LinkedHashSet<Event> events;

public Rastreio(SRO sro, LinkedHashSet<Event> events){
this.sro = sro;
this.events = events;
}

public LinkedHashSet<Event> getEvents() {
return events;
}

public SRO getSro() {
return sro;
}

public Event getLastEvent(){
return events.iterator().next();
}

@Override
public String toString() {
return "Rastreio{" +
"sro=" + sro +
", events=" + events +
'}';
}
}
29 changes: 29 additions & 0 deletions src/main/java/br/com/brunoxkk0/core/SRO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package br.com.brunoxkk0.core;

import br.com.brunoxkk0.utils.InvalidSroException;
import br.com.brunoxkk0.utils.SROParser;

public class SRO {

private final String sro;


public SRO(String sro) throws InvalidSroException {

if(sro == null || !SROParser.match(sro)) throw new InvalidSroException();

this.sro = sro;

}

public String getSro() {
return sro;
}

@Override
public String toString() {
return "SRO{" +
"sro='" + sro + '\'' +
'}';
}
}
13 changes: 13 additions & 0 deletions src/main/java/br/com/brunoxkk0/utils/InvalidRequestException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package br.com.brunoxkk0.utils;

public class InvalidRequestException extends Exception{

public InvalidRequestException(String value, Exception exception){
super(value, exception);
}

public InvalidRequestException(Exception exception){
super(exception);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package br.com.brunoxkk0.utils;

public class InvalidSroException extends Exception {

public InvalidSroException(){
System.out.println("Invalid SRO detected, please verify and try again!");
super("Invalid SRO detected, please verify and try again!");
}
}
117 changes: 0 additions & 117 deletions src/main/java/br/com/brunoxkk0/utils/RastreioParser.java

This file was deleted.

93 changes: 93 additions & 0 deletions src/main/java/br/com/brunoxkk0/utils/RequestFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package br.com.brunoxkk0.utils;

import br.com.brunoxkk0.core.SRO;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class RequestFactory {

private static final String TARGET_URL = "https://www2.correios.com.br/sistemas/rastreamento/resultado_semcontent.cfm";
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36";

private static Proxy INTERNAL_PROXY;

public static void setProxy(Proxy proxy){
INTERNAL_PROXY = proxy;
}

private HttpURLConnection open() throws IOException {

URL url = new URL(TARGET_URL);

HttpURLConnection httpURLConnection;

if(INTERNAL_PROXY != null){
httpURLConnection = (HttpURLConnection) url.openConnection(INTERNAL_PROXY);
}else {
httpURLConnection = (HttpURLConnection) url.openConnection();
}

httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);

httpURLConnection.setRequestProperty("Accept-Charset", StandardCharsets.UTF_8.name());
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);

return httpURLConnection;
}

public byte[] post(SRO sro) throws InvalidRequestException {

HttpURLConnection httpURLConnection;
OutputStream outputStream;

try {

httpURLConnection = open();

} catch (IOException ioException) {

if(ioException instanceof SocketTimeoutException){
throw new InvalidRequestException("TimeOut", ioException);
}

throw new InvalidRequestException(ioException);
}

try {

outputStream = httpURLConnection.getOutputStream();
outputStream.write(("objetos=" + sro.getSro()).getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[16384];

InputStream inputStream = httpURLConnection.getInputStream();

int len;
while ((len = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, len);
}

byteArrayOutputStream.flush();

return byteArrayOutputStream.toByteArray();

} catch (IOException ioException) {
throw new InvalidRequestException(ioException);
}

}

}
Loading

0 comments on commit 62f54c5

Please sign in to comment.