Author: [email protected]
Date: Fri Aug 26 13:52:42 2011
New Revision: 1346
Log:
[AMDATUOPENSOCIAL-101] Fixed proper encoding of gadget XML
Modified:
trunk/amdatu-opensocial/opensocial-shindig/pom.xml
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
Modified: trunk/amdatu-opensocial/opensocial-shindig/pom.xml
==============================================================================
--- trunk/amdatu-opensocial/opensocial-shindig/pom.xml (original)
+++ trunk/amdatu-opensocial/opensocial-shindig/pom.xml Fri Aug 26 13:52:42 2011
@@ -166,6 +166,12 @@
<version>20070829</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.1.2</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
<build>
Modified:
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
==============================================================================
---
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
(original)
+++
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
Fri Aug 26 13:52:42 2011
@@ -15,12 +15,7 @@
*/
package org.amdatu.opensocial.shindig.service;
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -41,6 +36,13 @@
import org.amdatu.opensocial.shindig.osgi.Activator;
import org.apache.felix.dm.Component;
import org.apache.felix.dm.DependencyManager;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.HttpConnectionParams;
import org.apache.shindig.auth.BlobCrypterSecurityToken;
import org.apache.shindig.common.crypto.BasicBlobCrypter;
import org.apache.shindig.common.crypto.BlobCrypterException;
@@ -72,8 +74,8 @@
private static final int GADGETSPEC_CACHE_TIMEOUT = 15 * 60 * 1000;
// Thread safe implementation of gadget spec cache
- private ConcurrentMap<URL, FutureTask<Object[]>> m_gadgetSpecCache =
- new ConcurrentHashMap<URL, FutureTask<Object[]>>();
+ private ConcurrentMap<String, FutureTask<Object[]>> m_gadgetSpecCache =
+ new ConcurrentHashMap<String, FutureTask<Object[]>>();
// Service dependencies injected by the dependency manager
private volatile Tenant m_tenant;
@@ -145,7 +147,7 @@
Map<String, String> gadgetSpec = new HashMap<String, String>();
try {
m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec for
'" + gadgetUrl + "'");
- String xml = loadXMLFromCache(new URL(gadgetUrl));
+ String xml = loadXMLFromCache(gadgetUrl);
if (xml != null) {
GadgetSpec spec = new GadgetSpec(Uri.parse(gadgetUrl), xml);
@@ -157,7 +159,7 @@
Locale locale = getPreferredLocale(defaultLocale, locales);
// First replace messages with the preferred language
labels
- String messageXML = internalLoadXML(new
URL(locales.get(locale).getMessages().toString()));
+ String messageXML =
loadContentFromURL(locales.get(locale).getMessages().toString());
MessageBundle bundle = new
MessageBundle(locales.get(locale), messageXML);
Substitutions substituter = new Substitutions();
substituter.addSubstitutions(Substitutions.Type.MESSAGE,
bundle.getMessages());
@@ -165,7 +167,7 @@
// Replace the remaining messages with 'all' messages
locale = getPreferredLocale(new Locale("all", "all"),
locales);
- messageXML = internalLoadXML(new
URL(locales.get(locale).getMessages().toString()));
+ messageXML =
loadContentFromURL(locales.get(locale).getMessages().toString());
bundle = new MessageBundle(locales.get(locale),
messageXML);
substituter = new Substitutions();
substituter.addSubstitutions(Substitutions.Type.MESSAGE,
bundle.getMessages());
@@ -211,6 +213,21 @@
}
return null;
}
+
+ private String loadContentFromURL(String url) throws
ClientProtocolException, IOException {
+ // Use the Apache HTTPClient as this will respect the returned
encoding type of the content
+ // (i.e. charset=... in Content-Type header). Without Apache
httpclient we should read the encoding type
+ // header from the response headers, read the content into a byte
array and convert the byte array to
+ // a String using the proper encoding. Using Apache HTTP client is
much easier as it does this job for
+ // us.
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpGet httpGet = new HttpGet(url);
+ HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
GADGETSPEC_READ_TIMEOUT);
+
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();
+ String responseBody = httpClient.execute(httpGet, responseHandler);
+ return responseBody;
+ }
private UserPrefs loadUserPrefs(GadgetSpec spec, final Map<String, String>
userPrefs) {
// Default userpref values, when there are no user specific userprefs
stored (yet)
@@ -277,7 +294,7 @@
}
// Return XML from cache or retrieve it
- private String loadXMLFromCache(final URL url) {
+ private String loadXMLFromCache(final String url) {
FutureTask<Object[]> futureTask = m_gadgetSpecCache.get(url);
try {
if (futureTask != null) {
@@ -293,7 +310,7 @@
if (futureTask == null) {
Callable<Object[]> callable = new Callable<Object[]>() {
public Object[] call() throws Exception {
- return new Object[] {System.currentTimeMillis(),
internalLoadXML(url)};
+ return new Object[] {System.currentTimeMillis(),
loadContentFromURL(url)};
}
};
FutureTask<Object[]> ft = new FutureTask<Object[]>(callable);
@@ -319,41 +336,6 @@
return null;
}
- // Loads the XML without cache
- private String internalLoadXML(final URL url) {
- long time = System.currentTimeMillis();
- BufferedReader reader = null;
- try {
- try {
- String xml = "";
- URLConnection inputConnection = url.openConnection();
- inputConnection.setReadTimeout(GADGETSPEC_READ_TIMEOUT);
- // TODO: assuming here it is UTF-8
- reader =
- new BufferedReader(
- new
InputStreamReader(inputConnection.getInputStream(), Charset.forName("UTF-8")));
- String inputLine;
- while ((inputLine = reader.readLine()) != null) {
- xml += inputLine + System.getProperty("line.separator");
- }
- m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec
'" + url + "' took "
- + (System.currentTimeMillis() - time) + " ms");
- return xml;
- }
- finally {
- if (reader != null) {
- reader.close();
- }
- }
- }
- catch (IOException e) {
- m_logService
- .log(LogService.LOG_ERROR, "Could not retrieve gadget XML from url
'" + url.toString() + "'", e);
- }
- m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec '" + url
+ "' failed");
- return null;
- }
-
public String getViewer(final HttpServletRequest request) {
String token = m_tokenProvider.getTokenFromRequest(request);
if (token != null) {
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits