本文整理汇总了Java中org.apache.camel.Converter类的典型用法代码示例。如果您正苦于以下问题:Java Converter类的具体用法?Java Converter怎么用?Java Converter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Converter类属于org.apache.camel包,在下文中一共展示了Converter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: genericFileToInputStream
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static InputStream genericFileToInputStream(GenericFile<?> file, Exchange exchange) throws IOException, NoTypeConversionAvailableException {
if (file.getFile() instanceof File) {
// prefer to use a file input stream if its a java.io.File
File f = (File) file.getFile();
// the file must exists
if (f.exists()) {
// read the file using the specified charset
String charset = file.getCharset();
if (charset != null) {
LOG.debug("Read file {} with charset {}", f, file.getCharset());
} else {
LOG.debug("Read file {} (no charset)", f);
}
return IOConverter.toInputStream(f, charset);
}
}
if (exchange != null) {
// otherwise ensure the body is loaded as we want the input stream of the body
file.getBinding().loadContent(exchange, file);
return exchange.getContext().getTypeConverter().convertTo(InputStream.class, exchange, file.getBody());
} else {
// should revert to fallback converter if we don't have an exchange
return null;
}
}
示例2: toClass
import org.apache.camel.Converter; //导入依赖的package包/类
/**
* Returns the converted value, or null if the value is null
*/
@Converter
public static Class<?> toClass(Object value, Exchange exchange) {
if (value instanceof Class) {
return (Class<?>) value;
} else if (value instanceof String) {
// prefer to use class resolver API
if (exchange != null) {
return exchange.getContext().getClassResolver().resolveClass((String) value);
} else {
return ObjectHelper.loadClass((String) value);
}
} else {
return null;
}
}
示例3: toByteBuffer
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static ByteBuffer toByteBuffer(File file) throws IOException {
InputStream in = null;
try {
byte[] buf = new byte[(int)file.length()];
in = IOHelper.buffered(new FileInputStream(file));
int sizeLeft = (int)file.length();
int offset = 0;
while (sizeLeft > 0) {
int readSize = in.read(buf, offset, sizeLeft);
sizeLeft -= readSize;
offset += readSize;
}
return ByteBuffer.wrap(buf);
} finally {
IOHelper.close(in, "Failed to close file stream: " + file.getPath(), LOG);
}
}
示例4: toList
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static List<Map<String, Object>> toList(DataSet dataSet) {
List<Map<String, Object>> answer = new ArrayList<Map<String, Object>>();
dataSet.goTop();
while (dataSet.next()) {
Map<String, Object> map = new HashMap<String, Object>();
putValues(map, dataSet);
answer.add(map);
}
return answer;
}
示例5: toByteArray
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static byte[] toByteArray(ByteBuffer buffer) {
buffer.mark();
try {
byte[] answer = new byte[buffer.remaining()];
buffer.get(answer);
return answer;
} finally {
buffer.reset();
}
}
示例6: sourceToCxfPayload
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static <T> CxfPayload<T> sourceToCxfPayload(Source src, Exchange exchange) {
List<T> headers = new ArrayList<T>();
List<Source> body = new ArrayList<Source>();
body.add(src);
return new CxfPayload<T>(headers, body, null);
}
示例7: toArrayList
import org.apache.camel.Converter; //导入依赖的package包/类
/**
* Converts an {@link Iterator} to a {@link ArrayList}
*/
@Converter
public static <T> ArrayList<T> toArrayList(Iterator<T> it) {
ArrayList<T> list = new ArrayList<T>();
while (it.hasNext()) {
list.add(it.next());
}
return list;
}
示例8: toString
import org.apache.camel.Converter; //导入依赖的package包/类
/**
* Converts the given JavaMail multipart to a String body, where the content-type of the multipart
* must be text based (ie start with text). Can return null.
*/
@Converter
public static String toString(Multipart multipart) throws MessagingException, IOException {
int size = multipart.getCount();
for (int i = 0; i < size; i++) {
BodyPart part = multipart.getBodyPart(i);
if (part.getContentType().toLowerCase().startsWith("text")) {
return part.getContent().toString();
}
}
return null;
}
示例9: toPayload
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static Payload toPayload(final StreamSourceCache cache, Exchange exchange) throws IOException {
long contentLength = ByteStreams.length(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return cache.getInputStream();
}
});
cache.reset();
InputStreamPayload payload = new InputStreamPayload(cache.getInputStream());
payload.getContentMetadata().setContentLength(contentLength);
setContentMetadata(payload, exchange);
return payload;
}
示例10: toOutgoingMessage
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static OutgoingMessage toOutgoingMessage(String message, Exchange exchange) {
if (message == null) {
// fail fast
return null;
}
Object typeObj = exchange.getIn().getHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE);
TelegramMediaType type;
if (typeObj instanceof String) {
type = TelegramMediaType.valueOf((String) typeObj);
} else {
type = (TelegramMediaType) typeObj;
}
// If the message is a string, it will be converted to a OutgoingTextMessage
if (type == null) {
type = TelegramMediaType.TEXT;
}
OutgoingMessage result;
switch (type) {
case TEXT: {
OutgoingTextMessage txt = new OutgoingTextMessage();
txt.setText(message);
result = txt;
break;
}
default: {
throw new IllegalArgumentException("Unsupported conversion from String to media type " + type);
}
}
return result;
}
示例11: toDeleteRequest
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static DeleteRequest toDeleteRequest(String id, Exchange exchange) {
return new DeleteRequest()
.index(exchange.getIn().getHeader(
ElasticsearchConstants.PARAM_INDEX_NAME,
String.class))
.type(exchange.getIn().getHeader(
ElasticsearchConstants.PARAM_INDEX_TYPE,
String.class)).id(id);
}
示例12: toDOMDocument
import org.apache.camel.Converter; //导入依赖的package包/类
/**
* Create a DOM document from the given Node.
*
* If the node is an document, just cast it, if the node is an root element, retrieve its
* owner element or create a new document and import the node.
*/
@Converter
public Document toDOMDocument(final Node node) throws ParserConfigurationException, TransformerException {
ObjectHelper.notNull(node, "node");
// If the node is the document, just cast it
if (node instanceof Document) {
return (Document) node;
// If the node is an element
} else if (node instanceof Element) {
Element elem = (Element) node;
// If this is the root element, return its owner document
if (elem.getOwnerDocument().getDocumentElement() == elem) {
return elem.getOwnerDocument();
// else, create a new doc and copy the element inside it
} else {
Document doc = createDocument();
// import node must not occur concurrent on the same node (must be its owner)
// so we need to synchronize on it
synchronized (node.getOwnerDocument()) {
doc.appendChild(doc.importNode(node, true));
}
return doc;
}
// other element types are not handled
} else {
throw new TransformerException("Unable to convert DOM node to a Document: " + node);
}
}
示例13: toByteArray
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static byte[] toByteArray(File file) throws IOException {
InputStream is = toInputStream(file);
try {
return toBytes(is);
} finally {
IOHelper.close(is, "file", LOG);
}
}
示例14: elementToCxfPayload
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static <T> CxfPayload<T> elementToCxfPayload(Element element, Exchange exchange) {
List<T> headers = new ArrayList<T>();
List<Element> body = new ArrayList<Element>();
body.add(element);
return new CxfPayload<T>(headers, body);
}
示例15: convertToString
import org.apache.camel.Converter; //导入依赖的package包/类
@Converter
public static String convertToString(ExecResult result, Exchange exchange) throws FileNotFoundException {
// special for string, as we want an empty string if no output from stdin / stderr
InputStream is = toInputStream(result);
if (is != null) {
return exchange.getContext().getTypeConverter().convertTo(String.class, exchange, is);
} else {
// no stdin/stdout, so return an empty string
return "";
}
}