LOG instance removed. Parent Logger is going to be used. Logging debug level instead of info.
CMProducer is a Processor. process() method throws Exception. Let camel do the work and not catch exceptions. In order to obtain the payload, Message.getMandatoryBody() is used instead of getBody() so there is no need to the check if the payload is null. Camel 's InvalidPayloadException is going to be thrown. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/354e7e93 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/354e7e93 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/354e7e93 Branch: refs/heads/master Commit: 354e7e93c7cbbfaabbb1cf3f591e0847870f4608 Parents: 0bd5398 Author: oalles <oal...@neivi.es> Authored: Mon Mar 21 13:11:09 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Mar 23 18:46:10 2016 +0100 ---------------------------------------------------------------------- .../apache/camel/component/cm/CMProducer.java | 324 +++++++++---------- 1 file changed, 159 insertions(+), 165 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/354e7e93/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMProducer.java b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMProducer.java index d175750..3ae6d9a 100644 --- a/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMProducer.java +++ b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMProducer.java @@ -1,165 +1,159 @@ -/** - * 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.cm; - -import java.util.Set; - -import javax.validation.ConstraintViolation; -import javax.validation.Validator; - -import org.apache.camel.Exchange; -import org.apache.camel.component.cm.client.SMSMessage; -import org.apache.camel.component.cm.exceptions.HostUnavailableException; -import org.apache.camel.component.cm.exceptions.InvalidPayloadException; -import org.apache.camel.impl.DefaultProducer; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.impl.client.HttpClientBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * is the exchange processor. Sends a validated sms message to CM Endpoints. - */ -public class CMProducer extends DefaultProducer { - - private static final Logger LOG = LoggerFactory.getLogger(CMProducer.class); - - private Validator validator; - - /** - * sends a valid message to CM endpoints. - */ - private CMSender sender; - - public CMProducer(final CMEndpoint endpoint, final CMSender sender) { - super(endpoint); - this.sender = sender; - } - - /** - * Producer is a exchange processor. This process is built in several steps. 1. Validate message receive from client 2. Send validated message to CM endpoints. 3. Process response from CM - * endpoints. - */ - @Override - public void process(final Exchange exchange) { - - try { - - // Immutable message receive from clients. - final SMSMessage smsMessage = exchange.getIn().getBody(SMSMessage.class); - if (smsMessage == null) { - throw new NullPointerException(); - } - - // Validates Payload - SMSMessage - LOG.trace("Validating SMSMessage instance provided: {}", smsMessage.toString()); - final Set<ConstraintViolation<SMSMessage>> constraintViolations = getValidator().validate(smsMessage); - if (constraintViolations.size() > 0) { - final StringBuffer msg = new StringBuffer(); - for (final ConstraintViolation<SMSMessage> cv : constraintViolations) { - msg.append(String.format("- Invalid value for %s: %s", cv.getPropertyPath().toString(), cv.getMessage())); - } - throw new InvalidPayloadException(msg.toString()); - } - LOG.trace("SMSMessage instance is valid: {}", smsMessage.toString()); - - // We have a valid (immutable) SMSMessage instance, lets extend to CMMessage - // This is the instance we will use to build the XML document to be - // sent to CM SMS GW. - final CMMessage cmMessage = new CMMessage(smsMessage.getPhoneNumber(), smsMessage.getMessage()); - LOG.debug("CMMessage instance build from valid SMSMessage instance"); - - if (smsMessage.getFrom() == null || smsMessage.getFrom().isEmpty()) { - String df = getConfiguration().getDefaultFrom(); - cmMessage.setSender(df); - LOG.debug("Dynamic sender is set to default dynamic sender: {}", df); - } - - // Remember, this can be null. - cmMessage.setIdAsString(smsMessage.getId()); - - // Unicode and multipart - cmMessage.setUnicodeAndMultipart(getConfiguration().getDefaultMaxNumberOfParts()); - - // 2. Send a validated sms message to CM endpoints - // throws MessagingException for abnormal situations. - sender.send(cmMessage); - - log.debug("Request accepted by CM Host: {}", cmMessage.toString()); - } catch (final NullPointerException e) { - // Body hast to be an instance of SMSMessage - final String m = "Check Message body - Has to be an SMSMessage instance"; - log.error(m, e); - exchange.setException(new InvalidPayloadException(m)); - } catch (final RuntimeException e) { - log.error("Message cannot be sent ", e); - exchange.setException(e); - } - } - - @Override - protected void doStart() throws Exception { - - // log at debug level for singletons, for prototype scoped log at trace - // level to not spam logs - - log.debug("Starting CMProducer"); - - final CMConfiguration configuration = getConfiguration(); - - if (configuration.isTestConnectionOnStartup()) { - try { - log.debug("Checking connection - {}", getEndpoint().getCMUrl()); - HttpClientBuilder.create().build().execute(new HttpHead(getEndpoint().getCMUrl())); - log.info("Connection to {}: OK", getEndpoint().getCMUrl()); - } catch (final Exception e) { - log.info("Connection to {}: NOT AVAILABLE", getEndpoint().getCMUrl()); - throw new HostUnavailableException(e); - } - } - - // keep starting - super.doStart(); - - log.info("CMProducer started"); - } - - @Override - public CMEndpoint getEndpoint() { - return (CMEndpoint) super.getEndpoint(); - } - - public CMConfiguration getConfiguration() { - return getEndpoint().getConfiguration(); - } - - public Validator getValidator() { - if (validator == null) { - validator = getEndpoint().getComponent().getValidator(); - } - return validator; - } - - public CMSender getSender() { - return sender; - } - - public void setSender(CMSender sender) { - this.sender = sender; - } - -} +/** + * 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.cm; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validator; + +import org.apache.camel.Exchange; +import org.apache.camel.component.cm.client.SMSMessage; +import org.apache.camel.component.cm.exceptions.HostUnavailableException; +import org.apache.camel.component.cm.exceptions.InvalidPayloadException; +import org.apache.camel.impl.DefaultProducer; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.impl.client.HttpClientBuilder; + +/** + * is the exchange processor. Sends a validated sms message to CM Endpoints. + */ +public class CMProducer extends DefaultProducer { + + private Validator validator; + + /** + * sends a valid message to CM endpoints. + */ + private CMSender sender; + + public CMProducer(final CMEndpoint endpoint, final CMSender sender) { + super(endpoint); + this.sender = sender; + } + + /** + * Producer is a exchange processor. This process is built in several steps. + * 1. Validate message receive from client 2. Send validated message to CM + * endpoints. 3. Process response from CM endpoints. + */ + @Override + public void process(final Exchange exchange) throws Exception { + + // Immutable message receive from clients. Throws camel ' s + // InvalidPayloadException + final SMSMessage smsMessage = exchange.getIn() + .getMandatoryBody(SMSMessage.class); + + // Validates Payload - SMSMessage + log.trace("Validating SMSMessage instance provided: {}", + smsMessage.toString()); + final Set<ConstraintViolation<SMSMessage>> constraintViolations = getValidator() + .validate(smsMessage); + if (constraintViolations.size() > 0) { + final StringBuffer msg = new StringBuffer(); + for (final ConstraintViolation<SMSMessage> cv : constraintViolations) { + msg.append(String.format("- Invalid value for %s: %s", + cv.getPropertyPath().toString(), cv.getMessage())); + } + throw new InvalidPayloadException(msg.toString()); + } + log.trace("SMSMessage instance is valid: {}", smsMessage.toString()); + + // We have a valid (immutable) SMSMessage instance, lets extend to + // CMMessage + // This is the instance we will use to build the XML document to be + // sent to CM SMS GW. + final CMMessage cmMessage = new CMMessage(smsMessage.getPhoneNumber(), + smsMessage.getMessage()); + log.debug("CMMessage instance build from valid SMSMessage instance"); + + if (smsMessage.getFrom() == null || smsMessage.getFrom().isEmpty()) { + String df = getConfiguration().getDefaultFrom(); + cmMessage.setSender(df); + log.debug("Dynamic sender is set to default dynamic sender: {}", + df); + } + + // Remember, this can be null. + cmMessage.setIdAsString(smsMessage.getId()); + + // Unicode and multipart + cmMessage.setUnicodeAndMultipart( + getConfiguration().getDefaultMaxNumberOfParts()); + + // 2. Send a validated sms message to CM endpoints + // throws MessagingException for abnormal situations. + sender.send(cmMessage); + + log.debug("Request accepted by CM Host: {}", cmMessage.toString()); + } + + @Override + protected void doStart() throws Exception { + + // log at debug level for singletons, for prototype scoped log at trace + // level to not spam logs + + log.debug("Starting CMProducer"); + + final CMConfiguration configuration = getConfiguration(); + + if (configuration.isTestConnectionOnStartup()) { + try { + log.debug("Checking connection - {}", getEndpoint().getCMUrl()); + HttpClientBuilder.create().build() + .execute(new HttpHead(getEndpoint().getCMUrl())); + log.debug("Connection to {}: OK", getEndpoint().getCMUrl()); + } catch (final Exception e) { + log.debug("Connection to {}: NOT AVAILABLE", + getEndpoint().getCMUrl()); + throw new HostUnavailableException(e); + } + } + + // keep starting + super.doStart(); + + log.debug("CMProducer started"); + } + + @Override + public CMEndpoint getEndpoint() { + return (CMEndpoint) super.getEndpoint(); + } + + public CMConfiguration getConfiguration() { + return getEndpoint().getConfiguration(); + } + + public Validator getValidator() { + if (validator == null) { + validator = getEndpoint().getComponent().getValidator(); + } + return validator; + } + + public CMSender getSender() { + return sender; + } + + public void setSender(CMSender sender) { + this.sender = sender; + } + +}