Skip to content

Instantly share code, notes, and snippets.

@Miltex
Created June 27, 2018 14:34
Show Gist options
  • Save Miltex/e4fb1d78bfe6d4951e32a0416817862a to your computer and use it in GitHub Desktop.
Save Miltex/e4fb1d78bfe6d4951e32a0416817862a to your computer and use it in GitHub Desktop.
Requests REST Java
package br.miltex.service;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
import br.com.hermespardini.corebusiness.facade.ValorParametroAplicacaoFacade;
import br.com.hermespardini.corebusiness.model.ext.ValorParametroAplicacaoExt;
import br.com.hermespardini.web.service.IhpMainService;
import br.miltex.dto.req.Pagamento;
import br.miltex.dto.resp.Mensagens;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @author milton.filho
* @since 19 de jun de 2018
*/
public class PagamentoService extends IhpMainService {
private Log log = LogFactory.getLog(getClass());
public PagamentoService(final HttpServletRequest parRequest) throws Exception {
super(parRequest);
}
public Mensagens realizarPagamento(final Pagamento pagamento) throws Exception {
final Gson gson = new Gson();
try {
pagamento.setToken(getClientSecret());
// final Response resposta = executePost(getEndPoint("ENDPOINT_IHP_MAGENTO_TOTEM_PEDIDOS"),
// pagamento);
final Response resposta = executePost(
"http://andariel.56.devs.pentagrama.net.br/pardini/magento/api/rest/pedido_totem",
pagamento);
/// final Response resposta =
// executeRestMethod("https://andariel.56.devs.pentagrama.net.br/pardini/magento/api/rest/pedido_totem",
// pagamento, 80L, TimeUnit.SECONDS);
if (!resposta.isSuccessful()) {
log.info("Resposta Erro API:\n" + resposta);
throw new IOException(resposta.message() + resposta.code());
}
return gson.fromJson(resposta.body().string(), Mensagens.class);
} catch (final Exception exc) {
log.error(exc);
throw exc;
}
}
public Mensagens realizarPagamento2(final Pagamento pagamento) throws Exception {
final Gson gson = new Gson();
try {
pagamento.setToken(getClientSecret());
final HttpClient httpClient = HttpClientBuilder.create().build();
final HttpPost post =
new HttpPost("https://andariel.56.devs.pentagrama.net.br/pardini/magento/api/rest/pedido_totem");
final StringEntity postingString = new StringEntity(gson.toJson(pagamento), "UTF-8");
post.setHeader("Content-type", "application/json; charset=UTF-8");
post.setEntity(postingString);
final HttpResponse response = httpClient.execute(post);
final String json = EntityUtils.toString(response.getEntity(), "UTF-8");
if (response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Error: " + response.getStatusLine().getStatusCode());
}
return gson.fromJson(json, Mensagens.class);
} catch (final Exception exc) {
log.error(exc);
throw exc;
}
}
public Mensagens realizarPagamento(final Pagamento pagamento) throws Exception {
final Gson gson = new Gson();
pagamento.setToken(getClientSecret());
try {
final MediaType jsonMediaType =
MediaType.parse("application/json; charset=UTF-8");
final RequestBody body =
RequestBody.create(jsonMediaType, gson.toJson(pagamento));
final Request locRequest = new Request.Builder()
.url(getEndPoint("ENDPOINT_IHP_MAGENTO_TOTEM_PEDIDOS"))
.header("Content-Type", "application/json")
.post(body)
.build();
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS);
final OkHttpClient client = builder.build();
log.info("Realizar Pagamento\n");
log.info("Dados Enviado:\n" +
gson.toJson(pagamento));
final Response locResponse = client.newCall(locRequest).execute();
if (!locResponse.isSuccessful()) {
log.info("Resposta Erro API:\n" + locResponse);
throw new IOException(locResponse.message() + locResponse.code());
}
log.info("Resposta API: " +
locResponse);
return gson.fromJson(locResponse.body().string(), Mensagens.class);
} catch (final Exception exc) {
log.error(exc);
throw exc;
}
}
public Mensagens realizarPagamento1(final Pagamento pagamento) throws Exception {
// pedido foi criado com exito com esse client...
try {
final Gson gson = new Gson();
pagamento.setToken(getClientSecret());
final Client cliente =
ClientBuilder.newClient();
final WebTarget endPoint =
cliente.target("http://andariel.56.devs.pentagrama.net.br");
final WebTarget recurso =
endPoint.path("pardini/magento/api/rest/pedido_totem");
final Invocation.Builder requisicao =
recurso.request(javax.ws.rs.core.MediaType.APPLICATION_JSON);
final Entity<Pagamento> enti =
Entity.json(pagamento);
final String json = gson.toJson(enti);
final javax.ws.rs.core.Response resposta = requisicao.post(enti);
if (resposta.getStatus() != 200) {
throw new IOException("Erro " + resposta.getStatus());
}
final Mensagens mesage =
resposta.readEntity(Mensagens.class);
return mesage;
} catch (final Exception exc) {
exc.printStackTrace();
throw exc;
}
}
public String getClientSecret() throws Exception {
final ValorParametroAplicacaoExt param = new ValorParametroAplicacaoExt();
param.setChave("CIELO-CHAVE");
final ValorParametroAplicacaoFacade facade = new ValorParametroAplicacaoFacade(param);
final List<ValorParametroAplicacaoExt> valorChave = facade.selectByChaveParametro();
return valorChave.get(0).getValorTexto();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment