You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by jo...@apache.org on 2006/05/24 22:07:26 UTC

svn commit: r409238 - in /webservices/xmlrpc/trunk: client/src/main/java/org/apache/xmlrpc/client/ common/src/main/java/org/apache/xmlrpc/common/ server/src/main/java/org/apache/xmlrpc/server/ server/src/main/java/org/apache/xmlrpc/webserver/

Author: jochen
Date: Wed May 24 13:07:25 2006
New Revision: 409238

URL: http://svn.apache.org/viewvc?rev=409238&view=rev
Log:
Cleanup of the server side code.

Added:
    webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/ServerHttpConnection.java
Modified:
    webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcLocalStreamTransport.java
    webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/LocalStreamConnection.java
    webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/ServerStreamConnection.java
    webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcLocalStreamServer.java
    webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcStreamServer.java
    webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/Connection.java
    webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/ConnectionServer.java
    webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServletServer.java

Modified: webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcLocalStreamTransport.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcLocalStreamTransport.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcLocalStreamTransport.java (original)
+++ webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcLocalStreamTransport.java Wed May 24 13:07:25 2006
@@ -53,7 +53,7 @@
 	}
 
 	protected InputStream getInputStream() throws XmlRpcException {
-		localServer.execute(conn.getConfig(), conn);
+		localServer.execute(conn.getConfig(), conn.getServerStreamConnection());
 		return new ByteArrayInputStream(conn.getResponse().toByteArray());
 	}
 

Modified: webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/LocalStreamConnection.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/LocalStreamConnection.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/LocalStreamConnection.java (original)
+++ webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/LocalStreamConnection.java Wed May 24 13:07:25 2006
@@ -1,18 +1,36 @@
 package org.apache.xmlrpc.common;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 
 
 /** Implementation of {@link ServerStreamConnection} for
  * use by the
  * {@link org.apache.xmlrpc.client.XmlRpcLocalStreamTransport}.
  */
-public class LocalStreamConnection
-		implements ServerStreamConnection {
-	private final InputStream request;
+public class LocalStreamConnection {
+    private class LocalServerStreamConnection implements ServerStreamConnection {
+        public InputStream newInputStream() throws IOException {
+            return request;
+        }
+
+        public OutputStream newOutputStream() throws IOException {
+            return response;
+        }
+
+        public void close() throws IOException {
+            if (response != null) {
+                response.close();
+            }
+        }
+    }
+
+    private final InputStream request;
 	private final XmlRpcStreamRequestConfig config;
 	private final ByteArrayOutputStream response = new ByteArrayOutputStream();
+    private final ServerStreamConnection serverStreamConnection;
 
 	/** Creates a new instance with the given request stream.
 	 */
@@ -20,6 +38,7 @@
 			InputStream pRequest) {
 		config = pConfig;
 		request = pRequest;
+        serverStreamConnection = new LocalServerStreamConnection();
 	}
 
 	/** Returns the request stream.
@@ -40,4 +59,10 @@
 	public ByteArrayOutputStream getResponse() {
 		return response;
 	}
+
+    /** Returns the servers connection.
+     */
+    public ServerStreamConnection getServerStreamConnection() {
+        return serverStreamConnection;
+    }
 }

Modified: webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/ServerStreamConnection.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/ServerStreamConnection.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/ServerStreamConnection.java (original)
+++ webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/common/ServerStreamConnection.java Wed May 24 13:07:25 2006
@@ -1,5 +1,9 @@
 package org.apache.xmlrpc.common;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
 
 /** Interface of an object, which is able to provide
  * an XML stream, containing an XML-RPC request.
@@ -7,5 +11,13 @@
  * write the response as an XML stream.
  */
 public interface ServerStreamConnection {
-
+    /** Returns the connections input stream.
+     */
+    InputStream newInputStream() throws IOException;
+    /** Returns the connections output stream.
+     */
+    OutputStream newOutputStream() throws IOException;
+    /** Closes the connection, and frees resources.
+     */
+    void close() throws IOException;
 }

