You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/10/19 18:36:22 UTC

[camel] 02/03: CAMEL-12854: Polished

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 84dbe8c717e31d72c4aa4d96b6ef32dc5333bf8c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Oct 19 20:07:27 2019 +0200

    CAMEL-12854: Polished
---
 components/camel-graphql/pom.xml                   | 15 +++++
 .../src/main/docs/graphql-component.adoc           |  8 +--
 .../camel/component/graphql/GraphqlEndpoint.java   |  8 +--
 .../camel/component/graphql/GraphqlProducer.java   | 15 ++---
 .../component/graphql/GraphqlProducerTest.java     | 20 ++++++-
 .../graphql/server/GraphqlDataFetchers.java        | 17 +++---
 .../component/graphql/server/GraphqlFactory.java   | 15 ++---
 .../component/graphql/server/GraphqlServer.java    | 23 ++++---
 .../src/test/resources/log4j2.properties           | 28 +++++++++
 .../dsl/GraphqlEndpointBuilderFactory.java         | 70 +++++++++++-----------
 10 files changed, 141 insertions(+), 78 deletions(-)

diff --git a/components/camel-graphql/pom.xml b/components/camel-graphql/pom.xml
index ab5cb9a..28335d8 100644
--- a/components/camel-graphql/pom.xml
+++ b/components/camel-graphql/pom.xml
@@ -59,5 +59,20 @@
             <artifactId>jackson-databind</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/components/camel-graphql/src/main/docs/graphql-component.adoc b/components/camel-graphql/src/main/docs/graphql-component.adoc
index 206e40b..020c893 100644
--- a/components/camel-graphql/src/main/docs/graphql-component.adoc
+++ b/components/camel-graphql/src/main/docs/graphql-component.adoc
@@ -24,7 +24,7 @@ for this component:
 The GraphQL endpoint is configured using URI syntax:
 
 ----
-graphql:httpUri?options
+graphql:httpUri
 ----
 
 with the following path and query parameters:
@@ -45,17 +45,17 @@ with the following path and query parameters:
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *accessToken* (producer) | The access token sent in the Authorization header. |  | String
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *operationName* (producer) | The query or mutation name. |  | String
-| *password* (producer) | The password for Basic authentication. |  | String
 | *proxyHost* (producer) | The proxy host in the format hostname:port. |  | String
 | *query* (producer) | The query text. |  | String
 | *queryFile* (producer) | The query file name located in the classpath. |  | String
-| *username* (producer) | The username for Basic authentication. |  | String
 | *variables* (producer) | The JsonObject instance containing the operation variables. |  | JsonObject
 | *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
 | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
+| *accessToken* (security) | The access token sent in the Authorization header. |  | String
+| *password* (security) | The password for Basic authentication. |  | String
+| *username* (security) | The username for Basic authentication. |  | String
 |===
 // endpoint options: END
 
diff --git a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
index adb5e97..b2881e7 100644
--- a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
+++ b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
@@ -45,7 +45,7 @@ import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.message.BasicHeader;
 
-@UriEndpoint(firstVersion = "3.0.0", scheme = "graphql", title = "GraphQL", syntax = "graphql:httpUri?options", label = "graphql", producerOnly = true)
+@UriEndpoint(firstVersion = "3.0.0", scheme = "graphql", title = "GraphQL", syntax = "graphql:httpUri", label = "api", producerOnly = true)
 public class GraphqlEndpoint extends DefaultEndpoint {
 
     @UriPath
@@ -53,11 +53,11 @@ public class GraphqlEndpoint extends DefaultEndpoint {
     private URI httpUri;
     @UriParam
     private String proxyHost;
-    @UriParam
+    @UriParam(label = "security", secret = true)
     private String accessToken;
-    @UriParam
+    @UriParam(label = "security", secret = true)
     private String username;
-    @UriParam
+    @UriParam(label = "security", secret = true)
     private String password;
     @UriParam
     private String query;
diff --git a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java
index 2fe86c1..a74e40b 100644
--- a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java
+++ b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java
@@ -18,8 +18,9 @@ package org.apache.camel.component.graphql;
 
 import java.net.URI;
 
+import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
-import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.DefaultAsyncProducer;
 import org.apache.camel.util.json.JsonObject;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpHeaders;
@@ -29,7 +30,7 @@ import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.util.EntityUtils;
 
-class GraphqlProducer extends DefaultProducer {
+public class GraphqlProducer extends DefaultAsyncProducer {
 
     public GraphqlProducer(GraphqlEndpoint endpoint) {
         super(endpoint);
@@ -41,7 +42,7 @@ class GraphqlProducer extends DefaultProducer {
     }
 
     @Override
-    public void process(Exchange exchange) {
+    public boolean process(Exchange exchange, AsyncCallback callback) {
         try {
             CloseableHttpClient httpClient = getEndpoint().getHttpclient();
             URI httpUri = getEndpoint().getHttpUri();
@@ -54,15 +55,16 @@ class GraphqlProducer extends DefaultProducer {
             httpPost.setHeader(HttpHeaders.ACCEPT_ENCODING, "gzip");
             httpPost.setEntity(requestEntity);
 
-            String responseContent = httpClient.execute(httpPost, response -> {
-                return EntityUtils.toString(response.getEntity());
-            });
+            String responseContent = httpClient.execute(httpPost, response -> EntityUtils.toString(response.getEntity()));
 
             exchange.getMessage().setBody(responseContent);
 
         } catch (Exception e) {
             exchange.setException(e);
         }
+
+        callback.done(true);
+        return true;
     }
 
     protected static String buildRequestBody(String query, String operationName, JsonObject variables) {
@@ -72,5 +74,4 @@ class GraphqlProducer extends DefaultProducer {
         jsonObject.put("variables", variables != null ? variables : new JsonObject());
         return jsonObject.toJson();
     }
-
 }
diff --git a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlProducerTest.java b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlProducerTest.java
index 7ad8e2c..f50164d 100644
--- a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlProducerTest.java
+++ b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlProducerTest.java
@@ -1,10 +1,26 @@
+/*
+ * 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.camel.component.graphql;
 
-import static org.junit.Assert.assertEquals;
-
 import org.apache.camel.util.json.JsonObject;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 public class GraphqlProducerTest {
 
     @Test
diff --git a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlDataFetchers.java b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlDataFetchers.java
index 86104ff..50960a4 100644
--- a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlDataFetchers.java
+++ b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlDataFetchers.java
@@ -22,26 +22,29 @@ import java.util.Map;
 
 import graphql.schema.DataFetcher;
 
-public class GraphqlDataFetchers {
+public final class GraphqlDataFetchers {
 
-    private static List<Book> books = Arrays.asList(
+    private static final List<Book> BOOKS = Arrays.asList(
             new Book("book-1", "Harry Potter and the Philosopher's Stone", "author-1"),
             new Book("book-2", "Moby Dick", "author-2"),
             new Book("book-3", "Interview with the vampire", "author-3"));
 
-    private static List<Author> authors = Arrays.asList(
+    private static final List<Author> AUTHORS = Arrays.asList(
             new Author("author-1", "Joanne Rowling"),
             new Author("author-2", "Herman Melville"),
             new Author("author-3", "Anne Rice"));
 
+    private GraphqlDataFetchers() {
+    }
+
     public static DataFetcher<List<Book>> getBooksDataFetcher() {
-        return dataFetchingEnvironment -> books;
+        return dataFetchingEnvironment -> BOOKS;
     }
 
     public static DataFetcher<Book> getBookByIdDataFetcher() {
         return dataFetchingEnvironment -> {
             String bookId = dataFetchingEnvironment.getArgument("id");
-            return books.stream().filter(book -> book.getId().equals(bookId)).findFirst().orElse(null);
+            return BOOKS.stream().filter(book -> book.getId().equals(bookId)).findFirst().orElse(null);
         };
     }
 
@@ -49,14 +52,14 @@ public class GraphqlDataFetchers {
         return dataFetchingEnvironment -> {
             Book book = dataFetchingEnvironment.getSource();
             String authorId = book.getAuthorId();
-            return authors.stream().filter(author -> author.getId().equals(authorId)).findFirst().orElse(null);
+            return AUTHORS.stream().filter(author -> author.getId().equals(authorId)).findFirst().orElse(null);
         };
     }
 
     public static DataFetcher<Book> addBookDataFetcher() {
         return dataFetchingEnvironment -> {
             Map<String, Object> bookInput = dataFetchingEnvironment.getArgument("bookInput");
-            String id = "book-" + (books.size() + 1);
+            String id = "book-" + (BOOKS.size() + 1);
             String name = (String) bookInput.get("name");
             String authorId = (String) bookInput.get("authorId");
             Book book = new Book(id, name, authorId);
diff --git a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlFactory.java b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlFactory.java
index 9b97f89..ba25737 100644
--- a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlFactory.java
+++ b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlFactory.java
@@ -16,19 +16,21 @@
  */
 package org.apache.camel.component.graphql.server;
 
-import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
-
-import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.ObjectHelper;
-
 import graphql.GraphQL;
 import graphql.schema.GraphQLSchema;
 import graphql.schema.idl.RuntimeWiring;
 import graphql.schema.idl.SchemaGenerator;
 import graphql.schema.idl.SchemaParser;
 import graphql.schema.idl.TypeDefinitionRegistry;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
+
+import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
 
-public class GraphqlFactory {
+public final class GraphqlFactory {
+
+    private GraphqlFactory() {
+    }
 
     public static GraphQL newGraphQL() {
         try {
@@ -36,7 +38,6 @@ public class GraphqlFactory {
             GraphQLSchema graphQLSchema = buildSchema(schema);
             return GraphQL.newGraphQL(graphQLSchema).build();
         } catch (Exception e) {
-            System.err.println(e);
             throw new RuntimeException(e);
         }
     }
diff --git a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java
index ffeddf6..076511a 100644
--- a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java
+++ b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java
@@ -22,7 +22,9 @@ import java.util.concurrent.TimeUnit;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
-
+import graphql.ExecutionInput;
+import graphql.ExecutionResult;
+import graphql.GraphQL;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
@@ -35,10 +37,6 @@ import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpRequestHandler;
 import org.apache.http.util.EntityUtils;
 
-import graphql.ExecutionInput;
-import graphql.ExecutionResult;
-import graphql.GraphQL;
-
 public class GraphqlServer {
 
     private final GraphQL graphql;
@@ -47,8 +45,8 @@ public class GraphqlServer {
     public GraphqlServer() {
         this.graphql = GraphqlFactory.newGraphQL();
         this.server = ServerBootstrap.bootstrap()
-            .registerHandler("/graphql", new GraphqlHandler())
-            .create();
+                .registerHandler("/graphql", new GraphqlHandler())
+                .create();
     }
 
     public void start() throws IOException {
@@ -79,10 +77,10 @@ public class GraphqlServer {
                 Map<String, Object> variables = (Map<String, Object>) map.get("variables");
 
                 ExecutionInput executionInput = ExecutionInput.newExecutionInput()
-                    .query(query)
-                    .operationName(operationName)
-                    .variables(variables)
-                    .build();
+                        .query(query)
+                        .operationName(operationName)
+                        .variables(variables)
+                        .build();
                 ExecutionResult executionResult = graphql.execute(executionInput);
                 Map<String, Object> resultMap = executionResult.toSpecification();
                 String result = objectMapper.writeValueAsString(resultMap);
@@ -93,7 +91,8 @@ public class GraphqlServer {
         }
 
         private Map<String, Object> jsonToMap(String json) throws IOException {
-            return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
+            return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
+            });
         }
 
     }
diff --git a/components/camel-graphql/src/test/resources/log4j2.properties b/components/camel-graphql/src/test/resources/log4j2.properties
new file mode 100644
index 0000000..d2bc579
--- /dev/null
+++ b/components/camel-graphql/src/test/resources/log4j2.properties
@@ -0,0 +1,28 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = target/camel-graphql-test.log
+appender.file.layout.type = PatternLayout
+appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+appender.out.type = Console
+appender.out.name = out
+appender.out.layout.type = PatternLayout
+appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
+rootLogger.level = INFO
+rootLogger.appenderRef.file.ref = file
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/GraphqlEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/GraphqlEndpointBuilderFactory.java
index 9cd4ca0..35196af 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/GraphqlEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/GraphqlEndpointBuilderFactory.java
@@ -38,17 +38,6 @@ public interface GraphqlEndpointBuilderFactory {
             return (AdvancedGraphqlEndpointBuilder) this;
         }
         /**
-         * The access token sent in the Authorization header.
-         * 
-         * The option is a: <code>java.lang.String</code> type.
-         * 
-         * Group: producer
-         */
-        default GraphqlEndpointBuilder accessToken(String accessToken) {
-            doSetProperty("accessToken", accessToken);
-            return this;
-        }
-        /**
          * The query or mutation name.
          * 
          * The option is a: <code>java.lang.String</code> type.
@@ -60,17 +49,6 @@ public interface GraphqlEndpointBuilderFactory {
             return this;
         }
         /**
-         * The password for Basic authentication.
-         * 
-         * The option is a: <code>java.lang.String</code> type.
-         * 
-         * Group: producer
-         */
-        default GraphqlEndpointBuilder password(String password) {
-            doSetProperty("password", password);
-            return this;
-        }
-        /**
          * The proxy host in the format hostname:port.
          * 
          * The option is a: <code>java.lang.String</code> type.
@@ -104,17 +82,6 @@ public interface GraphqlEndpointBuilderFactory {
             return this;
         }
         /**
-         * The username for Basic authentication.
-         * 
-         * The option is a: <code>java.lang.String</code> type.
-         * 
-         * Group: producer
-         */
-        default GraphqlEndpointBuilder username(String username) {
-            doSetProperty("username", username);
-            return this;
-        }
-        /**
          * The JsonObject instance containing the operation variables.
          * 
          * The option is a: <code>org.apache.camel.util.json.JsonObject</code>
@@ -138,6 +105,39 @@ public interface GraphqlEndpointBuilderFactory {
             doSetProperty("variables", variables);
             return this;
         }
+        /**
+         * The access token sent in the Authorization header.
+         * 
+         * The option is a: <code>java.lang.String</code> type.
+         * 
+         * Group: security
+         */
+        default GraphqlEndpointBuilder accessToken(String accessToken) {
+            doSetProperty("accessToken", accessToken);
+            return this;
+        }
+        /**
+         * The password for Basic authentication.
+         * 
+         * The option is a: <code>java.lang.String</code> type.
+         * 
+         * Group: security
+         */
+        default GraphqlEndpointBuilder password(String password) {
+            doSetProperty("password", password);
+            return this;
+        }
+        /**
+         * The username for Basic authentication.
+         * 
+         * The option is a: <code>java.lang.String</code> type.
+         * 
+         * Group: security
+         */
+        default GraphqlEndpointBuilder username(String username) {
+            doSetProperty("username", username);
+            return this;
+        }
     }
 
     /**
@@ -204,11 +204,11 @@ public interface GraphqlEndpointBuilderFactory {
      * GraphQL (camel-graphql)
      * A Camel GraphQL Component
      * 
-     * Category: graphql
+     * Category: api
      * Available as of version: 3.0
      * Maven coordinates: org.apache.camel:camel-graphql
      * 
-     * Syntax: <code>graphql:httpUri?options</code>
+     * Syntax: <code>graphql:httpUri</code>
      * 
      * Path parameter: httpUri (required)
      * The GraphQL server URI.