You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ma...@apache.org on 2015/01/09 21:35:29 UTC
[1/5] incubator-nifi git commit: NIFI-239: Fix deprecated methods in
SplitXml processor
Repository: incubator-nifi
Updated Branches:
refs/heads/develop 591134276 -> 7e2aa9f36
NIFI-239: Fix deprecated methods in SplitXml processor
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/76ea1c64
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/76ea1c64
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/76ea1c64
Branch: refs/heads/develop
Commit: 76ea1c64ff41ab19c10f3157a15bf9faf619c1fd
Parents: 3b16d01
Author: Mark Payne <ma...@hotmail.com>
Authored: Fri Jan 9 08:26:32 2015 -0500
Committer: Mark Payne <ma...@hotmail.com>
Committed: Fri Jan 9 08:26:32 2015 -0500
----------------------------------------------------------------------
.../main/java/org/apache/nifi/processors/standard/SplitXml.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/76ea1c64/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitXml.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitXml.java b/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitXml.java
index 1919dbe..c5eda3d 100644
--- a/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitXml.java
+++ b/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitXml.java
@@ -284,7 +284,7 @@ public class SplitXml extends AbstractProcessor {
int attCount = atts.getLength();
for (int i = 0; i < attCount; i++) {
String attName = atts.getQName(i);
- String attValue = StringEscapeUtils.escapeXml(atts.getValue(i));
+ String attValue = StringEscapeUtils.escapeXml10(atts.getValue(i));
sb.append(" ").append(attName).append("=").append("\"").append(attValue).append("\"");
}
[2/5] incubator-nifi git commit: NIFI-239: Updated PostHTTP to not
use deprecated methods and classes in Apache HTTP Client
Posted by ma...@apache.org.
NIFI-239: Updated PostHTTP to not use deprecated methods and classes in Apache HTTP Client
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/d99180a7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/d99180a7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/d99180a7
Branch: refs/heads/develop
Commit: d99180a7160ac8a189507168dc97a35e0501eef9
Parents: 76ea1c6
Author: Mark Payne <ma...@hotmail.com>
Authored: Fri Jan 9 13:18:00 2015 -0500
Committer: Mark Payne <ma...@hotmail.com>
Committed: Fri Jan 9 13:18:00 2015 -0500
----------------------------------------------------------------------
.../nifi/processors/standard/PostHTTP.java | 227 +++++++++++--------
1 file changed, 136 insertions(+), 91 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/d99180a7/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/PostHTTP.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/PostHTTP.java b/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/PostHTTP.java
index 14f8a28..ee16610 100644
--- a/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/PostHTTP.java
+++ b/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/PostHTTP.java
@@ -16,12 +16,18 @@
*/
package org.apache.nifi.processors.standard;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URISyntaxException;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -39,20 +45,45 @@ import java.util.regex.Pattern;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
+import javax.net.ssl.SSLSession;
import javax.security.cert.X509Certificate;
import javax.servlet.http.HttpServletResponse;
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.conn.ManagedHttpClientConnection;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLContexts;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.entity.ContentProducer;
+import org.apache.http.entity.EntityTemplate;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.util.EntityUtils;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.flowfile.attributes.CoreAttributes;
-import org.apache.nifi.stream.io.BufferedInputStream;
-import org.apache.nifi.stream.io.BufferedOutputStream;
-import org.apache.nifi.stream.io.GZIPOutputStream;
-import org.apache.nifi.stream.io.LeakyBucketStreamThrottler;
-import org.apache.nifi.stream.io.StreamThrottler;
-import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.logging.ProcessorLog;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.DataUnit;
@@ -69,7 +100,12 @@ import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback;
import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.ssl.SSLContextService;
-import org.apache.nifi.ssl.SSLContextService.ClientAuth;
+import org.apache.nifi.stream.io.BufferedInputStream;
+import org.apache.nifi.stream.io.BufferedOutputStream;
+import org.apache.nifi.stream.io.GZIPOutputStream;
+import org.apache.nifi.stream.io.LeakyBucketStreamThrottler;
+import org.apache.nifi.stream.io.StreamThrottler;
+import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.util.FlowFilePackager;
import org.apache.nifi.util.FlowFilePackagerV1;
import org.apache.nifi.util.FlowFilePackagerV2;
@@ -78,31 +114,6 @@ import org.apache.nifi.util.FormatUtils;
import org.apache.nifi.util.ObjectHolder;
import org.apache.nifi.util.StopWatch;
-import org.apache.http.Header;
-import org.apache.http.HttpException;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseInterceptor;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpHead;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.params.ClientPNames;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.conn.HttpRoutedConnection;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.entity.ContentProducer;
-import org.apache.http.entity.EntityTemplate;
-import org.apache.http.impl.client.AbstractHttpClient;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.ExecutionContext;
-import org.apache.http.protocol.HttpContext;
-import org.apache.http.util.EntityUtils;
-
import com.sun.jersey.api.client.ClientResponse.Status;
@SupportsBatching
@@ -308,28 +319,64 @@ public class PostHTTP extends AbstractProcessor {
return config;
}
- final ClientConnectionManager conMan = new PoolingClientConnectionManager();
- registerUrlWithManager(url, context, conMan);
+ final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
+ final SSLContext sslContext;
+ try {
+ sslContext = createSSLContext(sslContextService);
+ } catch (final Exception e) {
+ throw new ProcessException(e);
+ }
+
+ final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null,
+ SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+
+ final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
+ .register("https", sslsf).build();
+
+ final PoolingHttpClientConnectionManager conMan = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
+ conMan.setDefaultMaxPerRoute(context.getMaxConcurrentTasks());
+ conMan.setMaxTotal(context.getMaxConcurrentTasks());
config = new Config(conMan);
final Config existingConfig = configMap.putIfAbsent(baseUrl, config);
return (existingConfig == null) ? config : existingConfig;
}
+
+
+ private SSLContext createSSLContext(final SSLContextService service) throws KeyStoreException, IOException, NoSuchAlgorithmException,
+ CertificateException, KeyManagementException, UnrecoverableKeyException
+ {
+ final KeyStore truststore = KeyStore.getInstance(service.getTrustStoreType());
+ try (final InputStream in = new FileInputStream(new File(service.getTrustStoreFile()))) {
+ truststore.load(in, service.getTrustStorePassword().toCharArray());
+ }
+
+ final KeyStore keystore = KeyStore.getInstance(service.getKeyStoreType());
+ try (final InputStream in = new FileInputStream(new File(service.getKeyStoreFile()))) {
+ keystore.load(in, service.getKeyStorePassword().toCharArray());
+ }
+
+ SSLContext sslContext = SSLContexts.custom()
+ .loadTrustMaterial(truststore, new TrustSelfSignedStrategy())
+ .loadKeyMaterial(keystore, service.getKeyStorePassword().toCharArray())
+ .build();
+
+ return sslContext;
+ }
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
final boolean sendAsFlowFile = context.getProperty(SEND_AS_FLOWFILE).asBoolean();
final int compressionLevel = context.getProperty(COMPRESSION_LEVEL).asInteger();
-
- final HttpParams httpParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParams, context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
- HttpConnectionParams.setSoTimeout(httpParams, context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
- httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
final String userAgent = context.getProperty(USER_AGENT).getValue();
- if (userAgent != null) {
- httpParams.setParameter("http.useragent", userAgent);
- }
+ final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
+ requestConfigBuilder.setConnectionRequestTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
+ requestConfigBuilder.setConnectTimeout(context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
+ requestConfigBuilder.setRedirectsEnabled(false);
+ requestConfigBuilder.setSocketTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
+ final RequestConfig requestConfig = requestConfigBuilder.build();
+
final StreamThrottler throttler = throttlerRef.get();
final ProcessorLog logger = getLogger();
@@ -339,7 +386,7 @@ public class PostHTTP extends AbstractProcessor {
final List<FlowFile> toSend = new ArrayList<>();
DestinationAccepts destinationAccepts = null;
- HttpClient client = null;
+ CloseableHttpClient client = null;
final String transactionId = UUID.randomUUID().toString();
final ObjectHolder<String> dnHolder = new ObjectHolder<>("none");
@@ -371,15 +418,20 @@ public class PostHTTP extends AbstractProcessor {
if (client == null || destinationAccepts == null) {
final Config config = getConfig(url, context);
- final ClientConnectionManager conMan = config.getConnectionManager();
- client = new DefaultHttpClient(conMan, httpParams);
-
- ((AbstractHttpClient) client).addResponseInterceptor(new HttpResponseInterceptor() {
+ final HttpClientConnectionManager conMan = config.getConnectionManager();
+
+ final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
+ clientBuilder.setConnectionManager(conMan);
+ clientBuilder.setUserAgent(userAgent);
+ clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() {
@Override
- public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
- final HttpRoutedConnection httpRoutedConnection = (HttpRoutedConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
- if (httpRoutedConnection.isSecure()) {
- final X509Certificate[] certChain = httpRoutedConnection.getSSLSession().getPeerCertificateChain();
+ public void process(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
+ HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext);
+ ManagedHttpClientConnection conn = coreContext.getConnection(ManagedHttpClientConnection.class);
+ SSLSession sslSession = conn.getSSLSession();
+
+ if ( sslSession != null ) {
+ final X509Certificate[] certChain = sslSession.getPeerCertificateChain();
if (certChain == null || certChain.length == 0) {
throw new SSLPeerUnverifiedException("No certificates found");
}
@@ -389,6 +441,23 @@ public class PostHTTP extends AbstractProcessor {
}
}
});
+
+ clientBuilder.disableAutomaticRetries();
+ clientBuilder.disableContentCompression();
+
+ final String username = context.getProperty(USERNAME).getValue();
+ final String password = context.getProperty(PASSWORD).getValue();
+ // set the credentials if appropriate
+ if (username != null) {
+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ if (password == null) {
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username));
+ } else {
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
+ };
+ clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
+ }
+ client = clientBuilder.build();
// determine whether or not destination accepts flowfile/gzip
destinationAccepts = config.getDestinationAccepts();
@@ -491,7 +560,8 @@ public class PostHTTP extends AbstractProcessor {
entity.setChunked(context.getProperty(CHUNKED_ENCODING).asBoolean());
post.setEntity(entity);
-
+ post.setConfig(requestConfig);
+
final String contentType;
if (sendAsFlowFile) {
if (accepts.isFlowFileV3Accepted()) {
@@ -537,7 +607,7 @@ public class PostHTTP extends AbstractProcessor {
final String uploadDataRate;
final long uploadMillis;
- final HttpResponse response;
+ CloseableHttpResponse response = null;
try {
final StopWatch stopWatch = new StopWatch(true);
response = client.execute(post);
@@ -556,6 +626,14 @@ public class PostHTTP extends AbstractProcessor {
session.transfer(flowFile, REL_FAILURE);
}
return;
+ } finally {
+ if ( response != null ) {
+ try {
+ response.close();
+ } catch (IOException e) {
+ getLogger().warn("Failed to close HTTP Response due to {}", new Object[] {e});
+ }
+ }
}
// If we get a 'SEE OTHER' status code and an HTTP header that indicates that the intent
@@ -686,39 +764,6 @@ public class PostHTTP extends AbstractProcessor {
}
}
- private void registerUrlWithManager(final String url, final ProcessContext processContext, final ClientConnectionManager conMan) {
- URI uriObject;
- try {
- uriObject = new URI(url);
- } catch (URISyntaxException e) {
- throw new ProcessException(e); // won't happen because of our
- }
- int port = uriObject.getPort();
- if (port == -1) {
- port = 443;
- }
-
- final boolean secure = (url.toLowerCase().startsWith("https"));
- if (!secure) {
- return;
- }
-
- final SSLContext sslContext = createSslContext(processContext);
- final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);
- final Scheme newScheme = new Scheme("https", port, sslSocketFactory);
- conMan.getSchemeRegistry().register(newScheme);
- }
-
- /**
- * Creates a SSL context based on the processor's optional properties.
- * <p/>
- *
- * @return an SSLContext instance
- */
- private SSLContext createSslContext(final ProcessContext context) {
- final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
- return (sslContextService == null) ? null : sslContextService.createSSLContext(ClientAuth.REQUIRED);
- }
private DestinationAccepts getDestinationAcceptance(final HttpClient client, final String uri, final ProcessorLog logger, final String transactionId) throws IOException {
final HttpHead head = new HttpHead(uri);
@@ -838,9 +883,9 @@ public class PostHTTP extends AbstractProcessor {
private static class Config {
private volatile DestinationAccepts destinationAccepts;
- private final ClientConnectionManager conMan;
+ private final HttpClientConnectionManager conMan;
- public Config(final ClientConnectionManager conMan) {
+ public Config(final HttpClientConnectionManager conMan) {
this.conMan = conMan;
}
@@ -852,7 +897,7 @@ public class PostHTTP extends AbstractProcessor {
this.destinationAccepts = destinationAccepts;
}
- public ClientConnectionManager getConnectionManager() {
+ public HttpClientConnectionManager getConnectionManager() {
return conMan;
}
}
[5/5] incubator-nifi git commit: Merge branch 'develop' of
https://git-wip-us.apache.org/repos/asf/incubator-nifi into develop
Posted by ma...@apache.org.
Merge branch 'develop' of https://git-wip-us.apache.org/repos/asf/incubator-nifi into develop
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/7e2aa9f3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/7e2aa9f3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/7e2aa9f3
Branch: refs/heads/develop
Commit: 7e2aa9f368f9c63c107ff245bca033d4084ab496
Parents: 36d36c4 5911342
Author: Mark Payne <ma...@hotmail.com>
Authored: Fri Jan 9 15:35:25 2015 -0500
Committer: Mark Payne <ma...@hotmail.com>
Committed: Fri Jan 9 15:35:25 2015 -0500
----------------------------------------------------------------------
pom.xml | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 89 insertions(+)
----------------------------------------------------------------------
[3/5] incubator-nifi git commit: Merge branch 'develop' of
https://git-wip-us.apache.org/repos/asf/incubator-nifi into develop
Posted by ma...@apache.org.
Merge branch 'develop' of https://git-wip-us.apache.org/repos/asf/incubator-nifi into develop
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/dc4c3220
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/dc4c3220
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/dc4c3220
Branch: refs/heads/develop
Commit: dc4c3220361e1e792a40186af54cb7bb81d6371b
Parents: d99180a d996060
Author: Mark Payne <ma...@hotmail.com>
Authored: Fri Jan 9 14:46:20 2015 -0500
Committer: Mark Payne <ma...@hotmail.com>
Committed: Fri Jan 9 14:46:20 2015 -0500
----------------------------------------------------------------------
.../src/main/asciidoc/administration-guide.adoc | 73 ++++++++++++++++++--
1 file changed, 67 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
[4/5] incubator-nifi git commit: NIFI-239: Updated deprecated methods
and classes for GetHTTP
Posted by ma...@apache.org.
NIFI-239: Updated deprecated methods and classes for GetHTTP
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/36d36c43
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/36d36c43
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/36d36c43
Branch: refs/heads/develop
Commit: 36d36c43ae377f398210db32ca6ce0100bd52089
Parents: dc4c322
Author: Mark Payne <ma...@hotmail.com>
Authored: Fri Jan 9 15:25:14 2015 -0500
Committer: Mark Payne <ma...@hotmail.com>
Committed: Fri Jan 9 15:25:14 2015 -0500
----------------------------------------------------------------------
.../nifi/processors/standard/GetHTTP.java | 175 ++++++++++--------
.../processors/standard/HelloWorldServlet.java | 41 +++++
.../nifi/processors/standard/TestGetHTTP.java | 176 +++++++++++++-----
.../processors/standard/TestInvokeHTTP.java | 46 +----
.../nifi/processors/standard/TestServer.java | 177 +++++++++++++++++++
5 files changed, 464 insertions(+), 151 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/36d36c43/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java b/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java
index 85fc1ac..35873b1 100644
--- a/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java
+++ b/nar-bundles/standard-bundle/standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java
@@ -20,8 +20,15 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
@@ -42,6 +49,24 @@ import java.util.regex.Pattern;
import javax.net.ssl.SSLContext;
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLContexts;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
@@ -62,21 +87,6 @@ import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.ssl.SSLContextService;
import org.apache.nifi.ssl.SSLContextService.ClientAuth;
import org.apache.nifi.util.StopWatch;
-import org.apache.http.Header;
-import org.apache.http.HttpResponse;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.params.ClientPNames;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.BasicClientConnectionManager;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpParams;
@Tags({"get", "fetch", "poll", "http", "https", "ingest", "source", "input"})
@CapabilityDescription("Fetches a file via HTTP")
@@ -263,6 +273,28 @@ public class GetHTTP extends AbstractSessionFactoryProcessor {
return results;
}
+
+ private SSLContext createSSLContext(final SSLContextService service) throws KeyStoreException, IOException, NoSuchAlgorithmException,
+ CertificateException, KeyManagementException, UnrecoverableKeyException
+ {
+ final KeyStore truststore = KeyStore.getInstance(service.getTrustStoreType());
+ try (final InputStream in = new FileInputStream(new File(service.getTrustStoreFile()))) {
+ truststore.load(in, service.getTrustStorePassword().toCharArray());
+ }
+
+ final KeyStore keystore = KeyStore.getInstance(service.getKeyStoreType());
+ try (final InputStream in = new FileInputStream(new File(service.getKeyStoreFile()))) {
+ keystore.load(in, service.getKeyStorePassword().toCharArray());
+ }
+
+ SSLContext sslContext = SSLContexts.custom()
+ .loadTrustMaterial(truststore, new TrustSelfSignedStrategy())
+ .loadKeyMaterial(keystore, service.getKeyStorePassword().toCharArray())
+ .build();
+
+ return sslContext;
+ }
+
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
final ProcessorLog logger = getLogger();
@@ -274,6 +306,7 @@ public class GetHTTP extends AbstractSessionFactoryProcessor {
logger.warn("found FlowFile {} in input queue; transferring to success", new Object[]{incomingFlowFile});
}
+ // get the URL
final String url = context.getProperty(URL).getValue();
final URI uri;
String source = url;
@@ -283,32 +316,75 @@ public class GetHTTP extends AbstractSessionFactoryProcessor {
} catch (URISyntaxException swallow) {
// this won't happen as the url has already been validated
}
- final ClientConnectionManager conMan = createConnectionManager(context);
+
+ // get the ssl context service
+ final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
+
+ // create the connection manager
+ final HttpClientConnectionManager conMan;
+ if ( sslContextService == null ) {
+ conMan = new BasicHttpClientConnectionManager();
+ } else {
+ final SSLContext sslContext;
+ try {
+ sslContext = createSSLContext(sslContextService);
+ } catch (final Exception e) {
+ throw new ProcessException(e);
+ }
+
+ final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null,
+ SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+
+ final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
+ .register("https", sslsf).build();
+
+ conMan = new BasicHttpClientConnectionManager(socketFactoryRegistry);
+ }
+
try {
- final HttpParams httpParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParams, context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS)
- .intValue());
- HttpConnectionParams.setSoTimeout(httpParams, context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
- httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, context.getProperty(FOLLOW_REDIRECTS).asBoolean());
+ // build the request configuration
+ final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
+ requestConfigBuilder.setConnectionRequestTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
+ requestConfigBuilder.setConnectTimeout(context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
+ requestConfigBuilder.setRedirectsEnabled(false);
+ requestConfigBuilder.setSocketTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
+ requestConfigBuilder.setRedirectsEnabled(context.getProperty(FOLLOW_REDIRECTS).asBoolean());
+
+ // build the http client
+ final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
+ clientBuilder.setConnectionManager(conMan);
+
+ // include the user agent
final String userAgent = context.getProperty(USER_AGENT).getValue();
if (userAgent != null) {
- httpParams.setParameter("http.useragent", userAgent);
+ clientBuilder.setUserAgent(userAgent);
}
-
- final HttpClient client = new DefaultHttpClient(conMan, httpParams);
-
+
+ // set the ssl context if necessary
+ if (sslContextService != null) {
+ clientBuilder.setSslcontext(sslContextService.createSSLContext(ClientAuth.REQUIRED));
+ }
+
final String username = context.getProperty(USERNAME).getValue();
final String password = context.getProperty(PASSWORD).getValue();
+
+ // set the credentials if appropriate
if (username != null) {
+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (password == null) {
- ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username));
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username));
} else {
- ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY,
- new UsernamePasswordCredentials(username, password));
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}
+ clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
+ // create the http client
+ final HttpClient client = clientBuilder.build();
+
+ // create request
final HttpGet get = new HttpGet(url);
+ get.setConfig(requestConfigBuilder.build());
get.addHeader(HEADER_IF_MODIFIED_SINCE, lastModifiedRef.get());
get.addHeader(HEADER_IF_NONE_MATCH, entityTagRef.get());
@@ -401,47 +477,4 @@ public class GetHTTP extends AbstractSessionFactoryProcessor {
conMan.shutdown();
}
}
-
- private ClientConnectionManager createConnectionManager(final ProcessContext processContext) {
- final String url = processContext.getProperty(URL).getValue();
- final boolean secure = (url.toLowerCase().startsWith("https"));
- URI uriObject;
- try {
- uriObject = new URI(url);
- } catch (URISyntaxException e) {
- throw new ProcessException(e); // will not happen because of our validators
- }
- int port = uriObject.getPort();
- if (port == -1) {
- port = 443;
- }
-
- final ClientConnectionManager conMan = new BasicClientConnectionManager();
- if (secure) {
- try {
- final SSLContext context = createSslContext(processContext);
- final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(context);
- final Scheme sslScheme = new Scheme("https", port, sslSocketFactory);
- conMan.getSchemeRegistry().register(sslScheme);
- } catch (final Exception e) {
- getLogger().error("Unable to setup SSL connection due to ", e);
- return null;
- }
- }
-
- return conMan;
- }
-
- /**
- * Creates a SSL context based on the processor's optional properties.
- * <p/>
- *
- * @return a SSLContext instance
- * <p/>
- * @throws ProcessingException if the context could not be created
- */
- private SSLContext createSslContext(final ProcessContext context) {
- final SSLContextService service = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
- return (service == null) ? null : service.createSSLContext(ClientAuth.REQUIRED);
- }
}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/36d36c43/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/HelloWorldServlet.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/HelloWorldServlet.java b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/HelloWorldServlet.java
new file mode 100644
index 0000000..720bb74
--- /dev/null
+++ b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/HelloWorldServlet.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.processors.standard;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.nifi.stream.io.StreamUtils;
+
+public class HelloWorldServlet extends HttpServlet {
+ private static final long serialVersionUID = -8821242726929583763L;
+
+ @Override
+ protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
+ final ServletOutputStream out = resp.getOutputStream();
+ try (final FileInputStream fis = new FileInputStream("src/test/resources/hello.txt")) {
+ StreamUtils.copy(fis, out);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/36d36c43/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java
index 85cff88..63bdcf8 100644
--- a/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java
+++ b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java
@@ -16,24 +16,29 @@
*/
package org.apache.nifi.processors.standard;
-import org.apache.nifi.processors.standard.GetHTTP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Properties;
import org.apache.nifi.processor.ProcessorInitializationContext;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.ssl.SSLContextService;
+import org.apache.nifi.ssl.StandardSSLContextService;
+import org.apache.nifi.util.MockControllerServiceInitializationContext;
+import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.MockProcessContext;
import org.apache.nifi.util.MockProcessorInitializationContext;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
-
-import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.junit.AfterClass;
+import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
@@ -47,7 +52,7 @@ public class TestGetHTTP {
private static Logger LOGGER;
private TestRunner controller;
-
+
@BeforeClass
public static void before() {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
@@ -73,23 +78,40 @@ public class TestGetHTTP {
assertTrue(confDir.delete());
}
+ private static Map<String, String> createSslProperties() {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
+ map.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
+ map.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
+ map.put(StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
+ map.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
+ map.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
+ return map;
+ }
+
@Test
public final void testContentModified() throws Exception {
- String destination = "http://localhost:10203";
-
- // set up NiFi mock controller
- controller = TestRunners.newTestRunner(GetHTTP.class);
- controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
- controller.setProperty(GetHTTP.URL, destination);
- controller.setProperty(GetHTTP.FILENAME, "testFile");
- controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
// set up web service
- Server server = new Server(10203);
ServletHandler handler = new ServletHandler();
- server.setHandler(handler);
handler.addServletWithMapping(RESTServiceContentModified.class, "/*");
+
+ // create the service
+ TestServer server = new TestServer();
+ server.addHandler(handler);
+
try {
- server.start();
+ server.startServer();
+
+ // this is the base url with the random port
+ String destination = server.getUrl();
+
+ // set up NiFi mock controller
+ controller = TestRunners.newTestRunner(GetHTTP.class);
+ controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
+ controller.setProperty(GetHTTP.URL, destination);
+ controller.setProperty(GetHTTP.FILENAME, "testFile");
+ controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
+
GetHTTP getHTTPProcessor = (GetHTTP) controller.getProcessor();
assertEquals("", getHTTPProcessor.entityTagRef.get());
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", getHTTPProcessor.lastModifiedRef.get());
@@ -150,8 +172,7 @@ public class TestGetHTTP {
// shutdown web service
} finally {
- server.stop();
- server.destroy();
+ server.shutdownServer();
}
}
@@ -163,20 +184,30 @@ public class TestGetHTTP {
for (File file : files) {
assertTrue("Failed to delete " + file.getName(), file.delete());
}
- // set up NiFi mock controller
- controller = TestRunners.newTestRunner(GetHTTP.class);
- controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
- controller.setProperty(GetHTTP.FILENAME, "testFile");
- String destination = "http://localhost:10203";
- controller.setProperty(GetHTTP.URL, destination);
- controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
- Server server = new Server(10203);
+
+ // set up web service
ServletHandler handler = new ServletHandler();
- server.setHandler(handler);
handler.addServletWithMapping(RESTServiceContentModified.class, "/*");
+
+ // create the service
+ TestServer server = new TestServer();
+ server.addHandler(handler);
+
try {
- server.start();
+ server.startServer();
+
+ // get the server url
+ String destination = server.getUrl();
+
+ // set up NiFi mock controller
+ controller = TestRunners.newTestRunner(GetHTTP.class);
+ controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
+ controller.setProperty(GetHTTP.FILENAME, "testFile");
+ controller.setProperty(GetHTTP.URL, destination);
+ controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
+
GetHTTP getHTTPProcessor = (GetHTTP) controller.getProcessor();
+
assertEquals("", getHTTPProcessor.entityTagRef.get());
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", getHTTPProcessor.lastModifiedRef.get());
controller.run(2);
@@ -224,28 +255,32 @@ public class TestGetHTTP {
// shutdown web service
} finally {
- server.stop();
- server.destroy();
+ server.shutdownServer();
}
}
@Test
public final void testUserAgent() throws Exception {
- String destination = "http://localhost:10203";
-
- // set up NiFi mock controller
- controller = TestRunners.newTestRunner(GetHTTP.class);
- controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
- controller.setProperty(GetHTTP.URL, destination);
- controller.setProperty(GetHTTP.FILENAME, "testFile");
- controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
// set up web service
- Server server = new Server(10203);
ServletHandler handler = new ServletHandler();
- server.setHandler(handler);
handler.addServletWithMapping(UserAgentTestingServlet.class, "/*");
+
+ // create the service
+ TestServer server = new TestServer();
+ server.addHandler(handler);
+
try {
- server.start();
+ server.startServer();
+
+ String destination = server.getUrl();
+
+ // set up NiFi mock controller
+ controller = TestRunners.newTestRunner(GetHTTP.class);
+ controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
+ controller.setProperty(GetHTTP.URL, destination);
+ controller.setProperty(GetHTTP.FILENAME, "testFile");
+ controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
+
controller.run();
controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0);
@@ -255,8 +290,65 @@ public class TestGetHTTP {
// shutdown web service
} finally {
- server.stop();
- server.destroy();
+ server.shutdownServer();
+ }
+ }
+
+ private Map<String, String> getSslProperties() {
+ Map<String, String> props = new HashMap<String, String>();
+ props.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
+ props.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
+ props.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
+ props.put(StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
+ props.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
+ props.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
+ return props;
+ }
+
+ private void useSSLContextService() {
+ final SSLContextService service = new StandardSSLContextService();
+ try {
+ controller.addControllerService("ssl-service", service, getSslProperties());
+ } catch (InitializationException ex) {
+ ex.printStackTrace();
+ Assert.fail("Could not create SSL Context Service");
+ }
+
+ controller.setProperty(GetHTTP.SSL_CONTEXT_SERVICE, "ssl-service");
+ }
+
+ @Test
+ public final void testSecure() throws Exception {
+ // set up web service
+ ServletHandler handler = new ServletHandler();
+ handler.addServletWithMapping(HelloWorldServlet.class, "/*");
+
+ // create the service
+ TestServer server = new TestServer(getSslProperties());
+ server.addHandler(handler);
+
+ try {
+ server.startServer();
+
+ String destination = server.getSecureUrl();
+
+ // set up NiFi mock controller
+ controller = TestRunners.newTestRunner(GetHTTP.class);
+ useSSLContextService();
+
+ controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
+ controller.setProperty(GetHTTP.URL, destination);
+ controller.setProperty(GetHTTP.FILENAME, "testFile");
+ controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
+
+ controller.run();
+ controller.assertAllFlowFilesTransferred(GetHTTP.REL_SUCCESS, 1);
+ final MockFlowFile mff = controller.getFlowFilesForRelationship(GetHTTP.REL_SUCCESS).get(0);
+ mff.assertContentEquals("Hello, World!");
+ } finally {
+ server.shutdownServer();
}
}
+
+
}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/36d36c43/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java
index 85cd3db..b98ba13 100644
--- a/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java
+++ b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java
@@ -16,7 +16,6 @@
*/
package org.apache.nifi.processors.standard;
-import org.apache.nifi.processors.standard.InvokeHTTP;
import static org.junit.Assert.*;
@@ -42,11 +41,7 @@ import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
-import org.eclipse.jetty.server.handler.HandlerCollection;
-import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
@@ -57,7 +52,7 @@ import org.junit.Test;
public class TestInvokeHTTP {
private static Map<String, String> sslProperties;
- private static Server server;
+ private static TestServer server;
private static String url;
private TestRunner runner;
@@ -74,19 +69,15 @@ public class TestInvokeHTTP {
// create a Jetty server on a random port
server = createServer();
- server.start();
-
- // we need the port to construct the base url
- int port = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
+ server.startServer();
// this is the base url with the random port
- url = "https://localhost:" + port;
-
+ url = server.getSecureUrl();
}
@AfterClass
public static void afterClass() throws Exception {
- server.stop();
+ server.shutdownServer();
}
@Before
@@ -95,13 +86,7 @@ public class TestInvokeHTTP {
runner.addControllerService("ssl-context", new StandardSSLContextService(), sslProperties);
runner.setProperty(Config.PROP_SSL_CONTEXT_SERVICE, "ssl-context");
- HandlerCollection hc = (HandlerCollection) server.getHandler();
- Handler[] ha = hc.getHandlers();
- if (ha != null) {
- for (Handler h : ha) {
- hc.removeHandler(h);
- }
- }
+ server.clearHandlers();
}
@After
@@ -110,7 +95,7 @@ public class TestInvokeHTTP {
}
private void addHandler(Handler handler) {
- ((HandlerCollection) server.getHandler()).addHandler(handler);
+ server.addHandler(handler);
}
@Test
@@ -537,23 +522,8 @@ public class TestInvokeHTTP {
return map;
}
- private static Server createServer() throws IOException {
- SslContextFactory ssl = new SslContextFactory();
- ssl.setKeyStorePath(sslProperties.get(StandardSSLContextService.KEYSTORE.getName()));
- ssl.setKeyStorePassword(sslProperties.get(StandardSSLContextService.KEYSTORE_PASSWORD.getName()));
- ssl.setKeyStoreType(sslProperties.get(StandardSSLContextService.KEYSTORE_TYPE.getName()));
- ssl.setTrustStorePath(sslProperties.get(StandardSSLContextService.TRUSTSTORE.getName()));
- ssl.setTrustStorePassword(sslProperties.get(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName()));
- ssl.setTrustStoreType(sslProperties.get(StandardSSLContextService.TRUSTSTORE_TYPE.getName()));
-
- ssl.setNeedClientAuth(true);
-
- Server server = new Server();
-
- server.addConnector(new ServerConnector(server, ssl));
- server.setHandler(new HandlerCollection(true));
-
- return server;
+ private static TestServer createServer() throws IOException {
+ return new TestServer(sslProperties);
}
private static void createFlowFiles(final TestRunner testRunner) throws UnsupportedEncodingException {
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/36d36c43/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestServer.java
----------------------------------------------------------------------
diff --git a/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestServer.java b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestServer.java
new file mode 100644
index 0000000..53b79e6
--- /dev/null
+++ b/nar-bundles/standard-bundle/standard-processors/src/test/java/org/apache/nifi/processors/standard/TestServer.java
@@ -0,0 +1,177 @@
+/*
+ * 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.processors.standard;
+
+import java.util.Map;
+import org.apache.nifi.ssl.StandardSSLContextService;
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.server.handler.HandlerCollection;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+
+/**
+ * Test server to assist with unit tests that requires a server to be stood up.
+ */
+public class TestServer {
+
+ private Server jetty;
+ private boolean secure = false;
+
+ /**
+ * Creates the test server.
+ */
+ public TestServer() {
+ createServer(null);
+ }
+
+ /**
+ * Creates the test server.
+ *
+ * @param sslProperties SSLProps to be used in the secure connection. The keys
+ * should should use the StandardSSLContextService properties.
+ */
+ public TestServer(final Map<String, String> sslProperties) {
+ createServer(sslProperties);
+ }
+
+ /**
+ * Creates the server.
+ *
+ * @param webappContext
+ * @return
+ */
+ private void createServer(final Map<String, String> sslProperties) {
+ jetty = new Server();
+
+ // create the unsecure connector
+ createConnector();
+
+ // create the secure connector if sslProperties are specified
+ if (sslProperties != null) {
+ createSecureConnector(sslProperties);
+ }
+
+ jetty.setHandler(new HandlerCollection(true));
+ }
+
+ /**
+ * Creates the http connection
+ */
+ private void createConnector() {
+ final ServerConnector http = new ServerConnector(jetty);
+ http.setPort(0);
+ jetty.addConnector(http);
+ }
+
+ /**
+ * Creates the https connector.
+ *
+ * @return
+ */
+ private void createSecureConnector(final Map<String, String> sslProperties) {
+ SslContextFactory ssl = new SslContextFactory();
+ ssl.setKeyStorePath(sslProperties.get(StandardSSLContextService.KEYSTORE.getName()));
+ ssl.setKeyStorePassword(sslProperties.get(StandardSSLContextService.KEYSTORE_PASSWORD.getName()));
+ ssl.setKeyStoreType(sslProperties.get(StandardSSLContextService.KEYSTORE_TYPE.getName()));
+ ssl.setTrustStorePath(sslProperties.get(StandardSSLContextService.TRUSTSTORE.getName()));
+ ssl.setTrustStorePassword(sslProperties.get(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName()));
+ ssl.setTrustStoreType(sslProperties.get(StandardSSLContextService.TRUSTSTORE_TYPE.getName()));
+ ssl.setNeedClientAuth(true);
+
+ // build the connector
+ final ServerConnector https = new ServerConnector(jetty, ssl);
+
+ // set host and port
+ https.setPort(0);
+
+ // add the connector
+ jetty.addConnector(https);
+
+ // mark secure as enabled
+ secure = true;
+ }
+
+ public void clearHandlers() {
+ HandlerCollection hc = (HandlerCollection) jetty.getHandler();
+ Handler[] ha = hc.getHandlers();
+ if (ha != null) {
+ for (Handler h : ha) {
+ hc.removeHandler(h);
+ }
+ }
+ }
+
+ public void addHandler(Handler handler) {
+ ((HandlerCollection) jetty.getHandler()).addHandler(handler);
+ }
+
+ /**
+ * Starts the server.
+ *
+ * @throws Exception
+ */
+ public void startServer() throws Exception {
+ jetty.start();
+ }
+
+ /**
+ * Stops the server.
+ *
+ * @throws Exception
+ */
+ public void shutdownServer() throws Exception {
+ jetty.stop();
+ jetty.destroy();
+ }
+
+ private int getPort() {
+ if (!jetty.isStarted()) {
+ throw new IllegalStateException("Jetty server not started");
+ }
+ return ((ServerConnector) jetty.getConnectors()[0]).getLocalPort();
+ }
+
+ private int getSecurePort() {
+ if (!jetty.isStarted()) {
+ throw new IllegalStateException("Jetty server not started");
+ }
+ return ((ServerConnector) jetty.getConnectors()[1]).getLocalPort();
+ }
+
+ /**
+ * Returns the url for the server.
+ *
+ * @return
+ */
+ public String getUrl() {
+ return "http://localhost:" + getPort();
+ }
+
+ /**
+ * Returns the secure url for the server.
+ *
+ * @return
+ */
+ public String getSecureUrl() {
+ String url = null;
+ if (secure) {
+ url = "https://localhost:" + getSecurePort();
+ }
+ return url;
+ }
+}