/* * Copyright 2006 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.crawler.kiss.servlet; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.wamblee.crawler.kiss.main.Report; import org.wamblee.crawler.kiss.notification.Notifier; import org.wamblee.crawler.kiss.scheduling.CrawlerScheduler; import org.wamblee.crawler.kiss.scheduling.CrawlerStatus; import org.wamblee.general.BeanKernel; /** * */ public class CrawlerServlet extends HttpServlet { /* * (non-Javadoc) * * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override protected void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException { CrawlerScheduler scheduler = BeanKernel.getBeanFactory().find( CrawlerScheduler.class); CrawlerStatus status = BeanKernel.getBeanFactory().find( CrawlerStatus.class); try { if (aRequest.getParameter("details") != null) { Report report = status.getLastReport(); if (report != null) { Notifier notifier = BeanKernel.getBeanFactory().find(Notifier.class); aResponse.setContentType("text/html"); OutputStream os = aResponse.getOutputStream(); os.write(notifier.asHtml(report.asXml()).getBytes()); return; } } if (aRequest.getParameter("runnow") != null) { status.setMustExecute(true); scheduler.scheduleNow(); aResponse.sendRedirect(""); return; } aRequest.setAttribute("running", scheduler.isCrawlerRunning()); aRequest.setAttribute("lastExecuted", status.getLastExecuted()); aRequest.setAttribute("lastResult", status.getLastResult()); aRequest.setAttribute("lastException", status.getLastException()); aRequest.setAttribute("lastReport", status.getLastReport()); String msg = ""; Throwable e = status.getLastException(); while (e != null) { msg = msg + e.getClass().getName() + ": " + e.getMessage() + "
"; e = e.getCause(); } aRequest.setAttribute("lastMessage", msg); } catch (Exception e) { throw new ServletException("Error getting status", e); } aRequest.getRequestDispatcher("WEB-INF/overview.jsp").forward(aRequest, aResponse); } /* * (non-Javadoc) * * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override protected void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException { doPost(aRequest, aResponse); } }