Some fixes for the SiteProxy to handle encodings other than UTF-8 and slow data sources better.

git-svn-id: https://svn.libreccm.org/ccm/trunk@3122 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2015-02-06 18:21:30 +00:00
parent 60713c7070
commit 0363eb49d2
1 changed files with 51 additions and 9 deletions

View File

@ -12,13 +12,19 @@ import com.arsdigita.util.url.URLFetcher;
import com.arsdigita.util.url.URLPool; import com.arsdigita.util.url.URLPool;
import com.arsdigita.xml.Document; import com.arsdigita.xml.Document;
import com.arsdigita.xml.Element; import com.arsdigita.xml.Element;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Enumeration; import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
/** /**
* *
* @author Jens Pelzetter * @author Jens Pelzetter
@ -37,19 +43,22 @@ public class SiteProxyExtraXMLGenerator implements ExtraXMLGenerator {
private static final SiteProxyConfig config = new SiteProxyConfig(); private static final SiteProxyConfig config = new SiteProxyConfig();
// private static final URLCache s_cache = new URLCache(1000000, 1 * 60 * 1000); // private static final URLCache s_cache = new URLCache(1000000, 1 * 60 * 1000);
// private static final URLPool s_pool = new URLPool(10, 10000); // private static final URLPool s_pool = new URLPool(10, 10000);
private static final URLCache s_cache = new URLCache( private static final URLCache s_cache;// = new URLCache(
config.getUrlCacheSize(), config.getUrlCacheExpiryTime()); //config.getUrlCacheSize(), config.getUrlCacheExpiryTime());
private static final URLPool s_pool = new URLPool( private static final URLPool s_pool;// = new URLPool(
config.getUrlPoolSize(), config.getUrlPoolTimeout()); //config.getUrlPoolSize(), config.getUrlPoolTimeout());
static { static {
config.load(); config.load();
logger.debug("Static initalizer starting..."); logger.debug("Static initalizer starting...");
s_cache = new URLCache(config.getUrlCacheSize(), config.getUrlCacheExpiryTime());
s_pool = new URLPool(config.getUrlPoolSize(), config.getUrlPoolTimeout());
URLFetcher.registerService(s_cacheServiceKey, s_pool, s_cache); URLFetcher.registerService(s_cacheServiceKey, s_pool, s_cache);
logger.debug("Static initalizer finished."); logger.debug("Static initalizer finished.");
} }
@Override
public void generateXML(final ContentItem item, public void generateXML(final ContentItem item,
final Element element, final Element element,
final PageState state) { final PageState state) {
@ -138,16 +147,14 @@ public class SiteProxyExtraXMLGenerator implements ExtraXMLGenerator {
*/ */
public URLData getRemoteXML(Element child, String url) { public URLData getRemoteXML(Element child, String url) {
URLData data = URLFetcher.fetchURLData(url, s_cacheServiceKey); URLData data = URLFetcher.fetchURLData(url, s_cacheServiceKey);
if (data == null || data.getException() != null || data.getContent().length if (data == null || data.getException() != null || data.getContent().length == 0) {
== 0) {
return data; return data;
} }
String contentType = data.getContentType(); String contentType = data.getContentType();
boolean success = false; boolean success = false;
if (contentType != null && contentType.toLowerCase().indexOf("/xml") if (contentType != null && contentType.toLowerCase().indexOf("/xml") > -1) {
> -1) {
// we use the /xml intead of text/xml because // we use the /xml intead of text/xml because
// things like application/xml are also valid // things like application/xml are also valid
Document document = null; Document document = null;
@ -164,7 +171,7 @@ public class SiteProxyExtraXMLGenerator implements ExtraXMLGenerator {
final String xmlString = String.format( final String xmlString = String.format(
"<?xml version=\"1.0\"?> \n%s", "<?xml version=\"1.0\"?> \n%s",
new String(xml, new String(xml,
Charset.forName("UTF-8"))); guessCharset(data)));
document = new Document(xmlString); document = new Document(xmlString);
success = true; success = true;
logger.info("Adding the headers to " + url logger.info("Adding the headers to " + url
@ -187,4 +194,39 @@ public class SiteProxyExtraXMLGenerator implements ExtraXMLGenerator {
} }
return data; return data;
} }
/**
* Helper method to guess charset. Extracted from the old, depcrecated method
* {@link URLData#getContentAsString()}. Because we need only to deal with XML data which is
* text we can do this here. If no charset is given, the method will return UTF-8 (as opposed
* to the method in {@link URLData} which assumed UTF-8.
*
* @param data
* @return
*/
private Charset guessCharset(final URLData data) {
final String contentType = data.getContentType();
String encoding = "UTF-8";
if (contentType != null) {
final int offset = contentType.indexOf("charset=");
if (offset != -1) {
encoding = contentType.substring(offset + 8).trim();
}
}
try {
return Charset.forName(encoding);
} catch(IllegalCharsetNameException ex) {
logger.warn(String.format(
"SiteProxy response has unsupported charset %s. Using UTF-8 as fallback",
encoding), ex);
return Charset.forName("UTF-8");
} catch(UnsupportedCharsetException ex) {
logger.warn(String.format(
"SiteProxy response has unsupported charset %s. Using UTF-8 as fallback",
encoding), ex);
return Charset.forName("UTF-8");
}
}
} }