/** * */ package com.fitbank.bpm.client; import java.io.Closeable; import java.io.InputStream; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import com.fitbank.common.FileHelper; /** * @BANTEC inc */ public abstract class AbstractHTTPClient implements Closeable { protected String server; protected String user; protected int port; protected String password; protected String baseURL; protected HttpClient client = new HttpClient(); protected HttpMethod method = null; public void close() { if (this.method != null) { this.method.releaseConnection(); } } private InputStream executeMethod() throws Exception { /* * Credentials defaultcreds = new UsernamePasswordCredentials(this.user, * this.password); this.client.getState().setCredentials(new * AuthScope(this.server, this.port, AuthScope.ANY_REALM), * defaultcreds); */ this.method.setDoAuthentication(true); int statusCode = this.client.executeMethod(this.method); if (statusCode != HttpStatus.SC_OK) { throw new Exception("Error en la llamada al Servidor: " + statusCode); } return this.method.getResponseBodyAsStream(); } protected InputStream getData(String pResource) throws Exception { this.close(); String url = this.baseURL + pResource; this.method = new GetMethod(url); return this.executeMethod(); } protected InputStream postData(String pResource) throws Exception { this.close(); String url = this.baseURL + pResource; this.method = new PostMethod(url); return this.executeMethod(); } protected void sendCredecials() throws Exception { Credentials defaultcreds = new UsernamePasswordCredentials(this.user, this.password); // this.client.getState().setCredentials(new AuthScope(this.server, // this.port, AuthScope.ANY_REALM), defaultcreds); this.client.getState().setCredentials(new AuthScope(this.server, this.port), defaultcreds); } protected void imageLogin(String pResource) throws Exception { String url = this.baseURL + pResource + "/j_security_check"; this.method = new PostMethod(url); HttpMethodParams param = new HttpMethodParams(); param.setParameter("j_username", this.user); param.setParameter("j_password", this.password); this.method.setParams(param); InputStream in = this.executeMethod(); FileHelper.readStream(in); } protected void setBaseData(String pBaseURL) { this.baseURL = pBaseURL; String aux = this.baseURL.replaceAll("http://", ""); aux = aux.replaceAll("https://", ""); if (aux.indexOf('/') > 0) { aux = aux.substring(0, aux.indexOf('/')); } if (aux.indexOf(':') > 0) { this.server = aux.substring(0, aux.indexOf(':')); this.port = Integer.parseInt(aux.substring(aux.indexOf(':') + 1)); } else { this.port = 80; this.server = aux; } } }