KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > servlet > ViewResponseServlet


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
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.apache.commons.latka.servlet;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.http.HttpServlet JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import org.apache.commons.latka.http.Response;
33
34 import org.apache.log4j.Category;
35
36 /**
37  * A servlet to view the result of running a Latka suite
38  *
39  * @author Morgan Delagrange
40  * @version $Id: ViewResponseServlet.java 155424 2005-02-26 13:09:29Z dirkv $
41  */

42 public class ViewResponseServlet extends HttpServlet JavaDoc {
43     /** log4 category for output */
44     public static final Category _log = Category.getInstance(
45         ViewResponseServlet.class);
46
47     /**
48      * Perform a post request
49      *
50      * @param req the http request
51      * @param res the http response
52      * @throws IOException when an error occurs writing the response
53      */

54     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws
55         IOException JavaDoc {
56         doGet(req, res);
57     }
58
59     /**
60      * Perform a get request
61      *
62      * @param req the http request
63      * @param res the http response
64      * @throws IOException when an error occurs writing the response
65      */

66     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
67         throws IOException JavaDoc {
68         HttpSession JavaDoc session = req.getSession();
69
70         List JavaDoc list = (List JavaDoc) session.getAttribute("latka.failedResponses");
71
72         String JavaDoc responseHash = req.getParameter("responseId");
73
74         Collections.sort(list, new ToStringComparator());
75         int i = Collections.binarySearch(list, responseHash,
76             new ToStringComparator());
77
78         Response JavaDoc response = (Response JavaDoc) list.get(i);
79
80         // set the content type, if specified
81
String JavaDoc contentType = response.getHeader("Content-type");
82         _log.debug("Content type = " + contentType);
83         if (contentType != null) {
84             res.setHeader("Content-type", contentType);
85         }
86
87         InputStream JavaDoc responseStream = response.getStream();
88         OutputStream JavaDoc servletStream = res.getOutputStream();
89
90         _log.debug("got input and output streams");
91
92         int someByte = responseStream.read();
93
94         _log.debug("read first byte: " + someByte);
95
96         while (someByte > -1) {
97           servletStream.write(someByte);
98           someByte = responseStream.read();
99         }
100
101         _log.debug("end doGet(HttpServletRequest,HttpServletResponse)");
102     }
103
104     /**
105      * A comparator that converts the objects to strings before comparing
106      */

107     private class ToStringComparator implements Comparator JavaDoc {
108         /**
109          * compare the two objects
110          *
111          * @param o1 the object being compared with
112          * @param o2 the object being compared to
113          * @return 0 if equal, -1 if o1 < o2, 1 if o1 > o2
114          */

115         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
116             return o1.toString().compareTo(o2.toString());
117         }
118     }
119 }
Popular Tags