Skip to content

Commit 7f11531

Browse files
fix: fixed issue with URL parsing in airbyte demo project (#28)
1 parent ba63fb0 commit 7f11531

25 files changed

+117
-106
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Add the Speakeasy SDK to your project's build.gradle file:
2020

2121
```groovy
2222
dependencies {
23-
implementation 'dev.speakeasyapi:speakeasyapi-java-sdk:1.3.0'
23+
implementation 'dev.speakeasyapi:speakeasyapi-java-sdk:1.3.1'
2424
}
2525
```
2626

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group 'dev.speakeasyapi'
10-
version '1.3.0'
10+
version '1.3.1'
1111

1212

1313
repositories {
@@ -35,6 +35,7 @@ dependencies {
3535
implementation 'io.grpc:grpc-protobuf:1.45.1'
3636
implementation 'io.grpc:grpc-stub:1.45.1'
3737
implementation 'org.apache.commons:commons-lang3:3.9'
38+
implementation 'org.apache.httpcomponents:httpcore:4.4.15'
3839
compileOnly 'io.micronaut:micronaut-http:3.7.2'
3940
compileOnly 'io.micronaut:micronaut-http-server:3.7.2'
4041
compileOnly 'io.micronaut:micronaut-http-netty:3.7.2'
@@ -92,7 +93,7 @@ publishing {
9293
groupId = 'dev.speakeasyapi'
9394
artifactId = 'speakeasy-java-sdk'
9495

95-
version = '1.3.0'
96+
version = '1.3.1'
9697

9798
from components.java
9899

docs/micronaut.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Add the Speakeasy SDK to your project's build.gradle file:
1313

1414
```groovy
1515
dependencies {
16-
implementation 'dev.speakeasyapi:speakeasyapi-java-sdk:1.3.0'
16+
implementation 'dev.speakeasyapi:speakeasyapi-java-sdk:1.3.1'
1717
}
1818
```
1919

docs/springboot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Add the Speakeasy SDK to your project's build.gradle file:
1313

1414
```groovy
1515
dependencies {
16-
implementation 'dev.speakeasyapi:speakeasyapi-java-sdk:1.3.0'
16+
implementation 'dev.speakeasyapi:speakeasyapi-java-sdk:1.3.1'
1717
}
1818
```
1919

src/main/java/dev/speakeasyapi/micronaut/SpeakeasyNettyServerCustomizer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public NettyServerCustomizer specializeForChannel(Channel channel, ChannelRole r
3131

3232
@Override
3333
public void onStreamPipelineBuilt() {
34-
System.out.println("onStreamPipelineBuilt");
3534
channel.pipeline().addBefore(ChannelPipelineCustomizer.HANDLER_HTTP_STREAM, "speakeasy",
3635
new SpeakeasyChannelDuplexHandler());
3736
}

src/main/java/dev/speakeasyapi/micronaut/implementation/SpeakeasyCapture.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dev.speakeasyapi.micronaut.implementation;
22

33
import java.io.ByteArrayOutputStream;
4-
import java.net.URL;
54
import java.time.Instant;
65
import java.util.concurrent.Executor;
76
import java.util.concurrent.Executors;
@@ -56,14 +55,10 @@ public void run() {
5655
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
5756

5857
try {
59-
URL url = new URL(this.request.getRequest().uri());
60-
6158
new SpeakeasyHarBuilder(this.logger)
6259
.withStartTime(context.getStartTime())
6360
.withEndTime(endTime)
64-
.withHostName(url.getHost())
6561
.withOutputStream(outputStream)
66-
.withPort(url.getPort())
6762
.withMasking(controller.getMasking())
6863
.withRequest(this.request)
6964
.withResponse(this.response, this.request.getProtocol())

src/main/java/dev/speakeasyapi/micronaut/implementation/SpeakeasyNettyRequest.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package dev.speakeasyapi.micronaut.implementation;
22

3+
import java.util.Collections;
34
import java.util.List;
45
import java.util.Map;
56
import java.util.Set;
67
import java.util.UUID;
78
import java.util.stream.Collectors;
89

9-
import org.springframework.web.util.UriComponents;
10-
import org.springframework.web.util.UriComponentsBuilder;
11-
1210
import dev.speakeasyapi.sdk.SpeakeasyCookie;
1311
import dev.speakeasyapi.sdk.SpeakeasyRequest;
1412
import dev.speakeasyapi.sdk.utils.Utils;
@@ -58,8 +56,10 @@ public void buffer(final ByteBuf content) {
5856

5957
public Map<String, List<String>> getHeaders() {
6058
if (this.request.headers() != null) {
61-
return request.headers().entries().stream().collect(Collectors.groupingBy(Map.Entry::getKey,
62-
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
59+
return request.headers().entries().stream()
60+
.collect(Collectors.toMap(e -> e.getKey().toLowerCase(),
61+
e -> Collections.singletonList(e.getValue()),
62+
Utils.merge));
6363
}
6464

6565
return null;
@@ -122,11 +122,6 @@ public String getMethod() {
122122
}
123123

124124
public String getRequestURI() {
125-
UriComponents uriComponents = UriComponentsBuilder
126-
.fromUriString(this.request.uri())
127-
.query("")
128-
.build();
129-
130-
return uriComponents.toUriString();
125+
return this.request.uri();
131126
}
132127
}

src/main/java/dev/speakeasyapi/micronaut/implementation/SpeakeasyNettyResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.speakeasyapi.micronaut.implementation;
22

33
import java.time.Instant;
4+
import java.util.Collections;
45
import java.util.List;
56
import java.util.Map;
67
import java.util.stream.Collectors;
@@ -32,8 +33,9 @@ public void buffer(final ByteBuf content) {
3233

3334
public Map<String, List<String>> getHeaders() {
3435
if (this.response.headers() != null) {
35-
return response.headers().entries().stream().collect(Collectors.groupingBy(Map.Entry::getKey,
36-
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
36+
return response.headers().entries().stream()
37+
.collect(Collectors.toMap(e -> e.getKey().toLowerCase(),
38+
e -> Collections.singletonList(e.getValue())));
3739
}
3840

3941
return null;

src/main/java/dev/speakeasyapi/sdk/SpeakeasyHarBuilder.java

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import javax.servlet.http.HttpServletResponse;
1919

20+
import org.apache.commons.lang3.StringUtils;
21+
import org.apache.http.impl.EnglishReasonPhraseCatalog;
2022
import org.slf4j.Logger;
21-
import org.springframework.http.HttpStatus;
22-
import org.springframework.util.StringUtils;
2323

2424
import com.smartbear.har.builder.HarContentBuilder;
2525
import com.smartbear.har.builder.HarCookieBuilder;
@@ -42,7 +42,7 @@
4242

4343
public class SpeakeasyHarBuilder {
4444
private final String sdkName = "speakeasy-java-sdk";
45-
private final String speakeasyVersion = "1.3.0";
45+
private final String speakeasyVersion = "1.3.1";
4646
private final String droppedBodyText = "--dropped--";
4747

4848
private final DefaultHarStreamWriter.Builder harWriterBuilder;
@@ -108,16 +108,6 @@ public SpeakeasyHarBuilder withEndTime(Instant endTime) {
108108
return this;
109109
}
110110

111-
public SpeakeasyHarBuilder withHostName(String hostName) {
112-
this.hostName = hostName;
113-
return this;
114-
}
115-
116-
public SpeakeasyHarBuilder withPort(int port) {
117-
this.port = String.valueOf(port);
118-
return this;
119-
}
120-
121111
public SpeakeasyHarBuilder withMasking(Masking masking) {
122112
this.masking = masking;
123113
return this;
@@ -286,7 +276,8 @@ public SpeakeasyHarBuilder withResponse(SpeakeasyResponse response, String httpV
286276
}
287277

288278
String contentType = response.getContentType();
289-
if (!StringUtils.hasText(contentType)) {
279+
280+
if (StringUtils.isBlank(contentType)) {
290281
contentType = "application/octet-stream"; // Default HTTP content type
291282
}
292283

@@ -312,7 +303,7 @@ public SpeakeasyHarBuilder withResponse(SpeakeasyResponse response, String httpV
312303
int statusCode = response.getStatus();
313304
String statusText = null;
314305
try {
315-
statusText = HttpStatus.valueOf(statusCode).getReasonPhrase();
306+
statusText = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, null);
316307
} catch (Exception e) {
317308
logger.debug("speakeasy-sdk, error retrieving status: ", e);
318309
}
@@ -347,14 +338,14 @@ public void build() throws IOException {
347338
HarEntryBuilder builder = new HarEntryBuilder()
348339
.withRequest(harRequest)
349340
.withResponse(harResponse)
350-
.withServerIPAddress(hostName)
341+
.withServerIPAddress(this.hostName)
351342
.withStartedDateTime(startTime)
352343
.withTime(endTime.toEpochMilli() - startTime.toEpochMilli())
353344
.withCache(new HarCache())
354345
.withTimings(new HarTimings(0l, 0l, 0l, -1l, -1l, -1l, 0l, ""));
355346

356-
if (!port.equals("-1")) {
357-
builder.withConnection(port);
347+
if (this.port != null && !this.port.equals("-1")) {
348+
builder.withConnection(this.port);
358349
}
359350

360351
harWriter.addEntry(builder.build());
@@ -364,56 +355,82 @@ public void build() throws IOException {
364355
private String resolveURL(SpeakeasyRequest request) {
365356
String uri = request.getRequestURI();
366357

358+
String scheme = "";
359+
String host = "";
360+
int port = -1;
361+
String path = uri;
362+
String queryString = "";
363+
String ref = "";
364+
367365
try {
368366
URL url = new URL(uri);
367+
scheme = url.getProtocol();
368+
host = url.getHost();
369+
port = url.getPort();
370+
path = url.getPath();
371+
queryString = url.getQuery();
372+
ref = url.getRef();
373+
} catch (MalformedURLException e) {
374+
}
369375

370-
String scheme = url.getProtocol();
376+
String proxyScheme = getScheme(request);
377+
if (StringUtils.isNotBlank(proxyScheme)) {
378+
scheme = proxyScheme;
379+
}
371380

372-
String proxyScheme = getScheme(request);
373-
if (StringUtils.hasText(proxyScheme)) {
374-
scheme = proxyScheme;
381+
if (request.getHeaders().containsKey("x-forwarded-host")) {
382+
List<String> headers = request.getHeaders().get("x-forwarded-host");
383+
if (headers != null && headers.size() > 0) {
384+
host = headers.get(0);
375385
}
376-
377-
String host = url.getHost();
378-
379-
if (request.getHeaders().containsKey("x-forwarded-host")) {
380-
List<String> headers = request.getHeaders().get("x-forwarded-host");
381-
if (headers != null && headers.size() > 0) {
382-
host = headers.get(0);
383-
}
384-
} else if (request.getHeaders().containsKey("host")) {
385-
List<String> headers = request.getHeaders().get("host");
386-
if (headers != null && headers.size() > 0) {
387-
host = headers.get(0);
388-
}
386+
} else if (request.getHeaders().containsKey("host")) {
387+
List<String> headers = request.getHeaders().get("host");
388+
if (headers != null && headers.size() > 0) {
389+
host = headers.get(0);
389390
}
391+
}
390392

391-
StringBuilder builder = new StringBuilder();
393+
this.hostName = host;
392394

393-
builder
394-
.append(scheme)
395-
.append("://")
396-
.append(host);
395+
StringBuilder builder = new StringBuilder();
397396

398-
if (!host.contains(":") && url.getPort() != -1 && url.getPort() != 80 && url.getPort() != 443) {
399-
builder.append(":").append(url.getPort());
400-
}
397+
if (StringUtils.isNotBlank(scheme)) {
398+
builder.append(scheme);
399+
builder.append("://");
400+
}
401401

402-
builder.append(url.getPath());
402+
if (StringUtils.isNotBlank(host)) {
403+
builder.append(host);
403404

404-
if (StringUtils.hasText(url.getQuery())) {
405-
builder.append("?").append(getMaskedQueryString(url.getQuery()));
405+
if (port == -1) {
406+
String[] parts = host.split(":");
407+
if (parts.length > 1) {
408+
try {
409+
port = Integer.parseInt(parts[1]);
410+
} catch (Exception e) {
411+
port = -1;
412+
}
413+
}
406414
}
407415

408-
if (StringUtils.hasText(url.getRef())) {
409-
builder.append("#").append(url.getRef());
416+
if (!host.contains(":") && port != -1 && port != 80 && port != 443) {
417+
builder.append(":").append(port);
410418
}
419+
}
420+
421+
this.port = String.valueOf(port);
411422

412-
return builder.toString();
423+
builder.append(path);
413424

414-
} catch (MalformedURLException e) {
415-
return uri;
425+
if (StringUtils.isNotBlank(queryString)) {
426+
builder.append("?").append(getMaskedQueryString(queryString));
416427
}
428+
429+
if (StringUtils.isNotBlank(ref)) {
430+
builder.append("#").append(ref);
431+
}
432+
433+
return builder.toString();
417434
}
418435

419436
private String getScheme(SpeakeasyRequest request) {

src/main/java/dev/speakeasyapi/sdk/client/SpeakeasyClient.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ public void ingestGrpc(String harString, String pathHint, String customerID, Mas
3535
return;
3636
}
3737

38+
ManagedChannel channel = null;
39+
3840
try {
39-
ManagedChannel channel = createChannel();
41+
channel = createChannel();
42+
} catch (SSLException e) {
43+
throw new RuntimeException("Failed to create channel", e);
44+
}
4045

46+
try {
4147
IngestServiceGrpc.IngestServiceBlockingStub blockingStub = IngestServiceGrpc.newBlockingStub(channel);
4248

4349
MaskingMetadata maskingMetadata = MaskingMetadata.newBuilder()
@@ -64,7 +70,11 @@ public void ingestGrpc(String harString, String pathHint, String customerID, Mas
6470

6571
Ingest.IngestRequest ingestRequest = ingestRequestBuilder.build();
6672
blockingStub.ingest(ingestRequest);
73+
74+
channel.shutdown();
75+
channel.awaitTermination(100, java.util.concurrent.TimeUnit.MILLISECONDS);
6776
} catch (Exception e) {
77+
channel.shutdown();
6878
throw new RuntimeException(e);
6979
}
7080
}
@@ -89,7 +99,7 @@ private ManagedChannel createChannel() throws SSLException {
8999

90100
final String serverUrl = this.cfg.getServerUrl();
91101

92-
ManagedChannelBuilder channelBuilder;
102+
ManagedChannelBuilder<?> channelBuilder;
93103
if (this.cfg.isSecureGrpc()) {
94104
channelBuilder = NettyChannelBuilder.forTarget(serverUrl)
95105
.sslContext(GrpcSslContexts.forClient()

0 commit comments

Comments
 (0)