KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > sharedqueue > SimpleHttpHandler


1 /*
2 @COPYRIGHT@
3 */

4 package demo.sharedqueue;
5
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import org.mortbay.http.HttpContext;
9 import org.mortbay.http.HttpException;
10 import org.mortbay.http.HttpHandler;
11 import org.mortbay.http.HttpRequest;
12 import org.mortbay.http.HttpResponse;
13
14 public class SimpleHttpHandler
15        implements HttpHandler {
16
17    private boolean started = false;
18    private HttpContext context;
19    private Queue queue;
20    private final String JavaDoc htmlPath;
21    /**
22     * Description of the Field
23     */

24    public static final String JavaDoc ACTION = "/webapp/";
25    /**
26     * Description of the Field
27     */

28    public static final String JavaDoc UNITS_OF_WORK = "unitsOfWork";
29
30    /**
31     *@param queue Description of Parameter
32     *@param htmlPath Description of Parameter
33     */

34    public SimpleHttpHandler(Queue queue, String JavaDoc htmlPath) {
35       this.queue = queue;
36       this.htmlPath = htmlPath;
37    }
38
39    /**
40     *@return The Name value
41     */

42    public String JavaDoc getName() {
43       return "Simple Http Handler";
44    }
45
46    /**
47     *@return The HttpContext value
48     */

49    public HttpContext getHttpContext() {
50       return context;
51    }
52
53    /**
54     *@return The Started value
55     */

56    public boolean isStarted() {
57       return started;
58    }
59
60    /**
61     *@param pathInContext Description of Parameter
62     *@param pathParams Description of Parameter
63     *@param request Description of Parameter
64     *@param response Description of Parameter
65     *@exception HttpException Description of Exception
66     *@exception IOException Description of Exception
67     */

68    public void handle(String JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest request, HttpResponse response)
69           throws HttpException, IOException JavaDoc {
70       if (pathInContext.equals("/add_work_item.asp")) {
71          final int unitsOfWork = getIntForParameter(request, UNITS_OF_WORK);
72          Thread JavaDoc processor = new Thread JavaDoc(
73                   new Runnable JavaDoc() {
74                      public void run() {
75                         for (int i = 0; i < unitsOfWork; i++) {
76                            queue.addJob();
77                            try {
78                               Thread.sleep(50);
79                            }
80                            // added slight delay to improve visuals
81
catch (InterruptedException JavaDoc ie) {
82                               System.err.println(ie.getMessage());
83                            }
84                         }
85                      }
86                   });
87          processor.start();
88          response.sendRedirect(ACTION);
89       }
90
91       else if (pathInContext.equals("/get_info_xml.asp")) {
92          response.setContentType("text/xml");
93          response.setField("Cache-Control", "no-cache");
94          OutputStream JavaDoc os = response.getOutputStream();
95          os.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>".getBytes());
96          os.write("<root>".getBytes());
97          os.write(queue.getXmlData().getBytes());
98          os.write("</root>".getBytes());
99          response.commit();
100          request.setHandled(true);
101       }
102    }
103
104    /**
105     *@param initContext Description of Parameter
106     */

107    public void initialize(HttpContext initContext) {
108       this.context = initContext;
109    }
110
111    /**
112     *@exception Exception Description of Exception
113     */

114    public void start()
115           throws Exception JavaDoc {
116       this.started = true;
117    }
118
119    /**
120     */

121    public void stop() {
122       started = false;
123    }
124
125    /**
126     *@param request Description of Parameter
127     *@param name Description of Parameter
128     *@return The IntForParameter value
129     */

130    private int getIntForParameter(HttpRequest request, String JavaDoc name) {
131       String JavaDoc param = request.getParameter(name);
132       try {
133          return param == null ? 0 : Integer.parseInt(param);
134       }
135       catch (NumberFormatException JavaDoc nfe) {
136          return 0;
137       }
138    }
139 }
140
Popular Tags