Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAX-RS body writers for Vert.x JsonObject and JsonArray #1549

Merged
merged 1 commit into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extensions/vertx/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-runtime</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Red Hat 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 io.quarkus.vertx.runtime;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import io.vertx.core.json.JsonArray;

/**
* A body writer that allows to return a Vert.x {@link JsonArray} as JAX-RS response content.
*
* @author Thomas Segismont
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonArrayWriter implements MessageBodyWriter<JsonArray> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JsonArray.class;
}

@Override
public void writeTo(JsonArray jsonArray, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
entityStream.write(jsonArray.toBuffer().getBytes());
entityStream.flush();
entityStream.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Red Hat 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 io.quarkus.vertx.runtime;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import io.vertx.core.json.JsonObject;

/**
* A body writer that allows to return a Vert.x {@link JsonObject} as JAX-RS response content.
*
* @author Thomas Segismont
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonObjectWriter implements MessageBodyWriter<JsonObject> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JsonObject.class;
}

@Override
public void writeTo(JsonObject jsonObject, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
entityStream.write(jsonObject.toBuffer().getBytes());
entityStream.flush();
entityStream.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
io.quarkus.vertx.runtime.JsonObjectWriter
io.quarkus.vertx.runtime.JsonArrayWriter
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Red Hat 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 io.quarkus.vertx.tests;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

/**
* @author Thomas Ssegismont
*/
@Path("/body-writers")
public class JsonTestResource {

@GET
@Path("/json/sync")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject jsonSync() {
return new JsonObject().put("Hello", "World");
}

@GET
@Path("/array/sync")
@Produces(MediaType.APPLICATION_JSON)
public JsonArray arraySync() {
return new JsonArray().add("Hello").add("World");
}

@GET
@Path("/json/async")
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<JsonObject> jsonAsync() {
return CompletableFuture.completedFuture(new JsonObject().put("Hello", "World"));
}

@GET
@Path("/array/async")
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<JsonArray> arrayAsync() {
return CompletableFuture.completedFuture(new JsonArray().add("Hello").add("World"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Red Hat 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 io.quarkus.vertx.runtime.tests;

import io.quarkus.test.junit.SubstrateTest;

/**
* @author Thomas Segismont
*/
@SubstrateTest
public class JsonWriterIT extends JsonWriterTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Red Hat 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 io.quarkus.vertx.runtime.tests;

import static org.hamcrest.CoreMatchers.equalTo;

import java.util.Arrays;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;

/**
* @author Thomas Segismont
*/
@QuarkusTest
public class JsonWriterTest {

@Test
public void testJsonSync() {
RestAssured.when().get("/vertx-test/body-writers/json/sync").then()
.statusCode(200).body("Hello", equalTo("World"));
}

@Test
public void testArraySync() {
RestAssured.when().get("/vertx-test/body-writers/array/sync").then()
.statusCode(200).body("", equalTo(Arrays.asList("Hello", "World")));
}

@Test
public void testJsonAsync() {
RestAssured.when().get("/vertx-test/body-writers/json/async").then()
.statusCode(200).body("Hello", equalTo("World"));
}

@Test
public void testArrayAsync() {
RestAssured.when().get("/vertx-test/body-writers/array/async").then()
.statusCode(200).body("", equalTo(Arrays.asList("Hello", "World")));
}
}