Author: azeez
Date: Mon Sep 29 17:48:41 2008
New Revision: 700319

URL: http://svn.apache.org/viewvc?rev=700319&view=rev
Log:
Handling the reverse DNS lookup delay issue by avoiding 
InetAddress#getHostName()


Added:
    
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/util/NhttpUtil.java
Modified:
    
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java

Modified: 
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java?rev=700319&r1=700318&r2=700319&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
 (original)
+++ 
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
 Mon Sep 29 17:48:41 2008
@@ -38,6 +38,7 @@
 import org.apache.http.nio.NHttpServerConnection;
 import org.apache.http.protocol.HTTP;
 import org.apache.synapse.transport.nhttp.util.RESTUtil;
+import org.apache.synapse.transport.nhttp.util.NhttpUtil;
 import org.apache.ws.commons.schema.XmlSchema;
 
 import java.io.IOException;
@@ -163,7 +164,7 @@
             InetAddress remoteAddr = inetConn.getRemoteAddress();
             if (remoteAddr != null) {
                 msgContext.setProperty(MessageContext.REMOTE_ADDR, 
remoteAddr.getHostAddress());
-                msgContext.setProperty(NhttpConstants.REMOTE_HOST, 
remoteAddr.getHostName());
+                msgContext.setProperty(NhttpConstants.REMOTE_HOST, 
NhttpUtil.getHostName(remoteAddr));
             }
         }
 

Added: 
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/util/NhttpUtil.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/util/NhttpUtil.java?rev=700319&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/util/NhttpUtil.java
 (added)
+++ 
synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/util/NhttpUtil.java
 Mon Sep 29 17:48:41 2008
@@ -0,0 +1,62 @@
+/*
+*  Licensed to the Apache Software Foundation (ASF) under one
+*  or more contributor license agreements.  See the NOTICE file
+*  distributed with this work for additional information
+*  regarding copyright ownership.  The ASF licenses this file
+*  to you under the Apache License, Version 2.0 (the
+*  "License"); you may not use this file except in compliance
+*  with the License.  You may obtain a copy of the License at
+*
+*   http://www.apache.org/licenses/LICENSE-2.0
+*
+*  Unless required by applicable law or agreed to in writing,
+*  software distributed under the License is distributed on an
+*   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+*  KIND, either express or implied.  See the License for the
+*  specific language governing permissions and limitations
+*  under the License.
+*/
+package org.apache.synapse.transport.nhttp.util;
+
+import java.net.InetAddress;
+
+/**
+ * A useful set of utility methods for the HTTP transport
+ */
+public class NhttpUtil {
+
+    /**
+     * This method tries to determine the hostname of the given InetAddress 
without
+     * triggering a reverse DNS lookup.  [EMAIL PROTECTED] 
java.net.InetAddress#getHostName()}
+     * triggers a reverse DNS lookup which can be very costly in cases where 
reverse
+     * DNS fails. Tries to parse a symbolic hostname from [EMAIL PROTECTED] 
java.net.InetAddress#toString()},
+     * which is documented to return a String of the form "hostname / literal 
IP address"
+     * with 'hostname' blank if not already computed & stored in 
<code>address</code<.
+     * <p/>
+     * If the hostname cannot be determined from InetAddress.toString(),
+     * the value of [EMAIL PROTECTED] java.net.InetAddress#getHostAddress()} 
is returned.
+     *
+     * @param address The InetAddress whose hostname has to be determined
+     * @return hostsname, if it can be determined. hostaddress, if not.
+     *
+     * TODO: We may introduce a System property or some other method of 
configuration
+     * TODO: which will specify whether to allow reverse DNS lookup or not
+     */
+    public static String getHostName(InetAddress address) {
+        String result;
+        String hostAddress = address.getHostAddress();
+        String inetAddr = address.toString();
+        int index1 = inetAddr.lastIndexOf('/');
+        int index2 = inetAddr.indexOf(hostAddress);
+        if (index2 == index1 + 1) {
+            if (index1 == 0) {
+                result = hostAddress;
+            } else {
+                result = inetAddr.substring(0, index1);
+            }
+        } else {
+            result = hostAddress;
+        }
+        return result;
+    }
+}


Reply via email to