/** * Find and configure an unmarshaller for the specified data format. */ private synchronized UnmarshalProcessor resolveUnmarshaller( Exchange exchange, String dataFormatId) throws Exception { if (unmarshaller == null) { DataFormat dataFormat = DataFormatDefinition.getDataFormat( exchange.getUnitOfWork().getRouteContext(), null, dataFormatId); if (dataFormat == null) { throw new Exception("Unable to resolve data format for unmarshalling: " + dataFormatId); } // Wrap the data format in a processor and start/configure it. // Stop/shutdown is handled when the corresponding methods are // called on this producer. unmarshaller = new UnmarshalProcessor(dataFormat); unmarshaller.setCamelContext(exchange.getContext()); unmarshaller.start(); } return unmarshaller; }
/** * Find and configure an unmarshaller for the specified data format. */ private synchronized MarshalProcessor resolveMarshaller( Exchange exchange, String dataFormatId) throws Exception { if (marshaller == null) { DataFormat dataFormat = DataFormatDefinition.getDataFormat( exchange.getUnitOfWork().getRouteContext(), null, dataFormatId); if (dataFormat == null) { throw new Exception("Unable to resolve data format for marshalling: " + dataFormatId); } // Wrap the data format in a processor and start/configure it. // Stop/shutdown is handled when the corresponding methods are // called on this producer. marshaller = new MarshalProcessor(dataFormat); marshaller.setCamelContext(exchange.getContext()); marshaller.start(); } return marshaller; }
private static List<RouteDefinition> processCamelContextElement(CamelContextFactoryBean camelContextFactoryBean, SwitchYardCamelContext camelContext) throws Exception { if (camelContext != null) { if (camelContextFactoryBean.getEndpoints() != null) { // processing camelContext/endpoint for (CamelEndpointFactoryBean epBean : camelContextFactoryBean.getEndpoints()) { epBean.setCamelContext(camelContext); camelContext.getWritebleRegistry().put(epBean.getId(), epBean.getObject()); } } if (camelContextFactoryBean.getDataFormats() != null) { // processing camelContext/dataFormat for (DataFormatDefinition dataFormatDef : camelContextFactoryBean.getDataFormats().getDataFormats()) { camelContext.getDataFormats().put(dataFormatDef.getId(), dataFormatDef); } } } return camelContextFactoryBean.getRoutes(); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { org.apache.camel.model.dataformat.XStreamDataFormat xstreamDataFormat = new org.apache.camel.model.dataformat.XStreamDataFormat(); xstreamDataFormat.setConverters(Arrays.asList(new String[] {PersonConverter.class.getName()})); Map<String, DataFormatDefinition> dataFormats = new HashMap<String, DataFormatDefinition>(); dataFormats.put("custom-xstream", xstreamDataFormat); getContext().setDataFormats(dataFormats); from("direct:test-with-session").policy(new KiePolicy()).unmarshal("xstream").to("kie:ksession1").marshal("xstream"); from("direct:test-with-session-json").policy(new KiePolicy()).unmarshal("json").to("kie:ksession1").marshal("json"); from("direct:test-no-session").policy(new KiePolicy()).unmarshal("xstream").to("kie:dynamic").marshal("xstream"); from("direct:test-no-session-custom").policy(new KiePolicy()).unmarshal("custom-xstream").to("kie:dynamic").marshal("custom-xstream"); } }; }
/*** * @return A Map of the contained DataFormatType's indexed by id. */ public Map<String, DataFormatDefinition> asMap() { Map<String, DataFormatDefinition> dataFormatsAsMap = new HashMap<String, DataFormatDefinition>(); for (DataFormatDefinition dataFormatType : getDataFormats()) { dataFormatsAsMap.put(dataFormatType.getId(), dataFormatType); } return dataFormatsAsMap; }
public DataFormatDefinition resolveDataFormatDefinition(String name) { // lookup type and create the data format from it DataFormatDefinition type = lookup(this, name, DataFormatDefinition.class); if (type == null && getDataFormats() != null) { type = getDataFormats().get(name); } return type; }
@SuppressWarnings("unchecked") private T dataFormat(DataFormatDefinition dataFormatType) { switch (operation) { case Unmarshal: return (T) processorType.unmarshal(dataFormatType); case Marshal: return (T) processorType.marshal(dataFormatType); default: throw new IllegalArgumentException("Unknown DataFormat operation: " + operation); } }
private static ProcessorDefinition<?> dataFormat(DataFormatClause<?> self, DataFormatDefinition format) { try { Method m = self.getClass().getDeclaredMethod("dataFormat", DataFormatDefinition.class); m.setAccessible(true); return (ProcessorDefinition<?>) m.invoke(self, format); } catch (Exception e) { throw new IllegalArgumentException("Unknown DataFormat operation", e); } }
@Test public void configureCamelContextXML() throws Exception { domain.setProperty(CamelContextConfigurator.CAMEL_CONTEXT_CONFIG_XML, PATH_CAMEL_CONTEXT_XML); Assert.assertNull(context.getProperty("abc")); Assert.assertNotEquals("foobar-camel-context", context.getName()); Assert.assertEquals(false, context.isUseMDCLogging()); Assert.assertEquals(ManagementStatisticsLevel.All , context.getManagementStrategy().getStatisticsLevel()); Assert.assertEquals(true, context.isAllowUseOriginalMessage()); Assert.assertEquals(false, context.isStreamCaching()); context.start(); Assert.assertNotNull(context.getProperty("abc")); Assert.assertEquals("xyz", context.getProperty("abc")); Assert.assertEquals("foobar-camel-context", context.getName()); Assert.assertEquals(true, context.isUseMDCLogging()); Assert.assertEquals(ManagementStatisticsLevel.RoutesOnly , context.getManagementStrategy().getStatisticsLevel()); Assert.assertEquals(false, context.isAllowUseOriginalMessage()); Assert.assertEquals(true, context.isStreamCaching()); DataFormatDefinition dfd = context.getDataFormats().get("transform-json"); Assert.assertNotNull(dfd); Assert.assertEquals("json-jackson", dfd.getDataFormatName()); Assert.assertTrue(dfd instanceof JsonDataFormat); MockEndpoint mock = context.getEndpoint("mock:output", MockEndpoint.class); mock.expectedMessageCount(1); mock.expectedBodiesReceived("foobar-input"); context.createProducerTemplate().sendBody("direct:input", "foobar-input"); mock.assertIsSatisfied(); }
@Test public void testConfiguration() throws Exception { Assert.assertNotNull(_camelContext.getProperty("abc")); Assert.assertEquals("xyz", _camelContext.getProperty("abc")); Assert.assertEquals("foobar-camel-context", _camelContext.getName()); Assert.assertEquals(true, _camelContext.isUseMDCLogging()); Assert.assertEquals(ManagementStatisticsLevel.RoutesOnly , _camelContext.getManagementStrategy().getStatisticsLevel()); Assert.assertEquals(false, _camelContext.isAllowUseOriginalMessage()); Assert.assertEquals(true, _camelContext.isStreamCaching()); DataFormatDefinition dfd = _camelContext.getDataFormats().get("transform-json"); Assert.assertNotNull(dfd); Assert.assertEquals("json-jackson", dfd.getDataFormatName()); Assert.assertTrue(dfd instanceof JsonDataFormat); MockEndpoint mock = _camelContext.getEndpoint("mock:output", MockEndpoint.class); mock.expectedMessageCount(1); mock.expectedBodiesReceived("foobar-input"); _camelContext.createProducerTemplate().sendBody("direct:input", "foobar-input"); mock.assertIsSatisfied(); // CamelContext should be able to find CDI beans produced by this class from registry. Assert.assertEquals(true, _camelContext.isTracing()); DefaultTraceFormatter formatter = (DefaultTraceFormatter) ((Tracer)_camelContext.getDefaultTracer()).getFormatter(); Assert.assertEquals(false, formatter.isShowBody()); Assert.assertEquals(false, formatter.isShowBreadCrumb()); Assert.assertEquals(100, formatter.getMaxChars()); @SuppressWarnings("deprecation") ErrorHandlerBuilder builder = _camelContext.getErrorHandlerBuilder(); Assert.assertEquals(ErrorHandlerBuilderRef.class, builder.getClass()); Assert.assertEquals("foobarErrorHandler", ((ErrorHandlerBuilderRef)builder).getRef()); }
private AsynchRouteBuilder(String inUri, ServiceExtEnum serviceType, String operation, AsynchResponseProcessor responseProcessor, DataFormatDefinition responseMarshalling) { Assert.hasText(operation, "the operation must not be empty"); Assert.notNull(inUri, "the inUri must not be empty"); Assert.notNull(serviceType, "the serviceType must not be null"); Assert.notNull(responseProcessor, "the responseProcessor must not be null"); Assert.notNull(responseMarshalling, "the responseMarshalling must not be null"); this.inUri = inUri; this.serviceType = serviceType; this.operation = operation; this.responseProcessor = responseProcessor; this.responseMarshalling = responseMarshalling; }
/** * Sets response processor. If not set then general response {@link AsynchResponse} will be used. * * @param responseProcessor the response processor * @param responseMarshalling the response marshalling */ public AsynchRouteBuilder withResponseProcessor(AsynchResponseProcessor responseProcessor, DataFormatDefinition responseMarshalling) { Assert.notNull(responseProcessor, "the responseProcessor must not be null"); Assert.notNull(responseMarshalling, "the responseMarshalling must not be null"); this.responseProcessor = responseProcessor; this.responseMarshalling = responseMarshalling; return this; }
@Override protected DataFormat createDataFormat(RouteContext routeContext) { return DataFormatDefinition.getDataFormat(routeContext, null, ref); }
/** * A list holding the configured data formats */ public void setDataFormats(List<DataFormatDefinition> dataFormats) { this.dataFormats = dataFormats; }
public List<DataFormatDefinition> getDataFormats() { return dataFormats; }
public void setDataFormats(Map<String, DataFormatDefinition> dataFormats) { this.dataFormats = dataFormats; }
public Map<String, DataFormatDefinition> getDataFormats() { return dataFormats; }
public DataFormatDefinition resolveDataFormatDefinition(String name, CamelContext context) { return null; }
private void findDataFormat(DataFormatDefinition dfd, Set<String> dataformats) { if (dfd != null && dfd.getDataFormatName() != null) { dataformats.add(dfd.getDataFormatName()); } }
private static DataFormatDefinition slurper(boolean namespaceAware) { return new DataFormatDefinition(new XmlSlurperDataFormat(namespaceAware)); }
private static DataFormatDefinition parser(boolean namespaceAware) { return new DataFormatDefinition(new XmlParserDataFormat(namespaceAware)); }
@Override @Deprecated public void setDataFormats(Map<String, DataFormatDefinition> dataFormats) { context.setDataFormats(dataFormats); }
@Override @Deprecated public Map<String, DataFormatDefinition> getDataFormats() { return context.getDataFormats(); }
@Override @Deprecated public DataFormatDefinition resolveDataFormatDefinition(String name) { return context.resolveDataFormatDefinition(name); }
@Nullable public DataFormatDefinition getResponseMarshalling() { return responseMarshalling; }
/** * Sets the data formats that can be referenced in the routes. * * @param dataFormats the data formats */ void setDataFormats(Map<String, DataFormatDefinition> dataFormats);
/** * Gets the data formats that can be referenced in the routes. * * @return the data formats available */ Map<String, DataFormatDefinition> getDataFormats();
/** * Resolve a data format definition given its name * * @param name the data format definition name or a reference to it in the {@link Registry} * @return the resolved data format definition, or <tt>null</tt> if not found */ DataFormatDefinition resolveDataFormatDefinition(String name);
/** * Creates new instance with basic mandatory parameters. * * @param serviceType service type * @param operation operation name with processing route * @param inUri the from URI of this route * @param responseProcessor the response processor * @param responseMarshalling the response marshalling * @return new instance */ public static AsynchRouteBuilder newInstance(ServiceExtEnum serviceType, String operation, String inUri, AsynchResponseProcessor responseProcessor, DataFormatDefinition responseMarshalling) { return new AsynchRouteBuilder(inUri, serviceType, operation, responseProcessor, responseMarshalling); }