Author: chris.laprun(a)jboss.com
Date: 2007-10-31 23:08:43 -0400 (Wed, 31 Oct 2007)
New Revision: 8810
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/URLTools.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/URLToolsTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java
Log:
- Renamed performGET to getContent as it makes more sense.
- Renamed getInputStreamFor to getContentAsInputStream and do not close the input stream
anymore before returning (doh!). :)
- Renamed FastURLEncoder.getInstance to getUTF8Instance to be more consistent with
FastURLDecoder.
- Added FastURLDecoder.encode(String) and encode(String, StringBuilder) methods.
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -24,11 +24,11 @@
import org.jboss.portal.common.io.UndeclaredIOException;
import org.jboss.portal.common.io.WriterCharWriter;
-import org.jboss.portal.common.text.FastURLEncoder;
+import org.jboss.portal.common.text.AbstractCharEncoder;
import org.jboss.portal.common.text.CharEncoder;
-import org.jboss.portal.common.text.AbstractCharEncoder;
import org.jboss.portal.common.text.CharWriter;
import org.jboss.portal.common.text.EncodingException;
+import org.jboss.portal.common.text.FastURLEncoder;
import java.io.IOException;
import java.io.StringWriter;
@@ -164,7 +164,7 @@
}
else
{
- FastURLEncoder.getInstance().encode(c, writer);
+ FastURLEncoder.getUTF8Instance().encode(c, writer);
}
}
};
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/URLTools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/URLTools.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/URLTools.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -24,18 +24,17 @@
import org.apache.log4j.Logger;
import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.text.FastURLDecoder;
+import org.jboss.portal.common.text.FastURLEncoder;
import org.jboss.portal.common.util.ParameterValidation;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
-import java.net.URLDecoder;
-import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@@ -65,9 +64,9 @@
}
/**
- * Fetches content from an HTTP server performing a GET operation. If the status code
is 200 then it will return a
- * byte array of the body otherwise returns null. The timeout values must not be
negative integers, when it is equals
- * to zero it means that it does not setup a timeout and use the default values.
+ * Fetches content from of the URL as a byte array or <code>null</code> if
a problem occurred. The timeout values
+ * must not be negative integers, when it is equals to zero it means that it does not
setup a timeout and use the
+ * default values.
*
* @param url the URL the URL of the resource
* @param soTimeoutMillis the socket connection timeout in millis
@@ -75,12 +74,12 @@
* @return the retrieved byte array
* @throws IllegalArgumentException if the URL is null or any time out value is
negative
*/
- public static byte[] performGET(URL url, int soTimeoutMillis, int connTimeoutMillis)
throws IllegalArgumentException
+ public static byte[] getContent(URL url, int soTimeoutMillis, int connTimeoutMillis)
throws IllegalArgumentException
{
InputStream in = null;
try
{
- in = internalGetInputStreamFor(url, soTimeoutMillis, connTimeoutMillis);
+ in = getContentAsInputStream(url, soTimeoutMillis, connTimeoutMillis);
return IOTools.getBytes(in);
}
catch (IOException e)
@@ -104,39 +103,8 @@
* @throws IllegalArgumentException if the URL is null or any time out value is
negative
* @since 1.1
*/
- public static InputStream getInputStreamFor(URL url, int soTimeoutMillis, int
connTimeoutMillis)
+ public static InputStream getContentAsInputStream(URL url, int soTimeoutMillis, int
connTimeoutMillis) throws IOException
{
- //
- InputStream in = null;
- try
- {
- in = internalGetInputStreamFor(url, soTimeoutMillis, connTimeoutMillis);
- return in;
- }
- catch (SocketTimeoutException e)
- {
- return null;
- }
- catch (IOException e)
- {
- return null;
- }
- finally
- {
- IOTools.safeClose(in);
- }
- }
-
- /**
- * @param url
- * @param soTimeoutMillis
- * @param connTimeoutMillis
- * @return
- * @throws IOException
- * @since 1.1
- */
- private static InputStream internalGetInputStreamFor(URL url, int soTimeoutMillis, int
connTimeoutMillis) throws IOException
- {
if (url == null)
{
throw new IllegalArgumentException();
@@ -166,7 +134,15 @@
conn.setReadTimeout(connTimeoutMillis);
conn.connect();
- return new BufferedInputStream(conn.getInputStream());
+ try
+ {
+ return new BufferedInputStream(conn.getInputStream());
+ }
+ catch (SocketTimeoutException e)
+ {
+ log.debug("Time out on: " + url);
+ throw e;
+ }
}
/**
@@ -200,26 +176,12 @@
public static String encodeXWWWFormURL(String s)
{
- try
- {
- return URLEncoder.encode(s, "UTF-8");
- }
- catch (UnsupportedEncodingException e)
- {
- throw new Error("UTF-8 encoding missing");
- }
+ return FastURLEncoder.getUTF8Instance().encode(s);
}
public static String decodeXWWWFormURL(String s)
{
- try
- {
- return URLDecoder.decode(s, "UTF-8");
- }
- catch (UnsupportedEncodingException e)
- {
- throw new Error("UTF-8 encoding missing");
- }
+ return FastURLDecoder.getUTF8Instance().encode(s);
}
/** Return true is the address is not null and matches the email validation regular
expression. */
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -86,7 +86,7 @@
//
root = new LookupNonTerm();
- for (char c = FROM;c <= TO;c++)
+ for (char c = FROM; c <= TO; c++)
{
byte[] v = encoder.encode(c);
LookupNonTerm node = root;
@@ -111,10 +111,10 @@
//
conversions = new int[256][256];
- for (char i = 0;i < 256;i++)
+ for (char i = 0; i < 256; i++)
{
int x = hexValue(i);
- for (char j = 0;j < 256;j++)
+ for (char j = 0; j < 256; j++)
{
int y = hexValue(j);
if (x != -1 && y != -1)
@@ -128,10 +128,9 @@
}
}
-
//
decisions = new int[256];
- for (int i = 0;i < decisions.length;i++)
+ for (int i = 0; i < decisions.length; i++)
{
if (TextTools.isAlphaNumeric((char)i))
{
@@ -139,7 +138,7 @@
}
else
{
- switch(i)
+ switch (i)
{
case '+':
decisions[i] = PLUS;
@@ -228,7 +227,7 @@
else
{
char[] chars = new char[s.length()];
- for (int i = 0;i < s.length();i++)
+ for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
chars[i] = c;
@@ -237,13 +236,54 @@
}
}
+ /**
+ * @param s
+ * @return
+ * @throws IllegalArgumentException
+ * @since 1.1
+ */
+ public String encode(String s) throws IllegalArgumentException
+ {
+ StringBuilder tmp = new StringBuilder();
+ encode(s, tmp);
+ return tmp.toString();
+ }
+
+ /**
+ * @param s
+ * @param out
+ * @throws IllegalArgumentException
+ * @since 1.1
+ */
+ public void encode(String s, StringBuilder out) throws IllegalArgumentException
+ {
+ if (s == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (out == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ for (int i = 0; i < s.length(); i++)
+ {
+ char c = s.charAt(i);
+ int[] z = conversions[c];
+ if (z == null)
+ {
+ throw new MalformedInputException("Cannot encode char=" + (int)c);
+ }
+ out.append(z);
+ }
+ }
+
private void encodeNoCheck(char[] chars, int i, int length, CharWriter tmp)
{
while (i < length)
{
char c = chars[i++];
int decision = c < 256 ? decisions[c] : ERROR;
- switch(decision)
+ switch (decision)
{
case AS_IS:
tmp.append(c);
@@ -261,7 +301,7 @@
if (j + 2 > length)
{
throw new MalformedInputException("Not enough chars to decode
an escaped value length should have been" +
- (j + 2) + " but is " + length);
+ (j + 2) + " but is " + length);
}
//
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -39,7 +39,7 @@
/** . */
private static final FastURLEncoder DEFAULT_ENCODER = new
FastURLEncoder(CharToByteEncoder.Generic.UTF8);
- public static FastURLEncoder getInstance()
+ public static FastURLEncoder getUTF8Instance()
{
return DEFAULT_ENCODER;
}
@@ -61,7 +61,7 @@
this.table = new char[MAX + 1][];
//
- for (char c = 0;c <= MAX;c++)
+ for (char c = 0; c <= MAX; c++)
{
char[] z;
if (TextTools.isAlphaNumeric(c))
@@ -70,7 +70,7 @@
}
else
{
- switch(c)
+ switch (c)
{
case ' ':
z = new char[]{'+'};
@@ -87,7 +87,7 @@
{
z = new char[v.length * 3];
int index = 0;
- for (int i = 0;i < v.length;i++)
+ for (int i = 0; i < v.length; i++)
{
byte b = v[i];
z[index++] = '%';
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/URLToolsTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/URLToolsTestCase.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/URLToolsTestCase.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -24,15 +24,11 @@
import junit.framework.TestCase;
import org.apache.log4j.Logger;
-import org.jboss.portal.common.io.IOTools;
import org.jboss.portal.common.junit.ExtendedAssert;
import org.jboss.portal.common.net.URLTools;
-import java.io.BufferedInputStream;
import java.net.MalformedURLException;
-import java.net.ServerSocket;
import java.net.URL;
-import java.net.URLConnection;
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
@@ -183,7 +179,7 @@
public void testURLExistsTimeout() throws Exception
{
- AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractTimeoutServer(8080)
+ AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractTimeoutServer(8080)
{
protected void doClient()
{
@@ -210,7 +206,7 @@
public void testURLPerformGETTimeout() throws Exception
{
- AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractTimeoutServer(8080)
+ AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractTimeoutServer(8080)
{
protected void doClient()
{
@@ -218,7 +214,7 @@
try
{
URL url = new URL("http://localhost:8080/");
- bytes = URLTools.performGET(url, 500, 500);
+ bytes = URLTools.getContent(url, 500, 500);
}
catch (MalformedURLException e)
{
@@ -237,13 +233,13 @@
public void testURLPerformGET() throws Exception
{
- AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractOKServer(8080)
+ AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractOKServer(8080)
{
protected void doClient() throws Exception
{
URL url = new URL("http://localhost:8080/");
- byte[] bytes = URLTools.performGET(url, 5000, 5000);
+ byte[] bytes = URLTools.getContent(url, 5000, 5000);
ExtendedAssert.assertEquals("CAFEBABE".getBytes("UTF-8"),
bytes);
}
};
@@ -254,7 +250,7 @@
public void testURLExists() throws Exception
{
- AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractOKServer(8080)
+ AbstractSynchronizedServer server = new
AbstractSynchronizedServer.AbstractOKServer(8080)
{
protected void doClient() throws Exception
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -33,7 +33,7 @@
public class CharBufferTestCase extends TestCase
{
- private FastURLEncoder encoder = FastURLEncoder.getInstance();
+ private FastURLEncoder encoder = FastURLEncoder.getUTF8Instance();
public void testUTF8EncodedStringAppend()
{
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java 2007-10-31
23:53:29 UTC (rev 8809)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java 2007-11-01
03:08:43 UTC (rev 8810)
@@ -22,15 +22,13 @@
******************************************************************************/
package org.jboss.portal.test.common.text;
-import org.jboss.portal.common.text.FastURLEncoder;
+import junit.framework.TestCase;
import org.jboss.portal.common.text.CharBuffer;
import org.jboss.portal.common.text.CharEncoder;
+import org.jboss.portal.common.text.FastURLEncoder;
-import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
-import junit.framework.TestCase;
-
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
@@ -38,9 +36,9 @@
public class FastURLEncoderTestCase extends TestCase
{
- public void testEncodeThrowsIAE() throws Exception
+ public void testEncodeThrowsIAE() throws Exception
{
- CharEncoder encoder = FastURLEncoder.getInstance();
+ CharEncoder encoder = FastURLEncoder.getUTF8Instance();
try
{
encoder.encode(new char[10], -1, 0, new CharBuffer());
@@ -135,9 +133,9 @@
public void testEncode() throws Exception
{
- FastURLEncoder encoder = FastURLEncoder.getInstance();
+ FastURLEncoder encoder = FastURLEncoder.getUTF8Instance();
StringBuffer tmp = new StringBuffer();
- for (int i = 0;i < 512;i++)
+ for (int i = 0; i < 512; i++)
{
tmp.append((char)i);
}