forked from apache/camel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contract based type awareness and transformer which leverages the…
… type metadata
- Loading branch information
1 parent
8dc8d59
commit 498c27d
Showing
37 changed files
with
2,458 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
camel-core/src/main/java/org/apache/camel/impl/transformer/DataFormatTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* 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.impl.transformer; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
import org.apache.camel.CamelContext; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Message; | ||
import org.apache.camel.converter.stream.OutputStreamBuilder; | ||
import org.apache.camel.model.DataFormatDefinition; | ||
import org.apache.camel.model.transformer.DataFormatTransformerDefinition; | ||
import org.apache.camel.spi.DataFormat; | ||
import org.apache.camel.spi.DataType; | ||
import org.apache.camel.spi.Transformer; | ||
|
||
/** | ||
* A <a href="http://camel.apache.org/transformer.html">Transformer</a> | ||
* leverages DataFormat to perform transformation. | ||
*/ | ||
public class DataFormatTransformer extends Transformer { | ||
|
||
private String dataFormatRef; | ||
|
||
/** | ||
* Perform data transformation with specified from/to type using DataFormat. | ||
* @param message message to apply transformation | ||
* @param from 'from' data type | ||
* @param to 'to' data type | ||
*/ | ||
@Override | ||
public void transform(Message message, DataType from, DataType to) throws Exception { | ||
Exchange exchange = message.getExchange(); | ||
CamelContext context = exchange.getContext(); | ||
DataFormat dataFormat = DataFormatDefinition.getDataFormat( | ||
exchange.getUnitOfWork().getRouteContext(), null, dataFormatRef); | ||
|
||
// Unmarshalling into Java Object | ||
if (to.isJavaType() && (from.equals(getFrom()) || from.getModel().equals(getModel()))) { | ||
Object answer = dataFormat.unmarshal(exchange, message.getBody(InputStream.class)); | ||
Class<?> toClass = context.getClassResolver().resolveClass(to.getName()); | ||
if (!toClass.isAssignableFrom(answer.getClass())) { | ||
answer = context.getTypeConverter().mandatoryConvertTo(toClass, answer); | ||
} | ||
message.setBody(answer); | ||
|
||
// Marshalling from Java Object | ||
} else if (from.isJavaType() && (to.equals(getTo()) || to.getModel().equals(getModel()))) { | ||
Object input = message.getBody(); | ||
Class<?> fromClass = context.getClassResolver().resolveClass(from.getName()); | ||
if (!fromClass.isAssignableFrom(input.getClass())) { | ||
input = context.getTypeConverter().mandatoryConvertTo(fromClass, input); | ||
} | ||
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange); | ||
dataFormat.marshal(exchange, message.getBody(), osb); | ||
message.setBody(osb.build()); | ||
|
||
} else { | ||
throw new IllegalArgumentException("Unsupported transformation: from='" + from + ", to='" + to + "'"); | ||
} | ||
} | ||
|
||
/** | ||
* Set DataFormat ref. | ||
* @param ref DataFormat ref | ||
* @return this DataFormatTransformer instance | ||
*/ | ||
public DataFormatTransformer setDataFormatRef(String ref) { | ||
this.dataFormatRef = ref; | ||
return this; | ||
} | ||
} |
Oops, something went wrong.
In case of Exception, you put the transformer (which is null) in the map even if it has not been initialized?