1 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 42 public class ViewResponseServlet extends HttpServlet { 43 44 public static final Category _log = Category.getInstance( 45 ViewResponseServlet.class); 46 47 54 public void doPost(HttpServletRequest req, HttpServletResponse res) throws 55 IOException { 56 doGet(req, res); 57 } 58 59 66 public void doGet(HttpServletRequest req, HttpServletResponse res) 67 throws IOException { 68 HttpSession session = req.getSession(); 69 70 List list = (List ) session.getAttribute("latka.failedResponses"); 71 72 String responseHash = req.getParameter("responseId"); 73 74 Collections.sort(list, new ToStringComparator()); 75 int i = Collections.binarySearch(list, responseHash, 76 new ToStringComparator()); 77 78 Response response = (Response ) list.get(i); 79 80 String 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 responseStream = response.getStream(); 88 OutputStream 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 107 private class ToStringComparator implements Comparator { 108 115 public int compare(Object o1, Object o2) { 116 return o1.toString().compareTo(o2.toString()); 117 } 118 } 119 } | Popular Tags |