-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sashir Estela
committed
Mar 17, 2024
1 parent
1b4ffb5
commit 63344e4
Showing
8 changed files
with
155 additions
and
56 deletions.
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
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
63 changes: 52 additions & 11 deletions
63
src/main/java/io/github/sashirestela/cleverclient/support/CleverClientSSE.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 |
---|---|---|
@@ -1,31 +1,72 @@ | ||
package io.github.sashirestela.cleverclient.support; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class CleverClientSSE { | ||
|
||
private static final String EVENT_HEADER = "event: "; | ||
private static final String DATA_HEADER = "data: "; | ||
private static final String SEPARATOR = ""; | ||
|
||
private static List<String> linesToCheck = null; | ||
|
||
private static String endOfStream = null; | ||
private LineRecord record; | ||
private List<String> eventsToRead; | ||
private List<String> endsOfStream; | ||
|
||
private String rawData; | ||
public CleverClientSSE(LineRecord record) { | ||
this.record = record; | ||
this.eventsToRead = Configurator.one().getEventsToRead(); | ||
this.endsOfStream = Configurator.one().getEndsOfStream(); | ||
|
||
public CleverClientSSE(String rawData) { | ||
this.rawData = rawData; | ||
if (linesToCheck == null) { | ||
linesToCheck = this.eventsToRead.stream().filter(etr -> !etr.isEmpty()).map(etr -> (EVENT_HEADER + etr)) | ||
.collect(Collectors.toList()); | ||
linesToCheck.add(SEPARATOR); | ||
} | ||
} | ||
|
||
public String getRawData() { | ||
return rawData; | ||
public LineRecord getRecord() { | ||
return record; | ||
} | ||
|
||
public boolean isActualData() { | ||
return rawData.startsWith(DATA_HEADER) && (endOfStream == null || !rawData.contains(endOfStream)); | ||
return linesToCheck.contains(record.previous()) && record.current().startsWith(DATA_HEADER) | ||
&& endsOfStream.stream().anyMatch(eos -> !record.current().contains(eos)); | ||
} | ||
|
||
public String getActualData() { | ||
return rawData.replace(DATA_HEADER, "").strip(); | ||
return record.current().replace(DATA_HEADER, "").strip(); | ||
} | ||
|
||
public static void setEndOfStream(String endOfStream) { | ||
CleverClientSSE.endOfStream = endOfStream; | ||
public static class LineRecord { | ||
|
||
private String currentLine; | ||
private String previousLine; | ||
|
||
public LineRecord(String previousLine, String currentLine) { | ||
this.previousLine = previousLine; | ||
this.currentLine = currentLine; | ||
} | ||
|
||
public LineRecord() { | ||
this("", ""); | ||
} | ||
|
||
public void updateWith(String line) { | ||
this.previousLine = this.currentLine; | ||
this.currentLine = line; | ||
} | ||
|
||
public String current() { | ||
return this.currentLine; | ||
} | ||
|
||
public String previous() { | ||
return this.previousLine; | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/io/github/sashirestela/cleverclient/support/Configurator.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,37 @@ | ||
package io.github.sashirestela.cleverclient.support; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Singular; | ||
|
||
@Getter | ||
public class Configurator { | ||
|
||
private static Configurator configurator; | ||
|
||
private List<String> eventsToRead; | ||
private List<String> endsOfStream; | ||
|
||
private Configurator() { | ||
} | ||
|
||
@Builder | ||
public Configurator(@Singular("eventToRead") List<String> eventsToRead, | ||
@Singular("endOfStream") List<String> endsOfStream) { | ||
if (configurator != null) { | ||
return; | ||
} | ||
configurator = new Configurator(); | ||
configurator.eventsToRead = eventsToRead; | ||
configurator.endsOfStream = endsOfStream; | ||
} | ||
|
||
public static Configurator one() { | ||
if (configurator == null) { | ||
throw new CleverClientException("You have to call Configurator.builder() first."); | ||
} | ||
return configurator; | ||
} | ||
} |
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