Skip to content

Commit 1e95045

Browse files
authored
[#3934] fixed client not support sets accept:text/event-stream problem (#4935)
1 parent 74c5ea3 commit 1e95045

File tree

2 files changed

+59
-6
lines changed
  • common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition
  • demo/demo-spring-boot-transport/demo-spring-boot-springmvc-client/src/main/java/org/apache/servicecomb/springboot/springmvc/client

2 files changed

+59
-6
lines changed

common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.commons.lang3.StringUtils;
3232
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
3333
import org.apache.servicecomb.common.rest.codec.param.FormProcessorCreator.PartProcessor;
34+
import org.apache.servicecomb.common.rest.codec.produce.ProduceJsonProcessor;
3435
import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor;
3536
import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
3637
import org.apache.servicecomb.common.rest.definition.path.PathRegExp;
@@ -88,8 +89,6 @@ public class RestOperationMeta {
8889
// 快速构建URL path
8990
private URLPathBuilder pathBuilder;
9091

91-
protected static final String EVENTS_MEDIA_TYPE = MediaType.SERVER_SENT_EVENTS;
92-
9392
public void init(OperationMeta operationMeta) {
9493
this.operationMeta = operationMeta;
9594

@@ -260,10 +259,11 @@ protected void doCreateProduceProcessors(Class<?> serialViewClass) {
260259
ProduceProcessorManager.INSTANCE.getOrCreateAcceptMap(serialViewClass));
261260
} else {
262261
for (String produce : produces) {
263-
if (produce.contains(EVENTS_MEDIA_TYPE)) {
264-
// When the produce type is event-stream, the ProduceEventStreamProcessor implementation class corresponding
265-
// to event-stream is not added, and it is set to the default type ProduceJsonProcessor.
266-
// In case of an exception, the response result is parsed.
262+
if (produce.contains(MediaType.SERVER_SENT_EVENTS)) {
263+
// The event-stream type initializes the ProduceJsonProcessor in memory to handle parsing results
264+
// in exceptional scenarios. If a singleton parsing result is required in normal scenarios,
265+
// adjustments need to be made here.
266+
this.produceProcessorMap.put(produce, new ProduceJsonProcessor());
267267
continue;
268268
}
269269
if (produce.contains(";")) {

demo/demo-spring-boot-transport/demo-spring-boot-springmvc-client/src/main/java/org/apache/servicecomb/springboot/springmvc/client/ReactiveStreamIT.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.TimeUnit;
2222

2323
import org.apache.commons.lang3.StringUtils;
24+
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
2425
import org.apache.servicecomb.swagger.invocation.sse.SseEventResponseEntity;
2526
import org.apache.servicecomb.demo.CategorizedTestCase;
2627
import org.apache.servicecomb.demo.TestMgr;
@@ -31,21 +32,31 @@
3132
import org.reactivestreams.Subscription;
3233
import org.springframework.beans.factory.annotation.Autowired;
3334
import org.springframework.beans.factory.annotation.Qualifier;
35+
import org.springframework.http.HttpEntity;
36+
import org.springframework.http.HttpHeaders;
37+
import org.springframework.http.HttpMethod;
38+
import org.springframework.http.ResponseEntity;
3439
import org.springframework.stereotype.Component;
40+
import org.springframework.web.client.RestTemplate;
41+
42+
import jakarta.ws.rs.core.MediaType;
3543

3644
@Component
3745
public class ReactiveStreamIT implements CategorizedTestCase {
3846
@Autowired
3947
@Qualifier("reactiveStreamProvider")
4048
ReactiveStreamClient reactiveStreamProvider;
4149

50+
private RestTemplate restTemplate = RestTemplateBuilder.create();
51+
4252
@Override
4353
public void testRestTransport() throws Exception {
4454
testSseString(reactiveStreamProvider);
4555
testSseStringWithParam(reactiveStreamProvider);
4656
testSseModel(reactiveStreamProvider);
4757
testSseResponseEntity(reactiveStreamProvider);
4858
testSseMultipleData(reactiveStreamProvider);
59+
sseStringWithAccept();
4960
}
5061

5162
private void testSseString(ReactiveStreamClient client) throws Exception {
@@ -91,6 +102,48 @@ public void onComplete() {
91102
return buffer.toString();
92103
}
93104

105+
private void sseStringWithAccept() throws Exception {
106+
HttpHeaders headers = new HttpHeaders();
107+
headers.add(HttpHeaders.ACCEPT, MediaType.SERVER_SENT_EVENTS);
108+
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
109+
ResponseEntity<Publisher> responseEntity = restTemplate
110+
.exchange("cse://springmvc/sseString", HttpMethod.GET, requestEntity, Publisher.class);
111+
Publisher result = responseEntity.getBody();
112+
CountDownLatch countDownLatch = new CountDownLatch(1);
113+
StringBuilder buffer = new StringBuilder();
114+
result.subscribe(new Subscriber<>() {
115+
Subscription subscription;
116+
117+
@Override
118+
public void onSubscribe(Subscription s) {
119+
subscription = s;
120+
subscription.request(1);
121+
}
122+
123+
@Override
124+
public void onNext(Object entity) {
125+
SseEventResponseEntity<String> response = (SseEventResponseEntity<String>) entity;
126+
for (String str : response.getData()) {
127+
buffer.append(str);
128+
}
129+
subscription.request(1);
130+
}
131+
132+
@Override
133+
public void onError(Throwable t) {
134+
subscription.cancel();
135+
countDownLatch.countDown();
136+
}
137+
138+
@Override
139+
public void onComplete() {
140+
countDownLatch.countDown();
141+
}
142+
});
143+
countDownLatch.await(10, TimeUnit.SECONDS);
144+
TestMgr.check("abc", buffer.toString());
145+
}
146+
94147
private void testSseModel(ReactiveStreamClient client) throws Exception {
95148
Publisher<Model> result = client.sseModel();
96149
CountDownLatch countDownLatch = new CountDownLatch(1);

0 commit comments

Comments
 (0)