Repository: tajo Updated Branches: refs/heads/master 03801a312 -> 533601eac
TAJO-1286: Remove netty dependency from tajo-jdbc Closes #341 Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/533601ea Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/533601ea Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/533601ea Branch: refs/heads/master Commit: 533601eac6a21251485576ba693635d1650b63a4 Parents: 03801a3 Author: Jihun Kang <[email protected]> Authored: Fri Jan 9 15:51:25 2015 +0900 Committer: Jihun Kang <[email protected]> Committed: Fri Jan 9 15:51:25 2015 +0900 ---------------------------------------------------------------------- CHANGES | 2 + tajo-jdbc/pom.xml | 6 +- .../org/apache/tajo/jdbc/JdbcConnection.java | 3 +- .../tajo/jdbc/util/QueryStringDecoder.java | 139 +++++++++++++++++++ .../tajo/jdbc/util/TestQueryStringDecoder.java | 94 +++++++++++++ 5 files changed, 242 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/533601ea/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 66bb299..0df49b1 100644 --- a/CHANGES +++ b/CHANGES @@ -27,6 +27,8 @@ Release 0.9.1 - unreleased IMPROVEMENT + TAJO-1286: Remove netty dependency from tajo-jdbc. (jihun) + TAJO-1282: Cleanup the relationship of QueryInProgress and QueryJobManager. (hyunsik) http://git-wip-us.apache.org/repos/asf/tajo/blob/533601ea/tajo-jdbc/pom.xml ---------------------------------------------------------------------- diff --git a/tajo-jdbc/pom.xml b/tajo-jdbc/pom.xml index 15439c6..9847416 100644 --- a/tajo-jdbc/pom.xml +++ b/tajo-jdbc/pom.xml @@ -116,7 +116,11 @@ <groupId>org.apache.tajo</groupId> <artifactId>tajo-client</artifactId> </dependency> - + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> http://git-wip-us.apache.org/repos/asf/tajo/blob/533601ea/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/JdbcConnection.java ---------------------------------------------------------------------- diff --git a/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/JdbcConnection.java b/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/JdbcConnection.java index a76443d..f00dc25 100644 --- a/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/JdbcConnection.java +++ b/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/JdbcConnection.java @@ -19,6 +19,7 @@ package org.apache.tajo.jdbc; import com.google.protobuf.ServiceException; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tajo.TajoConstants; @@ -27,7 +28,7 @@ import org.apache.tajo.client.QueryClient; import org.apache.tajo.client.TajoClient; import org.apache.tajo.client.TajoClientImpl; import org.apache.tajo.conf.TajoConf; -import org.jboss.netty.handler.codec.http.QueryStringDecoder; +import org.apache.tajo.jdbc.util.QueryStringDecoder; import java.io.IOException; import java.net.URI; http://git-wip-us.apache.org/repos/asf/tajo/blob/533601ea/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/util/QueryStringDecoder.java ---------------------------------------------------------------------- diff --git a/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/util/QueryStringDecoder.java b/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/util/QueryStringDecoder.java new file mode 100644 index 0000000..9ec9340 --- /dev/null +++ b/tajo-jdbc/src/main/java/org/apache/tajo/jdbc/util/QueryStringDecoder.java @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.jdbc.util; + +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URLDecoder; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class QueryStringDecoder { + + private final Charset charset; + private final String rawUri; + private String queries; + private Map<String, List<String>> params; + + public QueryStringDecoder(String rawUri) { + this(rawUri, Charset.defaultCharset()); + } + + public QueryStringDecoder(String rawUri, Charset charset) { + this.rawUri = rawUri; + this.charset = charset; + } + + private void splitUri() { + if (rawUri != null && !rawUri.isEmpty()) { + int pathPos = rawUri.indexOf('?'); + if (pathPos < 0) { + queries = ""; + } else { + if ((pathPos + 1) > rawUri.length()) { + queries = ""; + } else { + queries = rawUri.substring(pathPos + 1); + } + } + } + } + + protected void decodeParams() throws MalformedURLException, UnsupportedEncodingException { + params = new HashMap<String, List<String>>(); + String queries = getQueries(); + + if (queries != null && !queries.isEmpty()) { + char c = 0; + int startPos = 0; + String name = null, value = null; + + for (int index = 0; index < queries.length(); index++) { + c = queries.charAt(index); + if (c == '=') { + name = queries.substring(startPos, index); + if (name.isEmpty()) { + throw new MalformedURLException(rawUri + " is not a valid URL."); + } + name = decodeString(name); + startPos = index+1; + } else if (c == '&') { + if (name == null || name.isEmpty()) { + throw new MalformedURLException(rawUri + " is not a valid URL."); + } + value = queries.substring(startPos, index); + if (value.isEmpty()) { + throw new MalformedURLException(rawUri + " is not a valid URL."); + } + value = decodeString(value); + addParameter(name, value); + startPos = index+1; + } + } + + if (startPos > 0 && name != null && !name.isEmpty()) { + value = queries.substring(startPos); + value = decodeString(value); + addParameter(name, value); + } + } + } + + protected String decodeString(String string) throws UnsupportedEncodingException { + String decoded = ""; + + if (string != null && !string.isEmpty()) { + decoded = URLDecoder.decode(string, charset.name()); + } + + return decoded; + } + + protected void addParameter(String name, String value) { + List<String> valueList = params.get(name); + + if (valueList == null) { + valueList = new ArrayList<String>(); + params.put(name, valueList); + } + + valueList.add(value); + } + + public String getRawUri() { + return rawUri; + } + + public String getQueries() { + if (queries == null || queries.isEmpty()) { + splitUri(); + } + return queries; + } + + public Map<String, List<String>> getParameters() throws MalformedURLException, UnsupportedEncodingException { + if (params == null || params.size() <= 0) { + decodeParams(); + } + return params; + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/533601ea/tajo-jdbc/src/test/java/org/apache/tajo/jdbc/util/TestQueryStringDecoder.java ---------------------------------------------------------------------- diff --git a/tajo-jdbc/src/test/java/org/apache/tajo/jdbc/util/TestQueryStringDecoder.java b/tajo-jdbc/src/test/java/org/apache/tajo/jdbc/util/TestQueryStringDecoder.java new file mode 100644 index 0000000..31a09d5 --- /dev/null +++ b/tajo-jdbc/src/test/java/org/apache/tajo/jdbc/util/TestQueryStringDecoder.java @@ -0,0 +1,94 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.jdbc.util; + +import java.net.MalformedURLException; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; + +public class TestQueryStringDecoder { + + @Test + public void testEmptyQuery() throws Exception { + QueryStringDecoder decoder = null; + String rawUriStr = ""; + + rawUriStr = "http://127.0.0.1:26002/"; + decoder = new QueryStringDecoder(rawUriStr); + assertThat(decoder.getQueries(), is(notNullValue())); + assertThat(decoder.getParameters(), is(notNullValue())); + assertThat(decoder.getParameters().size(), is(0)); + + rawUriStr = "/test_path/test2?"; + decoder = new QueryStringDecoder(rawUriStr); + assertThat(decoder.getQueries(), is(notNullValue())); + assertThat(decoder.getParameters(), is(notNullValue())); + assertThat(decoder.getParameters().size(), is(0)); + } + + @Test + public void testSingleQueries() throws Exception { + QueryStringDecoder decoder = null; + String rawUriStr = ""; + + rawUriStr = "http://127.0.0.1:26200/?qid=1234&tid=2345&pid=4567"; + decoder = new QueryStringDecoder(rawUriStr); + assertThat(decoder.getQueries(), is("qid=1234&tid=2345&pid=4567")); + assertThat(decoder.getParameters(), is(notNullValue())); + assertThat(decoder.getParameters().size(), is(3)); + assertThat(decoder.getParameters().get("qid").get(0), is("1234")); + assertThat(decoder.getParameters().get("pid").get(0), is("4567")); + + rawUriStr = "http://127.0.0.1:26200/?tid=2345"; + decoder = new QueryStringDecoder(rawUriStr); + assertThat(decoder.getQueries(), is("tid=2345")); + assertThat(decoder.getParameters(), is(notNullValue())); + assertThat(decoder.getParameters().size(), is(1)); + assertThat(decoder.getParameters().get("tid").get(0), is("2345")); + } + + @Test + public void testMultipleQueries() throws Exception { + QueryStringDecoder decoder = null; + String rawUriStr = ""; + + rawUriStr = "http://127.0.0.1:26200/?qid=1234&tid=2345&pid=4567&tid=4890"; + decoder = new QueryStringDecoder(rawUriStr); + assertThat(decoder.getQueries(), is("qid=1234&tid=2345&pid=4567&tid=4890")); + assertThat(decoder.getParameters(), is(notNullValue())); + assertThat(decoder.getParameters().size(), is(3)); + assertThat(decoder.getParameters().get("tid").size(), is(2)); + assertThat(decoder.getParameters().get("tid").get(0), is("2345")); + assertThat(decoder.getParameters().get("tid").get(1), is("4890")); + } + + @Test(expected=MalformedURLException.class) + public void testMalformedURI() throws Exception { + QueryStringDecoder decoder = null; + String rawUriStr = ""; + + rawUriStr = "http://127.0.0.1:26200/?=1234&tid=&pid=4567"; + decoder = new QueryStringDecoder(rawUriStr); + assertThat(decoder.getQueries(), is("=1234&tid=&pid=4567")); + decoder.getParameters(); + } +}
