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
25
26
27
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
55
56
57
58 abstract public String getCommand();
59
60
61
62
63
64 abstract protected void sendResponse();
65
66
67
68
69
70
71
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
87
88
89
90
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
102
103
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
115
116
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
129
130
131
132
133
134
135
136
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
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
170
171
172
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
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
204
205
206
207
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
218
219
220
221
222 protected int boolToInt(final boolean in) {
223 return in == true ? 1 : 0;
224 }
225 }