Skip to content

Commit

Permalink
Remove GSON from CrossDomainRpcLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed May 18, 2018
1 parent c218813 commit c2d3fa8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

package org.openqa.selenium.remote.server.xdrpc;

import com.google.common.io.ByteStreams;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.MAP_TYPE;

import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.json.JsonInput;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

Expand All @@ -33,6 +38,8 @@
*/
public class CrossDomainRpcLoader {

private final Json json = new Json();

/**
* Parses the request for a CrossDomainRpc.
*
Expand All @@ -43,36 +50,40 @@ public class CrossDomainRpcLoader {
* data.
*/
public CrossDomainRpc loadRpc(HttpServletRequest request) throws IOException {
JsonObject json;
InputStream stream = null;
Charset encoding;
try {
stream = request.getInputStream();
byte[] data = ByteStreams.toByteArray(stream);
json = new JsonParser().parse(new String(data, StandardCharsets.UTF_8)).getAsJsonObject();
} catch (JsonSyntaxException e) {
String enc = request.getCharacterEncoding();
encoding = Charset.forName(enc);
} catch (IllegalArgumentException | NullPointerException e) {
encoding = UTF_8;
}

// We tend to look at the input stream, rather than the reader.
try (InputStream in = request.getInputStream();
Reader reader = new InputStreamReader(in, encoding);
JsonInput jsonInput = json.newInput(reader)) {
Map<String, Object> read = jsonInput.read(MAP_TYPE);

return new CrossDomainRpc(
getField(read, Field.METHOD),
getField(read, Field.PATH),
getField(read, Field.DATA));
} catch (JsonException e) {
throw new IllegalArgumentException(
"Failed to parse JSON request: " + e.getMessage(), e);
} finally {
if (stream != null) {
stream.close();
}
}

return new CrossDomainRpc(
getField(json, Field.METHOD),
getField(json, Field.PATH),
getField(json, Field.DATA));
}

private String getField(JsonObject json, String key) {
if (!json.has(key) || json.get(key).isJsonNull()) {
private String getField(Map<String, Object> json, String key) {
if (json.get(key) == null) {
throw new IllegalArgumentException("Missing required parameter: " + key);
}

if (json.get(key).isJsonPrimitive() && json.get(key).getAsJsonPrimitive().isString()) {
return json.get(key).getAsString();
if (json.get(key) instanceof String) {
return json.get(key).toString();
}
return json.get(key).toString();

return this.json.toJson(json.get(key));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -38,10 +36,6 @@
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

/**
* Unit tests for {@link CrossDomainRpcLoader}.
*/
@RunWith(JUnit4.class)
public class CrossDomainRpcLoaderTest {

private HttpServletRequest mockRequest;
Expand Down Expand Up @@ -88,7 +82,7 @@ public void rpcRequestDataInitializedWithDataAsAString() throws IOException {
HttpServletRequest mockRequest = createJsonRequest("GET", "/", json);

CrossDomainRpc rpc = new CrossDomainRpcLoader().loadRpc(mockRequest);
assertEquals("{\"foo\":\"bar\"}", rpc.getData());
assertEquals("{\"foo\": \"bar\"}", rpc.getData());
}

private HttpServletRequest createJsonRequest(final String method,
Expand Down
16 changes: 15 additions & 1 deletion java/server/test/org/openqa/testing/FakeHttpServletRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

package org.openqa.testing;

import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.net.MediaType;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -217,7 +222,16 @@ public Enumeration<String> getAttributeNames() {
}

public String getCharacterEncoding() {
throw new UnsupportedOperationException();
try {
String contentType = getHeader(CONTENT_TYPE);
if (contentType != null) {
MediaType mediaType = MediaType.parse(contentType);
return mediaType.charset().or(UTF_8).toString();
}
} catch (IllegalArgumentException ignored) {
// Do nothing.
}
return UTF_8.toString();
}

public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
Expand Down

0 comments on commit c2d3fa8

Please sign in to comment.