# fix for ignite-167
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/53d1b805 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/53d1b805 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/53d1b805 Branch: refs/heads/ignite-160 Commit: 53d1b8051a014e533a515524fb7a09839f2a420d Parents: 878a289 Author: Yakov Zhdanov <yzhda...@gridgain.com> Authored: Wed Feb 4 16:38:48 2015 +0300 Committer: Yakov Zhdanov <yzhda...@gridgain.com> Committed: Wed Feb 4 16:38:48 2015 +0300 ---------------------------------------------------------------------- .../client/ClientDefaultCacheSelfTest.java | 70 +++++++++++++++++++- .../http/jetty/GridJettyRestHandler.java | 17 +++++ 2 files changed, 85 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/53d1b805/modules/clients/src/test/java/org/apache/ignite/client/ClientDefaultCacheSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/client/ClientDefaultCacheSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/client/ClientDefaultCacheSelfTest.java index e996c5e..8452a99 100644 --- a/modules/clients/src/test/java/org/apache/ignite/client/ClientDefaultCacheSelfTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/client/ClientDefaultCacheSelfTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.client; +import org.apache.ignite.cache.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.lang.*; @@ -25,6 +26,9 @@ import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; import org.apache.ignite.testframework.junits.common.*; +import java.io.*; +import java.net.*; +import java.nio.charset.*; import java.util.*; import static org.apache.ignite.IgniteSystemProperties.*; @@ -52,6 +56,15 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest { /** Http port. */ private static final int HTTP_PORT = 8081; + /** Url address to send HTTP request. */ + private static final String TEST_URL = "http://" + HOST + ":" + HTTP_PORT + "/gridgain"; + + /** Used to sent request charset. */ + private static final String CHARSET = StandardCharsets.UTF_8.name(); + + /** Name of node local cache. */ + private static final String LOCAL_CACHE = "local"; + /** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { System.setProperty(IGNITE_JETTY_PORT, String.valueOf(HTTP_PORT)); @@ -63,7 +76,7 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest { @Override protected void afterTestsStopped() throws Exception { stopGrid(); - System.clearProperty (IGNITE_JETTY_PORT); + System.clearProperty(IGNITE_JETTY_PORT); } /** {@inheritDoc} */ @@ -89,7 +102,15 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest { cfg.setDiscoverySpi(disco); - cfg.setCacheConfiguration(defaultCacheConfiguration()); + CacheConfiguration cLocal = new CacheConfiguration(); + + cLocal.setName(LOCAL_CACHE); + + cLocal.setCacheMode(CacheMode.LOCAL); + + cLocal.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); + + cfg.setCacheConfiguration(defaultCacheConfiguration(), cLocal); return cfg; } @@ -132,6 +153,34 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest { return srvs; } + /* + * Send HTTP request to Jetty server of node and process result. + * + * @param query Send query parameters. + * @return Processed response string. + */ + private String sendHttp(String query) { + String res = "No result"; + + try { + URLConnection connection = new URL(TEST_URL + "?" + query).openConnection(); + + connection.setRequestProperty("Accept-Charset", CHARSET); + + BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + res = r.readLine(); + + r.close(); + } + catch (IOException e) { + error("Failed to send HTTP request: " + TEST_URL + "?" + query, e); + } + + // Cut node id from response. + return res.substring(res.indexOf("\"response\"")); + } + /** * @throws Exception If failed. */ @@ -153,4 +202,21 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest { GridClientFactory.stopAll(); } } + + /** + * Json format string in cache should not transform to Json object on get request. + */ + public void testSkipString2JsonTransformation() { + // Put to cache JSON format string value. + assertEquals("Incorrect query response", "\"response\":true,\"sessionToken\":\"\",\"successStatus\":0}", + sendHttp("cmd=put&cacheName=" + LOCAL_CACHE + + "&key=a&val=%7B%22v%22%3A%22my%20Value%22%2C%22t%22%3A1422559650154%7D")); + + // Escape '\' symbols disappear from response string on transformation to JSON object. + assertEquals( + "Incorrect query response", + "\"response\":\"{\\\"v\\\":\\\"my Value\\\",\\\"t\\\":1422559650154}\"," + + "\"sessionToken\":\"\",\"successStatus\":0}", + sendHttp("cmd=get&cacheName=" + LOCAL_CACHE + "&key=a")); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/53d1b805/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java ---------------------------------------------------------------------- diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java index 731a532..cdbf195 100644 --- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java +++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java @@ -18,6 +18,7 @@ package org.apache.ignite.internal.processors.rest.protocols.http.jetty; import net.sf.json.*; +import net.sf.json.processors.*; import org.apache.ignite.*; import org.apache.ignite.internal.processors.rest.*; import org.apache.ignite.internal.processors.rest.request.*; @@ -43,6 +44,17 @@ import static org.apache.ignite.internal.processors.rest.GridRestResponse.*; * {@code /gridgain?cmd=cmdName¶m1=abc¶m2=123} */ public class GridJettyRestHandler extends AbstractHandler { + /** JSON value processor that does not transform input object. */ + private static final JsonValueProcessor SKIP_STR_VAL_PROC = new JsonValueProcessor() { + @Override public Object processArrayValue(Object o, JsonConfig jsonConfig) { + return o; + } + + @Override public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) { + return o; + } + }; + /** Logger. */ private final IgniteLogger log; @@ -262,6 +274,11 @@ public class GridJettyRestHandler extends AbstractHandler { } JsonConfig cfg = new GridJettyJsonConfig(); + + // Workaround for not needed transformation of string into JSON object. + if (cmdRes.getResponse() instanceof String) + cfg.registerJsonValueProcessor(cmdRes.getClass(), "response", SKIP_STR_VAL_PROC); + JSON json; try {