diff --git a/build.gradle b/build.gradle index 4602d1f..5784afd 100644 --- a/build.gradle +++ b/build.gradle @@ -12,5 +12,5 @@ repositories { } dependencies { - testCompile group: 'junit', name: 'junit', version: '4.12' + compile 'org.jsoup:jsoup:1.13.1' } diff --git a/build/classes/java/main/br/com/brunoxkk0/JavaRastreio.class b/build/classes/java/main/br/com/brunoxkk0/JavaRastreio.class deleted file mode 100644 index 570f320..0000000 Binary files a/build/classes/java/main/br/com/brunoxkk0/JavaRastreio.class and /dev/null differ diff --git a/build/classes/java/main/br/com/brunoxkk0/utils/WebHelper.class b/build/classes/java/main/br/com/brunoxkk0/utils/WebHelper.class deleted file mode 100644 index 068654d..0000000 Binary files a/build/classes/java/main/br/com/brunoxkk0/utils/WebHelper.class and /dev/null differ diff --git a/src/main/java/br/com/brunoxkk0/RastreioAPI.java b/src/main/java/br/com/brunoxkk0/RastreioAPI.java index fe8be88..ac9f157 100644 --- a/src/main/java/br/com/brunoxkk0/RastreioAPI.java +++ b/src/main/java/br/com/brunoxkk0/RastreioAPI.java @@ -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; - } } diff --git a/src/main/java/br/com/brunoxkk0/core/Event.java b/src/main/java/br/com/brunoxkk0/core/Event.java new file mode 100644 index 0000000..3dc9887 --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/core/Event.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/br/com/brunoxkk0/core/Rastreio.java b/src/main/java/br/com/brunoxkk0/core/Rastreio.java new file mode 100644 index 0000000..7dae071 --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/core/Rastreio.java @@ -0,0 +1,34 @@ +package br.com.brunoxkk0.core; + +import java.util.LinkedHashSet; + +public class Rastreio { + + SRO sro; + LinkedHashSet events; + + public Rastreio(SRO sro, LinkedHashSet events){ + this.sro = sro; + this.events = events; + } + + public LinkedHashSet 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 + + '}'; + } +} diff --git a/src/main/java/br/com/brunoxkk0/core/SRO.java b/src/main/java/br/com/brunoxkk0/core/SRO.java new file mode 100644 index 0000000..5d91f63 --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/core/SRO.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/br/com/brunoxkk0/utils/InvalidRequestException.java b/src/main/java/br/com/brunoxkk0/utils/InvalidRequestException.java new file mode 100644 index 0000000..17ff309 --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/utils/InvalidRequestException.java @@ -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); + } + +} diff --git a/src/main/java/br/com/brunoxkk0/utils/InvalidSroException.java b/src/main/java/br/com/brunoxkk0/utils/InvalidSroException.java index 7fa05af..6329add 100644 --- a/src/main/java/br/com/brunoxkk0/utils/InvalidSroException.java +++ b/src/main/java/br/com/brunoxkk0/utils/InvalidSroException.java @@ -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!"); } } diff --git a/src/main/java/br/com/brunoxkk0/utils/RastreioParser.java b/src/main/java/br/com/brunoxkk0/utils/RastreioParser.java deleted file mode 100644 index a08429e..0000000 --- a/src/main/java/br/com/brunoxkk0/utils/RastreioParser.java +++ /dev/null @@ -1,117 +0,0 @@ -package br.com.brunoxkk0.utils; - -import java.util.ArrayList; - -public class RastreioParser { - - private String body; - private String codeSro = ""; - private String currentStatus = ""; - private ArrayList events; - - public RastreioParser(String raw){ - body = raw.split("")[1]; - - codSro(body); - currentStatus(body); - events(body); - } - - private void codSro(String source){ - String[] base = source.split(""); - - for(String sro : base){ - if(sro.startsWith("")){ - codeSro += sro.replace("","").replace("",""); - } - } - - if(codeSro.split("")[0].length() == 13){ - codeSro = codeSro.split("")[0]; - } - } - - private void currentStatus(String source){ - String base = source.split("
")[1]; - base = base.substring(0, base.indexOf("")); - - - for(String string : base.split("")) { - if(string.substring(source.indexOf("<")).startsWith("
")){ - int i = string.substring(source.indexOf("<")).indexOf(">"); - currentStatus = string.substring(i).substring(3); - } - } - } - - private void events(String source){ - String base = source.substring(source.indexOf("
"),source.lastIndexOf("
")); - ArrayList arrayList = new ArrayList<>(); - - - for (String string : base.split("")){ - for(String substring : string.split("")){ - - if(substring.indexOf("")){ - String subtemp = temp.substring(0,temp.indexOf("")).replace("", ""); - String[] subbase = subtemp.split("
"); - - for(String stringg: subbase){ - result += " " + (stringg.replace(" ","").replace("\t","").replace("","").replace(" ","").replace("","")); - } - } - - int pt2 = -1; - if((pt2 = temp.indexOf("")) >= 0){ - String subtemp = temp.substring(pt2).replace("", ""); - String formated = ""; - - result += "," + subtemp.substring(subtemp.indexOf("")).replace("","").replace("","").replace("\t","").replace("
","").replace("","").replace("",""); - - } - - arrayList.add(result); - } - } - events = arrayList; - } - - public String convert(){ - String finalS = "{ \"events\": ["; - - for(String data : events){ - - - String[] splited = data.split(","); - String[] sub = splited[0].split(" "); - - if(sub.length >= 3){ - finalS += "{\"data\":\"" +sub[1]+"\","; - finalS += "\"time\":\"" +sub[2]+"\","; - finalS += "\"local\":\"" +sub[3]+"\","; - finalS += "\"event\":\"" +splited[1]+"\"},"; - } - - } - - if(finalS.charAt(finalS.length()-1) == ','){ - finalS = finalS.substring(0,finalS.length()-1); - } - - finalS += "]}"; - return finalS; - } - - public String getCodeSro() { - return codeSro; - } - - public String getCurrentStatus() { - return currentStatus; - } -} diff --git a/src/main/java/br/com/brunoxkk0/utils/RequestFactory.java b/src/main/java/br/com/brunoxkk0/utils/RequestFactory.java new file mode 100644 index 0000000..6d532dd --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/utils/RequestFactory.java @@ -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); + } + + } + +} diff --git a/src/main/java/br/com/brunoxkk0/utils/RequestProcessor.java b/src/main/java/br/com/brunoxkk0/utils/RequestProcessor.java new file mode 100644 index 0000000..ce9d523 --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/utils/RequestProcessor.java @@ -0,0 +1,37 @@ +package br.com.brunoxkk0.utils; + +import br.com.brunoxkk0.core.Event; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashSet; + +public class RequestProcessor { + + private final LinkedHashSet events; + + public RequestProcessor(byte[] data){ + Document document = Jsoup.parse(new String(data, StandardCharsets.UTF_8)); + + Elements element = document.body().getElementsByClass("listEvent sro"); + + events = new LinkedHashSet<>(); + + if(element != null){ + for (Element row : element.select("tr")) { + events.add(new Event( + row.getElementsByClass("sroDtEvent").text(), + row.getElementsByClass("sroLbEvent").text() + )); + } + } + + } + + public LinkedHashSet getEvents() { + return events; + } +} diff --git a/src/main/java/br/com/brunoxkk0/utils/SROParser.java b/src/main/java/br/com/brunoxkk0/utils/SROParser.java new file mode 100644 index 0000000..7b3fda7 --- /dev/null +++ b/src/main/java/br/com/brunoxkk0/utils/SROParser.java @@ -0,0 +1,12 @@ +package br.com.brunoxkk0.utils; + +import java.util.regex.Pattern; + +public class SROParser { + + private static final Pattern SRO_PATTERN = Pattern.compile("([A-Z]){2}([0-9]){9}([A-Z]){2}"); + + public static boolean match(String sro){ + return SRO_PATTERN.matcher(sro).find(); + } +} diff --git a/src/main/java/br/com/brunoxkk0/utils/WebHelper.java b/src/main/java/br/com/brunoxkk0/utils/WebHelper.java deleted file mode 100644 index 9d139d6..0000000 --- a/src/main/java/br/com/brunoxkk0/utils/WebHelper.java +++ /dev/null @@ -1,52 +0,0 @@ -package br.com.brunoxkk0.utils; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.StandardCharsets; - -public class WebHelper { - - public String post(String url, String query, String auth) { - try { - - URLConnection conn = open(url, auth); - OutputStream output = conn.getOutputStream(); - output.write(query.getBytes(StandardCharsets.UTF_8.name())); - output.flush(); - output.close(); - InputStream response = conn.getInputStream(); - String outputStr = null; - BufferedReader reader; - String line; - - reader = new BufferedReader(new InputStreamReader(response, StandardCharsets.UTF_8.name())); - while((line = reader.readLine()) != null){ - outputStr += line; - } - - reader.close(); - - return outputStr; - } catch (IOException var9) { - var9.printStackTrace(); - return null; - } - } - - private HttpURLConnection open(String url, String auth) throws IOException { - HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); - conn.setDoOutput(true); - conn.setRequestMethod("POST"); - conn.setRequestProperty("Accept-Charset", StandardCharsets.UTF_8.name()); - conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - if (auth != null) { - conn.setRequestProperty("Authorization", auth); - } - - return conn; - } - -}