Refine search
@Override public void process(Exchange exchange) throws Exception { InputSource is = exchange.getIn().getMandatoryBody(InputSource.class); XMLStreamReader stream = exchange.getIn().getMandatoryBody(XMLStreamReader.class); XMLReader reader = new StaxStreamXMLReader(stream); ContentHandler handler; if (contentHandlerClass != null) { handler = contentHandlerClass.newInstance(); } else { handler = contentHandler; } reader.setContentHandler(handler); reader.parse(is); if (ExchangeHelper.isOutCapable(exchange)) { // preserve headers exchange.getOut().setHeaders(exchange.getIn().getHeaders()); exchange.getOut().setBody(handler); } else { exchange.getIn().setBody(handler); } }
public void calculateSignature(Exchange exchange, Signature signer) throws Exception { Object payload = exchange.getIn().getBody(); if (payload != null) { InputStream payloadStream = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, payload); try { byte[] buffer = new byte[config.getBufferSize()]; int read; while ((read = payloadStream.read(buffer)) > 0) { signer.update(buffer, 0, read); } } finally { IOHelper.close(payloadStream); } } }
/** * Extract the subject URI from the incoming exchange. * @param exchange the incoming Exchange * @return the subject URI * @throws NoSuchHeaderException when the CamelFcrepoBaseUrl header is not present */ public static String getSubjectUri(final Exchange exchange) throws NoSuchHeaderException { final String uri = exchange.getIn().getHeader(FCREPO_URI, "", String.class); if (uri.isEmpty()) { final String base = getMandatoryHeader(exchange, FCREPO_BASE_URL, String.class); final String path = exchange.getIn().getHeader(FCREPO_IDENTIFIER, "", String.class); return trimTrailingSlash(base) + path; } return uri; }
@Override protected void onExchange(Exchange exchange) throws Exception { String newResourceUri = exchange.getIn().getHeader(MUSTACHE_RESOURCE_URI, String.class); if (newResourceUri == null) { // Get Mustache String newTemplate = exchange.getIn().getHeader(MUSTACHE_TEMPLATE, String.class); Mustache newMustache; if (newTemplate == null) { newMustache = getOrCreateMustache(); } else { newMustache = createMustache(new StringReader(newTemplate), "mustache:temp#" + newTemplate.hashCode()); exchange.getIn().removeHeader(MUSTACHE_TEMPLATE); } // Execute Mustache Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange); StringWriter writer = new StringWriter(); newMustache.execute(writer, variableMap); writer.flush(); // Fill out message Message out = exchange.getOut(); out.setBody(writer.toString()); out.setHeaders(exchange.getIn().getHeaders()); out.setAttachments(exchange.getIn().getAttachments()); } else { exchange.getIn().removeHeader(MustacheConstants.MUSTACHE_RESOURCE_URI); MustacheEndpoint newEndpoint = getCamelContext().getEndpoint(MUSTACHE_ENDPOINT_URI_PREFIX + newResourceUri, MustacheEndpoint.class); newEndpoint.onExchange(exchange); } }
@Override protected void onExchange(Exchange exchange) throws Exception { StringWriter buffer = new StringWriter(); @SuppressWarnings("unchecked") Map<String, Object> variableMap = exchange.getIn().getHeader(StringTemplateConstants.STRINGTEMPLATE_VARIABLE_MAP, Map.class); if (variableMap == null) { variableMap = ExchangeHelper.createVariableMap(exchange); } // getResourceAsInputStream also considers the content cache String text = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, getResourceAsInputStream()); ST template = new ST(text, delimiterStart, delimiterStop); for (Map.Entry<String, Object> entry : variableMap.entrySet()) { template.add(entry.getKey(), entry.getValue()); } log.debug("StringTemplate is writing using attributes: {}", variableMap); template.write(new NoIndentWriter(buffer)); // now lets output the results to the exchange Message out = exchange.getOut(); out.setBody(buffer.toString()); out.setHeaders(exchange.getIn().getHeaders()); out.setHeader(StringTemplateConstants.STRINGTEMPLATE_RESOURCE_URI, getResourceUri()); out.setAttachments(exchange.getIn().getAttachments()); } }
@SuppressWarnings("PMD.AvoidReassigningParameters") @Override public boolean matches(Exchange exchange) { Object msgBody = exchange.getIn().getBody(); Predicate predicate = this.predicate; // TODO: Maybe check for content-type, too ? // String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); // if ("application/json".equals(contentType)) { ... } // ??? if (msgBody instanceof String) { // If it is a json document , suppose that this is a document which needs to be parsed as JSON // Therefor we set a map instead of the string Map<String, Object> jsonDocument = jsonStringAsMap((String) msgBody, exchange); if (jsonDocument != null) { // Clone the exchange and set the JSON message converted to a Map / List as in message. // The intention is that only this predicate acts on the converted value, // but the original in-message still continues to carry the same format // The predicated is supposed to be read only with respect to the incoming messaeg. exchange = ExchangeHelper.createCopy(exchange, true); exchange.getIn().setBody(jsonDocument); predicate = this.ognlPredicate; } } return predicate.matches(exchange); }
@Override public void process(Exchange exchange) throws Exception { String command = ExchangeHelper.getMandatoryHeader(exchange, NetWeaverConstants.COMMAND, String.class); Exchange httpExchange = getEndpoint().createExchange(); httpExchange.getIn().setHeader(Exchange.HTTP_PATH, command); if (getEndpoint().isJson()) { httpExchange.getIn().setHeader("Accept", "application/json"); } log.debug("Calling SAP Net-Weaver {} with command {}", http, command); http.process(httpExchange); String data = httpExchange.getOut().getBody(String.class); if (data != null && getEndpoint().isJsonAsMap() && getEndpoint().isJson()) { // map json string to json map ObjectMapper mapper = new ObjectMapper(); Map<?, ?> map = mapper.readValue(data, Map.class); // if we only have one entry in the map, then put that as root (as it tends to return a single instance "d" if (map.size() == 1 && getEndpoint().isFlatternMap()) { exchange.getIn().setBody(map.values().iterator().next()); } else { exchange.getIn().setBody(map); } } else { // store data as is exchange.getIn().setBody(data); } }
message = getResponseMessage(exchange, ctx, msg); } catch (Exception e) { exchange.setException(e); callback.done(false); return; if (ExchangeHelper.isOutCapable(exchange)) { exchange.setOut(message); } else { exchange.setIn(message); if (ExchangeHelper.isOutCapable(exchange)) { 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); callback.done(false);
@Override public void onComplete(Exchange exchange) { // should channel be closed after complete? Boolean close; if (ExchangeHelper.isOutCapable(exchange)) { 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); } // should we disconnect, the header can override the configuration boolean disconnect = getConfiguration().isDisconnect(); if (close != null) { disconnect = close; } if (disconnect) { LOG.trace("Closing channel {} as routing the Exchange is done", channel); NettyHelper.close(channel); } releaseChannel(channelFuture); } });
@Override public void doWithMessage(WebServiceMessage responseMessage) throws IOException, TransformerException { SoapMessage soapMessage = (SoapMessage) responseMessage; if (ExchangeHelper.isOutCapable(exchange)) { exchange.getOut().copyFromWithNewBody(exchange.getIn(), soapMessage.getPayloadSource()); populateHeaderAndAttachmentsFromResponse(exchange.getOut(), soapMessage); } else { exchange.getIn().setBody(soapMessage.getPayloadSource()); populateHeaderAndAttachmentsFromResponse(exchange.getIn(), soapMessage); } } });
log.warn("Timeout occurred after {} millis waiting for reply message with correlationID [{}] on destination {}." + " Setting ExchangeTimedOutException on {} and continue routing.", holder.getRequestTimeout(), holder.getCorrelationId(), replyTo, ExchangeHelper.logIds(exchange)); exchange.setException(new ExchangeTimedOutException(exchange, holder.getRequestTimeout(), msg)); } else { if (exchange.hasOut()) { exchange.getOut().setHeader(RabbitMQConstants.CORRELATIONID, holder.getOriginalCorrelationId()); } else { exchange.getIn().setHeader(RabbitMQConstants.CORRELATIONID, holder.getOriginalCorrelationId()); callback.done(false);
@Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws NoTypeConversionAvailableException, MessagingException, IOException { if (multipartWithoutAttachment || headersInline || exchange.getIn().hasAttachments()) { ContentType contentType = getContentType(exchange); exchange.getOut().removeHeader(Exchange.CONTENT_TYPE); byte[] bodyContent = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, graph); Session session = Session.getInstance(System.getProperties()); MimeMessage mm = new MimeMessage(session); writeBodyPart(bodyContent, part, contentType); mp.addBodyPart(part); for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) { String attachmentFilename = entry.getKey(); Attachment attachment = entry.getValue(); for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) { if (includeHeaders.matcher(entry.getKey()).matches()) { String headerStr = ExchangeHelper.convertToType(exchange, String.class, entry.getValue()); if (headerStr != null) { mm.setHeader(entry.getKey(), headerStr); } else { InputStream is = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph); IOHelper.copyAndCloseInput(is, stream);
private static RequestEntity asRequestEntity(byte[] data, Exchange exchange) throws Exception { if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { return new InputStreamRequestEntity(GZIPHelper.compressGzip(exchange.getIn() .getHeader(Exchange.CONTENT_ENCODING, String.class), data), ExchangeHelper .getContentType(exchange)); } else { // should set the content type here if (exchange != null) { return new InputStreamRequestEntity(new ByteArrayInputStream(data), ExchangeHelper.getContentType(exchange)); } else { return new InputStreamRequestEntity(new ByteArrayInputStream(data)); } } } }
protected Message getAnswerMessage(final Exchange exchange) { Message answer = exchange.getIn(); if (ExchangeHelper.isOutCapable(exchange)) { answer = exchange.getOut(); // preserve headers answer.getHeaders().putAll(exchange.getIn().getHeaders()); } return answer; }
/** * Gets the Camel {@link Message} to use as the message to be set on the current {@link Exchange} when * we have received a reply message. * <p/> * * @param exchange the current exchange * @param ctx the channel handler context * @param message the incoming event which has the response message from Netty. * @return the Camel {@link Message} to set on the current {@link Exchange} as the response message. * @throws Exception is thrown if error getting the response message */ protected Message getResponseMessage(Exchange exchange, ChannelHandlerContext ctx, Object message) throws Exception { Object body = message; if (LOG.isDebugEnabled()) { LOG.debug("Channel: {} received body: {}", ctx.channel(), body); } // if textline enabled then covert to a String which must be used for textline if (producer.getConfiguration().isTextline()) { body = producer.getContext().getTypeConverter().mandatoryConvertTo(String.class, exchange, message); } // set the result on either IN or OUT on the original exchange depending on its pattern if (ExchangeHelper.isOutCapable(exchange)) { NettyPayloadHelper.setOut(exchange, body); return exchange.getOut(); } else { NettyPayloadHelper.setIn(exchange, body); return exchange.getIn(); } }
public static byte[] getPayloadFromExchange(Exchange exchange) { return ExchangeHelper.convertToType(exchange, byte[].class, exchange.getIn().getBody()); }
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); 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); Object token = ExchangeHelper.getMandatoryHeader(exchange, ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, Object.class); encryptedToken = (ByteSource) token; } else { throw new CamelExchangeException("Shiro security header " + ShiroSecurityConstants.SHIRO_SECURITY_TOKEN + " is unsupported type: " + ObjectHelper.classCanonicalName(token), exchange);
@Override public void onDone(final Exchange response) { // check for timeout, which then already would have invoked the latch if (latch.getCount() == 0) { if (log.isTraceEnabled()) { log.trace("{}. Timeout occurred so response will be ignored: {}", this, response.hasOut() ? response.getOut() : response.getIn()); } } else { if (log.isTraceEnabled()) { log.trace("{} with response: {}", this, response.hasOut() ? response.getOut() : response.getIn()); } try { ExchangeHelper.copyResults(exchange, response); } finally { // always ensure latch is triggered latch.countDown(); } } }
log.warn("Timeout occurred after {} millis waiting for reply message with correlationID [{}] on destination {}." + " Setting ExchangeTimedOutException on {} and continue routing.", new Object[]{holder.getRequestTimeout(), holder.getCorrelationId(), replyTo, ExchangeHelper.logIds(exchange)}); exchange.setException(new ExchangeTimedOutException(exchange, holder.getRequestTimeout(), msg)); } else { Message message = holder.getMessage(); exchange.setOut(response); Object body = response.getBody(); exchange.setException((Exception) body); } else { log.debug("Reply received. OUT message body set to reply payload: {}", body); boolean isFault = exchange.getContext().getTypeConverter().tryConvertTo(boolean.class, faultHeader); log.debug("Transfer fault on OUT message: {}", isFault); if (isFault) { exchange.getOut().setFault(true); exchange.getOut().setHeader("JMSCorrelationID", holder.getOriginalCorrelationId()); callback.done(false);
Message in = exchange.getIn(); if (in.getBody() == null) { return null; RequestEntity answer = in.getBody(RequestEntity.class); if (answer == null) { try { Object data = in.getBody(); if (data != null) { String contentType = ExchangeHelper.getContentType(exchange);