-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
camel-jackson-protobuf data format native support
Fixes #2615
- Loading branch information
1 parent
492724a
commit 6713ece
Showing
20 changed files
with
326 additions
and
92 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
51 changes: 0 additions & 51 deletions
51
.../java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufResource.java
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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
89 changes: 89 additions & 0 deletions
89
.../java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufResource.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,89 @@ | ||
/* | ||
* 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.quarkus.component.jackson.protobuf.it; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema; | ||
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader; | ||
import org.apache.camel.ProducerTemplate; | ||
import org.apache.camel.component.jackson.SchemaResolver; | ||
import org.apache.camel.component.jackson.protobuf.JacksonProtobufDataFormat; | ||
import org.apache.camel.util.IOHelper; | ||
|
||
@Path("/jackson-protobuf") | ||
@ApplicationScoped | ||
public class JacksonProtobufResource { | ||
|
||
@Inject | ||
ProducerTemplate producerTemplate; | ||
|
||
@Path("/marshal") | ||
@POST | ||
public Response protobufJacksonMarshal(String message) throws Exception { | ||
Pojo pojo = new Pojo(message); | ||
byte[] result = producerTemplate.requestBody("direct:marshal-pojo", pojo, byte[].class); | ||
return Response.ok(result).build(); | ||
} | ||
|
||
@Path("/unmarshal/{type}") | ||
@POST | ||
@Consumes("application/octet-stream") | ||
public Response protobufJacksonUnMarshal(@PathParam("type") String type, byte[] message) throws Exception { | ||
Response.ResponseBuilder builder = Response.ok(); | ||
String directUri = "direct:unmarshal-" + type; | ||
|
||
if (type.equals("pojo") || type.equals("defined-dataformat")) { | ||
Pojo result = producerTemplate.requestBody(directUri, message, Pojo.class); | ||
builder.entity(result.getText()); | ||
} else if (type.equals("json-node")) { | ||
JsonNode result = producerTemplate.requestBody(directUri, message, JsonNode.class); | ||
builder.entity(result.at("/text").asText()); | ||
} else { | ||
throw new IllegalArgumentException("Unknown unmarshal type: " + type); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
@Named | ||
public SchemaResolver protobufSchemaResolver() throws IOException { | ||
InputStream resource = JacksonProtobufResource.class.getResourceAsStream("/pojo.proto"); | ||
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(IOHelper.loadText(resource)); | ||
return ex -> schema; | ||
} | ||
|
||
@Named | ||
public JacksonProtobufDataFormat jacksonProtobufDataFormat() { | ||
JacksonProtobufDataFormat dataFormat = new JacksonProtobufDataFormat(); | ||
dataFormat.setAutoDiscoverObjectMapper(true); | ||
dataFormat.setAutoDiscoverSchemaResolver(true); | ||
dataFormat.setUnmarshalType(Pojo.class); | ||
return dataFormat; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...in/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufRoutes.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,38 @@ | ||
/* | ||
* 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.quarkus.component.jackson.protobuf.it; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.apache.camel.builder.RouteBuilder; | ||
import org.apache.camel.model.dataformat.ProtobufLibrary; | ||
|
||
public class JacksonProtobufRoutes extends RouteBuilder { | ||
@Override | ||
public void configure() throws Exception { | ||
from("direct:marshal-pojo") | ||
.marshal().protobuf(ProtobufLibrary.Jackson, Pojo.class, "protobufSchemaResolver"); | ||
|
||
from("direct:unmarshal-pojo") | ||
.unmarshal().protobuf(ProtobufLibrary.Jackson, Pojo.class, "protobufSchemaResolver"); | ||
|
||
from("direct:unmarshal-json-node") | ||
.unmarshal().protobuf(ProtobufLibrary.Jackson, JsonNode.class, "protobufSchemaResolver"); | ||
|
||
from("direct:unmarshal-defined-dataformat") | ||
.unmarshal("jacksonProtobufDataFormat"); | ||
} | ||
} |
Oops, something went wrong.