001 /* 002 * Copyright 1999-2001,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.latka.servlet; 018 019 import java.io.InputStream; 020 import java.io.IOException; 021 import java.io.OutputStream; 022 023 import java.util.Collections; 024 import java.util.Comparator; 025 import java.util.List; 026 027 import javax.servlet.http.HttpServlet; 028 import javax.servlet.http.HttpServletRequest; 029 import javax.servlet.http.HttpServletResponse; 030 import javax.servlet.http.HttpSession; 031 032 import org.apache.commons.latka.http.Response; 033 034 import org.apache.log4j.Category; 035 036 /** 037 * A servlet to view the result of running a Latka suite 038 * 039 * @author Morgan Delagrange 040 * @version $Id: ViewResponseServlet.java 155424 2005-02-26 13:09:29Z dirkv $ 041 */ 042 public class ViewResponseServlet extends HttpServlet { 043 /** log4 category for output */ 044 public static final Category _log = Category.getInstance( 045 ViewResponseServlet.class); 046 047 /** 048 * Perform a post request 049 * 050 * @param req the http request 051 * @param res the http response 052 * @throws IOException when an error occurs writing the response 053 */ 054 public void doPost(HttpServletRequest req, HttpServletResponse res) throws 055 IOException { 056 doGet(req, res); 057 } 058 059 /** 060 * Perform a get request 061 * 062 * @param req the http request 063 * @param res the http response 064 * @throws IOException when an error occurs writing the response 065 */ 066 public void doGet(HttpServletRequest req, HttpServletResponse res) 067 throws IOException { 068 HttpSession session = req.getSession(); 069 070 List list = (List) session.getAttribute("latka.failedResponses"); 071 072 String responseHash = req.getParameter("responseId"); 073 074 Collections.sort(list, new ToStringComparator()); 075 int i = Collections.binarySearch(list, responseHash, 076 new ToStringComparator()); 077 078 Response response = (Response) list.get(i); 079 080 // set the content type, if specified 081 String contentType = response.getHeader("Content-type"); 082 _log.debug("Content type = " + contentType); 083 if (contentType != null) { 084 res.setHeader("Content-type", contentType); 085 } 086 087 InputStream responseStream = response.getStream(); 088 OutputStream servletStream = res.getOutputStream(); 089 090 _log.debug("got input and output streams"); 091 092 int someByte = responseStream.read(); 093 094 _log.debug("read first byte: " + someByte); 095 096 while (someByte > -1) { 097 servletStream.write(someByte); 098 someByte = responseStream.read(); 099 } 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 { 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 return o1.toString().compareTo(o2.toString()); 117 } 118 } 119 }