Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adicionado processamento de consultas ao ws postmon
- Loading branch information
Showing
8 changed files
with
657 additions
and
75 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -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) |
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,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 + '\'' + | ||
'}'; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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 + '\'' + | ||
'}'; | ||
} | ||
} |
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,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 + '\'' + | ||
'}'; | ||
} | ||
} |
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.