Added: webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/ServerHttpConnection.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/ServerHttpConnection.java?rev=409238&view=auto
==============================================================================
--- webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/ServerHttpConnection.java (added)
+++ webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/ServerHttpConnection.java Wed May 24 13:07:25 2006
@@ -0,0 +1,16 @@
+package org.apache.xmlrpc.server;
+
+import org.apache.xmlrpc.common.ServerStreamConnection;
+
+
+/** Interface of a {@link ServerStreamConnection} for HTTP
+ * response transport.
+ */
+public interface ServerHttpConnection extends ServerStreamConnection {
+    /** Sets a response header.
+     */
+    void setResponseHeader(String pKey, String pValue);
+    /** Sets the content length.
+     */
+    void setContentLength(int pContentLength);
+}

Modified: webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcLocalStreamServer.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcLocalStreamServer.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcLocalStreamServer.java (original)
+++ webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcLocalStreamServer.java Wed May 24 13:07:25 2006
@@ -1,8 +1,6 @@
 package org.apache.xmlrpc.server;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 
 import org.apache.xmlrpc.XmlRpcException;
@@ -20,20 +18,5 @@
 	public Object execute(XmlRpcRequest pRequest) throws XmlRpcException {
 		XmlRpcRequestProcessor server = ((XmlRpcRequestProcessorFactory) pRequest.getConfig()).getXmlRpcServer();
 		return server.execute(pRequest);
-	}
-	protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, ServerStreamConnection pConnection) throws IOException {
-		LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
-		return lsc.getRequest();
-	}
-	protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, ServerStreamConnection pConnection) throws IOException {
-		LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
-		return lsc.getResponse();
-	}
-	protected void closeConnection(ServerStreamConnection pConnection) throws IOException {
-		LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
-		final ByteArrayOutputStream istream = lsc.getResponse();
-		if (istream != null) {
-			try { istream.close(); } catch (Throwable ignore) {}
-		}
 	}
 }

Modified: webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcStreamServer.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcStreamServer.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcStreamServer.java (original)
+++ webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/server/XmlRpcStreamServer.java Wed May 24 13:07:25 2006
@@ -125,25 +125,15 @@
 		return writerFactory;
 	}
 
-	/** Returns the connections input stream.
-	 */
-	protected abstract InputStream newInputStream(XmlRpcStreamRequestConfig pConfig,
-												  ServerStreamConnection pConnection) throws IOException;
-
 	protected InputStream getInputStream(XmlRpcStreamRequestConfig pConfig,
 										 ServerStreamConnection pConnection) throws IOException {
-		InputStream istream = newInputStream(pConfig, pConnection);
+		InputStream istream = pConnection.newInputStream();
 		if (pConfig.isEnabledForExtensions()  &&  pConfig.isGzipCompressing()) {
 			istream = new GZIPInputStream(istream);
 		}
 		return istream;
 	}
 
-	/** Creates the connections output stream.
-	 */
-	protected abstract OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig,
-												    ServerStreamConnection pConnection) throws IOException;
-
 	/** Called to prepare the output stream. Typically used for enabling
 	 * compression, or similar filters.
 	 */
