Hi

The getHeader has a overloaded method where you can specify the type,
so you avoid the ugly java type cast (and it uses camel type coverter
which is more powerful).

Also you can specify a default value, or if the header is missing then
it returns empty / null value by default.

So the code can likely be shorter

          hardwareId =
exchange.getIn().getHeader(JcloudsConstants.HARDWARE_ID,
String.class);



On Wed, Jul 1, 2015 at 5:31 PM,  <acosent...@apache.org> wrote:
> Repository: camel
> Updated Branches:
>   refs/heads/master f5e52380c -> f7ef4d7f9
>
>
> CAMEL-8922 Camel-Jclouds: Use ObjectHelper to check if headers exists
>
>
> Project: http://git-wip-us.apache.org/repos/asf/camel/repo
> Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f7ef4d7f
> Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f7ef4d7f
> Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f7ef4d7f
>
> Branch: refs/heads/master
> Commit: f7ef4d7f9459711188ef3f016374d7860f663167
> Parents: f5e5238
> Author: Andrea Cosentino <anco...@gmail.com>
> Authored: Wed Jul 1 17:24:33 2015 +0200
> Committer: Andrea Cosentino <anco...@gmail.com>
> Committed: Wed Jul 1 17:24:33 2015 +0200
>
> ----------------------------------------------------------------------
>  .../jclouds/JcloudsBlobStoreProducer.java       |  8 ++---
>  .../jclouds/JcloudsComputeProducer.java         | 31 +++++++++++---------
>  2 files changed, 21 insertions(+), 18 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/camel/blob/f7ef4d7f/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
> ----------------------------------------------------------------------
> diff --git 
> a/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
>  
> b/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
> index b5735c5..01a42b2 100644
> --- 
> a/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
> +++ 
> b/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
> @@ -82,7 +82,7 @@ public class JcloudsBlobStoreProducer extends 
> JcloudsProducer {
>       */
>      protected String getBlobName(Exchange exchange) {
>          String blobName = ((JcloudsBlobStoreEndpoint) 
> getEndpoint()).getBlobName();
> -        if (exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME)))
>  {
>              blobName = (String) 
> exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME);
>          }
>          return blobName;
> @@ -93,7 +93,7 @@ public class JcloudsBlobStoreProducer extends 
> JcloudsProducer {
>       */
>      protected String getContainerName(Exchange exchange) {
>          String containerName = ((JcloudsBlobStoreEndpoint) 
> getEndpoint()).getContainer();
> -        if (exchange.getIn().getHeader(JcloudsConstants.CONTAINER_NAME) != 
> null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.CONTAINER_NAME)))
>  {
>              containerName = (String) 
> exchange.getIn().getHeader(JcloudsConstants.CONTAINER_NAME);
>          }
>          return containerName;
> @@ -105,7 +105,7 @@ public class JcloudsBlobStoreProducer extends 
> JcloudsProducer {
>      public String getOperation(Exchange exchange) {
>          String operation = ((JcloudsBlobStoreEndpoint) 
> getEndpoint()).getOperation();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.OPERATION) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.OPERATION)))
>  {
>              operation = (String) 
> exchange.getIn().getHeader(JcloudsConstants.OPERATION);
>          }
>          return operation;
> @@ -117,7 +117,7 @@ public class JcloudsBlobStoreProducer extends 
> JcloudsProducer {
>      public String getLocationId(Exchange exchange) {
>          String operation = ((JcloudsBlobStoreEndpoint) 
> getEndpoint()).getLocationId();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID) != 
> null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID)))
>  {
>              operation = (String) 
> exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID);
>          }
>          return operation;
>
> http://git-wip-us.apache.org/repos/asf/camel/blob/f7ef4d7f/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
> ----------------------------------------------------------------------
> diff --git 
> a/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
>  
> b/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
> index c6ce1c2..4bc01d8 100644
> --- 
> a/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
> +++ 
> b/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
> @@ -17,10 +17,13 @@
>  package org.apache.camel.component.jclouds;
>
>  import java.util.Set;
> +
>  import com.google.common.base.Predicate;
> +
>  import org.apache.camel.CamelException;
>  import org.apache.camel.CamelExchangeException;
>  import org.apache.camel.Exchange;
> +import org.apache.camel.util.ObjectHelper;
>  import org.jclouds.compute.ComputeService;
>  import org.jclouds.compute.RunNodesException;
>  import org.jclouds.compute.domain.ComputeMetadata;
> @@ -85,19 +88,19 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>          String locationId = getLocationId(exchange);
>          String hardwareId = getHardwareId(exchange);
>
> -        if (group == null) {
> +        if (ObjectHelper.isEmpty(group)) {
>              throw new CamelExchangeException("Group must be specific in the 
> URI or as exchange property for the destroy node operation.", exchange);
>          }
>          TemplateBuilder builder = computeService.templateBuilder();
>          builder.any();
>
> -        if (locationId != null) {
> +        if (ObjectHelper.isNotEmpty(locationId)) {
>              builder.locationId(locationId);
>          }
> -        if (imageId != null) {
> +        if (ObjectHelper.isNotEmpty(imageId)) {
>              builder.imageId(imageId);
>          }
> -        if (hardwareId != null) {
> +        if (ObjectHelper.isNotEmpty(hardwareId)) {
>              builder.hardwareId(hardwareId);
>          }
>
> @@ -120,7 +123,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>
>          LoginCredentials credentials = null;
>
> -        if (user != null) {
> +        if (ObjectHelper.isNotEmpty(user)) {
>              credentials = LoginCredentials.builder().user(user).build();
>          }
>          ExecResponse execResponse = null;
> @@ -260,7 +263,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      public String getOperation(Exchange exchange) {
>          String operation = getEndpoint().getOperation();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.OPERATION) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.OPERATION)))
>  {
>              operation = (String) 
> exchange.getIn().getHeader(JcloudsConstants.OPERATION);
>          }
>          return operation;
> @@ -272,11 +275,11 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      public NodeMetadata.Status getNodeState(Exchange exchange) {
>          NodeMetadata.Status nodeState = null;
>          String state = getEndpoint().getNodeState();
> -        if (state != null) {
> +        if (ObjectHelper.isNotEmpty(state)) {
>              nodeState =  NodeMetadata.Status.valueOf(state);
>          }
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.NODE_STATE) != null) 
> {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.NODE_STATE)))
>  {
>              Object stateHeader = 
> exchange.getIn().getHeader(JcloudsConstants.NODE_STATE);
>              if (stateHeader == null) {
>                  nodeState = null;
> @@ -296,7 +299,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      protected String getImageId(Exchange exchange) {
>          String imageId = getEndpoint().getImageId();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.IMAGE_ID) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.IMAGE_ID)))
>  {
>              imageId = (String) 
> exchange.getIn().getHeader(JcloudsConstants.IMAGE_ID);
>          }
>          return imageId;
> @@ -308,7 +311,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      protected String getHardwareId(Exchange exchange) {
>          String hardwareId = getEndpoint().getHardwareId();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.HARDWARE_ID) != 
> null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.HARDWARE_ID)))
>  {
>              hardwareId = (String) 
> exchange.getIn().getHeader(JcloudsConstants.HARDWARE_ID);
>          }
>          return hardwareId;
> @@ -320,7 +323,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      protected String getLocationId(Exchange exchange) {
>          String locationId = getEndpoint().getLocationId();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID) != 
> null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID)))
>  {
>              locationId = (String) 
> exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID);
>          }
>          return locationId;
> @@ -332,7 +335,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      protected String getNodeId(Exchange exchange) {
>          String nodeId = getEndpoint().getNodeId();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.NODE_ID) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.NODE_ID)))
>  {
>              nodeId = (String) 
> exchange.getIn().getHeader(JcloudsConstants.NODE_ID);
>          }
>          return nodeId;
> @@ -344,7 +347,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      protected String getGroup(Exchange exchange) {
>          String group = getEndpoint().getGroup();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.GROUP) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.GROUP))) 
> {
>              group = (String) 
> exchange.getIn().getHeader(JcloudsConstants.GROUP);
>          }
>          return group;
> @@ -356,7 +359,7 @@ public class JcloudsComputeProducer extends 
> JcloudsProducer {
>      protected String getUser(Exchange exchange) {
>          String user = getEndpoint().getUser();
>
> -        if (exchange.getIn().getHeader(JcloudsConstants.USER) != null) {
> +        if 
> (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(JcloudsConstants.USER))) {
>              user = (String) 
> exchange.getIn().getHeader(JcloudsConstants.USER);
>          }
>          return user;
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cib...@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Reply via email to