Skip to content

Commit

Permalink
add generic request builder, add ability to use jakarta.ws.rs.Respons…
Browse files Browse the repository at this point in the history
…e as a return type
  • Loading branch information
vegegoku committed Aug 6, 2024
1 parent 92ed911 commit 2c723e9
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 1 deletion.
1 change: 0 additions & 1 deletion domino-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${jax.rs.version}</version>
</dependency>

<dependency>
<groupId>org.dominokit</groupId>
<artifactId>domino-rest-jaxrs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.dominokit.jackson.processor.deserialization.FieldDeserializersChainBuilder;
import org.dominokit.jackson.processor.serialization.FieldSerializerChainBuilder;
import org.dominokit.rest.shared.MultipartForm;
import org.dominokit.rest.shared.Response;
import org.dominokit.rest.shared.request.*;
import org.dominokit.rest.shared.request.service.annotations.*;
import org.dominokit.rest.shared.request.service.annotations.Request;
Expand Down Expand Up @@ -668,6 +669,10 @@ private TypeMirror getMappingType(TypeMirror returnType) {
return elements.getTypeElement(Void.class.getCanonicalName()).asType();
}

if (processorUtil.isAssignableFrom(returnType, jakarta.ws.rs.core.Response.class)) {
return elements.getTypeElement(Response.class.getCanonicalName()).asType();
}

if (Type.isArray(returnType)) {
return returnType;
}
Expand Down Expand Up @@ -959,6 +964,12 @@ private Optional<CodeBlock> getResponseReader(ServiceMethod serviceMethod) {
"setResponseReader(response -> new $T().read(response))",
TypeName.get(readerType)));
return Optional.of(builder.build());
} else if (isGenericResponse(serviceMethod)) {

builder.addStatement(
"setResponseReader(response -> new $T().read(response))",
TypeName.get(GeneralResponseReader.class));
return Optional.of(builder.build());
} else if (producesJson(serviceMethod)) {

TypeMirror responseBeanType = getResponseBeanType(serviceMethod);
Expand Down Expand Up @@ -997,6 +1008,10 @@ private Optional<CodeBlock> getResponseReader(ServiceMethod serviceMethod) {
return Optional.empty();
}

private boolean isGenericResponse(ServiceMethod serviceMethod) {
return processorUtil.isAssignableFrom(getResponseBeanType(serviceMethod), Response.class);
}

private boolean producesJson(ServiceMethod serviceMethod) {
String acceptResponse = getAcceptResponse(serviceMethod);
return acceptResponse.contains(MediaType.APPLICATION_JSON)
Expand Down
1 change: 1 addition & 0 deletions domino-rest-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<groupId>org.dominokit</groupId>
<artifactId>domino-rest-jaxrs</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request;

import org.dominokit.rest.shared.Response;

/** Reads the response body as a {@link String} */
public class GeneralResponseReader implements ResponseReader<Response> {
@Override
public Response read(Response response) {
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request.builder;

public interface Consumes<R, S> {
Produces<R, S> accepts(String consumes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request.builder;

import org.dominokit.rest.shared.request.RequestMeta;
import org.dominokit.rest.shared.request.ServerRequest;

class GenericRequest<R, S> extends ServerRequest<R, S> {

public GenericRequest(RequestMeta requestMeta, R requestBean) {
super(requestMeta, requestBean);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request.builder;

public interface HasMethod<R, S> {

HasPath<R, S> withMethod(String method);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request.builder;

public interface HasPath<R, S> {
Consumes<R, S> withPath(String consumes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request.builder;

public interface Produces<R, S> {
RestRequestBuilder<R, S> produces(String produces);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.shared.request.builder;

import static java.util.Objects.isNull;

import java.util.Optional;
import org.dominokit.rest.shared.request.RequestMeta;
import org.dominokit.rest.shared.request.RequestWriter;
import org.dominokit.rest.shared.request.ResponseReader;
import org.dominokit.rest.shared.request.ServerRequest;

public class RestRequestBuilder<R, S>
implements HasMethod<R, S>, HasPath<R, S>, Consumes<R, S>, Produces<R, S> {

private String key;
private Class<R> requestClass;
private Class<S> responseClass;
private String method;
private String consumes;
private String produce;
private String path;
private String serviceRoot = "";
private ResponseReader<S> responseReader = response -> null;
private RequestWriter<R> requestWriter = request -> null;

public static <R, S> HasMethod<R, S> of(
Class<R> requestClass, Class<S> responseClass, String key) {
RestRequestBuilder<R, S> builder = new RestRequestBuilder<>();
builder.key = key;
builder.requestClass = requestClass;
builder.responseClass = responseClass;
return builder;
}

@Override
public HasPath<R, S> withMethod(String method) {
this.method = method;
return this;
}

@Override
public Consumes<R, S> withPath(String path) {
this.path = path;
return this;
}

@Override
public Produces<R, S> accepts(String consumes) {
this.consumes = consumes;
return this;
}

@Override
public RestRequestBuilder<R, S> produces(String produces) {
this.produce = produces;
return this;
}

public RestRequestBuilder<R, S> withServiceRoot(String serviceRoot) {
this.serviceRoot = serviceRoot;
return this;
}

public RestRequestBuilder<R, S> withResponseReader(ResponseReader<S> responseReader) {
this.responseReader = responseReader;
return this;
}

public RestRequestBuilder<R, S> withRequestWriter(RequestWriter<R> requestWriter) {
this.requestWriter = requestWriter;
return this;
}

public ServerRequest<R, S> build() {
return build(null);
}

public ServerRequest<R, S> build(R requestBean) {
ServerRequest<R, S> request =
new GenericRequest<>(
new RequestMeta(RestRequestBuilder.class, key, requestClass, responseClass),
requestBean);
request.setHttpMethod(this.method);
request.setAccept(new String[] {this.consumes});
request.setContentType(new String[] {this.produce});
request.setPath(this.path);
request.setServiceRoot(isNull(this.serviceRoot) ? "" : this.serviceRoot);
Optional.ofNullable(this.responseReader).ifPresent(request::setResponseReader);
Optional.ofNullable(this.requestWriter).ifPresent(request::setRequestWriter);

return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.model;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.dominokit.rest.shared.request.service.annotations.RequestFactory;

@RequestFactory
@Path("test")
public interface JakartaResponseService {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Response getResponse();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © 2019 Dominokit
*
* 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.
*/
package org.dominokit.rest.test;

import static org.assertj.core.api.Assertions.assertThat;

import io.vertx.core.Vertx;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.dominokit.rest.model.JakartaResponseServiceFactory;
import org.dominokit.rest.shared.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(VertxExtension.class)
public class ResponseReturnTypeTest extends BaseRestTest {

@Test
@DisplayName("Test using jakarta.ws.rs.core.Response as a return type")
void nullQueryParamAsEmpty(Vertx vertx, VertxTestContext testContext) {
JakartaResponseServiceFactory.INSTANCE
.getResponse()
.onSuccess(
response -> {
assertThat(Response.class.isAssignableFrom(response.getClass()));
testContext.completeNow();
})
.send();
}
}

0 comments on commit 2c723e9

Please sign in to comment.