1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.support; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 23 /* 24 import javax.servlet.ServletException; 25 import javax.servlet.http.HttpServlet; 26 import javax.servlet.http.HttpServletRequest; 27 import javax.servlet.http.HttpServletResponse; 28 29 import org.mortbay.jetty.HttpConnection; 30 import org.mortbay.jetty.Request; 31 import org.mortbay.jetty.Server; 32 import org.mortbay.jetty.handler.AbstractHandler; 33 import org.mortbay.jetty.handler.ResourceHandler; 34 import org.mortbay.jetty.servlet.Context; 35 import org.mortbay.jetty.servlet.ServletHolder; 36 */ 37 38 public class Support_Jetty { 39 /* 40 public static Server DEFAULT_SERVER = null; 41 42 public static int DEFAULT_PORT = 0; 43 44 public static Server DEFAULT_SERVLET = null; 45 46 public static int DEFAULT_SERVLET_PORT = 0; 47 48 public static Server SERVER = null; 49 50 public static int PORT = 0; 51 52 static { 53 Runtime.getRuntime().addShutdownHook(new Thread() { 54 public void run() { 55 try { 56 stopDefaultServer(); 57 stopServer(); 58 stopDefaultServlet(); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 }); 64 } 65 66 private static class HYDefaultServlet extends HttpServlet 67 { 68 private static final long serialVersionUID = -7650071946216123835L; 69 70 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 71 { 72 InputStream in = request.getInputStream(); 73 int i; 74 StringBuilder s = new StringBuilder(); 75 while((i = in.read())!=-1){ 76 s.append((char)i); 77 } 78 response.setContentType("text/html"); 79 response.setStatus(HttpServletResponse.SC_OK); 80 response.getWriter().print(s); 81 } 82 } 83 84 private static class HYDefaultHandler extends AbstractHandler 85 { 86 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException 87 { 88 Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); 89 base_request.setHandled(true); 90 response.setContentType("text/html"); 91 response.addDateHeader("Date", System.currentTimeMillis()); 92 response.addDateHeader("Last-Modified", Support_Configuration.URLConnectionLastModified); 93 response.setStatus(HttpServletResponse.SC_OK); 94 response.getWriter().print("<h1>Hello OneHandler</h1>"); 95 } 96 } 97 */ startDefaultHttpServer()98 public static int startDefaultHttpServer() throws Exception { 99 /* 100 if (DEFAULT_SERVER != null){ 101 return DEFAULT_PORT; 102 } 103 DEFAULT_SERVER = new Server(0); 104 DEFAULT_SERVER.setHandler(new HYDefaultHandler()); 105 Context context = new Context(DEFAULT_SERVER,"/",Context.SESSIONS); 106 context.addServlet(new ServletHolder(new HYDefaultServlet()), "/servlet"); 107 DEFAULT_SERVER.start(); 108 DEFAULT_PORT = DEFAULT_SERVER.getConnectors()[0].getLocalPort(); 109 return DEFAULT_PORT; 110 */ 111 return -1; 112 } 113 startDefaultServlet()114 public static int startDefaultServlet() throws Exception { 115 /* 116 if (DEFAULT_SERVLET != null){ 117 return DEFAULT_SERVLET_PORT; 118 } 119 DEFAULT_SERVLET = new Server(0); 120 Context context = new Context(DEFAULT_SERVLET,"/",Context.SESSIONS); 121 context.addServlet(new ServletHolder(new HYDefaultServlet()), "/*"); 122 DEFAULT_SERVLET.start(); 123 DEFAULT_SERVLET_PORT = DEFAULT_SERVLET.getConnectors()[0].getLocalPort(); 124 return DEFAULT_SERVLET_PORT; 125 */ 126 return -1; 127 } 128 startHttpServerWithDocRoot(String root)129 public static int startHttpServerWithDocRoot(String root) throws Exception { 130 /* 131 if (SERVER != null) { 132 SERVER.stop(); 133 SERVER = null; 134 } 135 SERVER = new Server(0); 136 ResourceHandler resource_handler = new ResourceHandler(); 137 resource_handler.setResourceBase(root); 138 SERVER.setHandler(resource_handler); 139 SERVER.start(); 140 PORT = SERVER.getConnectors()[0].getLocalPort(); 141 return PORT; 142 */ 143 return -1; 144 } 145 stopDefaultServer()146 private static void stopDefaultServer() throws Exception { 147 /* 148 if (DEFAULT_SERVER != null) { 149 DEFAULT_SERVER.stop(); 150 DEFAULT_SERVER = null; 151 } 152 */ 153 } 154 stopServer()155 private static void stopServer() throws Exception { 156 /* 157 if (SERVER != null) { 158 SERVER.stop(); 159 SERVER = null; 160 } 161 */ 162 } 163 stopDefaultServlet()164 private static void stopDefaultServlet() throws Exception { 165 /* 166 if (DEFAULT_SERVLET != null) { 167 DEFAULT_SERVLET.stop(); 168 DEFAULT_SERVLET = null; 169 } 170 */ 171 } 172 } 173