enterprises.orbital.impl.evexmlapi.ApiConnector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eve-xml-api Show documentation
Show all versions of eve-xml-api Show documentation
Library to interact with EVE XML API servers
package enterprises.orbital.impl.evexmlapi;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.digester.Digester;
import org.apache.http.client.utils.URIBuilder;
import org.xml.sax.SAXException;
/**
* Class to initiate an API connection and retrieve a response.
*/
public class ApiConnector {
private final URI baseURI;
private final String agentField;
private final int connectTimeout;
private final int readTimeout;
protected ApiConnector() {
this(null, null, -1, -1);
}
public ApiConnector(URI baseURI, String agentField, int connectTimeout, int readTimeout) {
super();
this.baseURI = baseURI;
this.agentField = agentField;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}
public E execute(
ApiRequest request,
Digester digester,
Class clazz) throws IOException {
return getApiResponse(digester, getInputStream(getURI(request), getParams(request)), clazz);
}
@SuppressWarnings("unchecked")
protected E getApiResponse(
Digester digester,
InputStream inputStream,
Class clazz) throws IOException {
try {
return (E) digester.parse(inputStream);
} catch (SAXException e) {
// re-throw as IOException
throw new IOException(e);
}
}
protected InputStream getInputStream(
URI requestUri,
Map params) throws IOException {
OutputStreamWriter wr = null;
try {
URIBuilder builder = new URIBuilder(requestUri);
for (Entry entry : params.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
URL getter = builder.build().toURL();
HttpURLConnection conn = (HttpURLConnection) getter.openConnection();
if (agentField != null) conn.setRequestProperty("User-Agent", agentField);
if (connectTimeout > -1) conn.setConnectTimeout(connectTimeout);
if (readTimeout > -1) conn.setReadTimeout(readTimeout);
// Some calls return 400 (Bad Request) or 403 (Forbidden) with an error message. In those cases, we need
// the error stream instead of the input stream.
if (conn.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST || conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN)
return conn.getErrorStream();
else
return conn.getInputStream();
} catch (URISyntaxException e) {
// re-throw as IOException
throw new IOException(e);
} finally {
if (wr != null) try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected URI getURI(
ApiRequest request) throws IOException {
URIBuilder builder = new URIBuilder(getBaseURI());
builder.setPath(builder.getPath() + request.getEndpoint().getPath() + ".xml.aspx");
try {
return builder.build();
} catch (URISyntaxException e) {
// re-throw as IOException
throw new IOException(e);
}
}
protected Map getParams(
ApiRequest request) {
Map result = new HashMap();
result.put("version", Integer.toString(request.getEndpoint().getVersion()));
ApiAuth auth = request.getAuth();
if (auth != null) result.putAll(auth.getParams());
Map params = request.getParams();
if (params != null) {
result.putAll(params);
}
return result;
}
protected URI getBaseURI() {
return baseURI;
}
protected ApiConnector getConnector() {
return this;
}
}