summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorHenri Sara <hesara@vaadin.com>2016-05-05 10:09:08 +0300
committerVaadin Code Review <review@vaadin.com>2016-05-11 10:55:08 +0000
commit091b46e706b091b7da1ddb19753c9cdf61b5cb46 (patch)
tree00fa7d05523f3ff2c337a04cf3c23e336d2943a9 /server
parent5d6f189e323fe551af367eff7be358e0f8ab5ff0 (diff)
downloadvaadin-framework-091b46e706b091b7da1ddb19753c9cdf61b5cb46.tar.gz
vaadin-framework-091b46e706b091b7da1ddb19753c9cdf61b5cb46.zip
Eliminate most server-side Guava use
Change-Id: Iac0f5c96d827e2078ace0be76bc14aa34f379d57
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/data/util/GeneratedPropertyContainer.java28
-rw-r--r--server/src/main/java/com/vaadin/event/SelectionEvent.java26
-rw-r--r--server/src/main/java/com/vaadin/server/VaadinPortletResponse.java38
-rw-r--r--server/src/main/java/com/vaadin/server/VaadinServlet.java43
4 files changed, 105 insertions, 30 deletions
diff --git a/server/src/main/java/com/vaadin/data/util/GeneratedPropertyContainer.java b/server/src/main/java/com/vaadin/data/util/GeneratedPropertyContainer.java
index 98dd3ed0c1..ca33f8098d 100644
--- a/server/src/main/java/com/vaadin/data/util/GeneratedPropertyContainer.java
+++ b/server/src/main/java/com/vaadin/data/util/GeneratedPropertyContainer.java
@@ -27,7 +27,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
-import com.google.gwt.thirdparty.guava.common.collect.Sets;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
@@ -146,10 +145,11 @@ public class GeneratedPropertyContainer extends AbstractContainer implements
@Override
public Collection<?> getItemPropertyIds() {
- Set<?> wrappedProperties = asSet(wrappedItem.getItemPropertyIds());
- return Sets.union(
- Sets.difference(wrappedProperties, removedProperties),
- propertyGenerators.keySet());
+ Set<Object> wrappedProperties = new LinkedHashSet<Object>(
+ wrappedItem.getItemPropertyIds());
+ wrappedProperties.removeAll(removedProperties);
+ wrappedProperties.addAll(propertyGenerators.keySet());
+ return wrappedProperties;
}
@Override
@@ -366,14 +366,6 @@ public class GeneratedPropertyContainer extends AbstractContainer implements
return new GeneratedProperty<T>(item, propertyId, itemId, generator);
}
- private static <T> LinkedHashSet<T> asSet(Collection<T> collection) {
- if (collection instanceof LinkedHashSet) {
- return (LinkedHashSet<T>) collection;
- } else {
- return new LinkedHashSet<T>(collection);
- }
- }
-
/* Listener functionality */
@Override
@@ -618,11 +610,11 @@ public class GeneratedPropertyContainer extends AbstractContainer implements
*/
@Override
public Collection<?> getContainerPropertyIds() {
- Set<?> wrappedProperties = asSet(wrappedContainer
- .getContainerPropertyIds());
- return Sets.union(
- Sets.difference(wrappedProperties, removedProperties),
- propertyGenerators.keySet());
+ Set<Object> wrappedProperties = new LinkedHashSet<Object>(
+ wrappedContainer.getContainerPropertyIds());
+ wrappedProperties.removeAll(removedProperties);
+ wrappedProperties.addAll(propertyGenerators.keySet());
+ return wrappedProperties;
}
/**
diff --git a/server/src/main/java/com/vaadin/event/SelectionEvent.java b/server/src/main/java/com/vaadin/event/SelectionEvent.java
index e75369e6da..09cf57167f 100644
--- a/server/src/main/java/com/vaadin/event/SelectionEvent.java
+++ b/server/src/main/java/com/vaadin/event/SelectionEvent.java
@@ -22,8 +22,6 @@ import java.util.EventObject;
import java.util.LinkedHashSet;
import java.util.Set;
-import com.google.gwt.thirdparty.guava.common.collect.Sets;
-
/**
* An event that specifies what in a selection has changed, and where the
* selection took place.
@@ -52,7 +50,7 @@ public class SelectionEvent extends EventObject {
* @return a Collection of the itemIds that became selected
*/
public Set<Object> getAdded() {
- return Sets.difference(newSelection, oldSelection);
+ return setDifference(newSelection, oldSelection);
}
/**
@@ -64,7 +62,27 @@ public class SelectionEvent extends EventObject {
* @return a Collection of the itemIds that became deselected
*/
public Set<Object> getRemoved() {
- return Sets.difference(oldSelection, newSelection);
+ return setDifference(oldSelection, newSelection);
+ }
+
+ /**
+ * Slightly optimized set difference that can return the original set or a
+ * modified one.
+ *
+ * @param set1
+ * original set
+ * @param set2
+ * the set to subtract
+ * @return the difference set
+ */
+ private static <T> Set<T> setDifference(Set<T> set1, Set<T> set2) {
+ if (set2.isEmpty()) {
+ return set1;
+ } else {
+ LinkedHashSet<T> set = new LinkedHashSet<T>(set1);
+ set.removeAll(set2);
+ return set;
+ }
}
/**
diff --git a/server/src/main/java/com/vaadin/server/VaadinPortletResponse.java b/server/src/main/java/com/vaadin/server/VaadinPortletResponse.java
index 2b6e0c75fb..0e830666e1 100644
--- a/server/src/main/java/com/vaadin/server/VaadinPortletResponse.java
+++ b/server/src/main/java/com/vaadin/server/VaadinPortletResponse.java
@@ -30,8 +30,6 @@ import javax.portlet.PortletResponse;
import javax.portlet.ResourceResponse;
import javax.servlet.http.Cookie;
-import com.google.gwt.thirdparty.guava.common.html.HtmlEscapers;
-
/**
* Wrapper for {@link PortletResponse} and its subclasses.
*
@@ -139,11 +137,45 @@ public class VaadinPortletResponse implements VaadinResponse {
public void sendError(int errorCode, String message) throws IOException {
setStatus(errorCode);
if (message != null) {
- message = HtmlEscapers.htmlEscaper().escape(message);
+ message = escapeHtml(message);
}
getWriter().write(message);
}
+ /**
+ * Perform minimal HTML escaping similar to Guava HtmlEscapers.
+ *
+ * @param input
+ * string to escape
+ * @return minimally escaped HTML safe string
+ */
+ private static String escapeHtml(String input) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < input.length(); i++) {
+ char c = input.charAt(i);
+ switch (c) {
+ case '"':
+ sb.append("&quot;");
+ break;
+ case '\'':
+ sb.append("&#39;");
+ break;
+ case '&':
+ sb.append("&amp;");
+ break;
+ case '<':
+ sb.append("&lt;");
+ break;
+ case '>':
+ sb.append("&gt;");
+ break;
+ default:
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
@Override
public VaadinPortletService getService() {
return vaadinService;
diff --git a/server/src/main/java/com/vaadin/server/VaadinServlet.java b/server/src/main/java/com/vaadin/server/VaadinServlet.java
index cd6e4cd7cd..d50bc8370d 100644
--- a/server/src/main/java/com/vaadin/server/VaadinServlet.java
+++ b/server/src/main/java/com/vaadin/server/VaadinServlet.java
@@ -17,7 +17,9 @@ package com.vaadin.server;
import java.io.BufferedWriter;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -26,10 +28,11 @@ import java.io.PrintWriter;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
-import java.net.URISyntaxException;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -48,8 +51,6 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import com.google.gwt.thirdparty.guava.common.base.Charsets;
-import com.google.gwt.thirdparty.guava.common.io.Files;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.VaadinServletConfiguration.InitParameterName;
import com.vaadin.sass.internal.ScssStylesheet;
@@ -1069,7 +1070,7 @@ public class VaadinServlet extends HttpServlet implements Constants {
return null;
}
- String jsonString = Files.toString(scssCacheFile, Charsets.UTF_8);
+ String jsonString = readFile(scssCacheFile, Charset.forName("UTF-8"));
JsonObject entryJson = Json.parse(jsonString);
@@ -1379,13 +1380,45 @@ public class VaadinServlet extends HttpServlet implements Constants {
String cacheEntryJsonString = cacheEntry.asJson();
try {
- Files.write(cacheEntryJsonString, cacheFile, Charsets.UTF_8);
+ writeFile(cacheEntryJsonString, cacheFile, Charset.forName("UTF-8"));
} catch (IOException e) {
getLogger().log(Level.WARNING,
"Error persisting scss cache " + cacheFile, e);
}
}
+ private static String readFile(File file, Charset charset)
+ throws IOException {
+ InputStream in = new FileInputStream(file);
+ try {
+ // no point in reading files over 2GB to a String
+ byte[] b = new byte[(int) file.length()];
+ int len = b.length;
+ int total = 0;
+
+ while (total < len) {
+ int result = in.read(b, total, len - total);
+ if (result == -1) {
+ break;
+ }
+ total += result;
+ }
+ return new String(b, charset);
+ } finally {
+ in.close();
+ }
+ }
+
+ private static void writeFile(String content, File file, Charset charset)
+ throws IOException {
+ FileOutputStream fos = new FileOutputStream(file);
+ try {
+ fos.write(content.getBytes(charset));
+ } finally {
+ fos.close();
+ }
+ }
+
private static File getScssCacheFile(File scssFile) {
return new File(scssFile.getParentFile(), scssFile.getName() + ".cache");
}