This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
HelloRoute.java
133 lines (120 loc) · 4.61 KB
/
HelloRoute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package net.osgiliath.sample.enterprise.routes;
/*
* #%L
* net.osgiliath.hello.routes
* %%
* Copyright (C) 2013 Osgiliath
* %%
* Licensed 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.
* #L%
*/
import java.io.InputStream;
import java.io.StringWriter;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import lombok.Setter;
import net.osgiliath.sample.webapp.business.spi.model.Hellos;
import net.osgiliath.sample.webapp.model.entities.HelloEntity;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.spi.DataFormat;
import org.apache.commons.io.IOUtils;
/**
* sample route, see apache camel and EIP keyword on the net ;).
*
* @author charliemordant
*/
@ContextName(value = "camelctx")
public class HelloRoute extends RouteBuilder {
/**
* Json Dataformat.
*/
private final DataFormat helloObjectJSonFormat = new JacksonDataFormat(
HelloEntity.class, Hellos.class);
/**
* JSR303 Validation message processor.
*/
@Inject
@Named("thrownExceptionMessageToInBodyProcessor")
@Setter
private Processor thrownExceptionMessageToInBodyProcessor;
/**
* XmlJson processor.
*/
@Inject
@Named("xmljson")
private DataFormat xmljson;
/**
* changes inputstream to string.
*/
private Processor octetsStreamToStringProcessor = new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
final InputStream bodyObject = exchange.getIn()
.getBody(InputStream.class);
final StringWriter writer = new StringWriter();
IOUtils.copy(bodyObject, writer);
final String theString = writer.toString();
exchange.getIn().setBody(theString);
}
};
/**
* Messaging route.
*/
@Override
public void configure() throws Exception {
final JAXBContext ctx = JAXBContext
.newInstance(new Class[] { HelloEntity.class, Hellos.class, });
final DataFormat jaxBDataFormat = new JaxbDataFormat(ctx);
from("properties:{{helloApp.inCamelQueueJMS}}")
.log(LoggingLevel.INFO, "Received message: \"${in.body}\"").choice()
.when(header("httpRequestType").isEqualTo(HttpMethod.POST))
.to("direct:persistObject").endChoice().otherwise()
.setBody(
simple("{error: 'Command not supported for the JaxRS queue'}"))
.to("direct:toError");
from("direct:persistObject")
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethod.POST))
.setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_XML))
.unmarshal(this.helloObjectJSonFormat).marshal(jaxBDataFormat)
.log(LoggingLevel.INFO, "marshalled: ${body}").doTry()
.inOnly("properties:{{helloApp.restEndpoint}}").to("direct:updateTopic")
.doCatch(Exception.class)
.log(LoggingLevel.WARN, "Exception while persisting message")
.to("direct:helloValidationError").end();
from("direct:updateTopic")
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethod.GET))
.setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_XML))
.inOut("properties:{{helloApp.restEndpoint}}").inOut("direct:marshall")
.to("properties:{{helloApp.outCamelTopicJMS}}");
from("direct:marshall").process(this.octetsStreamToStringProcessor)
.log("hello data retrieved from JaxRS : ${in.body}")
.marshal(this.xmljson).log(LoggingLevel.INFO, "marshalled: ${body}");
from("direct:helloValidationError")
.process(this.thrownExceptionMessageToInBodyProcessor)
.process(exchange -> exchange.getIn()
.setBody(exchange.getIn().getBody(String.class)
.replaceAll("\"", "'").replaceAll("\n", "")))
.setBody(simple("{\"error\": \"${body}\"}"))
.log("Subscription error: ${body}")
.to("properties:{{helloApp.outCamelErrorQueueJMS}}");
}
}