Skip to content

Commit

Permalink
adicionado processamento de consultas ao ws postmon
Browse files Browse the repository at this point in the history
  • Loading branch information
uliss3s committed May 10, 2016
1 parent ece9c6e commit 963375a
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 75 deletions.
439 changes: 365 additions & 74 deletions .idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion README.md
@@ -1,3 +1,4 @@
# ceputil

Este projeto utiliza os webservices do correio SIGEP WEB (http://www.correios.com.br) e do viacep (https://viacep.com.br).
Este projeto utiliza os webservices do correio SIGEP WEB (http://www.correios.com.br), viacep (https://viacep.com.br) e
postmon (http://postmon.com.br)
32 changes: 32 additions & 0 deletions src/main/java/br/com/postmon/CidadeInfo.java
@@ -0,0 +1,32 @@
package br.com.postmon;

public class CidadeInfo {
private String area_km2;
private String codigo_ibge;

public String getArea_km2() {
return area_km2;
}

public CidadeInfo setArea_km2(String area_km2) {
this.area_km2 = area_km2;
return this;
}

public String getCodigo_ibge() {
return codigo_ibge;
}

public CidadeInfo setCodigo_ibge(String codigo_ibge) {
this.codigo_ibge = codigo_ibge;
return this;
}

@Override
public String toString() {
return "CidadeInfo{" +
"area_km2='" + area_km2 + '\'' +
", codigo_ibge='" + codigo_ibge + '\'' +
'}';
}
}
122 changes: 122 additions & 0 deletions src/main/java/br/com/postmon/ClienteWs.java
@@ -0,0 +1,122 @@
package br.com.postmon;

import com.github.uliss3s.ceputil.Util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonValue;
import java.util.*;

/**
* Classe para recuperar informações do WS do postmon.com.br
*/
public class ClienteWs {

private static final Set<String> CAMPOS = new HashSet<String>(Arrays.asList(
"bairro",
"cidade",
"cep",
"logradouro",
"estado_info",
"area_km2",
"codigo_ibge",
"nome",
"cidade_info",
"area_km2",
"codigo_ibge",
"estado"
));

/**
* Recupera objeto Endereco pelo CEP
* @param cep String no formato 00000000
* @return instancia de br.com.postmon.Endereco
*/
public static Endereco getEnderecoPorCep(String cep) {

JsonObject jsonObject = getCepResponse(cep);
Endereco endereco = new Endereco()
.setBairro(jsonObject.getString("bairro"))
.setCidade(jsonObject.getString("cidade"))
.setCep(jsonObject.getString("cep"))
.setLogradouro(jsonObject.getString("logradouro"))
.setEstadoInfo(new EstadoInfo()
.setNome(jsonObject.getJsonObject("estado_info").getString("nome"))
.setArea_km2(jsonObject.getJsonObject("estado_info").getString("area_km2"))
.setCodigo_ibge(jsonObject.getJsonObject("estado_info").getString("codigo_ibge"))
)
.setCidadeInfo(new CidadeInfo()
.setArea_km2(jsonObject.getJsonObject("cidade_info").getString("area_km2"))
.setCodigo_ibge(jsonObject.getJsonObject("cidade_info").getString("codigo_ibge"))
)
.setEstado(jsonObject.getString("estado"));

return endereco;
}

/**
* Recupera Map<String,String> pelo CEP
* @param cep String no formato 00000000
* @return instancia de Map<String,Object>
*/
public static Map<String, Object> getMapPorCep(String cep) {

JsonObject jsonObject = getCepResponse(cep);

Map<String, Object> mapa = null;
mapa = new HashMap<String, Object>();

for (Iterator<Map.Entry<String,JsonValue>> it = jsonObject.entrySet().iterator(); it.hasNext();) {
Map.Entry<String,JsonValue> entry = it.next();

if (entry.getValue().getValueType().equals(JsonValue.ValueType.OBJECT)) {
JsonObject subObject = jsonObject.getJsonObject(entry.getKey());

Map<String, String> mapaSo = new HashMap<String, String>();
for (Iterator<Map.Entry<String,JsonValue>> itso = subObject.entrySet().iterator(); itso.hasNext();) {
Map.Entry<String,JsonValue> entrySo = itso.next();

mapaSo.put(entrySo.getKey(), entrySo.getValue().toString());
}

mapa.put(entry.getKey(), mapaSo);
} else {
mapa.put(entry.getKey(), entry.getValue().toString());
}
}

return mapa;
}

private static JsonObject getCepResponse(String cep) {

JsonObject responseJO = null;

try {
if (!Util.validaCep(cep)) {
throw new RuntimeException("Formato de CEP inválido");
}

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://api.postmon.com.br/v1/cep/" + cep);
HttpResponse response = httpclient.execute(httpGet);

if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException(response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();

responseJO = Json.createReader(entity.getContent()).readObject();

} catch (Exception e) {
throw new RuntimeException(e);
}

return responseJO;
}
}
90 changes: 90 additions & 0 deletions src/main/java/br/com/postmon/Endereco.java
@@ -0,0 +1,90 @@
package br.com.postmon;

/**
* Entidade baseada nos dados do WS do postmon.com
*/
public class Endereco {
private String bairro;
private String cidade;
private String cep;
private String logradouro;
private EstadoInfo estadoInfo;
private CidadeInfo cidadeInfo;
private String estado;

public String getBairro() {
return bairro;
}

public Endereco setBairro(String bairro) {
this.bairro = bairro;
return this;
}

public String getCidade() {
return cidade;
}

public Endereco setCidade(String cidade) {
this.cidade = cidade;
return this;
}

public String getCep() {
return cep;
}

public Endereco setCep(String cep) {
this.cep = cep;
return this;
}

public String getLogradouro() {
return logradouro;
}

public Endereco setLogradouro(String logradouro) {
this.logradouro = logradouro;
return this;
}

public EstadoInfo getEstadoInfo() {
return estadoInfo;
}

public Endereco setEstadoInfo(EstadoInfo estadoInfo) {
this.estadoInfo = estadoInfo;
return this;
}

public CidadeInfo getCidadeInfo() {
return cidadeInfo;
}

public Endereco setCidadeInfo(CidadeInfo cidadeInfo) {
this.cidadeInfo = cidadeInfo;
return this;
}

public String getEstado() {
return estado;
}

public Endereco setEstado(String estado) {
this.estado = estado;
return this;
}

@Override
public String toString() {
return "Endereco{" +
"bairro='" + bairro + '\'' +
", cidade='" + cidade + '\'' +
", cep='" + cep + '\'' +
", logradouro='" + logradouro + '\'' +
", estadoInfo=" + estadoInfo +
", cidadeInfo=" + cidadeInfo +
", estado='" + estado + '\'' +
'}';
}
}
43 changes: 43 additions & 0 deletions src/main/java/br/com/postmon/EstadoInfo.java
@@ -0,0 +1,43 @@
package br.com.postmon;

public class EstadoInfo {
private String area_km2;
private String codigo_ibge;
private String nome;

public String getArea_km2() {
return area_km2;
}

public EstadoInfo setArea_km2(String area_km2) {
this.area_km2 = area_km2;
return this;
}

public String getCodigo_ibge() {
return codigo_ibge;
}

public EstadoInfo setCodigo_ibge(String codigo_ibge) {
this.codigo_ibge = codigo_ibge;
return this;
}

public String getNome() {
return nome;
}

public EstadoInfo setNome(String nome) {
this.nome = nome;
return this;
}

@Override
public String toString() {
return "EstadoInfo{" +
"area_km2='" + area_km2 + '\'' +
", codigo_ibge='" + codigo_ibge + '\'' +
", nome='" + nome + '\'' +
'}';
}
}
3 changes: 3 additions & 0 deletions src/test/java/Teste.java
Expand Up @@ -7,5 +7,8 @@ public static void main(String[] args) throws IOException {

System.out.println(br.com.viacep.ClienteWs.getEnderecoPorCep("70002900"));
System.out.println(br.com.viacep.ClienteWs.getMapPorCep("70002900"));

System.out.println(br.com.postmon.ClienteWs.getEnderecoPorCep("69046380"));
System.out.println(br.com.postmon.ClienteWs.getMapPorCep("69046380"));
}
}
Binary file modified target/cep-util-1.0.jar
Binary file not shown.

0 comments on commit 963375a

Please sign in to comment.