This is an automated email from the ASF dual-hosted git repository. onichols pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode-examples.git
The following commit(s) were added to refs/heads/develop by this push: new 54839ae GEODE-8737: Create new geode example about rest api (#104) 54839ae is described below commit 54839ae0d58f746cb153f33b72a021948183d746 Author: Ashish Choudhary <aashish.choudha...@gmail.com> AuthorDate: Tue Jan 5 04:34:38 2021 +0530 GEODE-8737: Create new geode example about rest api (#104) --- README.md | 1 + rest/README.md | 42 ++++++++++++ rest/scripts/start.gfsh | 25 +++++++ rest/scripts/stop.gfsh | 18 +++++ .../org/apache/geode_examples/rest/Example.java | 78 ++++++++++++++++++++++ settings.gradle | 1 + 6 files changed, 165 insertions(+) diff --git a/README.md b/README.md index 53c796c..bc97af5 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ tutorial. * [Security & SSL](clientSecurity/README.md) * [Colocation](colocation/README.md) * Off-heap +* [Rest](rest/README.md) ### Advanced diff --git a/rest/README.md b/rest/README.md new file mode 100644 index 0000000..272a9ae --- /dev/null +++ b/rest/README.md @@ -0,0 +1,42 @@ +<!-- +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. +--> + +# Geode REST API example + +This is a simple example that demonstrates putting values into a +replicated region and retrieving the values using the Geode REST API. For enabling the REST API you can follow the official [document](https://geode.apache.org/docs/guide/19/rest_apps/setup_config.html#setup_config_enabling_rest). + +This example assumes you have installed Java and Geode. + +## Steps + +1. From the `geode-examples/rest` directory, build the example and + run unit tests + + $ ../gradlew build + +2. Next start the locator and two servers + + $ gfsh run --file=scripts/start.gfsh + +3. Run the example to create and get entries using HTTP Java Client from the region + + $ ../gradlew run + +4. Shut down the system: + + $ gfsh run --file=scripts/stop.gfsh diff --git a/rest/scripts/start.gfsh b/rest/scripts/start.gfsh new file mode 100644 index 0000000..45ea96e --- /dev/null +++ b/rest/scripts/start.gfsh @@ -0,0 +1,25 @@ +# +# 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. +# +start locator --name=locator --bind-address=127.0.0.1 + +start server --name=server1 --locators=127.0.0.1[10334] --server-port=0 --start-rest-api --http-service-bind-address=127.0.0.1 --http-service-port=8080 +start server --name=server2 --locators=127.0.0.1[10334] --server-port=0 --start-rest-api --http-service-bind-address=127.0.0.1 --http-service-port=8081 + +create region --name=example-region --type=REPLICATE + +list members +describe region --name=example-region diff --git a/rest/scripts/stop.gfsh b/rest/scripts/stop.gfsh new file mode 100644 index 0000000..9281b31 --- /dev/null +++ b/rest/scripts/stop.gfsh @@ -0,0 +1,18 @@ +# +# 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. +# +connect --locator=127.0.0.1[10334] +shutdown --include-locators=true \ No newline at end of file diff --git a/rest/src/main/java/org/apache/geode_examples/rest/Example.java b/rest/src/main/java/org/apache/geode_examples/rest/Example.java new file mode 100644 index 0000000..66bcd39 --- /dev/null +++ b/rest/src/main/java/org/apache/geode_examples/rest/Example.java @@ -0,0 +1,78 @@ +/* + * 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.geode_examples.rest; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +public class Example { + private static final String GEODE_REST_END_POINT = "http://localhost:8080/gemfire-api/v1/"; + private static final String GET_REQUEST_PARAMETER = "?limit=ALL"; + private static final String POST_REQUEST_PARAMETER = "?key=1"; + private static final String DATA = "{\"name\": \"Dan Smith\", \"technology\": \"Java\"}"; + + public static void main(String[] args) throws IOException { + HttpURLConnection httpPostRequestConnection = createHttpPostRequest(); + writeData(httpPostRequestConnection); + HttpURLConnection httpGetRequestConnection = createHttpGetRequest(); + printValues(httpGetRequestConnection); + } + + private static HttpURLConnection createHttpGetRequest() throws IOException { + URL url = new URL(GEODE_REST_END_POINT + "example-region" + GET_REQUEST_PARAMETER); + HttpURLConnection httpURLConnection = getHttpURLConnection(url); + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setRequestProperty("Accept", "application/json"); + return httpURLConnection; + } + + private static void printValues(HttpURLConnection conn) throws IOException { + try (BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())))) { + String response; + while ((response = br.readLine()) != null) { + System.out.println(response); + } + } finally { + conn.disconnect(); + } + } + + private static HttpURLConnection createHttpPostRequest() throws IOException { + URL url = new URL(GEODE_REST_END_POINT + "example-region" + POST_REQUEST_PARAMETER); + HttpURLConnection httpURLConnection = getHttpURLConnection(url); + httpURLConnection.setRequestMethod("POST"); + httpURLConnection.setRequestProperty("Content-Type", "application/json; utf-8"); + httpURLConnection.setRequestProperty("Accept", "application/json"); + httpURLConnection.setDoOutput(true); + return httpURLConnection; + } + + private static HttpURLConnection getHttpURLConnection(URL url) throws IOException { + return (HttpURLConnection) url.openConnection(); + } + + private static void writeData(HttpURLConnection conn) throws IOException { + try (OutputStream outputStream = conn.getOutputStream()) { + outputStream.write(DATA.getBytes(StandardCharsets.UTF_8)); + conn.getInputStream(); + } + } +} + diff --git a/settings.gradle b/settings.gradle index 7a681e3..a2165fa 100644 --- a/settings.gradle +++ b/settings.gradle @@ -42,6 +42,7 @@ include 'sessionState' include 'colocation' include 'micrometerMetrics' include 'compression' +include 'rest' // Logic for defining a custom Geode clone for integration with this project // Define `-PgeodeCompositeDirectory` to your geode root, default `../geode`