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/10 07:00:03 UTC

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

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


##########
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;
+
+    StandardHttpEntityHeaders(final Map<String, List<String>> headers) {
+        this.headers = headers;

Review Comment:
   I see that we use unmodifiableList in the getHeader method. What do you think about making this headers Map immutable as well - so the whole class could be immutable?



##########
nifi-commons/nifi-web-client-api/src/main/java/org/apache/nifi/web/client/api/HttpUriBuilder.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.api;
+
+import java.net.URI;
+
+/**
+ * HTTP URI Builder supports construction of a URI using component elements
+ */
+public interface HttpUriBuilder {
+    /**
+     * Build URI based on current component elements
+     *
+     * @return URI
+     */
+    URI build();
+
+    /**
+     * Set URI scheme as http or https
+     *
+     * @param scheme URI scheme
+     * @return Builder
+     */
+    HttpUriBuilder scheme(String scheme);
+
+    /**
+     * Set URI host address
+     *
+     * @param host Host address
+     * @return Builder
+     */
+    HttpUriBuilder host(String host);
+
+    /**
+     * Set URI port number
+     *
+     * @param port Port number
+     * @return Builder
+     */
+    HttpUriBuilder port(int port);
+
+    /**
+     * Set path with segments encoded according to URL standard requirements
+     *
+     * @param encodedPath URL-encoded path
+     * @return Builder
+     */
+    HttpUriBuilder encodedPath(String encodedPath);
+
+    /**
+     * Add path segment appending to current path
+     *
+     * @param pathSegment Path segment
+     * @return Builder
+     */
+    HttpUriBuilder addPathSegment(String pathSegment);
+
+    /**
+     * Add path segment appending to current path

Review Comment:
   This should be "Add query parameter..." right?



##########
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;
+
+    StandardHttpEntityHeaders(final Map<String, List<String>> headers) {
+        this.headers = headers;
+    }
+
+    @Override
+    public Optional<String> getFirstHeader(final String headerName) {
+        final List<String> values = getHeader(headerName);
+        return values.stream().findFirst();
+    }
+
+    @Override
+    public List<String> getHeader(final String headerName) {
+        Objects.requireNonNull(headerName, "Header Name required");
+        final List<String> values = headers.get(headerName);
+        return values == null ? Collections.emptyList() : Collections.unmodifiableList(values);

Review Comment:
   Do we prefer null checks in the NiFi codebase over the Optional.ofNullable(headers.get(headerName)).map(Collections::unmodifiableList).orElse(Collections::emptyList) functional style? Or simply you feel it'd add an unnecessary wrapping here?



##########
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 SSLSocketFactoryProvider sslSocketFactoryProvider = new StandardSSLSocketFactoryProvider();
+
+    private OkHttpClient okHttpClient;
+
+    /**
+     * Standard Web Client Service constructor creates OkHttpClient using default settings
+     */
+    public StandardWebClientService() {
+        okHttpClient = new OkHttpClient.Builder().build();
+    }
+
+    /**
+     * Set timeout for initial socket connection
+     *
+     * @param connectTimeout Connect Timeout
+     */
+    public void setConnectTimeout(final Duration connectTimeout) {
+        Objects.requireNonNull(connectTimeout, "Connect Timeout required");
+        okHttpClient = okHttpClient.newBuilder().connectTimeout(connectTimeout).build();
+    }
+
+    /**
+     * Set timeout for reading responses from socket connection
+     *
+     * @param readTimeout Read Timeout
+     */
+    public void setReadTimeout(final Duration readTimeout) {
+        Objects.requireNonNull(readTimeout, "Read Timeout required");
+        okHttpClient = okHttpClient.newBuilder().readTimeout(readTimeout).build();
+    }
+
+    /**
+     * Set timeout for writing requests to socket connection
+     *
+     * @param writeTimeout Write Timeout
+     */
+    public void setWriteTimeout(final Duration writeTimeout) {
+        Objects.requireNonNull(writeTimeout, "Write Timeout required");
+        okHttpClient = okHttpClient.newBuilder().writeTimeout(writeTimeout).build();
+    }
+
+    /**
+     * Set Proxy Context configuration for socket communication
+     *
+     * @param proxyContext Proxy Context configuration
+     */
+    public void setProxyContext(final ProxyContext proxyContext) {
+        Objects.requireNonNull(proxyContext, "Proxy Context required");
+        final Proxy proxy = Objects.requireNonNull(proxyContext.getProxy(), "Proxy required");
+        okHttpClient = okHttpClient.newBuilder().proxy(proxy).build();
+
+        final Optional<String> proxyUsername = proxyContext.getUsername();
+        if (proxyUsername.isPresent()) {
+            final String username = proxyUsername.get();
+            final String password = proxyContext.getPassword().orElseThrow(() -> new IllegalArgumentException("Proxy password required"));
+            final String credentials = Credentials.basic(username, password);
+            final BasicProxyAuthenticator proxyAuthenticator = new BasicProxyAuthenticator(credentials);
+            okHttpClient = okHttpClient.newBuilder().proxyAuthenticator(proxyAuthenticator).build();
+        }
+    }
+
+    /**
+     * Set Redirect Handling strategy
+     *
+     * @param redirectHandling Redirect Handling strategy
+     */
+    public void setRedirectHandling(final RedirectHandling redirectHandling) {
+        Objects.requireNonNull(redirectHandling, "Redirect Handling required");
+        final boolean followRedirects = RedirectHandling.FOLLOWED == redirectHandling;
+        okHttpClient = okHttpClient.newBuilder().followRedirects(followRedirects).followSslRedirects(followRedirects).build();
+    }
+
+    /**
+     * Set TLS Context overrides system default TLS settings for HTTPS communication
+     *
+     * @param tlsContext TLS Context
+     */
+    public void setTlsContext(final TlsContext tlsContext) {
+        Objects.requireNonNull(tlsContext, "TLS Context required");
+        final X509TrustManager trustManager = Objects.requireNonNull(tlsContext.getTrustManager(), "Trust Manager required");
+        final SSLSocketFactory sslSocketFactory = sslSocketFactoryProvider.getSocketFactory(tlsContext);
+        okHttpClient = okHttpClient.newBuilder().sslSocketFactory(sslSocketFactory, trustManager).build();
+    }
+
+    /**
+     * Create HTTP Request builder starting with specified HTTP Request Method
+     *
+     * @param httpRequestMethod HTTP Request Method required
+     * @return HTTP Request URI Specification builder
+     */
+    @Override
+    public HttpRequestUriSpec method(final HttpRequestMethod httpRequestMethod) {
+        Objects.requireNonNull(httpRequestMethod, "HTTP Request Method required");
+        return new StandardHttpRequestUriSpec(httpRequestMethod);
+    }
+
+    /**
+     * Create HTTP Request builder starting with HTTP DELETE
+     *
+     * @return HTTP Request URI Specification builder
+     */
+    @Override
+    public HttpRequestUriSpec delete() {
+        return method(StandardHttpRequestMethod.DELETE);
+    }
+
+    /**
+     * Create HTTP Request builder starting with HTTP GET
+     *
+     * @return HTTP Request URI Specification builder
+     */
+    @Override
+    public HttpRequestUriSpec get() {
+        return method(StandardHttpRequestMethod.GET);
+    }
+
+    /**
+     * Create HTTP Request builder starting with HTTP PATCH
+     *
+     * @return HTTP Request URI Specification builder
+     */
+    @Override
+    public HttpRequestUriSpec patch() {
+        return method(StandardHttpRequestMethod.PATCH);
+    }
+
+    /**
+     * Create HTTP Request builder starting with HTTP POST
+     *
+     * @return HTTP Request URI Specification builder
+     */
+    public HttpRequestUriSpec post() {
+        return method(StandardHttpRequestMethod.POST);
+    }
+
+    /**
+     * Create HTTP Request builder starting with HTTP PUT
+     *
+     * @return HTTP Request URI Specification builder
+     */
+    public HttpRequestUriSpec put() {
+        return method(StandardHttpRequestMethod.PUT);
+    }
+
+    class StandardHttpRequestUriSpec implements HttpRequestUriSpec {
+        private final HttpRequestMethod httpRequestMethod;
+
+        StandardHttpRequestUriSpec(final HttpRequestMethod httpRequestMethod) {
+            this.httpRequestMethod = httpRequestMethod;
+        }
+
+        @Override
+        public HttpRequestBodySpec uri(final URI uri) {
+            Objects.requireNonNull(uri, "URI required");
+            return new StandardHttpRequestBodySpec(httpRequestMethod, uri);
+        }
+    }
+
+    class StandardHttpRequestBodySpec implements HttpRequestBodySpec {
+        private static final long UNKNOWN_CONTENT_LENGTH = -1;
+
+        private final byte[] emptyBytes = new byte[0];

Review Comment:
   This could be static



-- 
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