Author: jochen
Date: Tue Aug  5 06:34:51 2008
New Revision: 682709

URL: http://svn.apache.org/viewvc?rev=682709&view=rev
Log:
Fixed the FAQ entry for obtaining the clients IP address.

Added:
    
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ClientIpTest.java
Modified:
    
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ThreadPool.java
    webservices/xmlrpc/trunk/pom.xml
    webservices/xmlrpc/trunk/src/changes/changes.xml
    webservices/xmlrpc/trunk/src/site/apt/types.apt
    webservices/xmlrpc/trunk/src/site/fml/faq.fml
    
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ParserTest.java
    
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ServletWebServerProvider.java

Modified: 
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ThreadPool.java
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ThreadPool.java?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- 
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ThreadPool.java
 (original)
+++ 
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ThreadPool.java
 Tue Aug  5 06:34:51 2008
@@ -147,7 +147,7 @@
         * @param pTask The task being started.
         * @return True, if the task could be started immediately. False, if
         * the maxmimum number of concurrent tasks was exceeded. If so, you
-        * might consider to use the [EMAIL PROTECTED] #addTask(Task)} method 
instead.
+        * might consider to use the [EMAIL PROTECTED] 
#addTask(ThreadPool.Task)} method instead.
         */
        public synchronized boolean startTask(Task pTask) {
                if (maxSize != 0  &&  runningThreads.size() > maxSize) {

Modified: webservices/xmlrpc/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/pom.xml?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/pom.xml (original)
+++ webservices/xmlrpc/trunk/pom.xml Tue Aug  5 06:34:51 2008
@@ -156,6 +156,10 @@
             <email>[EMAIL PROTECTED]</email>
         </contributor>
         <contributor>
+            <name>Kay Tiong Khoo</name>
+            <email>[EMAIL PROTECTED]</email>
+        </contributor>
+        <contributor>
             <name>Jimisola Laursen</name>
             <email>[EMAIL PROTECTED]</email>
         </contributor>

Modified: webservices/xmlrpc/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/src/changes/changes.xml?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/src/changes/changes.xml (original)
+++ webservices/xmlrpc/trunk/src/changes/changes.xml Tue Aug  5 06:34:51 2008
@@ -26,6 +26,9 @@
         Added some clarification on the handling of Object[] and 
java.util.List to
         the page on "XML-RPC Data Types".
       </action>
+      <action dev="jochen" type="fix" issue="XMLRPC-157" due-to="Kay Tiong 
Khoo" due-to-email="[EMAIL PROTECTED]">
+        Minor grammar improvements.
+      </action>
     </release>
     <release version="3.1" date="2007-Aug-31">
       <action dev="jochen" type="add">

Modified: webservices/xmlrpc/trunk/src/site/apt/types.apt
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/src/site/apt/types.apt?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/src/site/apt/types.apt (original)
+++ webservices/xmlrpc/trunk/src/site/apt/types.apt Tue Aug  5 06:34:51 2008
@@ -39,7 +39,7 @@
 | java.util.List     |                    | elements may be any valid data     
 |
 |                    |                    | type, including another array.     
 |
 |                    |                    |                                    
 |
-|                    |                    | The server may other arrays (for   
 |
+|                    |                    | The server may return other arrays 
(for
 |                    |                    | example String[]) or lists (using  
 |
 |                    |                    | generics). However, the client 
will |
 |                    |                    | always return an Object[], because 
 |

Modified: webservices/xmlrpc/trunk/src/site/fml/faq.fml
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/src/site/fml/faq.fml?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- webservices/xmlrpc/trunk/src/site/fml/faq.fml (original)
+++ webservices/xmlrpc/trunk/src/site/fml/faq.fml Tue Aug  5 06:34:51 2008
@@ -204,50 +204,31 @@
       <answer>
         <p>That's a similar question than the question on initializing 
handlers.
           The main difference is, that in this case you want to initialize the
-          handler with any request. So, here's how to do it: First of all,
-          we assume that all handlers will implement an interface
-          RequestInitializableHandler. This interface has an init method,
-          which is being called to receive an object with the clients
-          IP address:</p>
+          handler with any request. In other words, you might achieve the goal
+          by creating a RequestProcessorFactoryFactory, that provides the
+          necessary details. However, there is an easier solution, which we
+          will demonstrate here: Use a ThreadLocal.</p>
+        <p>The class ThreadLocal allows to create information at some point
+          in the source code and use this information at one or more completely
+          different and decoupled places. The only assumption is, that you
+          are in the same thread. This is exactly our situation: We create
+          the information when processing of the XML-RPC request starts
+          and read it within the handler.</p>
+        <p>In the example below, you'd obtain the clients IP address by
+          writing <code>ClientInfoServlet.getClientIpAddress()</code>.</p>
         <source><![CDATA[
-public class MyConfig extends XmlRpcHttpRequestConfigImpl {
-       private String clientIpAddress;
-       public String getClientIpAddress() {
-           return clientIpAddress;
-       }
-       public void setClientIpAddress(String pClientIpAddress) {
-           clientIpAddress = pClientIpAddress;
-       }
-}
+public static class ClientInfoServlet extends XmlRpcServlet {
+    private static ThreadLocal clientIpAddress = new ThreadLocal();
 
-public interface RequestInitializableRequestProcessor {
-    public void init(MyConfig pConfig);
-}
+    public static String getClientIpAddress() {
+        return (String) clientIpAddress.get();
+    }
 
-public class MyXmlRpcServlet extends XmlRpcServlet {
-       protected XmlRpcServletServer newXmlRpcServer(ServletConfig pConfig)
-                       throws XmlRpcException {
-               return new XmlRpcServletServer(){
-               protected XmlRpcHttpRequestConfigImpl 
newConfig(HttpServletRequest pRequest) {
-                   MyConfig config = new MyConfig();
-                   config.setClientIpAddress(pRequest.getRemoteAddr());
-                   return config;
-               }
-               };
-       }
-       protected PropertyHandlerMapping newPropertyHandlerMapping(URL url) 
throws IOException, XmlRpcException {
-        PropertyHandlerMapping mapping = super.newPropertyHandlerMapping(url);
-           RequestProcessorFactoryFactory factory = new 
RequestSpecificProcessorFactoryFactory(){
-               protected Object getRequestProcessor(Class pClass, 
XmlRpcRequest pRequest) throws XmlRpcException {
-                   RequestInitializableRequestProcessor proc =
-                       (RequestInitializableRequestProcessor) 
super.getRequestProcessor(pClass, pRequest);
-                   proc.init((MyConfig) pRequest.getConfig());
-                   return proc;
-               }
-           };
-           mapping.setRequestProcessorFactoryFactory(factory);
-           return mapping;
-       }
+    public void doPost(HttpServletRequest pRequest, HttpServletResponse 
pResponse)
+            throws IOException, ServletException {
+        clientIpAddress.set(pRequest.getRemoteAddr());
+        super.doPost(pRequest, pResponse);
+    }
 }
         ]]></source>
       </answer>

Added: 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ClientIpTest.java
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ClientIpTest.java?rev=682709&view=auto
==============================================================================
--- 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ClientIpTest.java
 (added)
+++ 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ClientIpTest.java
 Tue Aug  5 06:34:51 2008
@@ -0,0 +1,108 @@
+package org.apache.xmlrpc.test;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.Assert;
+
+import org.apache.xmlrpc.XmlRpcException;
+import org.apache.xmlrpc.XmlRpcHandler;
+import org.apache.xmlrpc.XmlRpcRequest;
+import org.apache.xmlrpc.client.XmlRpcClient;
+import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
+import org.apache.xmlrpc.server.XmlRpcNoSuchHandlerException;
+import org.apache.xmlrpc.webserver.XmlRpcServlet;
+
+
+/**
+ * Test case for reading the clients IP address.
+ */
+public class ClientIpTest extends XmlRpcTestCase {
+    public static class ClientInfo {
+        private final String ipAddress;
+
+        public ClientInfo(String pIpAddress) {
+            ipAddress = pIpAddress;
+        }
+
+        public String getIpAddress() {
+            return ipAddress;
+        }
+    }
+
+    public static class ClientInfoServlet extends XmlRpcServlet {
+        private static ThreadLocal clientInfo = new ThreadLocal();
+
+        public static ClientInfo getClientInfo() {
+            return (ClientInfo) clientInfo.get();
+        }
+
+        public void doPost(HttpServletRequest pRequest,
+                HttpServletResponse pResponse) throws IOException,
+                ServletException {
+            clientInfo.set(new ClientInfo(pRequest.getRemoteAddr()));
+            super.doPost(pRequest, pResponse);
+        }
+    }
+
+    private static class ClientIpTestProvider extends ServletWebServerProvider 
{
+        ClientIpTestProvider(XmlRpcHandlerMapping pMapping, boolean 
pContentLength)
+                throws ServletException, IOException {
+            super(pMapping, pContentLength);
+        }
+
+        protected XmlRpcServlet newXmlRpcServlet() {
+            return new ClientInfoServlet();
+        }
+    }
+    
+    protected ClientProvider[] initProviders(XmlRpcHandlerMapping pMapping)
+            throws ServletException, IOException {
+        return new ClientProvider[]{
+            new ClientIpTestProvider(pMapping, false),
+            new ClientIpTestProvider(pMapping, true)
+        };
+    }
+
+    protected XmlRpcHandlerMapping getHandlerMapping() throws IOException,
+            XmlRpcException {
+        final XmlRpcHandler handler = new XmlRpcHandler(){
+            public Object execute(XmlRpcRequest pRequest) throws 
XmlRpcException {
+                final ClientInfo clientInfo = 
ClientInfoServlet.getClientInfo();
+                if (clientInfo == null) {
+                    return "";
+                }
+                final String ip = clientInfo.getIpAddress();
+                if (ip == null) {
+                    return "";
+                }
+                return ip;
+            }
+        };
+        return new XmlRpcHandlerMapping(){
+            public XmlRpcHandler getHandler(String pHandlerName)
+                    throws XmlRpcNoSuchHandlerException, XmlRpcException {
+                return handler;
+            }
+        };
+    }
+
+    private void testClientIpAddress(ClientProvider pProvider) throws 
Exception {
+        final XmlRpcClient client = pProvider.getClient();
+        client.setConfig(getConfig(pProvider));
+        final String ip = (String) client.execute("getIpAddress", new 
Object[]{});
+        assertEquals("127.0.0.1", ip);
+    }
+    
+    /** Test, whether we can invoke a method, returning a byte.
+     * @throws Exception The test failed.
+     */
+    public void testClientIpAddress() throws Exception {
+        for (int i = 0;  i < providers.length;  i++) {
+            testClientIpAddress(providers[i]);
+        }
+    }
+}

Modified: 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ParserTest.java
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ParserTest.java?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ParserTest.java
 (original)
+++ 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ParserTest.java
 Tue Aug  5 06:34:51 2008
@@ -14,12 +14,10 @@
 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
-import org.apache.xmlrpc.parser.CalendarParser;
 import org.apache.xmlrpc.parser.DateParser;
 import org.apache.xmlrpc.parser.XmlRpcRequestParser;
 import org.apache.xmlrpc.parser.XmlRpcResponseParser;
 import org.apache.xmlrpc.util.SAXParsers;
-import org.apache.xmlrpc.util.XmlRpcDateTimeDateFormat;
 import org.apache.xmlrpc.util.XmlRpcDateTimeFormat;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;

Modified: 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ServletWebServerProvider.java
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ServletWebServerProvider.java?rev=682709&r1=682708&r2=682709&view=diff
==============================================================================
--- 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ServletWebServerProvider.java
 (original)
+++ 
webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/ServletWebServerProvider.java
 Tue Aug  5 06:34:51 2008
@@ -39,6 +39,13 @@
        private final boolean contentLength;
        private final int port;
 
+       /**
+        * Creates a new instance of [EMAIL PROTECTED] XmlRpcServlet}.
+        */
+       protected XmlRpcServlet newXmlRpcServlet() {
+           return new XmlRpcServlet();
+    }
+       
        /** Creates a new instance.
         * @param pMapping The test servers handler mapping.
         * @throws ServletException 
@@ -47,7 +54,7 @@
        protected ServletWebServerProvider(XmlRpcHandlerMapping pMapping, 
boolean pContentLength) throws ServletException, IOException {
                super(pMapping);
                contentLength = pContentLength;
-               servlet = new XmlRpcServlet();
+               servlet = newXmlRpcServlet();
                webServer = new ServletWebServer(servlet, 0);
                XmlRpcServer server = servlet.getXmlRpcServletServer();
                server.setHandlerMapping(mapping);


Reply via email to