use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class VelocityTemplateInHeaderTest method assertRespondsWith.
protected void assertRespondsWith(final String headerName, final String headerValue, String expectedBody) throws InvalidPayloadException {
Exchange response = template.request("direct:a", new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
in.setHeader(VelocityConstants.VELOCITY_TEMPLATE, "<hello>${headers." + headerName + "}</hello>");
in.setHeader(headerName, headerValue);
}
});
assertOutMessageBodyEquals(response, expectedBody);
Object template = response.getOut().getHeader(VelocityConstants.VELOCITY_TEMPLATE);
assertNull("Template header should have been removed", template);
Set<Entry<String, Object>> entrySet = response.getOut().getHeaders().entrySet();
boolean keyFound = false;
for (Entry<String, Object> entry : entrySet) {
if (entry.getKey().equals(headerName)) {
keyFound = true;
}
}
assertTrue("Header should been found", keyFound);
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class StAXJAXBIteratorExpression method evaluate.
@Override
@SuppressWarnings("unchecked")
public Object evaluate(Exchange exchange) {
try {
XMLEventReader reader;
if (isNamespaceAware) {
reader = exchange.getIn().getMandatoryBody(XMLEventReader.class);
} else {
InputStream inputStream = exchange.getIn().getMandatoryBody(InputStream.class);
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
reader = xmlInputFactory.createXMLEventReader(inputStream);
}
Class<T> clazz = handled;
if (clazz == null && handledName != null) {
clazz = (Class<T>) exchange.getContext().getClassResolver().resolveMandatoryClass(handledName);
}
return createIterator(reader, clazz);
} catch (InvalidPayloadException e) {
exchange.setException(e);
return null;
} catch (JAXBException e) {
exchange.setException(e);
return null;
} catch (ClassNotFoundException e) {
exchange.setException(e);
return null;
} catch (XMLStreamException e) {
exchange.setException(e);
return null;
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class JaxbDataFormat method marshal.
void marshal(Exchange exchange, Object graph, OutputStream stream, Marshaller marshaller) throws XMLStreamException, JAXBException, NoTypeConversionAvailableException, IOException, InvalidPayloadException {
Object element = graph;
if (partialClass != null && getPartNamespace() != null) {
element = new JAXBElement<Object>(getPartNamespace(), partialClass, graph);
}
// only marshal if its possible
if (introspector.isElement(element)) {
if (asXmlStreamWriter(exchange)) {
XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
if (needFiltering(exchange)) {
writer = new FilteringXmlStreamWriter(writer);
}
if (xmlStreamWriterWrapper != null) {
writer = xmlStreamWriterWrapper.wrapWriter(writer);
}
marshaller.marshal(element, writer);
} else {
marshaller.marshal(element, stream);
}
return;
} else if (objectFactory && element != null) {
Method objectFactoryMethod = JaxbHelper.getJaxbElementFactoryMethod(camelContext, element.getClass());
if (objectFactoryMethod != null) {
try {
Object instance = objectFactoryMethod.getDeclaringClass().newInstance();
if (instance != null) {
Object toMarshall = objectFactoryMethod.invoke(instance, element);
if (asXmlStreamWriter(exchange)) {
XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
if (needFiltering(exchange)) {
writer = new FilteringXmlStreamWriter(writer);
}
if (xmlStreamWriterWrapper != null) {
writer = xmlStreamWriterWrapper.wrapWriter(writer);
}
marshaller.marshal(toMarshall, writer);
} else {
marshaller.marshal(toMarshall, stream);
}
return;
}
} catch (Exception e) {
LOG.debug("Unable to create JAXBElement object for type " + element.getClass() + " due to " + e.getMessage(), e);
}
}
}
// cannot marshal
if (!mustBeJAXBElement) {
// write the graph as is to the output stream
if (LOG.isDebugEnabled()) {
LOG.debug("Attempt to marshalling non JAXBElement with type {} as InputStream", ObjectHelper.classCanonicalName(graph));
}
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
IOHelper.copyAndCloseInput(is, stream);
} else {
throw new InvalidPayloadException(exchange, JAXBElement.class);
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class FileOperations method storeFile.
public boolean storeFile(String fileName, Exchange exchange) throws GenericFileOperationFailedException {
ObjectHelper.notNull(endpoint, "endpoint");
File file = new File(fileName);
// if an existing file already exists what should we do?
if (file.exists()) {
if (endpoint.getFileExist() == GenericFileExist.Ignore) {
// ignore but indicate that the file was written
LOG.trace("An existing file already exists: {}. Ignore and do not override it.", file);
return true;
} else if (endpoint.getFileExist() == GenericFileExist.Fail) {
throw new GenericFileOperationFailedException("File already exist: " + file + ". Cannot write new file.");
} else if (endpoint.getFileExist() == GenericFileExist.Move) {
// move any existing file first
doMoveExistingFile(fileName);
}
}
// Do an explicit test for a null body and decide what to do
if (exchange.getIn().getBody() == null) {
if (endpoint.isAllowNullBody()) {
LOG.trace("Writing empty file.");
try {
writeFileEmptyBody(file);
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
} else {
throw new GenericFileOperationFailedException("Cannot write null body to file: " + file);
}
}
// 3. write stream to file
try {
// is there an explicit charset configured we must write the file as
String charset = endpoint.getCharset();
// we can optimize and use file based if no charset must be used, and the input body is a file
File source = null;
boolean fileBased = false;
if (charset == null) {
// if no charset, then we can try using file directly (optimized)
Object body = exchange.getIn().getBody();
if (body instanceof WrappedFile) {
body = ((WrappedFile<?>) body).getFile();
}
if (body instanceof File) {
source = (File) body;
fileBased = true;
}
}
if (fileBased) {
// okay we know the body is a file based
// so try to see if we can optimize by renaming the local work path file instead of doing
// a full file to file copy, as the local work copy is to be deleted afterwards anyway
// local work path
File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class);
if (local != null && local.exists()) {
boolean renamed = writeFileByLocalWorkPath(local, file);
if (renamed) {
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
// clear header as we have renamed the file
exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, null);
// to the target.
return true;
}
} else if (source != null && source.exists()) {
// no there is no local work file so use file to file copy if the source exists
writeFileByFile(source, file);
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
return true;
}
}
if (charset != null) {
// charset configured so we must use a reader so we can write with encoding
Reader in = exchange.getContext().getTypeConverter().tryConvertTo(Reader.class, exchange, exchange.getIn().getBody());
if (in == null) {
// okay no direct reader conversion, so use an input stream (which a lot can be converted as)
InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
in = new InputStreamReader(is);
}
// buffer the reader
in = IOHelper.buffered(in);
writeFileByReaderWithCharset(in, file, charset);
} else {
// fallback and use stream based
InputStream in = exchange.getIn().getMandatoryBody(InputStream.class);
writeFileByStream(in, file);
}
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
} catch (InvalidPayloadException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class ProtobufDataFormat method unmarshal.
/*
* (non-Javadoc)
* @see org.apache.camel.spi.DataFormat#unmarshal(org.apache.camel.Exchange,
* java.io.InputStream)
*/
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
ObjectHelper.notNull(defaultInstance, "defaultInstance or instanceClassName must be set", this);
Builder builder = defaultInstance.newBuilderForType().mergeFrom(inputStream);
if (!builder.isInitialized()) {
// TODO which exception should be thrown here?
throw new InvalidPayloadException(exchange, defaultInstance.getClass());
}
return builder.build();
}
Aggregations