use of org.apache.camel.CamelExchangeException in project camel by apache.
the class SshProducer method process.
@Override
public void process(Exchange exchange) throws Exception {
final Message in = exchange.getIn();
String command = in.getMandatoryBody(String.class);
try {
SshResult result = SshHelper.sendExecCommand(command, endpoint, client);
exchange.getOut().setBody(result.getStdout());
exchange.getOut().setHeader(SshResult.EXIT_VALUE, result.getExitValue());
exchange.getOut().setHeader(SshResult.STDERR, result.getStderr());
} catch (Exception e) {
throw new CamelExchangeException("Cannot execute command: " + command, exchange, e);
}
// propagate headers and attachments
exchange.getOut().getHeaders().putAll(in.getHeaders());
exchange.getOut().setAttachments(in.getAttachments());
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class ServerResponseFutureListener method operationComplete.
@Override
public void operationComplete(ChannelFuture future) throws Exception {
// if it was not a success then thrown an exception
if (!future.isSuccess()) {
Exception e = new CamelExchangeException("Cannot write response to " + remoteAddress, exchange, future.getCause());
consumer.getExceptionHandler().handleException(e);
}
// should channel be closed after complete?
Boolean close;
if (exchange.hasOut()) {
close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
} else {
close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// check the setting on the exchange property
if (close == null) {
close = exchange.getProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// should we disconnect, the header can override the configuration
boolean disconnect = consumer.getConfiguration().isDisconnect();
if (close != null) {
disconnect = close;
}
if (disconnect) {
if (LOG.isTraceEnabled()) {
LOG.trace("Closing channel when complete at address: {}", remoteAddress);
}
NettyHelper.close(future.getChannel());
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class ClientChannelHandler method channelInactive.
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (LOG.isTraceEnabled()) {
LOG.trace("Channel closed: {}", ctx.channel());
}
Exchange exchange = getExchange(ctx);
AsyncCallback callback = getAsyncCallback(ctx);
// remove state
producer.removeState(ctx.channel());
// to keep track of open sockets
producer.getAllChannels().remove(ctx.channel());
// this channel is maybe closing graceful and the exchange is already done
// and if so we should not trigger an exception
boolean doneUoW = exchange.getUnitOfWork() == null;
NettyConfiguration configuration = producer.getConfiguration();
if (configuration.isSync() && !doneUoW && !messageReceived && !exceptionHandled) {
// To avoid call the callback.done twice
exceptionHandled = true;
// session was closed but no message received. This could be because the remote server had an internal error
// and could not return a response. We should count down to stop waiting for a response
String address = configuration != null ? configuration.getAddress() : "";
if (LOG.isDebugEnabled()) {
LOG.debug("Channel closed but no message received from address: {}", address);
}
// don't fail the exchange if we actually specify to disconnect
if (!configuration.isDisconnect()) {
exchange.setException(new CamelExchangeException("No response received from remote server: " + address, exchange));
}
// signal callback
callback.done(false);
}
// make sure the event can be processed by other handlers
super.channelInactive(ctx);
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class ServerResponseFutureListener method operationComplete.
@Override
public void operationComplete(ChannelFuture future) throws Exception {
// if it was not a success then thrown an exception
if (!future.isSuccess()) {
Exception e = new CamelExchangeException("Cannot write response to " + remoteAddress, exchange, future.cause());
consumer.getExceptionHandler().handleException(e);
}
// should channel be closed after complete?
Boolean close;
if (exchange.hasOut()) {
close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
} else {
close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// check the setting on the exchange property
if (close == null) {
close = exchange.getProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// should we disconnect, the header can override the configuration
boolean disconnect = consumer.getConfiguration().isDisconnect();
if (close != null) {
disconnect = close;
}
if (disconnect) {
if (LOG.isTraceEnabled()) {
LOG.trace("Closing channel when complete at address: {}", remoteAddress);
}
NettyHelper.close(future.channel());
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class ShiroSecurityProcessor method applySecurityPolicy.
private void applySecurityPolicy(Exchange exchange) throws Exception {
ByteSource encryptedToken;
// if we have username and password as headers then use them to create a token
String username = exchange.getIn().getHeader(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME, String.class);
String password = exchange.getIn().getHeader(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD, String.class);
if (username != null && password != null) {
ShiroSecurityToken token = new ShiroSecurityToken(username, password);
// store the token as header, either as base64 or as the object as-is
if (policy.isBase64()) {
ByteSource bytes = ShiroSecurityHelper.encrypt(token, policy.getPassPhrase(), policy.getCipherService());
String base64 = bytes.toBase64();
exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, base64);
} else {
exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, token);
}
// and now remove the headers as we turned those into the token instead
exchange.getIn().removeHeader(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME);
exchange.getIn().removeHeader(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD);
}
Object token = ExchangeHelper.getMandatoryHeader(exchange, ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, Object.class);
// we support the token in a number of ways
if (token instanceof ShiroSecurityToken) {
ShiroSecurityToken sst = (ShiroSecurityToken) token;
encryptedToken = ShiroSecurityHelper.encrypt(sst, policy.getPassPhrase(), policy.getCipherService());
// Remove unencrypted token + replace with an encrypted token
exchange.getIn().removeHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN);
exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, encryptedToken);
} else if (token instanceof String) {
String data = (String) token;
if (policy.isBase64()) {
byte[] bytes = Base64.decode(data);
encryptedToken = ByteSource.Util.bytes(bytes);
} else {
encryptedToken = ByteSource.Util.bytes(data);
}
} else if (token instanceof ByteSource) {
encryptedToken = (ByteSource) token;
} else {
throw new CamelExchangeException("Shiro security header " + ShiroSecurityConstants.SHIRO_SECURITY_TOKEN + " is unsupported type: " + ObjectHelper.classCanonicalName(token), exchange);
}
ByteSource decryptedToken = policy.getCipherService().decrypt(encryptedToken.getBytes(), policy.getPassPhrase());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decryptedToken.getBytes());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
ShiroSecurityToken securityToken;
try {
securityToken = (ShiroSecurityToken) objectInputStream.readObject();
} finally {
IOHelper.close(objectInputStream, byteArrayInputStream);
}
Subject currentUser = SecurityUtils.getSubject();
// Authenticate user if not authenticated
try {
authenticateUser(currentUser, securityToken);
// Test whether user's role is authorized to perform functions in the permissions list
authorizeUser(currentUser, exchange);
} finally {
if (policy.isAlwaysReauthenticate()) {
currentUser.logout();
}
}
}
Aggregations