NIFI-1378 added validator to ensure the JMS URI has a scheme set

Signed-off-by: Aldrin Piri <aldrin@apache.org>
This commit is contained in:
joewitt 2016-01-09 22:08:44 -05:00 committed by Aldrin Piri
parent 4f7a4e83b1
commit 0c68e2c3a8
3 changed files with 48 additions and 2 deletions

View File

@ -16,7 +16,11 @@
*/
package org.apache.nifi.processors.standard.util;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.components.Validator;
import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.ssl.SSLContextService;
@ -49,6 +53,27 @@ public class JmsProperties {
.name("URL")
.description("The URL of the JMS Server")
.addValidator(StandardValidators.URI_VALIDATOR)
.addValidator(new Validator() {
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
}
final ValidationResult.Builder builder = new ValidationResult.Builder();
builder.subject(subject).input(input).explanation("Valid URL").valid(true);
try {
final String evaluatedInput = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();
final URI uri = new URI(evaluatedInput);
if (uri.getScheme() == null) {
builder.explanation("JMS URI must have a scheme set such as 'jms','ssl','tcp','vm',etc..").valid(false);
}
} catch (final URISyntaxException urie) {
builder.explanation("JMS URI not valid").valid(false);
}
return builder.build();
}
}
)
.required(true)
.build();
public static final PropertyDescriptor TIMEOUT = new PropertyDescriptor.Builder()

View File

@ -40,6 +40,17 @@ import org.junit.Test;
public class TestGetJMSQueue {
@Test
public void testInvalidURL() throws Exception {
GetJMSQueue getJmsQueue = new GetJMSQueue();
TestRunner runner = TestRunners.newTestRunner(getJmsQueue);
runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
runner.setProperty(JmsProperties.URL, "localhost");
runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
runner.assertNotValid();
}
@Test
public void testSendTextToQueue() throws Exception {
PutJMS putJms = new PutJMS();

View File

@ -78,6 +78,17 @@ public class TestPutJMS {
assertTrue(relationships.contains(PutJMS.REL_SUCCESS));
}
@Test
public void testInvalidURL() throws Exception {
PutJMS putJms = new PutJMS();
TestRunner runner = TestRunners.newTestRunner(putJms);
runner.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
runner.setProperty(JmsProperties.URL, "localhost");
runner.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_TYPE);
runner.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
runner.assertNotValid();
}
@Test
public void testCleanupResources() throws JMSException, NoSuchFieldException, IllegalAccessException {
final PutJMS putJMS = new PutJMS();
@ -98,7 +109,6 @@ public class TestPutJMS {
assertNull(wrappedMessageProducerQueue.peek());
}
@Test
public void testCreateMessageDirectly() throws JMSException {
final PutJMS putJMS = new PutJMS();