org.apache.camel.component.jclouds.JcloudsPayloadConverter Maven / Gradle / Ivy
The newest version!
/*
* 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.component.jclouds;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.xml.transform.stream.StreamSource;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.apache.camel.TypeConverter;
import org.apache.camel.WrappedFile;
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.xml.StreamSourceCache;
import org.jclouds.io.Payload;
import org.jclouds.io.payloads.ByteSourcePayload;
import org.jclouds.io.payloads.InputStreamPayload;
@Converter(generateLoader = true)
public final class JcloudsPayloadConverter {
private JcloudsPayloadConverter() {
//Utility Class
}
@Converter
public static Payload toPayload(byte[] bytes) {
return new ByteSourcePayload(ByteSource.wrap(bytes));
}
@Converter
public static Payload toPayload(String str, Exchange ex) throws UnsupportedEncodingException {
return toPayload(str.getBytes(ExchangeHelper.getCharsetName(ex)));
}
public static Payload toPayload(String str) throws UnsupportedEncodingException {
return toPayload(str, null);
}
@Converter
public static Payload toPayload(File file) {
return new ByteSourcePayload(Files.asByteSource(file));
}
protected static Payload setContentMetadata(Payload payload, Exchange exchange) {
// Just add an NPE check on the payload
if (exchange == null) {
return payload;
}
String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
String contentDisposition = exchange.getIn().getHeader(JcloudsConstants.CONTENT_DISPOSITION, String.class);
String contentLanguage = exchange.getIn().getHeader(JcloudsConstants.CONTENT_LANGUAGE, String.class);
Date payloadExpires = exchange.getIn().getHeader(JcloudsConstants.PAYLOAD_EXPIRES, Date.class);
if (ObjectHelper.isNotEmpty(contentType)) {
payload.getContentMetadata().setContentType(contentType);
}
if (ObjectHelper.isNotEmpty(contentEncoding)) {
payload.getContentMetadata().setContentEncoding(contentEncoding);
}
if (ObjectHelper.isNotEmpty(contentDisposition)) {
payload.getContentMetadata().setContentDisposition(contentDisposition);
}
if (ObjectHelper.isNotEmpty(contentLanguage)) {
payload.getContentMetadata().setContentLanguage(contentLanguage);
}
if (ObjectHelper.isNotEmpty(payloadExpires)) {
payload.getContentMetadata().setExpires(payloadExpires);
}
return payload;
}
@Converter
public static Payload toPayload(final InputStream is, Exchange exchange) throws IOException {
InputStreamPayload payload = new InputStreamPayload(is);
// only set the contentlength if possible
if (is.markSupported()) {
long contentLength = ByteStreams.toByteArray(is).length;
is.reset();
payload.getContentMetadata().setContentLength(contentLength);
}
return payload;
}
@Converter
public static Payload toPayload(StreamSource source, Exchange exchange) throws IOException {
return toPayload(new StreamSourceCache(source, exchange), exchange);
}
@Converter
public static Payload toPayload(final StreamSourceCache cache, Exchange exchange) throws IOException {
long contentLength = ByteStreams.toByteArray(cache.getInputStream()).length;
cache.reset();
InputStreamPayload payload = new InputStreamPayload(cache.getInputStream());
payload.getContentMetadata().setContentLength(contentLength);
setContentMetadata(payload, exchange);
return payload;
}
@Converter(fallback = true)
@SuppressWarnings("unchecked")
public static T convertTo(Class type, Exchange exchange, Object value, TypeConverterRegistry registry) {
Class> sourceType = value.getClass();
if (type == Payload.class && WrappedFile.class.isAssignableFrom(sourceType)) {
// attempt to convert to JClouds Payload from a file
WrappedFile wf = (WrappedFile) value;
if (wf.getFile() != null) {
TypeConverter converter = registry.lookup(Payload.class, wf.getFile().getClass());
if (converter != null) {
return (T) converter.tryConvertTo(Payload.class, wf.getFile());
}
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy