This project has retired. For details please refer to its Attic page.
TimeHandler xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.hicc;
20  
21  
22  import javax.servlet.http.*;
23  
24  import org.apache.hadoop.chukwa.util.XssFilter;
25  
26  import java.util.Calendar;
27  import java.util.TimeZone;
28  import java.util.StringTokenizer;
29  import java.text.SimpleDateFormat;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import com.mdimension.jchronic.Chronic;
33  import com.mdimension.jchronic.Options;
34  import com.mdimension.jchronic.utils.Span;
35  
36  public class TimeHandler {
37    private HttpSession session = null;
38    private TimeZone tz = null;
39    private long start = 0;
40    private long end = 0;
41    private String startDate = null;
42    private String startHour = null;
43    private String startMin = null;
44    private String endDate = null;
45    private String endHour = null;
46    private String endMin = null;
47    private String startS = null;
48    private String endS = null;
49    private XssFilter xf = null;
50      private static Log log=LogFactory.getLog(TimeHandler.class);
51    
52    public TimeHandler(HttpServletRequest request) {
53      this.tz = TimeZone.getTimeZone("UTC");
54      init(request);
55    }
56  
57    public TimeHandler(HttpServletRequest request, String tz) {
58      if (tz != null) {
59        this.tz = TimeZone.getTimeZone(tz);
60      } else {
61        this.tz = TimeZone.getTimeZone("UTC");
62      }
63      init(request);
64    }
65  
66      /*
67       * Using the Chronic library to parse the english string
68       * and convert it to a long (millis seconds since 1970)
69       */
70      public long parseDateShorthand(String d) {
71  	Calendar now = Calendar.getInstance();
72  	long l=now.getTimeInMillis();
73  	d=d.trim();
74  	if (d.compareToIgnoreCase("now")!=0) {
75  	    Options options= new Options(false);
76  	    options.setCompatibilityMode(true);
77  	    options.setNow(now);
78  	    try {
79  		Span span = Chronic.parse(d, options);
80  		l = span.getBegin()*1000;
81  	    } catch (Exception e) {
82  		// exception when parsing
83  		log.error("parse error for: "+d);		
84  	    }
85  	}
86  
87  	/*
88  	 * debug 
89  	 */
90  	/*
91          SimpleDateFormat sf =
92              new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
93  	Date ld=new Date(l);
94  
95  	log.error("Convert:"+d+" to "+Long.toString(l)+" - "+sf.format(ld)+ "-"+ld.getTime());
96  	*/
97  
98  	return l;
99      }
100 
101     public void parsePeriodValue(String period) {
102 	Calendar now = Calendar.getInstance();
103 	this.start = now.getTimeInMillis();
104 	this.end = now.getTimeInMillis();
105 	if (period.equals("last1hr")) {
106 	    start = end - (60 * 60 * 1000);
107 	} else if (period.equals("last2hr")) {
108 	    start = end - (2 * 60 * 60 * 1000);
109 	} else if (period.equals("last3hr")) {
110 	    start = end - (3 * 60 * 60 * 1000);
111 	} else if (period.equals("last6hr")) {
112 	    start = end - (6 * 60 * 60 * 1000);
113 	} else if (period.equals("last12hr")) {
114 	    start = end - (12 * 60 * 60 * 1000);
115 	} else if (period.equals("last24hr")) {
116 	    start = end - (24 * 60 * 60 * 1000);
117 	} else if (period.equals("last7d")) {
118 	    start = end - (7 * 24 * 60 * 60 * 1000);
119 	} else if (period.equals("last30d")) {
120 	    start = end - (30L * 24 * 60 * 60 * 1000);
121 	} else if (period.startsWith("custom;")) {
122 	    
123 	    // default value is between 2 days ago and now
124 	    String startString="2 days ago";
125 	    String endString="now";
126 	    
127 	    // tokenize the value to "custom;2 days ago;now" 
128 	    StringTokenizer st=new StringTokenizer(period,";");
129 	    if (st.hasMoreTokens()) {
130 		st.nextToken(); // skip the first token
131 		if (st.hasMoreTokens()) {
132 		    startString=st.nextToken();
133 		    if (st.hasMoreTokens()) {
134 			endString=st.nextToken();
135 		    }
136 		}
137 	    }
138 	    
139 	    // parse the parameter strings
140 	    start = parseDateShorthand(startString);
141 	    end = parseDateShorthand(endString);
142 	}
143     }
144 
145   public void init(HttpServletRequest request) {
146     xf = new XssFilter(request);
147     Calendar now = Calendar.getInstance();
148     this.session = request.getSession();
149     if (request.getParameter("time_type") == null
150         && session.getAttribute("time_type") == null
151         && session.getAttribute("period") == null
152         && request.getParameter("period") == null) {
153       end = now.getTimeInMillis();
154       start = end - 60 * 60 * 1000;
155       session.setAttribute("period", "last1hr");
156       session.setAttribute("time_type", "last");
157       session.setAttribute("start", "" + start);
158       session.setAttribute("end", "" + end);
159     } else if (request.getParameter("period") != null
160         && !"".equals(request.getParameter("period"))) {
161       String period = xf.getParameter("period");
162       parsePeriodValue(period);
163     } else if (request.getParameter("start") != null
164         && request.getParameter("end") != null) {
165       start = Long.parseLong(request.getParameter("start"));
166       end = Long.parseLong(request.getParameter("end"));
167     } else if ("range".equals(session.getAttribute("time_type"))) {
168       start = Long.parseLong((String) session.getAttribute("start"));
169       end = Long.parseLong((String) session.getAttribute("end"));
170     } else if ("last".equals(session.getAttribute("time_type"))
171         && session.getAttribute("period") != null) {
172       String period = (String) session.getAttribute("period");
173       parsePeriodValue(period);
174     }
175 
176     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
177     SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
178     SimpleDateFormat formatHour = new SimpleDateFormat("HH");
179     SimpleDateFormat formatMin = new SimpleDateFormat("mm");
180 
181     formatter.setTimeZone(this.tz);
182     formatDate.setTimeZone(this.tz);
183     formatHour.setTimeZone(this.tz);
184     formatMin.setTimeZone(this.tz);
185 
186     startS = formatter.format(start);
187     this.startDate = formatDate.format(start);
188     this.startHour = formatHour.format(start);
189     this.startMin = formatMin.format(start);
190     endS = formatter.format(end);
191     this.endDate = formatDate.format(end);
192     this.endHour = formatHour.format(end);
193     this.endMin = formatMin.format(end);
194   }
195 
196   public String getStartDate(String format) {
197     SimpleDateFormat formatter = new SimpleDateFormat(format);
198     formatter.setTimeZone(this.tz);
199     return formatter.format(this.start);
200   }
201 
202   public String getStartDate() {
203     return this.startDate;
204   }
205 
206   public String getStartHour() {
207     return this.startHour;
208   }
209 
210   public String getStartMinute() {
211     return this.startMin;
212   }
213 
214   public String getStartTimeText() {
215     return this.startS;
216   }
217 
218   public long getStartTime() {
219     return start;
220   }
221 
222   public String getEndDate(String format) {
223     SimpleDateFormat formatter = new SimpleDateFormat(format);
224     formatter.setTimeZone(this.tz);
225     return formatter.format(this.end);
226   }
227 
228   public String getEndDate() {
229     return this.endDate;
230   }
231 
232   public String getEndHour() {
233     return this.endHour;
234   }
235 
236   public String getEndMinute() {
237     return this.endMin;
238   }
239 
240   public String getEndTimeText() {
241     return this.endS;
242   }
243 
244   public long getEndTime() {
245     return end;
246   }
247 
248 }