001 package org.otfeed.j2ee.web;
002
003 import java.io.IOException;
004 import java.io.PrintWriter;
005 import java.text.SimpleDateFormat;
006
007 import javax.naming.NamingException;
008 import javax.servlet.ServletException;
009 import javax.servlet.http.HttpServlet;
010 import javax.servlet.http.HttpServletRequest;
011 import javax.servlet.http.HttpServletResponse;
012
013 import org.otfeed.OTConnectionSpec;
014 import org.otfeed.command.AggregationSpan;
015 import org.otfeed.event.OTOHLC;
016 import org.otfeed.support.alt.IOutboundConnection;
017 import org.otfeed.support.alt.IOutboundConnectionFactory;
018 import org.otfeed.support.alt.IOutboundConnection.IDataReader;
019 import org.slf4j.Logger;
020 import org.slf4j.LoggerFactory;
021
022 public class OtfeedServlet extends HttpServlet {
023
024 private static final long serialVersionUID = -7822738809089512033L;
025
026 private final static Logger LOG = LoggerFactory.getLogger(OtfeedServlet.class);
027
028 private final static String CONNECTOR_NAME = "java:comp/env/ra/OtfeedResourceAdapter";
029
030 private IOutboundConnection getConnection(String username, String password) throws NamingException, IOException {
031 LOG.debug("requesting connection from web-scoped JND resource named {}", CONNECTOR_NAME);
032 javax.naming.InitialContext ctx = new javax.naming.InitialContext();
033 IOutboundConnectionFactory factory =
034 (IOutboundConnectionFactory)ctx.lookup(CONNECTOR_NAME);
035 LOG.debug("got factory handle, connecting");
036 OTConnectionSpec spec = new OTConnectionSpec();
037 spec.setUsername(username);
038 spec.setPassword(password);
039 return factory.getConnection(spec);
040 }
041
042 private final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
043 private final AggregationSpan SPAN = AggregationSpan.months();
044
045 protected void service(HttpServletRequest request, HttpServletResponse response)
046 throws ServletException, IOException {
047 LOG.debug("Servlet.service()");
048 String startDate = request.getParameter("startDate");
049 String endDate = request.getParameter("endDate");
050 String exchange = request.getParameter("exchange");
051 String symbol = request.getParameter("symbol");
052 String username = request.getParameter("username");
053 String password = request.getParameter("password");
054 LOG.debug("Servlet.service. Params: startDate={}, endDate={}, exchange={}, symbol={}",
055 new Object[] {startDate, endDate, exchange, symbol});
056
057 if (startDate == null || endDate == null || exchange == null || symbol == null || username == null || password == null) {
058 response.sendError(HttpServletResponse.SC_BAD_REQUEST);
059 return;
060 }
061
062 IOutboundConnection connection = null;
063 IDataReader<OTOHLC> reader = null;
064 try {
065 connection = getConnection(username, password);
066 reader = connection.requestHistData(
067 exchange,
068 symbol,
069 format.parse(startDate),
070 format.parse(endDate),
071 SPAN);
072
073 PrintWriter writer = response.getWriter();
074 writer.write("<html><body>\n");
075 writer.write("<h2>" + exchange + "/" + symbol + "</h2>\n");
076 writer.write("<table>\n");
077 writer.write("<tr><td>Date</td><td>Open</td><td>High</td><td>Low</td><td>Close</td></tr>\n");
078
079 for(OTOHLC candle : reader) {
080 writer.write("<tr><td>" + candle.getTimestamp() + "</td>");
081 writer.write("<td>" + candle.getOpenPrice() + "</td>");
082 writer.write("<td>" + candle.getHighPrice() + "</td>");
083 writer.write("<td>" + candle.getLowPrice() + "</td>");
084 writer.write("<td>" + candle.getClosePrice() + "</td></tr>\n");
085 }
086 writer.write("</table>\n");
087 writer.write("</body></html>\n");
088 } catch (Exception e) {
089 throw new ServletException(e);
090 } finally {
091 if(reader != null) {
092 try {
093 reader.cancel();
094 } catch(Exception e) {
095 }
096 }
097 if (connection != null) {
098 connection.close();
099 }
100 }
101 }
102 }