View Javadoc
1   package gov.usgs.volcanoes.winston.legacyServer.cmd.http;
2   
3   import java.nio.channels.SocketChannel;
4   import java.text.DateFormat;
5   import java.text.DecimalFormat;
6   import java.text.NumberFormat;
7   import java.text.ParseException;
8   import java.text.SimpleDateFormat;
9   import java.util.Date;
10  import java.util.Map;
11  import java.util.TimeZone;
12  
13  import gov.usgs.net.HttpRequest;
14  import gov.usgs.net.HttpResponse;
15  import gov.usgs.net.NetTools;
16  import gov.usgs.volcanoes.core.time.J2kSec;
17  import gov.usgs.volcanoes.core.util.StringUtils;
18  import gov.usgs.volcanoes.winston.db.Data;
19  import gov.usgs.volcanoes.winston.db.WaveServerEmulator;
20  import gov.usgs.volcanoes.winston.db.WinstonDatabase;
21  import gov.usgs.volcanoes.winston.legacyServer.WWS;
22  
23  /**
24   * All possible HTTP commands are expected to extend this class. When a new
25   * class is created, it must be added to HttpGetCommand
26   *
27   * @author Tom Parker
28   *
29   */
30  public abstract class AbstractHttpCommand {
31    protected static final String INPUT_DATE_FORMAT = "yyyyMMddHHmm";
32    protected static final String DISPLAY_DATE_FORMAT = "yyyy-MM-dd HH:mm";
33    protected final static int ONE_MINUTE = 60;
34    protected final static int ONE_HOUR = 60 * ONE_MINUTE;
35    protected final static int ONE_DAY = 24 * ONE_HOUR;
36    protected static final String DEFAULT_TZ = "UTC";
37  
38  
39    protected SocketChannel socketChannel;
40    protected NetTools netTools;
41    protected WinstonDatabase winston;
42    protected WWS wws;
43    protected Data data;
44    protected WaveServerEmulator emulator;
45    protected int maxDays;
46    protected HttpRequest request;
47    protected String cmd;
48    protected Map<String, String> arguments;
49    protected DecimalFormat decimalFormat;
50  
51  
52    
53    /**
54     * Command as a http file
55     * 
56     * @return command, including leading /
57     */
58    abstract public String getCommand();
59  
60  
61    /**
62     * Do the work. Return response to the browser.
63     */
64    abstract protected void sendResponse();
65  
66    /**
67     * Class constructor.
68     * 
69     * @param nt
70     * @param db
71     * @param wws
72     */
73    protected AbstractHttpCommand(final NetTools nt, final WinstonDatabase db, final WWS wws) {
74      netTools = nt;
75      winston = db;
76      this.wws = wws;
77      maxDays = wws.getMaxDays();
78      emulator = new WaveServerEmulator(db);
79      data = new Data(db);
80      decimalFormat = (DecimalFormat) NumberFormat.getInstance();
81      decimalFormat.setMaximumFractionDigits(3);
82      decimalFormat.setGroupingUsed(false);
83    }
84  
85    /**
86     * Initiate response. Set variable and pass control to the command.
87     * 
88     * @param cmd
89     * @param c
90     * @param request
91     */
92    public void respond(final String cmd, final SocketChannel c, final HttpRequest request) {
93      this.socketChannel = c;
94      this.request = request;
95      this.cmd = cmd;
96      arguments = request.getArguments();
97      sendResponse();
98    }
99  
100   /**
101    * Return a string as a HTML page
102    * 
103    * @param msg
104    */
105   public void writeSimpleHTML(final String msg) {
106     final String html = "<html><body>" + msg + "</body></html>";
107     final HttpResponse response = new HttpResponse("text/html");
108     response.setLength(html.length());
109     netTools.writeString(response.getHeaderString(), socketChannel);
110     netTools.writeString(html, socketChannel);
111   }
112 
113   /**
114    * Return a string as a Text page
115    * 
116    * @param msg
117    */
118   public void writeSimpleText(final String msg) {
119 
120     final HttpResponse response = new HttpResponse("text/plain");
121     response.setLength(msg.length());
122     netTools.writeString(response.getHeaderString(), socketChannel);
123     netTools.writeString(msg, socketChannel);
124   }
125 
126   /**
127    * 
128    * @param t1
129    *          start time String
130    * @param endTime
131    *          end time
132    * @param mult
133    *          number of seconds per interval used for relative times. 60 for
134    *          minutes, 60*60 for hours, 60*60*24 for days
135    * @return
136    * @throws ParseException
137    */
138   protected Double getStartTime(final String t1, final Double endTime, final long mult) {
139     Double startTime = Double.NaN;
140 
141     if (t1 == null || t1.substring(0, 1).equals("-")) {
142       final double hrs = StringUtils.stringToDouble(t1, -12);
143       startTime = endTime + hrs * mult;
144     } else {
145       final DateFormat dateFormat = new SimpleDateFormat(INPUT_DATE_FORMAT);
146       dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
147       Date bt;
148       try {
149         bt = dateFormat.parse(t1);
150         startTime = J2kSec.fromDate(bt);
151       } catch (final ParseException e) {
152         startTime = Double.NaN;
153       }
154     }
155 
156     return timeOrMaxDays(startTime);
157   }
158 
159   /*
160    * convert time back to UTC
161    */
162   protected Double getStartTime(final String t1, final Double endTime, final long mult,
163       final TimeZone tz) {
164     final double startTime = getStartTime(t1, endTime, mult);
165     return timeOrMaxDays(startTime - (tz.getOffset(J2kSec.asEpoch(endTime))));
166   }
167 
168   /**
169    * parse end time
170    * 
171    * @param t2
172    * @return
173    */
174   protected Double getEndTime(final String t2) {
175     Double endTime = Double.NaN;
176     if (t2 == null || t2.equalsIgnoreCase("now"))
177       endTime = J2kSec.now();
178     else {
179       final DateFormat dateFormat = new SimpleDateFormat(INPUT_DATE_FORMAT);
180       dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
181       Date bt;
182       try {
183         bt = dateFormat.parse(t2);
184         endTime = J2kSec.fromDate(bt) - 1;
185       } catch (final ParseException e) {
186         endTime = Double.NaN;
187       }
188     }
189 
190     return timeOrMaxDays(endTime);
191   }
192 
193   /**
194    * convert back to UTC
195    * 
196    */
197   protected Double getEndTime(final String t2, final TimeZone tz) {
198     final Double endTime = getEndTime(t2);
199     return timeOrMaxDays(endTime - (tz.getOffset(J2kSec.asEpoch(endTime))));
200   }
201 
202   /**
203    * Apply maxDays to time
204    * 
205    * @param t
206    *          time
207    * @return greater of t or now less maxDays
208    */
209   protected double timeOrMaxDays(final double t) {
210     if (maxDays == 0)
211       return t;
212     else
213       return Math.max(t, J2kSec.now() - (maxDays * ONE_DAY));
214   }
215 
216   /**
217    * Convert a boolean value to an integer as passed in arguments.
218    * 
219    * @param in
220    * @return
221    */
222   protected int boolToInt(final boolean in) {
223     return in == true ? 1 : 0;
224   }
225 }