package servlet_tests;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.security.cert.*;
public class AlternateDocrootTestServlet extends HttpServlet{
private static final String EXPECTED = "Hello World";
private String host;
private int port;
private ServletContext context;
private PrintWriter out;
private boolean debug = false;
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
out = response.getWriter();
out.println("
");
out.println("");
out.println("AlternateDocrootTestServlet
");
if (request.getParameter("debug") != null) {
debug = true;
}
context = getServletContext();
HttpSession session = request.getSession();
if (session != null) {
host = request.getServerName();
port = request.getServerPort();
String protocol = request.getScheme();
try{
invokeServletTest();
Enumeration enames = request.getHeaderNames();
while (enames.hasMoreElements()){
String name = (String) enames.nextElement();
String value = request.getHeader(name);
System.out.println("Request Headers");
System.out.println("name :" +name +"value: " + value);
}
}catch (Exception ex){
out.println("Exception : " + ex + " --> see server.log for details
");
ex.printStackTrace();
}
}
out.println("");
}
private void invokeServletTest() throws Exception {
System.out.println("I am here ");
Socket sock = new Socket(host, new Integer(port).intValue());
OutputStream os = sock.getOutputStream();
String get = "GET " + "/" + "domain.xml" + " HTTP/1.1\n";
System.out.println("printing : get");
System.out.println(get);
os.write(get.getBytes());
System.out.println(get.getBytes());
os.write("Host: localhost\n".getBytes());
System.out.println("Iam here");
os.write("\n".getBytes());
/**Enumeration enames = request.getHeaderNames();
while (enames.hasMoreElements()){
String name = (String) enames.nextElement();
String value = request.getHeader(name);
System.out.println("Request Headers");
System.out.println("name :" +name +"value: " + value);
}
**/
InputStream is = sock.getInputStream();
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = bis.readLine()) != null) {
System.out.println(" Response:" +line+ "
");
// if (!(line.startsWith("Last-Modified")) && !(line.startsWith("Date")) && !(line.startsWith("ETag"))){
// Check if the domain.xml s returned by checking its first line, it should contain ");
out.println("Test Passed."+"
");}
}
}
}
/** private void invokeAtRootContext() throws Exception {
Socket sock = new Socket(host, new Integer(port).intValue());
OutputStream os = sock.getOutputStream();
String get = "GET / HTTP/1.1\n";
System.out.println(get);
os.write(get.getBytes());
os.write("Host: abc\n".getBytes());
os.write("\n".getBytes());
InputStream is = sock.getInputStream();
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = bis.readLine()) != null) {
System.out.println(line);
if (line.equals(EXPECTED)) {
break;
}
}
bis.close();
if (line == null) {
throw new Exception("Unable to find expected response: " +
EXPECTED);
}
} **/
//}