AdmMono/src/com/rjconsultores/ventaboletos/rest/IntegracaoSapRest.java

111 lines
4.3 KiB
Java

package com.rjconsultores.ventaboletos.rest;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.rjconsultores.ventaboletos.enums.TipoEnvioRest;
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
public class IntegracaoSapRest {
private static Logger log = Logger.getLogger(IntegracaoSapRest.class);
private static IntegracaoSapRest instance;
public static synchronized IntegracaoSapRest getInstance() {
if(instance == null) {
instance = new IntegracaoSapRest();
}
return instance;
}
public JSONObject enviarIntegracaoSap( FechamentoCntCorrenteVO fechamento, String url, String credenciais ) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = getRequest( url, TipoEnvioRest.POST, credenciais, fechamtoToJson(fechamento));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK
&& response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
throw new IllegalStateException("Ocorreu um erro na requisição." + response.getStatusLine() !=null ? response.getStatusLine().getReasonPhrase() : "");
}
return getRetornoJSON(response);
} catch (IOException e) {
log.error(e);
} catch (JSONException e) {
log.error(e);
} catch (Exception e) {
log.error("Erro no envio/retorno da requisicao para integração SAP", e);
}
return null;
}
private JSONObject getRetornoJSON(HttpResponse response) throws IOException, JSONException {
HttpEntity entity = response.getEntity();
if (entity != null) {
String rest = EntityUtils.toString(entity);
JSONObject result = new JSONObject(rest);
JSONObject boleto = result.getJSONObject("MT_Boleto_RES");
return boleto.getJSONArray("ReturnERP").getJSONObject(0);
}
return null;
}
private String fechamtoToJson( FechamentoCntCorrenteVO fcc){
StringBuilder json = new StringBuilder();
json.append("{");
json.append(" \"ID_LEGADO\": \"010\",");
json.append(" \"AGENCIA\": {");
json.append(" \"KUNNR\": \"").append(fcc.getNumPuntoVenta()).append("\"");
json.append(" },");
json.append(" \"BOLETO\": [{");
json.append(" \"ZBUKRS\": \"").append(fcc.getEmpresaId()).append("\",");
json.append(" \"ZZFCAIXA\": \"").append(fcc.getFechamentocntcorrenteId()).append("\",");
json.append(" \"FILIAL\": {");
json.append(" \"UF\": \"").append(fcc.getUfEmpresa()).append("\",");
json.append(" },");
json.append(" \"GJAHR\": \"").append(fcc.getAnofechamento()).append("\",");
json.append(" \"MONAT\": \"").append(fcc.getMesfechamento()).append("\",");
json.append(" \"BLDAT\": \"").append(fcc.getFecfechamento()).append("\",");
json.append(" \"BUDAT\": \"").append(fcc.getFeclancamento()).append("\",");
json.append(" \"XBLNR\": \"").append(fcc.getNumPuntoVenta()).append("\",");
json.append(" \"WRBTR\": \"").append( fcc.getTotal().toString().replace(",", ".") ).append("\",");
json.append(" }]");
json.append("}");
return json.toString();
}
private HttpUriRequest getRequest(String uri, TipoEnvioRest tipoEnvio, String credenciais, String json) throws Exception {
HttpPost request = new HttpPost(uri);
StringEntity entity = new StringEntity(json);
request.addHeader("Authorization", getAuthorization(credenciais));
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setEntity(entity);
// request.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
return request;
}
private String getAuthorization( String credenciais ) {
return "Basic " + new String(Base64.encodeBase64(credenciais.getBytes()));
}
}