-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generic request builder, add ability to use jakarta.ws.rs.Respons…
…e as a return type
- Loading branch information
Showing
12 changed files
with
334 additions
and
1 deletion.
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
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
26 changes: 26 additions & 0 deletions
26
...no-rest-shared/src/main/java/org/dominokit/rest/shared/request/GeneralResponseReader.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,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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/builder/Consumes.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,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); | ||
} |
26 changes: 26 additions & 0 deletions
26
...o-rest-shared/src/main/java/org/dominokit/rest/shared/request/builder/GenericRequest.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,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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/builder/HasMethod.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,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); | ||
} |
20 changes: 20 additions & 0 deletions
20
domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/builder/HasPath.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,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); | ||
} |
20 changes: 20 additions & 0 deletions
20
domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/builder/Produces.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,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); | ||
} |
107 changes: 107 additions & 0 deletions
107
...st-shared/src/main/java/org/dominokit/rest/shared/request/builder/RestRequestBuilder.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,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; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
domino-rest-test/src/test/java/org/dominokit/rest/model/JakartaResponseService.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,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(); | ||
} |
44 changes: 44 additions & 0 deletions
44
domino-rest-test/src/test/java/org/dominokit/rest/test/ResponseReturnTypeTest.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,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(); | ||
} | ||
} |