@@ -162,7 +152,7 @@
 	protected OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig,
 										   ServerStreamConnection pConnection,
 										   int pSize) throws IOException {
-		return newOutputStream(pConfig, pConnection);
+	    return pConnection.newOutputStream();
 	}
 
 	/** Returns, whether the requests content length is required.
@@ -171,10 +161,6 @@
 		return false;
 	}
 
-	/** Closes the connection, releasing all resources.
-	 */
-	protected abstract void closeConnection(ServerStreamConnection pConnection) throws IOException;
-
 	/** Returns, whether the 
 	/** Processes a "connection". The "connection" is an opaque object, which is
 	 * being handled by the subclasses.
@@ -213,7 +199,7 @@
 				ostream = baos;
 			} else {
 				baos = null;
-				ostream = newOutputStream(pConfig, pConnection);
+				ostream = pConnection.newOutputStream();
 			}
 			ostream = getOutputStream(pConnection, pConfig, ostream);
 			try {
@@ -237,13 +223,13 @@
 					if (dest != null) { try { dest.close(); } catch (Throwable ignore) {} }
 				}
 			}
-			closeConnection(pConnection);
+            pConnection.close();
 			pConnection = null;
 		} catch (IOException e) {
 			throw new XmlRpcException("I/O error while processing request: "
 					+ e.getMessage(), e);
 		} finally {
-			if (pConnection != null) { try { closeConnection(pConnection); } catch (Throwable ignore) {} }
+			if (pConnection != null) { try { pConnection.close(); } catch (Throwable ignore) {} }
 		}
 		log.debug("execute: <-");
 	}

Modified: webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/Connection.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/Connection.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/Connection.java (original)
+++ webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/Connection.java Wed May 24 13:07:25 2006
@@ -30,7 +30,6 @@
 import org.apache.xmlrpc.common.ServerStreamConnection;
 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfig;
 import org.apache.xmlrpc.common.XmlRpcNotAuthorizedException;
-import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
 import org.apache.xmlrpc.server.XmlRpcHttpServerConfig;
 import org.apache.xmlrpc.server.XmlRpcStreamServer;
 import org.apache.xmlrpc.util.HttpUtil;
@@ -81,6 +80,7 @@
     private final XmlRpcStreamServer server;
     private byte[] buffer;
     private Map headers;
+    private RequestData requestData;
 
     /** Creates a new webserver connection on the given socket.
      * @param pWebServer The webserver maintaining this connection.
@@ -113,14 +113,14 @@
      * @throws IOException Reading the request headers failed.
      */
     private RequestData getRequestConfig() throws IOException {
-        RequestData result = new RequestData(this);
+        requestData = new RequestData(this);
         if (headers != null) {
             headers.clear();
         }
         XmlRpcHttpServerConfig serverConfig = (XmlRpcHttpServerConfig) server.getConfig();
-        result.setBasicEncoding(serverConfig.getBasicEncoding());
-        result.setContentLengthOptional(serverConfig.isContentLengthOptional());
-        result.setEnabledForExtensions(serverConfig.isEnabledForExtensions());
+        requestData.setBasicEncoding(serverConfig.getBasicEncoding());
+        requestData.setContentLengthOptional(serverConfig.isContentLengthOptional());
+        requestData.setEnabledForExtensions(serverConfig.isEnabledForExtensions());
 
         // reset user authentication
         String line = readLine();
@@ -138,11 +138,11 @@
         if (!"POST".equalsIgnoreCase(method)) {
             throw new BadRequestException(method);
         }
-        result.setMethod(method);
+        requestData.setMethod(method);
         tokens.nextToken(); // Skip URI
         String httpVersion = tokens.nextToken();
-        result.setHttpVersion(httpVersion);
-        result.setKeepAlive(serverConfig.isKeepAliveEnabled()
+        requestData.setHttpVersion(httpVersion);
+        requestData.setKeepAlive(serverConfig.isKeepAliveEnabled()
                 && WebServer.HTTP_11.equals(httpVersion));
         do {
             line = readLine();
@@ -150,19 +150,19 @@
                 String lineLower = line.toLowerCase();
                 if (lineLower.startsWith("content-length:")) {
                     String cLength = line.substring("content-length:".length());
-                    result.setContentLength(Integer.parseInt(cLength.trim()));
+                    requestData.setContentLength(Integer.parseInt(cLength.trim()));
                 } else if (lineLower.startsWith("connection:")) {
-                    result.setKeepAlive(serverConfig.isKeepAliveEnabled()
+                    requestData.setKeepAlive(serverConfig.isKeepAliveEnabled()
                             &&  lineLower.indexOf("keep-alive") > -1);
                 } else if (lineLower.startsWith("authorization:")) {
                     String credentials = line.substring("authorization:".length());
-                    HttpUtil.parseAuthorization(result, credentials);
+                    HttpUtil.parseAuthorization(requestData, credentials);
                 }
             }
         }
         while (line != null && line.length() != 0);
 
-        return result;
+        return requestData;
     }
 
     public void run() {
@@ -206,38 +206,6 @@
         return new String(buffer, 0, count, US_ASCII);
     }
 
-    /** Returns the contents input stream.
-     * @param pData The request data
-     * @return The contents input stream.
-     */
-    public InputStream getInputStream(RequestData pData) {
-        int contentLength = pData.getContentLength();
-        if (contentLength == -1) {
-            return input;
-        } else {
-            return new LimitedInputStream(input, contentLength);
-        }
-    }
-
-    /** Returns the output stream for writing the response.
-     * @param pConfig The request configuration.
-     * @return The response output stream.
-     */
-    public OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig) {
-        boolean useContentLength;
-        if (pConfig instanceof XmlRpcHttpRequestConfig) {
-            useContentLength = !pConfig.isEnabledForExtensions()
-            ||  !((XmlRpcHttpRequestConfig) pConfig).isContentLengthOptional();
-        } else {
-            useContentLength = true;
-        }
-        if (useContentLength) {
-            return new ByteArrayOutputStream();
-        } else {
-            return output;
-        }
-    }
-
     /** Writes the response header and the response to the
      * output stream.
      * @param pData The request data.
@@ -337,5 +305,29 @@
      */
     public void setResponseHeader(String pHeader, String pValue) {
         headers.put(pHeader, pValue);
+    }
+
+
+    public OutputStream newOutputStream() throws IOException {
+        boolean useContentLength;
+        useContentLength = !requestData.isEnabledForExtensions()
+            ||  !((XmlRpcHttpRequestConfig) requestData).isContentLengthOptional();
+        if (useContentLength) {
+            return new ByteArrayOutputStream();
+        } else {
+            return output;
+        }
+    }
+
+    public InputStream newInputStream() throws IOException {
+        int contentLength = requestData.getContentLength();
+        if (contentLength == -1) {
+            return input;
+        } else {
+            return new LimitedInputStream(input, contentLength);
+        }
+    }
+
+    public void close() throws IOException {
     }
 }

