Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ViewResponseServlet |
|
| 1.6666666666666667;1.667 | ||||
ViewResponseServlet$1 |
|
| 1.6666666666666667;1.667 | ||||
ViewResponseServlet$ToStringComparator |
|
| 1.6666666666666667;1.667 |
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; | |
20 | import java.io.IOException; | |
21 | import java.io.OutputStream; | |
22 | ||
23 | import java.util.Collections; | |
24 | import java.util.Comparator; | |
25 | import java.util.List; | |
26 | ||
27 | import javax.servlet.http.HttpServlet; | |
28 | import javax.servlet.http.HttpServletRequest; | |
29 | import javax.servlet.http.HttpServletResponse; | |
30 | import javax.servlet.http.HttpSession; | |
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 | 0 | public class ViewResponseServlet extends HttpServlet { |
43 | /** log4 category for output */ | |
44 | 0 | public static final Category _log = Category.getInstance( |
45 | 0 | 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 req, HttpServletResponse res) throws | |
55 | IOException { | |
56 | 0 | doGet(req, res); |
57 | 0 | } |
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 req, HttpServletResponse res) | |
67 | throws IOException { | |
68 | 0 | HttpSession session = req.getSession(); |
69 | ||
70 | 0 | List list = (List) session.getAttribute("latka.failedResponses"); |
71 | ||
72 | 0 | String responseHash = req.getParameter("responseId"); |
73 | ||
74 | 0 | Collections.sort(list, new ToStringComparator()); |
75 | 0 | int i = Collections.binarySearch(list, responseHash, |
76 | new ToStringComparator()); | |
77 | ||
78 | 0 | Response response = (Response) list.get(i); |
79 | ||
80 | // set the content type, if specified | |
81 | 0 | String contentType = response.getHeader("Content-type"); |
82 | 0 | _log.debug("Content type = " + contentType); |
83 | 0 | if (contentType != null) { |
84 | 0 | res.setHeader("Content-type", contentType); |
85 | } | |
86 | ||
87 | 0 | InputStream responseStream = response.getStream(); |
88 | 0 | OutputStream servletStream = res.getOutputStream(); |
89 | ||
90 | 0 | _log.debug("got input and output streams"); |
91 | ||
92 | 0 | int someByte = responseStream.read(); |
93 | ||
94 | 0 | _log.debug("read first byte: " + someByte); |
95 | ||
96 | 0 | while (someByte > -1) { |
97 | 0 | servletStream.write(someByte); |
98 | 0 | someByte = responseStream.read(); |
99 | } | |
100 | ||
101 | 0 | _log.debug("end doGet(HttpServletRequest,HttpServletResponse)"); |
102 | 0 | } |
103 | ||
104 | /** | |
105 | * A comparator that converts the objects to strings before comparing | |
106 | */ | |
107 | 0 | private class ToStringComparator implements Comparator { |
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 o1, Object o2) { | |
116 | 0 | return o1.toString().compareTo(o2.toString()); |
117 | } | |
118 | } | |
119 | } |