[CAMEL-10786] Adding the missing resources
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/507e8b5f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/507e8b5f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/507e8b5f Branch: refs/heads/master Commit: 507e8b5f849e85f78548cba2acfd9341e5975b07 Parents: 1b40de6 Author: Sergey Beryozkin <sberyoz...@gmail.com> Authored: Tue Feb 21 12:43:51 2017 +0000 Committer: Sergey Beryozkin <sberyoz...@gmail.com> Committed: Tue Feb 21 12:43:51 2017 +0000 ---------------------------------------------------------------------- .../azure/queue/QueueServiceComponent.java | 70 ++++++++++ .../azure/queue/QueueServiceConfiguration.java | 66 ++++++++++ .../azure/queue/QueueServiceConstants.java | 28 ++++ .../azure/queue/QueueServiceConsumer.java | 68 ++++++++++ .../azure/queue/QueueServiceEndpoint.java | 83 ++++++++++++ .../azure/queue/QueueServiceOperations.java | 22 ++++ .../azure/queue/QueueServiceProducer.java | 93 ++++++++++++++ .../component/azure/queue/QueueServiceUtil.java | 67 ++++++++++ .../org/apache/camel/component/azure-queue | 18 +++ .../QueueServiceComponentConfigurationTest.java | 128 +++++++++++++++++++ .../azure/queue/QueueServiceUtilTest.java | 88 +++++++++++++ .../QueueServiceComponentAutoConfiguration.java | 80 ++++++++++++ 12 files changed, 811 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java new file mode 100644 index 0000000..42bd47d --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java @@ -0,0 +1,70 @@ +/** + * 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.azure.queue; + +import java.util.Map; + +import com.microsoft.azure.storage.StorageCredentials; +import com.microsoft.azure.storage.queue.CloudQueue; +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.impl.UriEndpointComponent; + +public class QueueServiceComponent extends UriEndpointComponent { + + public QueueServiceComponent() { + super(QueueServiceEndpoint.class); + } + + public QueueServiceComponent(CamelContext context) { + super(context, QueueServiceEndpoint.class); + } + + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + QueueServiceConfiguration configuration = new QueueServiceConfiguration(); + setProperties(configuration, parameters); + + String[] parts = null; + if (remaining != null) { + parts = remaining.split("/"); + } + if (parts == null || parts.length < 2) { + throw new IllegalArgumentException("The account and queue names must be specified."); + } + if (parts.length > 2) { + throw new IllegalArgumentException("Only the account and queue names must be specified."); + } + + configuration.setAccountName(parts[0]); + configuration.setQueueName(parts[1]); + + checkCredentials(configuration); + + QueueServiceEndpoint endpoint = new QueueServiceEndpoint(uri, this, configuration); + setProperties(endpoint, parameters); + return endpoint; + } + + private void checkCredentials(QueueServiceConfiguration cfg) { + CloudQueue client = cfg.getAzureQueueClient(); + StorageCredentials creds = client == null ? cfg.getCredentials() + : client.getServiceClient().getCredentials(); + if (creds == null) { + throw new IllegalArgumentException("Credentials must be specified."); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java new file mode 100644 index 0000000..d211d7e --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java @@ -0,0 +1,66 @@ +/** + * 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.azure.queue; + +import com.microsoft.azure.storage.queue.CloudQueue; +import org.apache.camel.component.azure.common.AbstractConfiguration; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriParams; + +@UriParams +public class QueueServiceConfiguration extends AbstractConfiguration { + + private String queueName; + @UriParam + private CloudQueue azureQueueClient; + + @UriParam(label = "producer", defaultValue = "getMessage") + private QueueServiceOperations operation = QueueServiceOperations.getMessage; + + public String getQueueName() { + return queueName; + } + + /** + * The queue resource name + */ + public void setQueueName(String queueName) { + this.queueName = queueName; + } + + public CloudQueue getAzureQueueClient() { + return azureQueueClient; + } + + /** + * The queue service client + */ + public void setAzureQueueClient(CloudQueue azureQueueClient) { + this.azureQueueClient = azureQueueClient; + } + + public QueueServiceOperations getOperation() { + return operation; + } + + /** + * Queue service operation hint to the producer + */ + public void setOperation(QueueServiceOperations operation) { + this.operation = operation; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConstants.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConstants.java new file mode 100644 index 0000000..0e7a523 --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConstants.java @@ -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. + */ +package org.apache.camel.component.azure.queue; + +public interface QueueServiceConstants { + + String OPERATION = "operation"; + String QUEUE_CLIENT = "AzureQueueClient"; + + String SERVICE_URI_SEGMENT = ".queue.core.windows.net"; + String ACCESS_CONDITION = "BlobAccessCondition"; + String BLOB_REQUEST_OPTIONS = "BlobRequestOptions"; + String OPERATION_CONTEXT = "BlobOperationContext"; +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConsumer.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConsumer.java new file mode 100644 index 0000000..9793a1c --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConsumer.java @@ -0,0 +1,68 @@ +/** + * 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.azure.queue; + +import com.microsoft.azure.storage.StorageException; +import org.apache.camel.Exchange; +import org.apache.camel.NoFactoryAvailableException; +import org.apache.camel.Processor; +import org.apache.camel.impl.ScheduledPollConsumer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Consumer of the queue content from the Azure Queue Service + */ +public class QueueServiceConsumer extends ScheduledPollConsumer { + private static final Logger LOG = LoggerFactory.getLogger(QueueServiceConsumer.class); + + public QueueServiceConsumer(QueueServiceEndpoint endpoint, Processor processor) throws NoFactoryAvailableException { + super(endpoint, processor); + } + + @Override + protected int poll() throws Exception { + Exchange exchange = super.getEndpoint().createExchange(); + try { + getMessage(exchange); + super.getAsyncProcessor().process(exchange); + return 1; + } catch (StorageException ex) { + if (404 == ex.getHttpStatusCode()) { + return 0; + } else { + throw ex; + } + } + } + + private void getMessage(Exchange exchange) throws Exception { + LOG.trace("Getting the message from the queue [{}] from exchange [{}]...", + getConfiguration().getQueueName(), exchange); + throw new UnsupportedOperationException(); + } + + protected QueueServiceConfiguration getConfiguration() { + return getEndpoint().getConfiguration(); + } + + @Override + public QueueServiceEndpoint getEndpoint() { + return (QueueServiceEndpoint) super.getEndpoint(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceEndpoint.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceEndpoint.java new file mode 100644 index 0000000..3f70804 --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceEndpoint.java @@ -0,0 +1,83 @@ +/** + * 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.azure.queue; + +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.azure.blob.BlobServiceConfiguration; +import org.apache.camel.component.azure.blob.BlobServiceConsumer; +import org.apache.camel.component.azure.blob.BlobServiceOperations; +import org.apache.camel.component.azure.blob.BlobServiceProducer; +import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The azure-queue component is used for storing and retrieving messages from Azure Storage Queue Service. + */ +@UriEndpoint(firstVersion = "2.19.0", + scheme = "azure-queue", + title = "Azure Storage Queue Service", + syntax = "azure-blob:queueOrMessageUri", + consumerClass = QueueServiceConsumer.class, + label = "cloud,queue,azure") +public class QueueServiceEndpoint extends DefaultEndpoint { + + private static final Logger LOG = LoggerFactory.getLogger(QueueServiceEndpoint.class); + + @UriPath(description = "Queue or Message compact Uri") + @Metadata(required = "true") + private String queueOrMessageUri; // to support component docs + @UriParam + private QueueServiceConfiguration configuration; + + public QueueServiceEndpoint(String uri, Component comp, QueueServiceConfiguration configuration) { + super(uri, comp); + this.configuration = configuration; + } + + public Consumer createConsumer(Processor processor) throws Exception { + LOG.trace("Creating a consumer"); + QueueServiceConsumer consumer = new QueueServiceConsumer(this, processor); + configureConsumer(consumer); + return consumer; + } + + public Producer createProducer() throws Exception { + LOG.trace("Creating a producer"); + return new QueueServiceProducer(this); + } + + public boolean isSingleton() { + return true; + } + + public QueueServiceConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration(QueueServiceConfiguration configuration) { + this.configuration = configuration; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceOperations.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceOperations.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceOperations.java new file mode 100644 index 0000000..a1e0898 --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceOperations.java @@ -0,0 +1,22 @@ +/** + * 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.azure.queue; + +public enum QueueServiceOperations { + getMessage, + putMessage +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceProducer.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceProducer.java new file mode 100644 index 0000000..434ed2d --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceProducer.java @@ -0,0 +1,93 @@ +/** + * 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.azure.queue; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.component.azure.blob.BlobServiceConstants; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.URISupport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Producer which sends messages to the Azure Storage Queue Service + */ +public class QueueServiceProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory.getLogger(QueueServiceProducer.class); + + public QueueServiceProducer(final Endpoint endpoint) { + super(endpoint); + } + + @Override + public void process(final Exchange exchange) throws Exception { + QueueServiceOperations operation = determineOperation(exchange); + if (ObjectHelper.isEmpty(operation)) { + operation = QueueServiceOperations.getMessage; + } else { + switch (operation) { + case getMessage: + getMessage(exchange); + break; + case putMessage: + putMessage(exchange); + break; + default: + throw new IllegalArgumentException("Unsupported operation"); + } + } + + } + + private void getMessage(Exchange exchange) { + LOG.trace("Getting the message from the queue [{}] from exchange [{}]...", + getConfiguration().getQueueName(), exchange); + throw new UnsupportedOperationException(); + } + private void putMessage(Exchange exchange) { + LOG.trace("Putting the message into the queue [{}] from exchange [{}]...", + getConfiguration().getQueueName(), exchange); + throw new UnsupportedOperationException(); + } + + private QueueServiceOperations determineOperation(Exchange exchange) { + QueueServiceOperations operation = exchange.getIn().getHeader(BlobServiceConstants.OPERATION, + QueueServiceOperations.class); + if (operation == null) { + operation = getConfiguration().getOperation(); + } + return operation; + } + + protected QueueServiceConfiguration getConfiguration() { + return getEndpoint().getConfiguration(); + } + + @Override + public String toString() { + return "StorageQueueProducer[" + URISupport.sanitizeUri(getEndpoint().getEndpointUri()) + "]"; + } + + @Override + public QueueServiceEndpoint getEndpoint() { + return (QueueServiceEndpoint) super.getEndpoint(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java new file mode 100644 index 0000000..17121b0 --- /dev/null +++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java @@ -0,0 +1,67 @@ +/** + * 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.azure.queue; + +import java.net.URI; + +import com.microsoft.azure.storage.StorageCredentials; +import com.microsoft.azure.storage.queue.CloudQueue; + +public final class QueueServiceUtil { + private QueueServiceUtil() { + } + + public static URI prepareStorageQueueUri(QueueServiceConfiguration cfg) { + return prepareStorageQueueUri(cfg, true); + } + + public static URI prepareStorageQueueUri(QueueServiceConfiguration cfg, boolean isForMessages) { + StringBuilder uriBuilder = new StringBuilder(); + uriBuilder.append("https://") + .append(cfg.getAccountName()) + .append(QueueServiceConstants.SERVICE_URI_SEGMENT) + .append("/") + .append(cfg.getQueueName()); + if (isForMessages) { + uriBuilder.append("/messages"); + } + return URI.create(uriBuilder.toString()); + } + + public static CloudQueue createQueueClient(QueueServiceConfiguration cfg) + throws Exception { + CloudQueue client = (CloudQueue) getConfiguredClient(cfg); + if (client == null) { + URI uri = prepareStorageQueueUri(cfg); + StorageCredentials creds = getAccountCredentials(cfg); + client = new CloudQueue(uri, creds); + } + return client; + } + + public static CloudQueue getConfiguredClient(QueueServiceConfiguration cfg) { + CloudQueue client = cfg.getAzureQueueClient(); + if (client != null && !client.getUri().equals(prepareStorageQueueUri(cfg))) { + throw new IllegalArgumentException("Invalid Client URI"); + } + return client; + } + + public static StorageCredentials getAccountCredentials(QueueServiceConfiguration cfg) { + return cfg.getCredentials(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/main/resources/META-INF/services/org/apache/camel/component/azure-queue ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/main/resources/META-INF/services/org/apache/camel/component/azure-queue b/components/camel-azure/src/main/resources/META-INF/services/org/apache/camel/component/azure-queue new file mode 100644 index 0000000..debd117 --- /dev/null +++ b/components/camel-azure/src/main/resources/META-INF/services/org/apache/camel/component/azure-queue @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +class=org.apache.camel.component.azure.queue.QueueServiceComponent http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java new file mode 100644 index 0000000..800aebc --- /dev/null +++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java @@ -0,0 +1,128 @@ +/** + * 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.azure.queue; + +import java.net.URI; + +import com.microsoft.azure.storage.StorageCredentials; +import com.microsoft.azure.storage.StorageCredentialsAccountAndKey; +import com.microsoft.azure.storage.core.Base64; +import com.microsoft.azure.storage.queue.CloudQueue; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.impl.PropertyPlaceholderDelegateRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class QueueServiceComponentConfigurationTest extends CamelTestSupport { + + @Test + public void testCreateEndpointWithMinConfigForClientOnly() throws Exception { + CloudQueue client = + new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue/messages"), + newAccountKeyCredentials()); + + JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); + registry.bind("azureQueueClient", client); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?azureQueueClient=#azureQueueClient"); + + doTestCreateEndpointWithMinConfig(endpoint, true); + } + + @Test + public void testCreateEndpointWithMinConfigForCredsOnly() throws Exception { + registerCredentials(); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?credentials=#creds"); + + doTestCreateEndpointWithMinConfig(endpoint, false); + } + + private void doTestCreateEndpointWithMinConfig(QueueServiceEndpoint endpoint, boolean clientExpected) + throws Exception { + assertEquals("camelazure", endpoint.getConfiguration().getAccountName()); + assertEquals("testqueue", endpoint.getConfiguration().getQueueName()); + if (clientExpected) { + assertNotNull(endpoint.getConfiguration().getAzureQueueClient()); + assertNull(endpoint.getConfiguration().getCredentials()); + } else { + assertNull(endpoint.getConfiguration().getAzureQueueClient()); + assertNotNull(endpoint.getConfiguration().getCredentials()); + } + createConsumer(endpoint); + } + + @Test + public void testNoCredentials() throws Exception { + QueueServiceComponent component = new QueueServiceComponent(context); + try { + component.createEndpoint("azure-queue://camelazure/testqueue"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Credentials must be specified.", ex.getMessage()); + } + } + + @Test + public void testTooManyPathSegments() throws Exception { + QueueServiceComponent component = new QueueServiceComponent(context); + try { + component.createEndpoint("azure-queue://camelazure/testqueue/1"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Only the account and queue names must be specified.", ex.getMessage()); + } + } + + @Test + public void testTooFewPathSegments() throws Exception { + QueueServiceComponent component = new QueueServiceComponent(context); + try { + component.createEndpoint("azure-queue://camelazure"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("The account and queue names must be specified.", ex.getMessage()); + } + } + + + private static void createConsumer(Endpoint endpoint) throws Exception { + endpoint.createConsumer(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + // noop + } + }); + } + + private void registerCredentials() { + JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); + registry.bind("creds", newAccountKeyCredentials()); + } + private StorageCredentials newAccountKeyCredentials() { + return new StorageCredentialsAccountAndKey("camelazure", + Base64.encode("key".getBytes())); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java ---------------------------------------------------------------------- diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java new file mode 100644 index 0000000..40bf2a4 --- /dev/null +++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java @@ -0,0 +1,88 @@ +/** + * 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.azure.queue; + +import java.net.URI; + +import com.microsoft.azure.storage.StorageCredentials; +import com.microsoft.azure.storage.StorageCredentialsAccountAndKey; +import com.microsoft.azure.storage.core.Base64; +import com.microsoft.azure.storage.queue.CloudQueue; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.impl.PropertyPlaceholderDelegateRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class QueueServiceUtilTest extends CamelTestSupport { + + @Test + public void testPrepareUri() throws Exception { + registerCredentials(); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?credentials=#creds"); + URI uri = + QueueServiceUtil.prepareStorageQueueUri(endpoint.getConfiguration()); + assertEquals("https://camelazure.queue.core.windows.net/testqueue/messages", uri.toString()); + } + + @Test + public void testGetConfiguredClient() throws Exception { + CloudQueue client = + new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue/messages"), + newAccountKeyCredentials()); + + JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); + registry.bind("azureQueueClient", client); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?azureQueueClient=#azureQueueClient"); + assertSame(client, QueueServiceUtil.getConfiguredClient(endpoint.getConfiguration())); + } + @Test + public void testGetConfiguredClientUriMismatch() throws Exception { + CloudQueue client = + new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue"), + newAccountKeyCredentials()); + + JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); + registry.bind("azureQueueClient", client); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue2?azureQueueClient=#azureQueueClient"); + + try { + QueueServiceUtil.getConfiguredClient(endpoint.getConfiguration()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Invalid Client URI", ex.getMessage()); + } + } + + private void registerCredentials() { + JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); + registry.bind("creds", newAccountKeyCredentials()); + } + + private StorageCredentials newAccountKeyCredentials() { + return new StorageCredentialsAccountAndKey("camelazure", + Base64.encode("key".getBytes())); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/507e8b5f/platforms/spring-boot/components-starter/camel-azure-starter/src/main/java/org/apache/camel/component/azure/queue/springboot/QueueServiceComponentAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-azure-starter/src/main/java/org/apache/camel/component/azure/queue/springboot/QueueServiceComponentAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-azure-starter/src/main/java/org/apache/camel/component/azure/queue/springboot/QueueServiceComponentAutoConfiguration.java new file mode 100644 index 0000000..a929af0 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-azure-starter/src/main/java/org/apache/camel/component/azure/queue/springboot/QueueServiceComponentAutoConfiguration.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.camel.component.azure.queue.springboot; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.azure.queue.QueueServiceComponent; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.type.AnnotatedTypeMetadata; + +/** + * Generated by camel-package-maven-plugin - do not edit this file! + */ +@Configuration +@ConditionalOnBean(type = "org.apache.camel.spring.boot.CamelAutoConfiguration") +@Conditional(QueueServiceComponentAutoConfiguration.Condition.class) +@AutoConfigureAfter(name = "org.apache.camel.spring.boot.CamelAutoConfiguration") +public class QueueServiceComponentAutoConfiguration { + + @Lazy + @Bean(name = "azure-queue-component") + @ConditionalOnClass(CamelContext.class) + @ConditionalOnMissingBean(QueueServiceComponent.class) + public QueueServiceComponent configureQueueServiceComponent( + CamelContext camelContext) throws Exception { + QueueServiceComponent component = new QueueServiceComponent(); + component.setCamelContext(camelContext); + return component; + } + + public static class Condition extends SpringBootCondition { + @Override + public ConditionOutcome getMatchOutcome( + ConditionContext conditionContext, + AnnotatedTypeMetadata annotatedTypeMetadata) { + boolean groupEnabled = isEnabled(conditionContext, + "camel.component.", true); + ConditionMessage.Builder message = ConditionMessage + .forCondition("camel.component.azure-queue"); + if (isEnabled(conditionContext, "camel.component.azure-queue.", + groupEnabled)) { + return ConditionOutcome.match(message.because("enabled")); + } + return ConditionOutcome.noMatch(message.because("not enabled")); + } + + private boolean isEnabled( + org.springframework.context.annotation.ConditionContext context, + java.lang.String prefix, boolean defaultValue) { + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( + context.getEnvironment(), prefix); + return resolver.getProperty("enabled", Boolean.class, defaultValue); + } + } +} \ No newline at end of file