This repository has been archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refatorando projeto, limpando e otimizando o código.
- Loading branch information
Showing
14 changed files
with
260 additions
and
201 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
Binary file not shown.
Binary file not shown.
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,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; | ||
} | ||
} |
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,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 + '\'' + | ||
'}'; | ||
} | ||
} |
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,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 + | ||
'}'; | ||
} | ||
} |
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,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
13
src/main/java/br/com/brunoxkk0/utils/InvalidRequestException.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,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); | ||
} | ||
|
||
} |
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,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
117
src/main/java/br/com/brunoxkk0/utils/RastreioParser.java
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.