Created
March 1, 2018 07:03
-
-
Save zzuummaa/7dd70c28697c923cff08a245587935d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.node.ObjectNode; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.conn.ClientConnectionManager; | |
import org.apache.http.conn.scheme.Scheme; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.conn.ssl.SSLSocketFactory; | |
import org.apache.http.conn.ssl.TrustStrategy; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.impl.conn.PoolingClientConnectionManager; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.security.KeyManagementException; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.UnrecoverableKeyException; | |
import java.util.Scanner; | |
public class BitfinexAPIServlet extends HttpServlet{ | |
private Exception exception; | |
private String urlOverHttps = "https://api.bitfinex.com/v1/pubticker/btcusd"; | |
private double usdbtc; | |
private long lastRespTime; | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
if (lastRespTime + 60 * 1000 < System.currentTimeMillis()) { | |
try { | |
usdbtc = requestUSDBTC(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
exception = e; | |
} finally { | |
lastRespTime = System.currentTimeMillis(); | |
} | |
exception = null; | |
} | |
ObjectMapper mapper = new ObjectMapper(); | |
ObjectNode node = mapper.createObjectNode(); | |
node.put("last_price", usdbtc); | |
node.put("error", exception != null); | |
final Object obj = mapper.treeToValue(node, Object.class); | |
final String json = mapper.writeValueAsString(obj); | |
resp.getWriter().print(json); | |
resp.setStatus(HttpServletResponse.SC_OK); | |
} | |
public double requestUSDBTC() throws IOException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { | |
TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; | |
SSLSocketFactory sf = new SSLSocketFactory( | |
acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
SchemeRegistry registry = new SchemeRegistry(); | |
registry.register(new Scheme("https", 8443, sf)); | |
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); | |
DefaultHttpClient httpClient = new DefaultHttpClient(ccm); | |
HttpGet getMethod = new HttpGet(urlOverHttps); | |
HttpResponse response = httpClient.execute(getMethod); | |
String result; | |
try(Scanner s = new Scanner(response.getEntity().getContent()).useDelimiter("\\A")) { | |
result = s.hasNext() ? s.next() : ""; | |
} | |
ObjectMapper objectMapper = new ObjectMapper(); | |
JsonNode jsonNode = objectMapper.readTree(result); | |
return jsonNode.get("last_price").asDouble(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment