* otherwise, send a NOT_AVAILABLE and fills the right Retry-After header
* @return a Reply if blocked, null otherwise
*/
public synchronized ReplyInterface ingoingFilter(RequestInterface req) {
Request request = (Request) req;
Reply reply = null;
int n_year, n_month, n_week, n_day, n_time;
int a, b, n;
Calendar cal = Calendar.getInstance();
n_year = cal.get(Calendar.YEAR);
n_month = cal.get(Calendar.MONTH);
n_week = cal.get(Calendar.DAY_OF_WEEK);
n_day = cal.get(Calendar.DAY_OF_MONTH);
n_time = cal.get(Calendar.HOUR_OF_DAY) * 3600 +
cal.get(Calendar.MINUTE) * 60 +
cal.get(Calendar.SECOND);
if (getDayRepeat()) { // check it if it is repeated every day
if ((n_time < a_time) || (n_time > b_time)) {
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
if (n_time < a_time)
reply.setRetryAfter(a_time - n_time);
else
reply.setRetryAfter(84600 + n_time - a_time);
}
} else if (getWeekRepeat()) { // check it if it is repeated every week
a = a_time + a_week * 86400;
b = b_time + b_week * 86400;
n = n_time + n_week * 86400;
if ((n < a) || (n > b)) {
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
if (n < a)
reply.setRetryAfter(a - n);
else
reply.setRetryAfter(84600*7 + n - a);
}
} else if (getMonthRepeat()) { // check it if it's repeated every month
a = a_time + a_day * 86400;
b = b_time + b_day * 86400;
n = n_time + n_day * 86400;
if ((n < a) || (n > b)) {
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
if (n < a)
reply.setRetryAfter(a - n);
else {
cal.setTime(new Date(getLong(ATTR_DATE_START, -1)));
cal.set(Calendar.YEAR, n_year);
cal.set(Calendar.MONTH, n_month);
cal.roll(Calendar.MONTH, true);
reply.setRetryAfter(cal.getTime().getTime());
}
}
} else if (getYearRepeat()) { // check it if it's repeated every year
Calendar c_a = Calendar.getInstance();
Calendar c_b = Calendar.getInstance();
c_a.setTime(new Date(getLong(ATTR_DATE_START, -1)));
c_b.setTime(new Date(getLong(ATTR_DATE_END, -1)));
c_a.set(Calendar.YEAR, n_year);
c_b.set(Calendar.YEAR, n_year);
if (cal.before(c_a)) {
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
reply.setRetryAfter(c_a.getTime().getTime());
} else if (cal.after(c_b)) {
c_a.roll(Calendar.YEAR, true);
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
reply.setRetryAfter(c_a.getTime().getTime());
}
} else { /* no repeat */
Calendar c_a = Calendar.getInstance();
Calendar c_b = Calendar.getInstance();
c_a.setTime(new Date(getLong(ATTR_DATE_START, -1)));
c_b.setTime(new Date(getLong(ATTR_DATE_END, -1)));
if (cal.before(c_a)) {
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
reply.setRetryAfter(c_a.getTime().getTime());
} else if (cal.after(c_b)) {
reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
}
}
if (reply != null) {
HtmlGenerator g = new HtmlGenerator("Service Unavailable");
g.append("You may retry after the delay or the date given");