Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
import dev.rsdlang.sample.client.ScalarSubstition_ServiceService;
import dev.rsdlang.sample.client.SpecSamplesClient;

public class JDKSpecSamplesClient implements SpecSamplesClient {
public class JDKSpecSamplesClient implements SpecSamplesClient, AutoCloseable {
public enum ContentTypeEncoding {
APPLICATION_JSON("application/json"),
APPLICATION_VND_MSGPACK("application/vnd.msgpack");
Expand All @@ -160,7 +160,7 @@ public enum ContentTypeEncoding {

public static class Builder {
private URI baseURI;
private HttpClient httpClient;
private Supplier<HttpClient> httpClientSupplier;
private ContentTypeEncoding contentTypeEncoding;

public Builder baseURI(URI baseURI) {
Expand All @@ -169,7 +169,11 @@ public Builder baseURI(URI baseURI) {
}

public Builder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return httpClientSupplier(() -> httpClient);
}

public Builder httpClientSupplier(Supplier<HttpClient> httpClientSupplier) {
this.httpClientSupplier = httpClientSupplier;
return this;
}

Expand All @@ -182,11 +186,20 @@ public SpecSamplesClient build() {
if (baseURI == null) {
throw new IllegalStateException("baseURI must be set");
}
var client = (httpClient != null) ? httpClient : HttpClient.newHttpClient();
if (contentTypeEncoding == null) {
contentTypeEncoding = ContentTypeEncoding.APPLICATION_JSON;
var contentType = contentTypeEncoding == null ? ContentTypeEncoding.APPLICATION_JSON : contentTypeEncoding;
if (httpClientSupplier == null) {
return new JDKSpecSamplesClient(baseURI, new InternalClientSupplier(), contentType);
}
return new JDKSpecSamplesClient(baseURI, client, contentTypeEncoding);
return new JDKSpecSamplesClient(baseURI, httpClientSupplier, contentType);
}
}

static class InternalClientSupplier implements Supplier<HttpClient> {
private final HttpClient client = HttpClient.newBuilder().build();

@Override
public HttpClient get() {
return client;
}
}

Expand Down Expand Up @@ -306,12 +319,12 @@ private static void registerServiceCreator(Class<?> clazz, BiFunction<JDKSpecSam
}

private final URI baseURI;
private final HttpClient httpClient;
private final Supplier<HttpClient> httpClientSupplier;
private final ContentTypeEncoding contentTypeEncoding;

JDKSpecSamplesClient(URI baseURI, HttpClient httpClient, ContentTypeEncoding contentTypeEncoding) {
JDKSpecSamplesClient(URI baseURI, Supplier<HttpClient> httpClientSupplier, ContentTypeEncoding contentTypeEncoding) {
this.baseURI = baseURI;
this.httpClient = httpClient;
this.httpClientSupplier = httpClientSupplier;
this.contentTypeEncoding = contentTypeEncoding;
}

Expand All @@ -320,7 +333,7 @@ public ContentTypeEncoding contentTypeEncoding() {
}

public HttpClient httpClient() {
return this.httpClient;
return this.httpClientSupplier.get();
}

public URI baseURI() {
Expand Down Expand Up @@ -397,4 +410,18 @@ public RSDBlob createBlob(Path file, String mimeType) {
public RSDFile createFile(Path file, String mimeType, String filename) {
return _FileImpl.of(file, mimeType, filename);
}

@Override
public void close() {
if (this.httpClientSupplier instanceof InternalClientSupplier) {
var client = this.httpClientSupplier.get();
client.close();
} else if (this.httpClientSupplier instanceof AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static BinaryTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static BodyParameterTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static HeaderParameterTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static ListBodyParameterTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static ListHeaderParameterTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static ListQueryParameterTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static ListSampleServiceService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static PathParameterTypeServiceService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static QueryParameterTypesService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static SampleServiceService[] serviceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ static void setUp() {

@AfterAll
static void tearDown() {
((JDKSpecSamplesClient) JSON).httpClient().close();
((JDKSpecSamplesClient) MSGPACK).httpClient().close();
((JDKSpecSamplesClient) JSON).close();
((JDKSpecSamplesClient) MSGPACK).close();
}

static ScalarSubstition_ServiceService[] serviceProvider() {
Expand Down
56 changes: 45 additions & 11 deletions dsl/packages/cli/src/java-rest-client-jdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function generateClient(
const Optional = fqn('java.util.Optional');

const content = new CompositeGeneratorNode();
content.append(`public class JDK${toCamelCaseIdentifier(generatorConfig.name)}Client implements ${Client} {`, NL);
content.append(
`public class JDK${toCamelCaseIdentifier(generatorConfig.name)}Client implements ${Client}, AutoCloseable {`,
NL,
);
content.indent(classBody => {
const contentEncodings =
artifactConfig.contentTypeEncodings === undefined || artifactConfig.contentTypeEncodings.length === 0
Expand All @@ -65,7 +68,7 @@ export function generateClient(
const builder = toNodeTree(`
public static class Builder {
private URI baseURI;
private HttpClient httpClient;
private Supplier<HttpClient> httpClientSupplier;
private ContentTypeEncoding contentTypeEncoding;

public Builder baseURI(URI baseURI) {
Expand All @@ -74,7 +77,11 @@ export function generateClient(
}

public Builder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return httpClientSupplier(() -> httpClient);
}

public Builder httpClientSupplier(Supplier<HttpClient> httpClientSupplier) {
this.httpClientSupplier = httpClientSupplier;
return this;
}

Expand All @@ -87,11 +94,20 @@ export function generateClient(
if (baseURI == null) {
throw new IllegalStateException("baseURI must be set");
}
var client = (httpClient != null) ? httpClient : HttpClient.newHttpClient();
if (contentTypeEncoding == null) {
contentTypeEncoding = ContentTypeEncoding.${toEnumLiteral(contentEncodings[0])};
var contentType = contentTypeEncoding == null ? ContentTypeEncoding.${toEnumLiteral(contentEncodings[0])} : contentTypeEncoding;
if (httpClientSupplier == null) {
return new JDK${toCamelCaseIdentifier(generatorConfig.name)}Client(baseURI, new InternalClientSupplier(), contentType);
}
return new JDK${toCamelCaseIdentifier(generatorConfig.name)}Client(baseURI, client, contentTypeEncoding);
return new JDK${toCamelCaseIdentifier(generatorConfig.name)}Client(baseURI, httpClientSupplier, contentType);
}
}

static class InternalClientSupplier implements Supplier<HttpClient> {
private final HttpClient client = HttpClient.newBuilder().build();

@Override
public HttpClient get() {
return client;
}
}

Expand Down Expand Up @@ -181,18 +197,18 @@ export function generateClient(
clBody.append('}', NL);
clBody.appendNewLine();
clBody.append(`private final ${URI} baseURI;`, NL);
clBody.append(`private final ${HttpClient} httpClient;`, NL);
clBody.append(`private final ${Supplier}<${HttpClient}> httpClientSupplier;`, NL);
clBody.append(`private final ContentTypeEncoding contentTypeEncoding;`, NL);
clBody.appendNewLine();
clBody.append(
`JDK${toCamelCaseIdentifier(
generatorConfig.name,
)}Client(${URI} baseURI, ${HttpClient} httpClient, ContentTypeEncoding contentTypeEncoding) {`,
)}Client(${URI} baseURI, ${Supplier}<${HttpClient}> httpClientSupplier, ContentTypeEncoding contentTypeEncoding) {`,
NL,
);
clBody.indent(initBlock => {
initBlock.append('this.baseURI = baseURI;', NL);
initBlock.append('this.httpClient = httpClient;', NL);
initBlock.append('this.httpClientSupplier = httpClientSupplier;', NL);
initBlock.append('this.contentTypeEncoding = contentTypeEncoding;', NL);
});
clBody.append('}', NL);
Expand All @@ -203,7 +219,7 @@ export function generateClient(
}

public HttpClient httpClient() {
return this.httpClient;
return this.httpClientSupplier.get();
}

public URI baseURI() {
Expand Down Expand Up @@ -351,6 +367,24 @@ export function generateClient(
}
});

const autoClose = toNodeTree(`
@Override
public void close() {
if (this.httpClientSupplier instanceof InternalClientSupplier) {
var client = this.httpClientSupplier.get();
client.close();
} else if (this.httpClientSupplier instanceof AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}`);
content.indent(clBody => {
clBody.appendNewLine();
clBody.append(autoClose, NL);
});
content.append('}', NL);

return {
Expand Down
Loading