Modified: webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/ConnectionServer.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/ConnectionServer.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/ConnectionServer.java (original)
+++ webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/ConnectionServer.java Wed May 24 13:07:25 2006
@@ -59,17 +59,6 @@
 		}
 	}
 
-	protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, ServerStreamConnection pConnection) throws IOException {
-		return ((Connection) pConnection).getInputStream((RequestData) pConfig);
-	}
-
-	protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, ServerStreamConnection pConnection) throws IOException {
-		return ((Connection) pConnection).getOutputStream(pConfig);
-	}
-
-	protected void closeConnection(ServerStreamConnection pConnection) throws IOException {
-	}
-
 	protected void setResponseHeader(ServerStreamConnection pConnection, String pHeader, String pValue) {
 		((Connection) pConnection).setResponseHeader(pHeader, pValue);
 	}

Modified: webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServletServer.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServletServer.java?rev=409238&r1=409237&r2=409238&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServletServer.java (original)
+++ webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServletServer.java Wed May 24 13:07:25 2006
@@ -52,7 +52,20 @@
 		/** Returns the servlet response.
 		 */
 		public HttpServletResponse getResponse() { return response; }
-	}
+
+		public InputStream newInputStream() throws IOException {
+            return request.getInputStream();
+        }
+
+		public OutputStream newOutputStream() throws IOException {
+            response.setContentType("text/xml");
+            return response.getOutputStream();
+        }
+
+        public void close() throws IOException {
+            response.getOutputStream().close();
+        }
+    }
 
 	protected XmlRpcHttpRequestConfigImpl newConfig() {
 		return new XmlRpcHttpRequestConfigImpl();
@@ -103,18 +116,6 @@
 		return !((XmlRpcHttpServerConfig) getConfig()).isContentLengthOptional();
 	}
 
-	protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig,
-			ServerStreamConnection pConnection) throws IOException {
-		return ((ServletStreamConnection) pConnection).getRequest().getInputStream();
-	}
-
-	protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig,
-			ServerStreamConnection pConnection) throws IOException {
-		HttpServletResponse response = ((ServletStreamConnection) pConnection).getResponse();
-		response.setContentType("text/xml");
-		return response.getOutputStream();
-	}
-
 	protected OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig,
 										   ServerStreamConnection pConnection,
 										   int pSize) throws IOException {
@@ -122,10 +123,6 @@
 			((ServletStreamConnection) pConnection).getResponse().setContentLength(pSize);
 		}
 		return super.getOutputStream(pConfig, pConnection, pSize);
-	}
-
-	protected void closeConnection(ServerStreamConnection pConnection) throws IOException {
-		((ServletStreamConnection) pConnection).getResponse().getOutputStream().close();
 	}
 
 	protected void setResponseHeader(ServerStreamConnection pConnection, String pHeader, String pValue) {



---------------------------------------------------------------------
To unsubscribe, e-mail: xmlrpc-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: xmlrpc-dev-help@ws.apache.org