diff --git a/.gitignore b/.gitignore index d96023ed..6280c868 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ release.properties .settings .project .classpath +/oss-java-client.iml +/.idea/ diff --git a/pom.xml b/pom.xml index 435b38d2..0038d89d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,150 +1,146 @@ - 4.0.0 - com.opensearchserver - oss-java-client - OpenSearchServer Java Client - jar - - 3.2.1 - - https://github.com/opensearchserver/oss-java-client - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - 1.0.1-SNAPSHOT - - OpenSearchServer Inc. - http://www.opensearchserver.com - - - scm:git:git@github.com:opensearchserver/oss-java-client.git - scm:git:git@github.com:opensearchserver/oss-java-client.git - git@github.com:opensearchserver/oss-java-client.git - - - https://github.com/opensearchserver/oss-java-client/issues - GIT - - - http://www.open-search-server.com/jenkins - Jenkins - - - org.sonatype.oss - oss-parent - 7 - - - UTF-8 - UTF-8 - true - - - - oss-sonatype - oss-sonatype - https://oss.sonatype.org/content/repositories/snapshots/ - - true - - - - - - org.apache.httpcomponents - fluent-hc - 4.4 - - - com.qwazr - qwazr-utils - 1.0.1-SNAPSHOT - - - junit - junit - 4.12 - test - - - com.cybozu.labs - langdetect - 1.1-20120112 - - - - ${project.artifactId}-${project.version} - - - src/main/resources - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.2 - - 1.7 - 1.7 - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.1 - - true - protected - true - - http://github.com:opensearchserver/oss-java-client - http://www.opensearchserver.com/ - - - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - ${skipTests} - - - - org.apache.maven.surefire - surefire-junit47 - 2.18.1 - - - - - org.apache.maven.plugins - maven-scm-publish-plugin - 1.1 - - ${project.build.directory}/scmpublish - Publishing javadoc for - ${project.artifactId}:${project.version} - ${project.reporting.outputDirectory}/apidocs - true - scm:git:git@github.com:opensearchserver/oss-java-client.git - gh-pages - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.opensearchserver + oss-java-client + OpenSearchServer Java Client + jar + + 3.2.1 + + https://github.com/jaeksoft/oss-java-client + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + 1.0.1-SNAPSHOT + + OpenSearchServer Inc. + http://www.opensearchserver.com + + + scm:git:git@github.com:jaeksoft/oss-java-client.git + scm:git:git@github.com:jaeksoft/oss-java-client.git + git@github.com:jaeksoft/oss-java-client.git + + + https://github.com/jaeksoft/oss-java-client/issues + GIT + + + http://www.open-search-server.com/jenkins + Jenkins + + + org.sonatype.oss + oss-parent + 7 + + + UTF-8 + UTF-8 + true + + + + oss-sonatype + oss-sonatype + https://oss.sonatype.org/content/repositories/snapshots/ + + true + + + + + + com.qwazr + qwazr-utils + 1.0.1-SNAPSHOT + + + junit + junit + 4.12 + test + + + com.cybozu.labs + langdetect + 1.1-20120112 + + + + ${project.artifactId}-${project.version} + + + src/main/resources + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.1 + + true + protected + true + + http://github.com:jaeksoft/oss-java-client + http://www.opensearchserver.com/ + + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + ${skipTests} + + + + org.apache.maven.surefire + surefire-junit47 + 2.18.1 + + + + + org.apache.maven.plugins + maven-scm-publish-plugin + 1.1 + + ${project.build.directory}/scmpublish + Publishing javadoc for + ${project.artifactId}:${project.version} + + ${project.reporting.outputDirectory}/apidocs + true + scm:git:git@github.com:jaeksoft/oss-java-client.git + gh-pages + + + + diff --git a/src/main/java/com/opensearchserver/client/JsonClient1.java b/src/main/java/com/opensearchserver/client/JsonClient1.java index 60f0d3ef..bd1f6fe9 100644 --- a/src/main/java/com/opensearchserver/client/JsonClient1.java +++ b/src/main/java/com/opensearchserver/client/JsonClient1.java @@ -1,12 +1,12 @@ /** * Copyright 2014-2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,49 +15,71 @@ */ package com.opensearchserver.client; -import java.net.URISyntaxException; -import java.net.URI; +import com.qwazr.utils.LinkUtils; import com.qwazr.utils.StringUtils; +import com.qwazr.utils.UBuilder; +import com.qwazr.utils.http.HttpRequest; +import com.qwazr.utils.http.ResponseValidator; import com.qwazr.utils.json.client.JsonClientAbstract; import com.qwazr.utils.server.RemoteService; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; + /** * This client is for OpenSearchServer v1.5.x - * */ public class JsonClient1 extends JsonClientAbstract { + private final String baseUrl; private final String login; private final String key; - public JsonClient1(String url, String login, String key, int msTimeout) + public JsonClient1(final String baseUrl, final String login, final String key, int msTimeout) throws URISyntaxException { - super(new RemoteService(new URI(url), msTimeout)); + super(new RemoteService(new URI(baseUrl), msTimeout)); + this.baseUrl = baseUrl + "/services/rest/"; this.login = login; this.key = key; } - public JsonClient1(ServerResource serverResource) throws URISyntaxException { - this(serverResource.url, serverResource.login, serverResource.api_key, - serverResource.time_out); + public JsonClient1(final ServerResource serverResource) throws URISyntaxException { + this(serverResource.url, serverResource.login, serverResource.api_key, serverResource.time_out); } /** * Build an URL for OpenSearchServer 1.5 - * - * @param paths - * an array of path which will be concatened + * + * @param paths an array of path which will be concatened * @return a prepared URIBuilder - * @throws URISyntaxException - * if the builded URI is not valid + * @throws URISyntaxException if the builded URI is not valid */ - final public UBuilder getBaseUrl(String... paths) throws URISyntaxException { - UBuilder uriBuilder = new UBuilder(StringUtils.fastConcat( - "/services/rest/", paths)); + final public UBuilder getBaseUrl(final String... paths) throws URISyntaxException, UnsupportedEncodingException { + final StringBuilder sb = new StringBuilder(baseUrl); + if (paths != null) + for (String path : paths) { + if (path.indexOf('/') == -1) + sb.append(LinkUtils.UTF8_URL_Encode(path)); + else + sb.append(path); + } + final UBuilder uriBuilder = new UBuilder(sb.toString()); if (!StringUtils.isEmpty(login)) uriBuilder.addParameter("login", login); if (!StringUtils.isEmpty(key)) uriBuilder.addParameter("key", key); return uriBuilder; } + + final public boolean execute200True404False(final HttpRequest request, final Object bodyObject, + final Integer msTimeOut, final ResponseValidator validator) { + switch (executeStatusCode(request, bodyObject, msTimeOut, validator)) { + case 200: + return true; + case 404: + return false; + } + return false; + } } diff --git a/src/main/java/com/opensearchserver/client/common/AbstractApi.java b/src/main/java/com/opensearchserver/client/common/AbstractApi.java index f747a0cb..c15b9d46 100644 --- a/src/main/java/com/opensearchserver/client/common/AbstractApi.java +++ b/src/main/java/com/opensearchserver/client/common/AbstractApi.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,13 +15,19 @@ */ package com.opensearchserver.client.common; +import com.qwazr.utils.http.ResponseValidator; import com.qwazr.utils.json.client.JsonClientAbstract; public class AbstractApi { + protected final static ResponseValidator validator_200 = ResponseValidator.create().status(200); + protected final static ResponseValidator validator_200_201 = ResponseValidator.create().status(200, 201); + protected final static ResponseValidator validator_200_404 = ResponseValidator.create().status(200, 404); + protected final T client; public AbstractApi(T client) { this.client = client; } + } diff --git a/src/main/java/com/opensearchserver/client/v1/AnalyzerApi1.java b/src/main/java/com/opensearchserver/client/v1/AnalyzerApi1.java index 77ac58db..001e772e 100644 --- a/src/main/java/com/opensearchserver/client/v1/AnalyzerApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/AnalyzerApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,18 +15,15 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.common.LanguageEnum; import com.opensearchserver.client.common.analyzer.AnalyzerItem; -import com.qwazr.utils.http.HttpUtils; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; public class AnalyzerApi1 extends AbstractApi { @@ -36,58 +33,38 @@ public AnalyzerApi1(JsonClient1 client) { /** * Check if an analyzer exists in an index. - * - * @param indexName - * The name of the index. - * @param analyzerName - * The name of the analyzer. - * @param analyzerLang - * Lang of the analyzer. + * + * @param indexName The name of the index. + * @param analyzerName The name of the analyzer. + * @param analyzerLang Lang of the analyzer. * @return true if the analyzer exists, false if not. - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public boolean checkAnalyzerExists(String indexName, String analyzerName, - LanguageEnum analyzerLang) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/analyzer/", analyzerName, "/lang/", analyzerLang.toString()); - Request request = Request.Get(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - switch (HttpUtils.checkStatusCodes(response, 200, 404)) { - case 200: - return true; - case 404: - return false; - } - return false; + public boolean checkAnalyzerExists(String indexName, String analyzerName, LanguageEnum analyzerLang) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = + client.getBaseUrl("index/", indexName, "/analyzer/", analyzerName, "/lang/", analyzerLang.toString()); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.execute200True404False(request, null, null, validator_200_404); } /** * Create an analyzer - * - * @param indexName - * The name of the index - * @param analyzer - * The analyzer to create - * @param analyzerName - * Name of the analyzer to create - * @param analyzerLang - * Lang of the analyzer to create - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param analyzer The analyzer to create + * @param analyzerName Name of the analyzer to create + * @param analyzerLang Lang of the analyzer to create + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void createAnalyzer(String indexName, AnalyzerItem analyzer, - String analyzerName, LanguageEnum analyzerLang) throws IOException, - URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/analyzer/", analyzerName, "/lang/", analyzerLang.toString()); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, analyzer, null); - HttpUtils.checkStatusCodes(response, 200); + public void createAnalyzer(String indexName, AnalyzerItem analyzer, String analyzerName, LanguageEnum analyzerLang) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = + client.getBaseUrl("index/", indexName, "/analyzer/", analyzerName, "/lang/", analyzerLang.toString()); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, analyzer, null, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/DictionaryApi1.java b/src/main/java/com/opensearchserver/client/v1/DictionaryApi1.java index d9105fa2..8dc4edb9 100644 --- a/src/main/java/com/opensearchserver/client/v1/DictionaryApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/DictionaryApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,16 +15,13 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; -import com.qwazr.utils.http.HttpUtils; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; public class DictionaryApi1 extends AbstractApi { @@ -34,145 +31,91 @@ public DictionaryApi1(JsonClient1 client) { /** * Create a list of synonyms - * - * @param indexName - * The name of the index - * @param listName - * Name of the list - * @param listSynonyms - * List of synonyms, one set of synonyms by line, raw text - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param listName Name of the list + * @param listSynonyms List of synonyms, one set of synonyms by line, raw text + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void createSynonymsList(String indexName, String listName, - String listSynonyms) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/synonyms/", listName); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, listSynonyms, null); - HttpUtils.checkStatusCodes(response, 200); + public void createSynonymsList(String indexName, String listName, String listSynonyms) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/synonyms/", listName); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, listSynonyms, null, validator_200); } /** * Check if a list of synonyms exists. - * - * @param indexName - * The name of the index. - * @param listName - * The name of the list. + * + * @param indexName The name of the index. + * @param listName The name of the list. * @return true if the analyzer exists, false if not. - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public boolean checkSynonymsListExists(String indexName, String listName) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/synonyms/", listName); - Request request = Request.Head(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - switch (HttpUtils.checkStatusCodes(response, 200, 404)) { - case 200: - return true; - case 404: - return false; - } - return false; + public boolean checkSynonymsListExists(String indexName, String listName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/synonyms/", listName); + final HttpRequest request = HttpRequest.Head(uriBuilder.build()); + return client.execute200True404False(request, null, null, validator_200_404); } /** * Delete a synonym list. - * - * @param indexName - * The name of the index. - * @param listName - * The name of the list. - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index. + * @param listName The name of the list. + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void deleteSynonymsList(String indexName, String listName) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/synonyms/", listName); - Request request = Request.Delete(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200); + public void deleteSynonymsList(String indexName, String listName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/synonyms/", listName); + final HttpRequest request = HttpRequest.Delete(uriBuilder.build()); + client.execute(request, null, null, validator_200); } /** * Create a list of stopwords - * - * @param indexName - * The name of the index - * @param listName - * Name of the list - * @param stopwordsList - * List of synonyms, one set of synonyms by line, raw text - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param listName Name of the list + * @param stopwordsList List of synonyms, one set of synonyms by line, raw text + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void createStopWordsList(String indexName, String listName, - String stopwordsList) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/stopwords/", listName); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, stopwordsList, null); - HttpUtils.checkStatusCodes(response, 200); + public void createStopWordsList(String indexName, String listName, String stopwordsList) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/stopwords/", listName); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, stopwordsList, null, validator_200); } /** * Check if a list of stopwords exists. - * - * @param indexName - * The name of the index. - * @param listName - * The name of the list. + * + * @param indexName The name of the index. + * @param listName The name of the list. * @return true if the analyzer exists, false if not. - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public boolean checkStopWordsListExists(String indexName, String listName) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/stopwords/", listName); - Request request = Request.Head(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - switch (HttpUtils.checkStatusCodes(response, 200, 404)) { - case 200: - return true; - case 404: - return false; - } - return false; + public boolean checkStopWordsListExists(String indexName, String listName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/stopwords/", listName); + final HttpRequest request = HttpRequest.Head(uriBuilder.build()); + return client.execute200True404False(request, null, null, validator_200_404); } /** * Delete a stopwords list. - * - * @param indexName - * The name of the index. - * @param listName - * The name of the list. - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index. + * @param listName The name of the list. + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void deleteStopWordsList(String indexName, String listName) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/stopwords/", listName); - Request request = Request.Delete(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200); + public void deleteStopWordsList(String indexName, String listName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/stopwords/", listName); + final HttpRequest request = HttpRequest.Delete(uriBuilder.build()); + client.execute(request, null, null, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/DocumentApi1.java b/src/main/java/com/opensearchserver/client/v1/DocumentApi1.java index 5335b359..bf7b0ccc 100644 --- a/src/main/java/com/opensearchserver/client/v1/DocumentApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/DocumentApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,16 +15,15 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.common.search.query.DocumentsQuery; import com.opensearchserver.client.v1.search.DocumentsResult1; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; /** * @version For OpenSearchServer v1.5.x @@ -37,24 +36,18 @@ public DocumentApi1(JsonClient1 client) { /** * Create or update a search field template - * - * @param indexName - * The name of the index - * @param query - * The query + * + * @param indexName The name of the index + * @param query The query * @return the current instance - * @throws IOException - * if any I/O error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any I/O error occurs + * @throws URISyntaxException if the URI is not valid */ - public DocumentsResult1 documentsSearch(String indexName, - DocumentsQuery query) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/documents"); - Request request = Request.Post(uriBuilder.build()); - return client - .execute(request, query, null, DocumentsResult1.class, 200); + public DocumentsResult1 documentsSearch(String indexName, DocumentsQuery query) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/documents"); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + return client.executeJson(request, query, null, DocumentsResult1.class, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/FieldApi1.java b/src/main/java/com/opensearchserver/client/v1/FieldApi1.java index 8822e3da..35708ef3 100644 --- a/src/main/java/com/opensearchserver/client/v1/FieldApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/FieldApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,20 +15,17 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.List; - -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.v1.field.ResultField; import com.opensearchserver.client.v1.field.ResultFieldList; import com.opensearchserver.client.v1.field.SchemaField; -import com.qwazr.utils.http.HttpUtils; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.List; public class FieldApi1 extends AbstractApi { @@ -38,130 +35,91 @@ public FieldApi1(JsonClient1 client) { /** * Get one field - * - * @param indexName - * The index name - * @param fieldName - * The field name + * + * @param indexName The index name + * @param fieldName The field name * @return The field details - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public ResultField getField(String indexName, String fieldName) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/field/", fieldName); - Request request = Request.Get(uriBuilder.build()); - return client.execute(request, null, null, ResultField.class, 200, 404); + public ResultField getField(String indexName, String fieldName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/field/", fieldName); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.executeJson(request, null, null, ResultField.class, validator_200_404); } /** * Get the field list - * - * @param indexName - * The index name + * + * @param indexName The index name * @return A list of fields - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public ResultFieldList getFields(String indexName) throws IOException, - URISyntaxException { - URIBuilder uriBuilder = client - .getBaseUrl("index/", indexName, "/field"); - Request request = Request.Get(uriBuilder.build()); - return client.execute(request, null, null, ResultFieldList.class, 200); + public ResultFieldList getFields(String indexName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/field"); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.executeJson(request, null, null, ResultFieldList.class, validator_200); } /** * Create or replace a field - * - * @param indexName - * The name of the index - * @param field - * The field to create or update - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param field The field to create or update + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void setField(String indexName, SchemaField field) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/field/" + field.name); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, field, null); - HttpUtils.checkStatusCodes(response, 200); + public void setField(String indexName, SchemaField field) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/field/" + field.name); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, field, null, validator_200); } /** * Create or update a set of fields - * - * @param indexName - * The name of the index - * @param fields - * A list of fields - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param fields A list of fields + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void setFields(String indexName, List fields) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client - .getBaseUrl("index/", indexName, "/field"); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, fields, null); - HttpUtils.checkStatusCodes(response, 200); + public void setFields(String indexName, List fields) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/field"); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, fields, null, validator_200); } /** * Delete a field from a schema - * - * @param indexName - * The name of the index - * @param fieldName - * The name of the field to delete - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param fieldName The name of the field to delete + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void deleteField(String indexName, String fieldName) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/field/", fieldName); - Request request = Request.Delete(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200, 404); + public void deleteField(String indexName, String fieldName) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/field/", fieldName); + final HttpRequest request = HttpRequest.Delete(uriBuilder.build()); + client.execute(request, null, null, validator_200_404); } /** * Set the default and unique field - * - * @param indexName - * The name of the index - * @param defaultField - * The name of the default field - * @param uniqueField - * The name of the unique field - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param defaultField The name of the default field + * @param uniqueField The name of the unique field + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void setDefaultUniqueField(String indexName, String defaultField, - String uniqueField) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client - .getBaseUrl("index/", indexName, "/field") + public void setDefaultUniqueField(String indexName, String defaultField, String uniqueField) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/field") .addParameter("default", defaultField) .addParameter("unique", uniqueField); - Request request = Request.Post(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + client.execute(request, null, null, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/IndexApi1.java b/src/main/java/com/opensearchserver/client/v1/IndexApi1.java index 046d1f08..d6e69ec2 100644 --- a/src/main/java/com/opensearchserver/client/v1/IndexApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/IndexApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,17 +15,14 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.common.index.TemplateEnum; -import com.qwazr.utils.http.HttpUtils; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; public class IndexApi1 extends AbstractApi { @@ -35,72 +32,49 @@ public IndexApi1(JsonClient1 client) { /** * Create a new index - * - * @param indexName - * The name of the index - * @param template - * The template - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param template The template + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void createIndex(String indexName, TemplateEnum template) - throws IOException, URISyntaxException { + public void createIndex(String indexName, TemplateEnum template) throws IOException, URISyntaxException { if (indexName == null || indexName.isEmpty()) throw new IllegalArgumentException("The index name is missing."); - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/template/", template.name()); - Request request = Request.Post(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200); + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/template/", template.name()); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + client.execute(request, null, null, validator_200); } /** * Check if the index exists. - * - * @param indexName - * The name of the index. + * + * @param indexName The name of the index. * @return true if the index exists, false if not. - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public boolean indexExists(String indexName) throws URISyntaxException, - IOException { + public boolean indexExists(String indexName) throws URISyntaxException, IOException { if (indexName == null || indexName.isEmpty()) throw new IllegalArgumentException("The index name is missing."); - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName); - Request request = Request.Get(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - switch (HttpUtils.checkStatusCodes(response, 200, 404)) { - case 200: - return true; - case 404: - return false; - } - return false; + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.execute200True404False(request, null, null, validator_200_404); } /** * Delete index - * - * @param indexName - * The name of the index - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void deleteIndex(String indexName) throws IOException, - URISyntaxException { + public void deleteIndex(String indexName) throws IOException, URISyntaxException { if (indexName == null || indexName.isEmpty()) throw new IllegalArgumentException("The index name is missing."); - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName); - Request request = Request.Delete(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200); + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName); + final HttpRequest request = HttpRequest.Delete(uriBuilder.build()); + client.execute(request, null, null, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/ReplicationApi1.java b/src/main/java/com/opensearchserver/client/v1/ReplicationApi1.java index b7997fa3..8b47f45a 100644 --- a/src/main/java/com/opensearchserver/client/v1/ReplicationApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/ReplicationApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,17 +15,16 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.common.CommonResult; import com.opensearchserver.client.v1.replication.ReplicationItem; import com.opensearchserver.client.v1.replication.ReplicationResult; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; /** * @version For OpenSearchServer v1.5.x @@ -38,89 +37,69 @@ public ReplicationApi1(JsonClient1 client) { /** * Retrieve a replication item - * - * @param indexName - * the name of the index - * @param replicationName - * the name of the replication item + * + * @param indexName the name of the index + * @param replicationName the name of the replication item * @return a replication item - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public ReplicationResult getReplication(String indexName, - String replicationName) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/replication").addParameter("name", replicationName); - Request request = Request.Get(uriBuilder.build()); - return client - .execute(request, null, null, ReplicationResult.class, 200); + public ReplicationResult getReplication(String indexName, String replicationName) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = + client.getBaseUrl("index/", indexName, "/replication").addParameter("name", replicationName); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.executeJson(request, null, null, ReplicationResult.class, validator_200); } /** * Create or update a replication item - * - * @param indexName - * the name of the index - * @param replicationItem - * the replication item + * + * @param indexName the name of the index + * @param replicationItem the replication item * @return the status - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public CommonResult createUpdateReplication(String indexName, - ReplicationItem replicationItem) throws IOException, - URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/replication"); - Request request = Request.Put(uriBuilder.build()); - return client.execute(request, replicationItem, null, - CommonResult.class, 200, 201); + public CommonResult createUpdateReplication(String indexName, ReplicationItem replicationItem) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/replication"); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + return client.executeJson(request, replicationItem, null, CommonResult.class, validator_200_201); } /** * Start a replication process - * - * @param indexName - * the name of the index - * @param replicationName - * the name of the replication item + * + * @param indexName the name of the index + * @param replicationName the name of the replication item * @return the status - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public CommonResult executeReplication(String indexName, - String replicationName) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/replication/run").addParameter("name", replicationName); - Request request = Request.Put(uriBuilder.build()); - return client.execute(request, null, null, CommonResult.class, 200); + public CommonResult executeReplication(String indexName, String replicationName) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = + client.getBaseUrl("index/", indexName, "/replication/run").addParameter("name", replicationName); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + return client.executeJson(request, null, null, CommonResult.class, validator_200); } /** * Delete a replication item - * - * @param indexName - * the name of the index - * @param replicationName - * the name of the replication item + * + * @param indexName the name of the index + * @param replicationName the name of the replication item * @return the status - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public CommonResult deleteReplication(String indexName, - String replicationName) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/replication").addParameter("name", replicationName); - Request request = Request.Delete(uriBuilder.build()); - return client.execute(request, null, null, CommonResult.class, 200); + public CommonResult deleteReplication(String indexName, String replicationName) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = + client.getBaseUrl("index/", indexName, "/replication").addParameter("name", replicationName); + final HttpRequest request = HttpRequest.Delete(uriBuilder.build()); + return client.executeJson(request, null, null, CommonResult.class, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/SearchApi1.java b/src/main/java/com/opensearchserver/client/v1/SearchApi1.java index 26f93863..ff84438f 100644 --- a/src/main/java/com/opensearchserver/client/v1/SearchApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/SearchApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,21 +15,18 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.List; - -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.common.search.query.SearchFieldQuery; import com.opensearchserver.client.common.search.query.SearchPatternQuery; import com.opensearchserver.client.common.search.query.SearchQueryBatch; import com.opensearchserver.client.v1.search.SearchResult1; -import com.qwazr.utils.http.HttpUtils; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.List; /** * @version For OpenSearchServer v1.5.x @@ -42,137 +39,100 @@ public SearchApi1(JsonClient1 client) { /** * Create or update a search field template - * - * @param indexName - * The name of the index - * @param template - * The name of the template - * @param query - * The query - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param template The name of the template + * @param query The query + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void createSearchFieldTemplate(String indexName, String template, - SearchFieldQuery query) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/search/field/", template); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, query, null); - HttpUtils.checkStatusCodes(response, 200, 201); + public void createSearchFieldTemplate(String indexName, String template, SearchFieldQuery query) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/search/field/", template); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, query, null, validator_200_201); } /** * Execute a search with a search field template - * - * @param indexName - * The name of the index - * @param template - * The name of the template - * @param query - * Any overriding query parameter + * + * @param indexName The name of the index + * @param template The name of the template + * @param query Any overriding query parameter * @return the search result - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public SearchResult1 executeSearchFieldTemplate(String indexName, - String template, SearchFieldQuery query) throws IOException, - URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/search/field/", template); - Request request = Request.Post(uriBuilder.build()); - return client.execute(request, query, null, SearchResult1.class, 200); + public SearchResult1 executeSearchFieldTemplate(String indexName, String template, SearchFieldQuery query) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/search/field/", template); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + return client.executeJson(request, query, null, SearchResult1.class, validator_200); } /** * Execute a search with a search field template - * - * @param indexName - * The name of the index - * @param template - * The name of the template - * @param query - * Any overriding query parameter + * + * @param indexName The name of the index + * @param template The name of the template + * @param query Any overriding query parameter * @return the search result - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public SearchResult1 executeSearchPatternTemplate(String indexName, - String template, SearchPatternQuery query) throws IOException, - URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/search/pattern/", template); - Request request = Request.Post(uriBuilder.build()); - return client.execute(request, query, null, SearchResult1.class, 200); + public SearchResult1 executeSearchPatternTemplate(String indexName, String template, SearchPatternQuery query) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/search/pattern/", template); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + return client.executeJson(request, query, null, SearchResult1.class, validator_200); } /** * Execute a search field - * - * @param indexName - * The name of the index - * @param query - * Any overriding query parameter + * + * @param indexName The name of the index + * @param query Any overriding query parameter * @return the search result - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public SearchResult1 executeSearchField(String indexName, - SearchFieldQuery query) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/search/field"); - Request request = Request.Post(uriBuilder.build()); - return client.execute(request, query, null, SearchResult1.class, 200); + public SearchResult1 executeSearchField(String indexName, SearchFieldQuery query) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/search/field"); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + return client.executeJson(request, query, null, SearchResult1.class, validator_200); } /** * Execute a search pattern - * - * @param indexName - * The name of the index - * @param query - * Any overriding query parameter + * + * @param indexName The name of the index + * @param query Any overriding query parameter * @return the search result - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public SearchResult1 executeSearchPattern(String indexName, - SearchPatternQuery query) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/search/pattern"); - Request request = Request.Post(uriBuilder.build()); - return client.execute(request, query, null, SearchResult1.class, 200); + public SearchResult1 executeSearchPattern(String indexName, SearchPatternQuery query) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/search/pattern"); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + return client.executeJson(request, query, null, SearchResult1.class, validator_200); } /** * Execute a batch of search - * - * @param indexName - * The name of the index - * @param queryBatch - * The queries + * + * @param indexName The name of the index + * @param queryBatch The queries * @return a list of results - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public List searchBatch(String indexName, - SearchQueryBatch queryBatch) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/search/batch"); - Request request = Request.Post(uriBuilder.build()); - return client.execute(request, queryBatch, null, - SearchResult1.LIST_TYPEREF, 200); + public List searchBatch(String indexName, SearchQueryBatch queryBatch) + throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/search/batch"); + final HttpRequest request = HttpRequest.Post(uriBuilder.build()); + return client.executeJson(request, queryBatch, null, SearchResult1.LIST_TYPEREF, validator_200); } } diff --git a/src/main/java/com/opensearchserver/client/v1/UpdateApi1.java b/src/main/java/com/opensearchserver/client/v1/UpdateApi1.java index 2578354a..2e344a56 100644 --- a/src/main/java/com/opensearchserver/client/v1/UpdateApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/UpdateApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,20 +15,17 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; - -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.AbstractApi; import com.opensearchserver.client.common.LanguageEnum; import com.opensearchserver.client.common.update.DocumentUpdate; -import com.qwazr.utils.http.HttpUtils; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; public class UpdateApi1 extends AbstractApi { @@ -38,51 +35,39 @@ public UpdateApi1(JsonClient1 client) { /** * Delete the documents which contains the value in the specified field. - * - * @param indexName - * The name of the index - * @param fieldName - * The name of the field - * @param values - * A list of value - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param fieldName The name of the field + * @param values A list of value + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public void deleteDocumentsByFieldValue(String indexName, String fieldName, - List values) throws IOException, URISyntaxException { - StringBuilder deleteValues = new StringBuilder(); + public void deleteDocumentsByFieldValue(String indexName, String fieldName, List values) + throws IOException, URISyntaxException { + final StringBuilder deleteValues = new StringBuilder(); for (String value : values) { deleteValues.append('/'); deleteValues.append(value); } - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/document/" + fieldName + deleteValues.toString()); - Request request = Request.Delete(uriBuilder.build()); - HttpResponse response = client.execute(request, null, null); - HttpUtils.checkStatusCodes(response, 200); + final URIBuilder uriBuilder = + client.getBaseUrl("index/", indexName, "/document/" + fieldName + deleteValues.toString()); + final HttpRequest request = HttpRequest.Delete(uriBuilder.build()); + client.execute(request, null, null, validator_200); } /** * Create or update a set of documents - * - * @param indexName - * The name of the index - * @param documents - * A list of documents - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * + * @param indexName The name of the index + * @param documents A list of documents + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ public void updateDocuments(String indexName, List documents) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/document"); - Request request = Request.Put(uriBuilder.build()); - HttpResponse response = client.execute(request, documents, null); - HttpUtils.checkStatusCodes(response, 200); + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/document"); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + client.execute(request, documents, null, validator_200); } /** @@ -93,13 +78,11 @@ public List getNewDocumentUpdateList() { } /** - * @param langNameOfCode - * the language of the document + * @param langNameOfCode the language of the document * @return a new DocumentUpdate record */ public DocumentUpdate getNewDocumentUpdate(String langNameOfCode) { - return new DocumentUpdate().setLang(LanguageEnum - .findByNameOrCode(langNameOfCode)); + return new DocumentUpdate().setLang(LanguageEnum.findByNameOrCode(langNameOfCode)); } } diff --git a/src/main/java/com/opensearchserver/client/v1/WebCrawlerApi1.java b/src/main/java/com/opensearchserver/client/v1/WebCrawlerApi1.java index 13333a23..621a44ca 100644 --- a/src/main/java/com/opensearchserver/client/v1/WebCrawlerApi1.java +++ b/src/main/java/com/opensearchserver/client/v1/WebCrawlerApi1.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,15 +15,6 @@ */ package com.opensearchserver.client.v1; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.List; - -import javax.xml.bind.annotation.XmlTransient; - -import org.apache.http.client.fluent.Request; -import org.apache.http.client.utils.URIBuilder; - import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.core.type.TypeReference; import com.opensearchserver.client.JsonClient1; @@ -31,6 +22,13 @@ import com.opensearchserver.client.common.CommonListResult; import com.opensearchserver.client.common.CommonResult; import com.opensearchserver.client.v1.search.FieldValueList1; +import com.qwazr.utils.http.HttpRequest; +import org.apache.http.client.utils.URIBuilder; + +import javax.xml.bind.annotation.XmlTransient; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.List; public class WebCrawlerApi1 extends AbstractApi { @@ -40,84 +38,65 @@ public WebCrawlerApi1(JsonClient1 client) { @XmlTransient @JsonIgnore - public final static TypeReference>> LISTCRAWL_TYPEREF = new TypeReference>>() { - }; + public final static TypeReference>> LISTCRAWL_TYPEREF = + new TypeReference>>() { + }; /** * Crawl an URL and return data - * - * @param indexName - * The name of the index - * @param url - * The URL to crawl - * @param msTimeOut - * The timeout in milliseconds * + * + * @param indexName The name of the index + * @param url The URL to crawl + * @param msTimeOut The timeout in milliseconds * * @return the result of the crawl - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public CommonListResult> crawlWithData( - String indexName, String url, Integer msTimeOut) + public CommonListResult> crawlWithData(String indexName, String url, Integer msTimeOut) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client - .getBaseUrl("index/", indexName, "/crawler/web/crawl") - .addParameter("url", url).addParameter("returnData", "true"); - Request request = Request.Get(uriBuilder.build()); - return client.execute(request, null, msTimeOut, LISTCRAWL_TYPEREF, 200); + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/crawler/web/crawl") + .addParameter("url", url) + .addParameter("returnData", "true"); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.executeJson(request, null, msTimeOut, LISTCRAWL_TYPEREF, validator_200); } /** * Crawl an URL - * - * @param indexName - * The name of the index - * @param url - * The URL to crawl - * @param msTimeOut - * The timeout in milliseconds + * + * @param indexName The name of the index + * @param url The URL to crawl + * @param msTimeOut The timeout in milliseconds * @return the status of the crawl - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public CommonResult crawl(String indexName, String url, Integer msTimeOut) - throws IOException, URISyntaxException { - URIBuilder uriBuilder = client - .getBaseUrl("index/", indexName, "/crawler/web/crawl") - .addParameter("url", url).addParameter("returnData", "false"); - Request request = Request.Get(uriBuilder.build()); - return client - .execute(request, null, msTimeOut, CommonResult.class, 200); + public CommonResult crawl(String indexName, String url, Integer msTimeOut) throws IOException, URISyntaxException { + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/crawler/web/crawl") + .addParameter("url", url) + .addParameter("returnData", "false"); + final HttpRequest request = HttpRequest.Get(uriBuilder.build()); + return client.executeJson(request, null, msTimeOut, CommonResult.class, validator_200); } /** * Enable or disable pattern inclusion and exclusion - * - * @param indexName - * The name of the index - * @param inclusionStatus - * Enable or disable inclusion list - * @param exclusionStatus - * Enable or disable inclusion list + * + * @param indexName The name of the index + * @param inclusionStatus Enable or disable inclusion list + * @param exclusionStatus Enable or disable inclusion list * @return the result of the call - * @throws IOException - * if any IO error occurs - * @throws URISyntaxException - * if the URI is not valid + * @throws IOException if any IO error occurs + * @throws URISyntaxException if the URI is not valid */ - public CommonResult setPatternStatus(String indexName, - Boolean inclusionStatus, Boolean exclusionStatus) + public CommonResult setPatternStatus(String indexName, Boolean inclusionStatus, Boolean exclusionStatus) throws IOException, URISyntaxException { - URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, - "/crawler/web/patterns/status"); + final URIBuilder uriBuilder = client.getBaseUrl("index/", indexName, "/crawler/web/patterns/status"); if (inclusionStatus != null) uriBuilder.addParameter("inclusion", inclusionStatus.toString()); if (exclusionStatus != null) uriBuilder.addParameter("exclusion", exclusionStatus.toString()); - Request request = Request.Put(uriBuilder.build()); - return client.execute(request, null, null, CommonResult.class, 200); + final HttpRequest request = HttpRequest.Put(uriBuilder.build()); + return client.executeJson(request, null, null, CommonResult.class, validator_200); } } diff --git a/src/test/java/com/opensearchserver/client/test/v1/ReplicationTest.java b/src/test/java/com/opensearchserver/client/test/v1/ReplicationTest.java index db8fa39b..2e17cc85 100644 --- a/src/test/java/com/opensearchserver/client/test/v1/ReplicationTest.java +++ b/src/test/java/com/opensearchserver/client/test/v1/ReplicationTest.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,14 +15,6 @@ */ package com.opensearchserver.client.test.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.junit.Assert; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; - import com.opensearchserver.client.common.index.TemplateEnum; import com.opensearchserver.client.common.search.query.SearchFieldQuery; import com.opensearchserver.client.v1.IndexApi1; @@ -31,14 +23,20 @@ import com.opensearchserver.client.v1.replication.ReplicationItem; import com.opensearchserver.client.v1.replication.ReplicationResult; import com.opensearchserver.client.v1.replication.ReplicationType; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import java.io.IOException; +import java.net.URISyntaxException; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ReplicationTest extends AbstractTest { public static final String INDEX_NAME = "oss-java-client test replication"; public static final String INDEX_NAME_REPL = "oss-java-client test replication repl"; - public static final String REPLICATION_ITEM_NAME = OSS_URL + "/" - + INDEX_NAME_REPL; + public static final String REPLICATION_ITEM_NAME = OSS_URL + "/" + INDEX_NAME_REPL; public static final Integer REPLICATION_TIME_OUT = 60; public static final String SEARCH_TEMPLATE_NAME = "search"; @@ -54,31 +52,28 @@ public void test001createIndexes() throws IOException, URISyntaxException { } @Test - public void test010createReplication() throws IOException, - URISyntaxException { + public void test010createReplication() throws IOException, URISyntaxException { ReplicationApi1 replicationApi = new ReplicationApi1(client); - ReplicationItem replicationItem = new ReplicationItem() - .setReplicationType(ReplicationType.BACKUP_INDEX) - .setRemoteUrl(OSS_URL).setRemoteIndexName(INDEX_NAME_REPL) - .setRemoteLogin(OSS_LOGIN).setRemoteApiKey(OSS_APIKEY) + ReplicationItem replicationItem = new ReplicationItem().setReplicationType(ReplicationType.BACKUP_INDEX) + .setRemoteUrl(OSS_URL) + .setRemoteIndexName(INDEX_NAME_REPL) + .setRemoteLogin(OSS_LOGIN) + .setRemoteApiKey(OSS_APIKEY) .setSecTimeOut(REPLICATION_TIME_OUT); replicationApi.createUpdateReplication(INDEX_NAME, replicationItem); } @Test - public void test020executeReplication() throws IOException, - URISyntaxException { + public void test020executeReplication() throws IOException, URISyntaxException { ReplicationApi1 replicationApi = new ReplicationApi1(client); replicationApi.executeReplication(INDEX_NAME, REPLICATION_ITEM_NAME); } @Test - public void test030waitReplication() throws IOException, - URISyntaxException, InterruptedException { + public void test030waitReplication() throws IOException, URISyntaxException, InterruptedException { ReplicationApi1 replicationApi = new ReplicationApi1(client); for (int i = 0; i < 60; i++) { - ReplicationResult result = replicationApi.getReplication( - INDEX_NAME, REPLICATION_ITEM_NAME); + ReplicationResult result = replicationApi.getReplication(INDEX_NAME, REPLICATION_ITEM_NAME); if (result.lastThread != null && !result.isActiveThread) return; Thread.sleep(2000); @@ -89,15 +84,12 @@ public void test030waitReplication() throws IOException, @Test public void test040search() throws IOException, URISyntaxException { SearchApi1 searchApi1 = new SearchApi1(client); - SearchFieldQuery query = (SearchFieldQuery) new SearchFieldQuery() - .setQuery("test"); - searchApi1.executeSearchFieldTemplate(INDEX_NAME_REPL, - SEARCH_TEMPLATE_NAME, query); + SearchFieldQuery query = (SearchFieldQuery) new SearchFieldQuery().setQuery("test"); + searchApi1.executeSearchFieldTemplate(INDEX_NAME_REPL, SEARCH_TEMPLATE_NAME, query); } @Test - public void test500deleteReplication() throws IOException, - URISyntaxException { + public void test500deleteReplication() throws IOException, URISyntaxException { ReplicationApi1 replicationApi = new ReplicationApi1(client); replicationApi.deleteReplication(INDEX_NAME, REPLICATION_ITEM_NAME); } diff --git a/src/test/java/com/opensearchserver/client/test/v1/SearchFieldTest.java b/src/test/java/com/opensearchserver/client/test/v1/SearchFieldTest.java index cac69642..f8a39e05 100644 --- a/src/test/java/com/opensearchserver/client/test/v1/SearchFieldTest.java +++ b/src/test/java/com/opensearchserver/client/test/v1/SearchFieldTest.java @@ -1,12 +1,12 @@ /** * Copyright 2015 OpenSearchServer Inc. - * + *

