Coverage Report - org.apache.xmlrpc.webserver.ServletWebServer
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletWebServer
36% 
N/A 
1,154
 
 1  44
 /*
 2  
  * Copyright 1999,2005 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  
 package org.apache.xmlrpc.webserver;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.net.InetAddress;
 20  
 import java.net.Socket;
 21  
 import java.util.Enumeration;
 22  
 import java.util.NoSuchElementException;
 23  
 
 24  
 import javax.servlet.ServletConfig;
 25  
 import javax.servlet.ServletContext;
 26  
 import javax.servlet.ServletException;
 27  
 import javax.servlet.http.HttpServlet;
 28  
 
 29  
 import org.apache.xmlrpc.server.XmlRpcStreamServer;
 30  
 import org.apache.xmlrpc.util.ThreadPool;
 31  
 
 32  
 
 33  
 /** A subclass of {@link WebServer}, which emulates a servlet
 34  
  * container. Mainly useful for debugging.
 35  
  */
 36  
 public class ServletWebServer extends WebServer {
 37  
         /** This exception is thrown by the request handling classes,
 38  
          * advising the server, that it should return an error response.
 39  
          */
 40  
         public static class Exception extends IOException {
 41  
                 private static final long serialVersionUID = 49879832748972394L;
 42  
                 private final int statusCode;
 43  
                 private final String description;
 44  
 
 45  
                 /** Creates a new instance.
 46  
                  * @param pStatusCode The HTTP status code being sent to the client.
 47  
                  * @param pMessage The HTTP status message being sent to the client.
 48  
                  * @param pDescription The error description being sent to the client
 49  
                  * in the response body.
 50  
                  */
 51  
                 public Exception(int pStatusCode, String pMessage, String pDescription) {
 52  0
                         super(pMessage);
 53  0
                         statusCode = pStatusCode;
 54  0
                         description = pDescription;
 55  0
                 }
 56  
 
 57  0
                 public String getMessage() { return statusCode + " " + super.getMessage(); }
 58  
 
 59  
                 /** Returns the error description. The server will send the description
 60  
                  * as plain text in the response body.
 61  
                  * @return The error description.
 62  
                  */
 63  0
                 public String getDescription() { return description; }
 64  
 
 65  
                 /** Returns the HTTP status code.
 66  
                  * @return The status code.
 67  
                  */
 68  0
                 public int getStatusCode() { return statusCode; }
 69  
         }
 70  
 
 71  0
         private final HttpServlet servlet;
 72  
 
 73  
         /** Creates a new instance, which is listening on all
 74  
          * local IP addresses and the given port.
 75  
          * @param pServlet The servlet, which is handling requests.
 76  
          * @param pPort The servers port number; 0 for a random
 77  
          * port being choosen.
 78  
          * @throws ServletException Initializing the servlet failed.
 79  
          */
 80  
         public ServletWebServer(HttpServlet pServlet, int pPort) throws ServletException {
 81  44
                 this(pServlet, pPort, null);
 82  44
         }
 83  
 
 84  
         /** Creates a new instance, which is listening on the
 85  
          * given IP address and the given port.
 86  
          * @param pServlet The servlet, which is handling requests.
 87  
          * @param pPort The servers port number; 0 for a random
 88  
          * port being choosen.
 89  
          * @param pAddr The servers IP address.
 90  
          * @throws ServletException Initializing the servlet failed.
 91  
          */
 92  
         public ServletWebServer(HttpServlet pServlet, int pPort, InetAddress pAddr)
 93  
                         throws ServletException {
 94  44
                 super(pPort, pAddr);
 95  44
                 servlet = pServlet;
 96  44
                 servlet.init(new ServletConfig(){
 97  0
                         public String getServletName() { return servlet.getClass().getName(); }
 98  
                         public ServletContext getServletContext() {
 99  0
                                 throw new IllegalStateException("Context not available");
 100  
                         }
 101  
                         public String getInitParameter(String pArg0) {
 102  0
                                 return null;
 103  
                         }
 104  
                 
 105  
                         public Enumeration getInitParameterNames() {
 106  0
                                 return new Enumeration(){
 107  0
                                         public boolean hasMoreElements() { return false; }
 108  
                                         public Object nextElement() {
 109  0
                                                 throw new NoSuchElementException();
 110  
                                         }
 111  
                                 };
 112  
                         }
 113  
                         
 114  
                 });
 115  44
         }
 116  
 
 117  
         protected ThreadPool.Task newTask(WebServer pWebServer,
 118  
                                                                           XmlRpcStreamServer pXmlRpcServer,
 119  
                                                                           Socket pSocket) throws IOException {
 120  70
                 return new ServletConnection(servlet, pSocket);
 121  
         }
 122  
 }