(no commit message)
[utils] / crawler / kissweb / src / org / wamblee / crawler / kiss / servlet / CrawlerServlet.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.crawler.kiss.servlet;
18
19 import java.io.IOException;
20 import java.io.OutputStream;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.wamblee.crawler.kiss.main.Report;
28 import org.wamblee.crawler.kiss.notification.Notifier;
29 import org.wamblee.crawler.kiss.scheduling.CrawlerScheduler;
30 import org.wamblee.crawler.kiss.scheduling.CrawlerStatus;
31 import org.wamblee.general.BeanKernel;
32
33 /**
34  * 
35  */
36 public class CrawlerServlet extends HttpServlet {
37
38     /*
39      * (non-Javadoc)
40      * 
41      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
42      *      javax.servlet.http.HttpServletResponse)
43      */
44     @Override
45     protected void doPost(HttpServletRequest aRequest,
46             HttpServletResponse aResponse) throws ServletException, IOException {
47
48         CrawlerScheduler scheduler = BeanKernel.getBeanFactory().find(
49                 CrawlerScheduler.class);
50         CrawlerStatus status = BeanKernel.getBeanFactory().find(
51                 CrawlerStatus.class);
52
53         try {
54             if (aRequest.getParameter("details") != null) {
55                 Report report = status.getLastReport();
56                 if (report != null) {
57                     Notifier notifier = BeanKernel.getBeanFactory().find(Notifier.class);
58                     aResponse.setContentType("text/html");
59                     OutputStream os = aResponse.getOutputStream();
60                     os.write(notifier.asHtml(report.asXml()).getBytes());
61                     return;
62                 }
63             }
64             if (aRequest.getParameter("runnow") != null) {
65                 status.setMustExecute(true);
66                 scheduler.scheduleNow();
67                 aResponse.sendRedirect("");
68                 return;
69             }
70             aRequest.setAttribute("running", scheduler.isCrawlerRunning());
71             aRequest.setAttribute("lastExecuted", status.getLastExecuted());
72             aRequest.setAttribute("lastResult", status.getLastResult());
73             aRequest.setAttribute("lastException", status.getLastException());
74             aRequest.setAttribute("lastReport", status.getLastReport());
75             String msg = "";
76             Throwable e = status.getLastException();
77             while (e != null) {
78                 msg = msg + e.getClass().getName() + ": " + e.getMessage()
79                         + "<br/>";
80                 e = e.getCause();
81             }
82             aRequest.setAttribute("lastMessage", msg);
83         } catch (Exception e) {
84             throw new ServletException("Error getting status", e);
85         }
86         aRequest.getRequestDispatcher("WEB-INF/overview.jsp").forward(aRequest,
87                 aResponse);
88     }
89
90     /*
91      * (non-Javadoc)
92      * 
93      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
94      *      javax.servlet.http.HttpServletResponse)
95      */
96     @Override
97     protected void doGet(HttpServletRequest aRequest,
98             HttpServletResponse aResponse) throws ServletException, IOException {
99         doPost(aRequest, aResponse);
100     }
101 }