7220939877f7249bf88f75c9c922684c053f1f26
[utils] / crawler / kissweb / src / main / java / 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  * @author Erik Brakkee
37  */
38 public class CrawlerServlet extends HttpServlet {
39
40     /*
41      * (non-Javadoc)
42      * 
43      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
44      *      javax.servlet.http.HttpServletResponse)
45      */
46     @Override
47     protected void doPost(HttpServletRequest aRequest,
48             HttpServletResponse aResponse) throws ServletException, IOException {
49
50         CrawlerScheduler scheduler = BeanKernel.getBeanFactory().find(
51                 CrawlerScheduler.class);
52         CrawlerStatus status = BeanKernel.getBeanFactory().find(
53                 CrawlerStatus.class);
54
55         try {
56             if (aRequest.getParameter("details") != null) {
57                 Report report = status.getLastReport();
58                 if (report != null) {
59                     Notifier notifier = BeanKernel.getBeanFactory().find(Notifier.class);
60                     aResponse.setContentType("text/html");
61                     OutputStream os = aResponse.getOutputStream();
62                     os.write(notifier.asHtml(report.asXml()).getBytes());
63                     return;
64                 }
65             }
66             if (aRequest.getParameter("runnow") != null) {
67                 status.setMustExecute(true);
68                 scheduler.scheduleNow();
69                 aResponse.sendRedirect("");
70                 return;
71             }
72             aRequest.setAttribute("running", scheduler.isCrawlerRunning());
73             aRequest.setAttribute("lastExecuted", status.getLastExecuted());
74             aRequest.setAttribute("lastResult", status.getLastResult());
75             aRequest.setAttribute("lastException", status.getLastException());
76             aRequest.setAttribute("lastReport", status.getLastReport());
77             String msg = "";
78             Throwable e = status.getLastException();
79             while (e != null) {
80                 msg = msg + e.getClass().getName() + ": " + e.getMessage()
81                         + "<br/>";
82                 e = e.getCause();
83             }
84             aRequest.setAttribute("lastMessage", msg);
85         } catch (Exception e) {
86             throw new ServletException("Error getting status", e);
87         }
88         aRequest.getRequestDispatcher("WEB-INF/overview.jsp").forward(aRequest,
89                 aResponse);
90     }
91
92     /*
93      * (non-Javadoc)
94      * 
95      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
96      *      javax.servlet.http.HttpServletResponse)
97      */
98     @Override
99     protected void doGet(HttpServletRequest aRequest,
100             HttpServletResponse aResponse) throws ServletException, IOException {
101         doPost(aRequest, aResponse);
102     }
103 }