* Licensed 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 - * + *

+ * 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. @@ -15,11 +15,6 @@ */ package com.opensearchserver.client.test.v1; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.junit.Test; - import com.opensearchserver.client.JsonClient1; import com.opensearchserver.client.common.search.query.SearchField; import com.opensearchserver.client.common.search.query.SearchField.SearchFieldMode; @@ -28,30 +23,31 @@ import com.opensearchserver.client.v1.SearchApi1; import com.opensearchserver.client.v1.search.SearchResult1; import com.qwazr.utils.json.JsonMapper; +import org.junit.Test; + +import java.io.IOException; +import java.net.URISyntaxException; public class SearchFieldTest { @Test public void testSearchBooleanGroup() throws IOException, URISyntaxException { - JsonClient1 client = new JsonClient1("http://localhost:8080", null, - null, 50000); + JsonClient1 client = new JsonClient1("http://localhost:8080", null, null, 50000); String queryGroup1 = "open search server"; String queryGroup2 = "en"; SearchApi1 searchApi = new SearchApi1(client); - SearchFieldQuery searchFieldQuery = (SearchFieldQuery) new SearchFieldQuery() - .setEmptyReturnsAll(false).setOperator(OperatorEnum.AND); + SearchFieldQuery searchFieldQuery = + (SearchFieldQuery) new SearchFieldQuery().setEmptyReturnsAll(false).setOperator(OperatorEnum.AND); // If the queryGroup1 is not empty, we add the required fields if (queryGroup1 != null && !queryGroup1.isEmpty()) { // Create the search fields, affected to the first boolean group - searchFieldQuery.addSearchField(new SearchField() - .setField("content").setMode(SearchFieldMode.PATTERN) - .setBooleanGroup(1)); + searchFieldQuery.addSearchField( + new SearchField().setField("content").setMode(SearchFieldMode.PATTERN).setBooleanGroup(1)); - searchFieldQuery.addSearchField(new SearchField() - .setField("contentExact").setMode(SearchFieldMode.PATTERN) - .setBooleanGroup(1)); + searchFieldQuery.addSearchField( + new SearchField().setField("contentExact").setMode(SearchFieldMode.PATTERN).setBooleanGroup(1)); // We set the text query searchFieldQuery.setQueryStringMap("content", queryGroup1); @@ -63,16 +59,14 @@ public void testSearchBooleanGroup() throws IOException, URISyntaxException { if (queryGroup2 != null && !queryGroup2.isEmpty()) { // Create the search fields, affected to the first boolean group - searchFieldQuery.addSearchField(new SearchField().setField("lang") - .setMode(SearchFieldMode.PATTERN).setBooleanGroup(2)); + searchFieldQuery.addSearchField( + new SearchField().setField("lang").setMode(SearchFieldMode.PATTERN).setBooleanGroup(2)); searchFieldQuery.setQueryStringMap("lang", queryGroup2); } - System.out.println(JsonMapper.MAPPER - .writeValueAsString(searchFieldQuery)); - SearchResult1 searchResult = searchApi.executeSearchField("web", - searchFieldQuery); + System.out.println(JsonMapper.MAPPER.writeValueAsString(searchFieldQuery)); + SearchResult1 searchResult = searchApi.executeSearchField("web", searchFieldQuery); System.out.println(JsonMapper.MAPPER.writeValueAsString(searchResult)); }