package org.jboss.axis.components.net;
import org.jboss.axis.encoding.Base64;
import org.jboss.axis.transport.http.HTTPConstants;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import java.net.Socket;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class DefaultSocketFactory implements SocketFactory
{
private static Logger log = Logger.getLogger(DefaultSocketFactory.class.getName());
protected Hashtable attributes = null;
public DefaultSocketFactory(Hashtable attributes)
{
this.attributes = attributes;
}
public Socket create(String host, int port, StringBuffer otherHeaders, BooleanHolder useFullURL)
throws Exception
{
TransportClientProperties tcp = TransportClientPropertiesFactory.create("http");
Socket sock = null;
boolean hostInNonProxyList = isHostInNonProxyList(host, tcp.getNonProxyHosts());
if (tcp.getProxyUser().length() != 0)
{
StringBuffer tmpBuf = new StringBuffer();
tmpBuf.append(tcp.getProxyUser())
.append(":")
.append(tcp.getProxyPassword());
otherHeaders.append(HTTPConstants.HEADER_PROXY_AUTHORIZATION)
.append(": Basic ")
.append(Base64.encode(tmpBuf.toString().getBytes()))
.append("\r\n");
}
if (port == -1)
{
port = 80;
}
if ((tcp.getProxyHost().length() == 0) ||
(tcp.getProxyPort().length() == 0) ||
hostInNonProxyList)
{
sock = new Socket(host, port);
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("createdHTTP00"));
}
}
else
{
sock = new Socket(tcp.getProxyHost(),
new Integer(tcp.getProxyPort()).intValue());
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("createdHTTP01", tcp.getProxyHost(),
tcp.getProxyPort()));
}
useFullURL.value = true;
}
return sock;
}
protected boolean isHostInNonProxyList(String host, String nonProxyHosts)
{
if ((nonProxyHosts == null) || (host == null))
{
return false;
}
StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|\"");
while (tokenizer.hasMoreTokens())
{
String pattern = tokenizer.nextToken();
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("match00",
new String[]{"HTTPSender",
host,
pattern}));
}
if (match(pattern, host, false))
{
return true;
}
}
return false;
}
protected static boolean match(String pattern, String str,
boolean isCaseSensitive)
{
char[] patArr = pattern.toCharArray();
char[] strArr = str.toCharArray();
int patIdxStart = 0;
int patIdxEnd = patArr.length - 1;
int strIdxStart = 0;
int strIdxEnd = strArr.length - 1;
char ch;
boolean containsStar = false;
for (int i = 0; i < patArr.length; i++)
{
if (patArr[i] == '*')
{
containsStar = true;
break;
}
}
if (!containsStar)
{
if (patIdxEnd != strIdxEnd)
{
return false; }
for (int i = 0; i <= patIdxEnd; i++)
{
ch = patArr[i];
if (isCaseSensitive && (ch != strArr[i]))
{
return false; }
if (!isCaseSensitive
&& (Character.toUpperCase(ch)
!= Character.toUpperCase(strArr[i])))
{
return false; }
}
return true; }
if (patIdxEnd == 0)
{
return true; }
while ((ch = patArr[patIdxStart]) != '*'
&& (strIdxStart <= strIdxEnd))
{
if (isCaseSensitive && (ch != strArr[strIdxStart]))
{
return false; }
if (!isCaseSensitive
&& (Character.toUpperCase(ch)
!= Character.toUpperCase(strArr[strIdxStart])))
{
return false; }
patIdxStart++;
strIdxStart++;
}
if (strIdxStart > strIdxEnd)
{
for (int i = patIdxStart; i <= patIdxEnd; i++)
{
if (patArr[i] != '*')
{
return false;
}
}
return true;
}
while ((ch = patArr[patIdxEnd]) != '*' && (strIdxStart <= strIdxEnd))
{
if (isCaseSensitive && (ch != strArr[strIdxEnd]))
{
return false; }
if (!isCaseSensitive
&& (Character.toUpperCase(ch)
!= Character.toUpperCase(strArr[strIdxEnd])))
{
return false; }
patIdxEnd--;
strIdxEnd--;
}
if (strIdxStart > strIdxEnd)
{
for (int i = patIdxStart; i <= patIdxEnd; i++)
{
if (patArr[i] != '*')
{
return false;
}
}
return true;
}
while ((patIdxStart != patIdxEnd) && (strIdxStart <= strIdxEnd))
{
int patIdxTmp = -1;
for (int i = patIdxStart + 1; i <= patIdxEnd; i++)
{
if (patArr[i] == '*')
{
patIdxTmp = i;
break;
}
}
if (patIdxTmp == patIdxStart + 1)
{
patIdxStart++;
continue;
}
int patLength = (patIdxTmp - patIdxStart - 1);
int strLength = (strIdxEnd - strIdxStart + 1);
int foundIdx = -1;
strLoop:
for (int i = 0; i <= strLength - patLength; i++)
{
for (int j = 0; j < patLength; j++)
{
ch = patArr[patIdxStart + j + 1];
if (isCaseSensitive
&& (ch != strArr[strIdxStart + i + j]))
{
continue strLoop;
}
if (!isCaseSensitive && (Character
.toUpperCase(ch) != Character
.toUpperCase(strArr[strIdxStart + i + j])))
{
continue strLoop;
}
}
foundIdx = strIdxStart + i;
break;
}
if (foundIdx == -1)
{
return false;
}
patIdxStart = patIdxTmp;
strIdxStart = foundIdx + patLength;
}
for (int i = patIdxStart; i <= patIdxEnd; i++)
{
if (patArr[i] != '*')
{
return false;
}
}
return true;
}
}