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.equals("lastyear")) {
122     start = end - (365L * 24 * 60 * 60 * 1000);
123 	} else if (period.startsWith("custom;")) {
124 	    
125 	    // default value is between 2 days ago and now
126 	    String startString="2 days ago";
127 	    String endString="now";
128 	    
129 	    // tokenize the value to "custom;2 days ago;now" 
130 	    StringTokenizer st=new StringTokenizer(period,";");
131 	    if (st.hasMoreTokens()) {
132 		st.nextToken(); // skip the first token
133 		if (st.hasMoreTokens()) {
134 		    startString=st.nextToken();
135 		    if (st.hasMoreTokens()) {
136 			endString=st.nextToken();
137 		    }
138 		}
139 	    }
140 	    
141 	    // parse the parameter strings
142 	    start = parseDateShorthand(startString);
143 	    end = parseDateShorthand(endString);
144 	}
145     }
146 
147   public void init(HttpServletRequest request) {
148     xf = new XssFilter(request);
149     Calendar now = Calendar.getInstance();
150     this.session = request.getSession();
151     if (request.getParameter("time_type") == null
152         && session.getAttribute("time_type") == null
153         && session.getAttribute("period") == null
154         && request.getParameter("period") == null) {
155       end = now.getTimeInMillis();
156       start = end - 60 * 60 * 1000;
157       session.setAttribute("period", "last1hr");
158       session.setAttribute("time_type", "last");
159       session.setAttribute("start", "" + start);
160       session.setAttribute("end", "" + end);
161     } else if (request.getParameter("period") != null
162         && !"".equals(request.getParameter("period"))) {
163       String period = xf.getParameter("period");
164       parsePeriodValue(period);
165     } else if (request.getParameter("start") != null
166         && request.getParameter("end") != null) {
167       start = Long.parseLong(request.getParameter("start"));
168       end = Long.parseLong(request.getParameter("end"));
169     } else if ("range".equals(session.getAttribute("time_type"))) {
170       start = Long.parseLong((String) session.getAttribute("start"));
171       end = Long.parseLong((String) session.getAttribute("end"));
172     } else if ("last".equals(session.getAttribute("time_type"))
173         && session.getAttribute("period") != null) {
174       String period = (String) session.getAttribute("period");
175       parsePeriodValue(period);
176     }
177 
178     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
179     SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
180     SimpleDateFormat formatHour = new SimpleDateFormat("HH");
181     SimpleDateFormat formatMin = new SimpleDateFormat("mm");
182 
183     formatter.setTimeZone(this.tz);
184     formatDate.setTimeZone(this.tz);
185     formatHour.setTimeZone(this.tz);
186     formatMin.setTimeZone(this.tz);
187 
188     startS = formatter.format(start);
189     this.startDate = formatDate.format(start);
190     this.startHour = formatHour.format(start);
191     this.startMin = formatMin.format(start);
192     endS = formatter.format(end);
193     this.endDate = formatDate.format(end);
194     this.endHour = formatHour.format(end);
195     this.endMin = formatMin.format(end);
196   }
197 
198   public String getStartDate(String format) {
199     SimpleDateFormat formatter = new SimpleDateFormat(format);
200     formatter.setTimeZone(this.tz);
201     return formatter.format(this.start);
202   }
203 
204   public String getStartDate() {
205     return this.startDate;
206   }
207 
208   public String getStartHour() {
209     return this.startHour;
210   }
211 
212   public String getStartMinute() {
213     return this.startMin;
214   }
215 
216   public String getStartTimeText() {
217     return this.startS;
218   }
219 
220   public long getStartTime() {
221     return start;
222   }
223 
224   public String getEndDate(String format) {
225     SimpleDateFormat formatter = new SimpleDateFormat(format);
226     formatter.setTimeZone(this.tz);
227     return formatter.format(this.end);
228   }
229 
230   public String getEndDate() {
231     return this.endDate;
232   }
233 
234   public String getEndHour() {
235     return this.endHour;
236   }
237 
238   public String getEndMinute() {
239     return this.endMin;
240   }
241 
242   public String getEndTimeText() {
243     return this.endS;
244   }
245 
246   public long getEndTime() {
247     return end;
248   }
249 
250 }