diff --git a/pom.xml b/pom.xml index 8359889d9..811e0d099 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,7 @@ 1.0.0 1.8 + 1.8 1.3.21 2.22.2 3.3.0 @@ -80,6 +81,11 @@ commons-compress ${version.commons-compress} + + com.squareup.okhttp3 + okhttp + 4.2.2 + io.rest-assured rest-assured diff --git a/src/main/frontend/src/code-quarkus/__tests__/launcher-quarkus.spec.tsx b/src/main/frontend/src/code-quarkus/__tests__/launcher-quarkus.spec.tsx index b07e659de..93e27637d 100644 --- a/src/main/frontend/src/code-quarkus/__tests__/launcher-quarkus.spec.tsx +++ b/src/main/frontend/src/code-quarkus/__tests__/launcher-quarkus.spec.tsx @@ -66,10 +66,7 @@ jest.mock('../backend-api', () => ({ }, ]), fetchConfig: () => { throw new Error("not used"); }, - shortenUrl: async (url: string) => Promise.resolve( - { - link: 'http://bit.ly/2Mzdn9Z' - }) + shortenUrl: async (params: string) => Promise.resolve('http://bit.ly/2Mzdn9Z') })); diff --git a/src/main/frontend/src/code-quarkus/backend-api.ts b/src/main/frontend/src/code-quarkus/backend-api.ts index 1c2379020..cfa3b8fa3 100644 --- a/src/main/frontend/src/code-quarkus/backend-api.ts +++ b/src/main/frontend/src/code-quarkus/backend-api.ts @@ -20,18 +20,7 @@ export async function fetchConfig() { } } -export async function shortenUrl(downloadLink: string) { - const response = await fetch('https://api-ssl.bitly.com/v4/shorten', { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - 'Authorization': 'Bearer a3badec1a128e46e42f36a138165cbbd21ed6cee' - }, - body: JSON.stringify({ - 'group_guid': 'Bjam8omLBjB', - 'long_url': downloadLink - }) - }); - return response.ok ? await response.json() : undefined +export async function shortenUrl(params: string) { + const response = await fetch(`${backendUrl}/api/shorten?${params}`); + return response.ok ? await response.text() : undefined } diff --git a/src/main/frontend/src/code-quarkus/code-quarkus.tsx b/src/main/frontend/src/code-quarkus/code-quarkus.tsx index 75976f0d4..2972daaf1 100644 --- a/src/main/frontend/src/code-quarkus/code-quarkus.tsx +++ b/src/main/frontend/src/code-quarkus/code-quarkus.tsx @@ -49,9 +49,9 @@ async function generateProject(project: QuarkusProject): Promise<{ downloadLink: const backendUrl = process.env.REACT_APP_BACKEND_URL || publicUrl; const downloadLink = `${backendUrl}/api/download?${stringify(params)}`; - const data = await shortenUrl(downloadLink); + const link = await shortenUrl(stringify(params)); window.open(downloadLink, '_blank'); - return { downloadLink: data ? data.link : downloadLink }; + return { downloadLink: link || downloadLink }; } const DEFAULT_PROJECT = { diff --git a/src/main/kotlin/io/quarkus/code/CodeQuarkusResource.kt b/src/main/kotlin/io/quarkus/code/CodeQuarkusResource.kt index e4f9a5339..9d54329bb 100644 --- a/src/main/kotlin/io/quarkus/code/CodeQuarkusResource.kt +++ b/src/main/kotlin/io/quarkus/code/CodeQuarkusResource.kt @@ -11,14 +11,19 @@ import org.eclipse.microprofile.openapi.annotations.media.Content import org.eclipse.microprofile.openapi.annotations.media.Schema import org.eclipse.microprofile.openapi.annotations.responses.APIResponse import javax.inject.Inject +import javax.json.bind.JsonbBuilder import javax.validation.Valid import javax.ws.rs.BeanParam import javax.ws.rs.GET import javax.ws.rs.Path import javax.ws.rs.Produces import javax.ws.rs.core.MediaType.APPLICATION_JSON +import javax.ws.rs.core.MediaType.TEXT_PLAIN import javax.ws.rs.core.Response +import okhttp3.* +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.RequestBody.Companion.toRequestBody @Path("/") class CodeQuarkusResource { @@ -32,6 +37,8 @@ class CodeQuarkusResource { @Inject lateinit var projectCreator: QuarkusProjectCreator + private val httpClient = OkHttpClient() + @GET @Path("/config") @Produces(APPLICATION_JSON) @@ -57,6 +64,36 @@ class CodeQuarkusResource { return extensionCatalog.extensions } + @GET + @Path("/shorten") + @Produces(TEXT_PLAIN) + @Operation(summary = "Create a short url based on the parameters") + fun shorten(@Valid @BeanParam project: QuarkusProject): Response { + val JSON = "application/json; charset=utf-8".toMediaType() + val body = """ + { + "group_guid": "$bitlyGroupId", + "long_url": "https://code.quarkus.io/api/download?g=${project.groupId}&a=${project.artifactId}&v=${project.version}&c=${project.className}&e=${project.extensions}" + } + """.toRequestBody(JSON) + + val request = Request.Builder() + .url("https://api-ssl.bitly.com/v4/shorten") + .addHeader("Authorization", "Bearer $bitlyAccessToken") + .post(body) + .build() + + httpClient.newCall(request).execute().use { response -> + + if (!response.isSuccessful) throw IOException("Unexpected code $response") + + val jsonb = JsonbBuilder.create() + val obj = jsonb.fromJson(response.body!!.string(), BitlyResponse::class.java) + + return Response.ok(obj.link).build() + } + } + @GET @Path("/download") @Produces("application/zip") @@ -67,6 +104,5 @@ class CodeQuarkusResource { .type("application/zip") .header("Content-Disposition", "attachment; filename=\"${project.artifactId}.zip\"") .build() - } } \ No newline at end of file diff --git a/src/main/kotlin/io/quarkus/code/model/BitlyResponse.kt b/src/main/kotlin/io/quarkus/code/model/BitlyResponse.kt new file mode 100644 index 000000000..f503919c0 --- /dev/null +++ b/src/main/kotlin/io/quarkus/code/model/BitlyResponse.kt @@ -0,0 +1,5 @@ +package io.quarkus.code + +class BitlyResponse { + var link: String? = null +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e7cbfef54..4e7c1491b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,4 +3,7 @@ quarkus.log.file.enable=false quarkus.http.cors=true io.quarkus.code.quarkus-version=${version.quarkus} io.quarkus.code.quarkus-platform-version=${version.quarkus-platform} -io.quarkus.code.git-commit-id=${git.commit.id.abbrev} \ No newline at end of file +io.quarkus.code.git-commit-id=${git.commit.id.abbrev} + +io.quarkus.code.bitly.generic-access-token=a3badec1a128e46e42f36a138165cbbd21ed6cee +io.quarkus.code.bitly.group-id=Bjam8omLBjB \ No newline at end of file