twitter4j.HttpClientBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twitter4j-core Show documentation
Show all versions of twitter4j-core Show documentation
A Java library for the Twitter API
The newest version!
package twitter4j;
import twitter4j.auth.Authorization;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public abstract class HttpClientBase implements HttpClient, Serializable {
private static final Logger logger = Logger.getLogger(HttpClientBase.class);
private static final long serialVersionUID = -8016974810651763053L;
protected final HttpClientConfiguration CONF;
private final Map requestHeaders;
public HttpClientBase(HttpClientConfiguration conf) {
this.CONF = conf;
requestHeaders = new HashMap();
requestHeaders.put("X-Twitter-Client-Version", Version.getVersion());
requestHeaders.put("X-Twitter-Client-URL", "http://twitter4j.org/en/twitter4j-" + Version.getVersion() + ".xml");
requestHeaders.put("X-Twitter-Client", "Twitter4J");
requestHeaders.put("User-Agent", "twitter4j http://twitter4j.org/ /" + Version.getVersion());
if (conf.isGZIPEnabled()) {
requestHeaders.put("Accept-Encoding", "gzip");
}
}
protected boolean isProxyConfigured() {
return CONF.getHttpProxyHost() != null && !CONF.getHttpProxyHost().equals("");
}
public void write(DataOutputStream out, String outStr) throws IOException {
out.writeBytes(outStr);
logger.debug(outStr);
}
@Override
public Map getRequestHeaders() {
return requestHeaders;
}
@Override
public void addDefaultRequestHeader(String name, String value) {
requestHeaders.put(name, value);
}
@Override
public final HttpResponse request(HttpRequest req) throws TwitterException {
return handleRequest(req);
}
@Override
public final HttpResponse request(HttpRequest req, HttpResponseListener listener) throws TwitterException {
try {
HttpResponse res = handleRequest(req);
if (listener != null) {
listener.httpResponseReceived(new HttpResponseEvent(req, res, null));
}
return res;
} catch (TwitterException te) {
if (listener != null) {
listener.httpResponseReceived(new HttpResponseEvent(req, null, te));
}
throw te;
}
}
abstract HttpResponse handleRequest(HttpRequest req) throws TwitterException;
@Override
public HttpResponse get(String url, HttpParameter[] parameters
, Authorization authorization, HttpResponseListener listener) throws TwitterException {
return request(new HttpRequest(RequestMethod.GET, url, parameters, authorization, this.requestHeaders), listener);
}
@Override
public HttpResponse get(String url) throws TwitterException {
return request(new HttpRequest(RequestMethod.GET, url, null, null, this.requestHeaders));
}
@Override
public HttpResponse post(String url, HttpParameter[] parameters
, Authorization authorization, HttpResponseListener listener) throws TwitterException {
return request(new HttpRequest(RequestMethod.POST, url, parameters, authorization, this.requestHeaders), listener);
}
@Override
public HttpResponse post(String url) throws TwitterException {
return request(new HttpRequest(RequestMethod.POST, url, null, null, this.requestHeaders));
}
@Override
public HttpResponse delete(String url, HttpParameter[] parameters
, Authorization authorization, HttpResponseListener listener) throws TwitterException {
return request(new HttpRequest(RequestMethod.DELETE, url, parameters, authorization, this.requestHeaders), listener);
}
@Override
public HttpResponse delete(String url) throws TwitterException {
return request(new HttpRequest(RequestMethod.DELETE, url, null, null, this.requestHeaders));
}
@Override
public HttpResponse head(String url) throws TwitterException {
return request(new HttpRequest(RequestMethod.HEAD, url, null, null, this.requestHeaders));
}
@Override
public HttpResponse put(String url, HttpParameter[] parameters
, Authorization authorization, HttpResponseListener listener) throws TwitterException {
return request(new HttpRequest(RequestMethod.PUT, url, parameters, authorization, this.requestHeaders), listener);
}
@Override
public HttpResponse put(String url) throws TwitterException {
return request(new HttpRequest(RequestMethod.PUT, url, null, null, this.requestHeaders));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy