You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/08/11 17:07:26 UTC

[GitHub] [nifi] greyp9 commented on a diff in pull request #6268: NIFI-10244 Add nifi-web-client-api and implementation

greyp9 commented on code in PR #6268:
URL: https://github.com/apache/nifi/pull/6268#discussion_r943667436


##########
nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/StandardHttpRequestMethod.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.nifi.web.client.api;
+
+/**
+ * Enumeration of standard HTTP Request Methods
+ */
+public enum StandardHttpRequestMethod implements HttpRequestMethod {
+    DELETE,

Review Comment:
   Is it possible to make a request with a method not in this enumeration?  (HEAD, for example)



##########
nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/WebClientService.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.nifi.web.client.api;
+
+/**
+ * Service abstraction for HTTP client operations
+ */
+public interface WebClientService {
+    /**

Review Comment:
   It might be nice for this interface to accept headers that should be included for each request.  Other headers might be dynamic based on request context.
   
   ```
       @Test
       public void testHeadersPerRequest() {
           final StandardWebClientService webClientService = new StandardWebClientService();
           for (int i = 0; (i < 2); ++i) {
               final HttpResponseEntity httpResponseEntity = webClientService.get()
                       .uri(URI.create("https://google.com"))
                       .header("accept", "*/*")
                       .header("accept-encoding", "text/plain")
                       .retrieve();
               logger.info("status code: {}", httpResponseEntity.statusCode());
           }
       }
   
       @Test
       public void testHeadersPreloaded() {
           final StandardWebClientService webClientService = new StandardWebClientService()
                   .header("accept", "*/*")
                   .header("accept-encoding", "text/plain");
           for (int i = 0; (i < 2); ++i) {
               final HttpResponseEntity httpResponseEntity = webClientService.get()
                       .uri(URI.create("https://google.com"))
                       .retrieve();
               logger.info("status code: {}", httpResponseEntity.statusCode());
           }
       }
   ```



##########
nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpResponseStatus.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.nifi.web.client.api;
+
+/**
+ * Enumeration of Standard HTTP Response Status Codes
+ */
+public enum HttpResponseStatus {
+    OK(200),

Review Comment:
   How would a response be handled where the response code was not in this enumeration?



##########
nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardWebClientService.java:
##########
@@ -0,0 +1,303 @@
+/*
+ * 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.nifi.web.client;
+
+import okhttp3.Call;
+import okhttp3.Credentials;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
+import org.apache.nifi.web.client.api.HttpEntityHeaders;
+import org.apache.nifi.web.client.api.HttpRequestBodySpec;
+import org.apache.nifi.web.client.api.HttpRequestHeadersSpec;
+import org.apache.nifi.web.client.api.HttpRequestMethod;
+import org.apache.nifi.web.client.api.HttpRequestUriSpec;
+import org.apache.nifi.web.client.api.HttpResponseEntity;
+import org.apache.nifi.web.client.api.StandardHttpRequestMethod;
+import org.apache.nifi.web.client.api.WebClientService;
+import org.apache.nifi.web.client.api.WebClientServiceException;
+import org.apache.nifi.web.client.proxy.ProxyContext;
+import org.apache.nifi.web.client.redirect.RedirectHandling;
+import org.apache.nifi.web.client.ssl.SSLSocketFactoryProvider;
+import org.apache.nifi.web.client.ssl.StandardSSLSocketFactoryProvider;
+import org.apache.nifi.web.client.ssl.TlsContext;
+
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.X509TrustManager;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.Proxy;
+import java.net.URI;
+import java.time.Duration;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.OptionalLong;
+
+/**
+ * Standard implementation of Web Client Service using OkHttp
+ */
+public class StandardWebClientService implements WebClientService {
+    private static final byte[] EMPTY_BYTES = new byte[0];
+
+    private static final SSLSocketFactoryProvider sslSocketFactoryProvider = new StandardSSLSocketFactoryProvider();
+
+    private OkHttpClient okHttpClient;

Review Comment:
   If a second implementation (apache/httpclient?) were added at some point, how would we integrate it into the project structure?  Should the implementation class provide a hint about the underlying library in use?
   
   - StandardWebClientServiceOkHttp
   - StandardWebClientServiceApache
   



##########
nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpEntityHeaders.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.nifi.web.client;
+
+import org.apache.nifi.web.client.api.HttpEntityHeaders;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * Standard implementation of HTTP Entity Headers for Standard Web Client Service
+ */
+class StandardHttpEntityHeaders implements HttpEntityHeaders {
+    private final Map<String, List<String>> headers;

Review Comment:
   Might it be important to preserve the order of the headers in the response?



##########
nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpUriBuilder.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.nifi.web.client;
+
+import okhttp3.HttpUrl;
+import org.apache.nifi.web.client.api.HttpUriBuilder;
+
+import java.net.URI;
+import java.util.Objects;
+
+/**
+ * Standard HTTP URI Builder based on OkHttp HttpUrl
+ */
+public class StandardHttpUriBuilder implements HttpUriBuilder {
+    private final HttpUrl.Builder builder;
+
+    public StandardHttpUriBuilder() {
+        this.builder = new HttpUrl.Builder();
+    }
+
+    @Override
+    public URI build() {
+        final HttpUrl httpUrl = builder.build();
+        return httpUrl.uri();
+    }
+
+    @Override
+    public HttpUriBuilder scheme(final String scheme) {
+        Objects.requireNonNull(scheme, "Scheme required");
+        builder.scheme(scheme);
+        return this;
+    }
+
+    @Override
+    public HttpUriBuilder host(final String host) {
+        Objects.requireNonNull(host, "Host required");
+        builder.host(host);
+        return this;
+    }
+
+    @Override
+    public HttpUriBuilder port(int port) {
+        builder.port(port);
+        return this;
+    }
+
+    @Override
+    public HttpUriBuilder encodedPath(final String encodedPath) {

Review Comment:
   The path API is encoded, but the path segment is unencoded.  
   (1) Is this conforming too closely to the okhttp implementation?
   (2) Should the API offer the ability to supply either?



##########
nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpEntityHeaders.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * 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.nifi.web.client.api;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * HTTP Entity Headers supporting retrieval of single or multiple header values
+ */
+public interface HttpEntityHeaders {
+    /**

Review Comment:
   It would be really useful for this interface to have a means of enumerating the header set.  Two options (either works):
   
     fun names(): Set<String> {
       public open operator fun iterator(): kotlin.collections.Iterator<kotlin.Pair<kotlin.String, kotlin.String>> { /* compiled code */ }
   
   
   https://github.com/square/okhttp/blob/okhttp_4.9.x/okhttp/src/main/kotlin/okhttp3/Headers.kt#L91
   https://github.com/square/okhttp/blob/okhttp_4.9.x/okhttp/src/main/kotlin/okhttp3/Headers.kt#L132
   



##########
nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/proxy/ProxyContext.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.nifi.web.client.proxy;
+
+import java.net.Proxy;
+import java.util.Optional;
+
+/**
+ * Proxy Context for provides information necessary

Review Comment:
   Proxy Context provides information necessary to access remote websites through an http proxy using the basic proxy authentication method
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org