Coverage Report - org.apache.xmlrpc.webserver.XmlRpcServletServer
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcServletServer
86% 
67% 
1,455
 
 1  
 /*
 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.io.InputStream;
 20  
 import java.io.OutputStream;
 21  
 
 22  
 import javax.servlet.ServletException;
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 
 26  
 import org.apache.xmlrpc.XmlRpcException;
 27  
 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
 28  
 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
 29  
 import org.apache.xmlrpc.server.XmlRpcHttpServerConfig;
 30  
 import org.apache.xmlrpc.server.XmlRpcStreamServer;
 31  
 import org.apache.xmlrpc.util.HttpUtil;
 32  
 
 33  
 
 34  
 /** An extension of {@link org.apache.xmlrpc.server.XmlRpcServer},
 35  
  * which is suitable for processing servlet requests.
 36  
  */
 37  44
 public class XmlRpcServletServer extends XmlRpcStreamServer {
 38  
         /** This class is used as a "connection" while processing the request.
 39  
          * In other words, it is what
 40  
          * {@link XmlRpcStreamServer#execute(XmlRpcStreamRequestConfig, Object)}
 41  
          * receives as a connection object.
 42  
          */
 43  
         protected static class RequestData extends XmlRpcHttpRequestConfigImpl {
 44  70
                 private final HttpServletRequest request;
 45  
 
 46  
                 /** Creates a new instance with the given request and response.
 47  
                  * @param pRequest The servlet request.
 48  
                  */
 49  70
                 public RequestData(HttpServletRequest pRequest) {
 50  70
                         request = pRequest;
 51  70
                 }
 52  
                 /** Returns the servlet request.
 53  
                  * @return The {@link HttpServletRequest}.
 54  
                  */
 55  0
                 public HttpServletRequest getRequest() { return request; }
 56  
         }
 57  
 
 58  
         protected RequestData newConfig(HttpServletRequest pRequest) {
 59  70
                 return new RequestData(pRequest);
 60  
         }
 61  
 
 62  
         protected RequestData getConfig(HttpServletRequest pRequest) {
 63  70
                 RequestData result = newConfig(pRequest);
 64  70
                 XmlRpcHttpServerConfig serverConfig = (XmlRpcHttpServerConfig) getConfig();
 65  70
                 result.setBasicEncoding(serverConfig.getBasicEncoding());
 66  70
                 result.setContentLengthOptional(serverConfig.isContentLengthOptional());
 67  70
                 result.setEnabledForExtensions(serverConfig.isEnabledForExtensions());
 68  70
                 result.setGzipCompressing(HttpUtil.isUsingGzipEncoding(pRequest.getHeader("Content-Encoding")));
 69  70
                 result.setGzipRequesting(HttpUtil.isUsingGzipEncoding(pRequest.getHeaders("Accept-Encoding")));
 70  70
                 result.setEncoding(pRequest.getCharacterEncoding());
 71  70
                 HttpUtil.parseAuthorization(result, pRequest.getHeader("Authorization"));
 72  70
                 return result;
 73  
         }
 74  
 
 75  
         protected RequestData newRequestData(HttpServletRequest pRequest) {
 76  0
                 return new RequestData(pRequest);
 77  
         }
 78  
 
 79  
         /** Processes the servlet request.
 80  
          * @param pRequest The servlet request being read.
 81  
          * @param pResponse The servlet response being created.
 82  
          * @throws IOException Reading the request or writing the response failed.
 83  
          * @throws ServletException Processing the request failed.
 84  
          */
 85  
         public void execute(HttpServletRequest pRequest, HttpServletResponse pResponse)
 86  
                         throws ServletException, IOException {
 87  70
                 RequestData config = getConfig(pRequest);
 88  
                 try {
 89  70
                         super.execute(config, pResponse);
 90  0
                 } catch (XmlRpcException e) {
 91  0
                         throw new ServletException(e);
 92  
                 }
 93  70
         }
 94  
 
 95  
         /** Returns, whether the requests content length is required.
 96  
          */
 97  
         protected boolean isContentLengthRequired(XmlRpcStreamRequestConfig pConfig) {
 98  70
                 RequestData data = (RequestData) pConfig;
 99  70
                 if (data.isEnabledForExtensions()) {
 100  
                         // The spec requires a content-length.
 101  70
                         return true;
 102  
                 }
 103  0
                 return !((XmlRpcHttpServerConfig) getConfig()).isContentLengthOptional();
 104  
         }
 105  
 
 106  
         protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws IOException {
 107  70
                 return ((RequestData) pConfig).request.getInputStream();
 108  
         }
 109  
 
 110  
         protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws IOException {
 111  70
                 HttpServletResponse response = ((HttpServletResponse) pConnection);
 112  70
                 response.setContentType("text/xml");
 113  70
                 return response.getOutputStream();
 114  
         }
 115  
 
 116  
         protected OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig,
 117  
                                                                                    Object pConnection,
 118  
                                                                                    int pSize) throws IOException {
 119  70
                 if (pSize != -1) {
 120  70
                         ((HttpServletResponse) pConnection).setContentLength(pSize);
 121  
                 }
 122  70
                 return super.getOutputStream(pConfig, pConnection, pSize);
 123  
         }
 124  
 
 125  
         protected void closeConnection(Object pConnection) throws IOException {
 126  70
                 ((HttpServletResponse) pConnection).getOutputStream().close();
 127  70
         }
 128  
 }