Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processing of Accept header to AudioServlet #2960

Merged
merged 2 commits into from
May 16, 2022
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 @@ -18,10 +18,12 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -53,6 +55,8 @@ public class AudioServlet extends OpenHABServlet implements AudioHTTPServer {

private static final long serialVersionUID = -3364664035854567854L;

private static final List<String> WAV_MIME_TYPES = List.of("audio/wav", "audio/x-wav", "audio/vnd.wave");

private static final String SERVLET_NAME = "/audio";

private final Map<String, AudioStream> oneTimeStreams = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -90,8 +94,8 @@ private void tryClose(@Nullable AudioStream stream) {
}
}

private @Nullable InputStream prepareInputStream(final String streamId, final HttpServletResponse resp)
throws AudioException {
private @Nullable InputStream prepareInputStream(final String streamId, final HttpServletResponse resp,
List<String> acceptedMimeTypes) throws AudioException {
final AudioStream stream;
final boolean multiAccess;
if (oneTimeStreams.containsKey(streamId)) {
Expand All @@ -111,7 +115,7 @@ private void tryClose(@Nullable AudioStream stream) {
if (AudioFormat.CODEC_MP3.equals(stream.getFormat().getCodec())) {
mimeType = "audio/mpeg";
} else if (AudioFormat.CONTAINER_WAVE.equals(stream.getFormat().getContainer())) {
mimeType = "audio/wav";
mimeType = WAV_MIME_TYPES.stream().filter(acceptedMimeTypes::contains).findFirst().orElse("audio/wav");
} else if (AudioFormat.CONTAINER_OGG.equals(stream.getFormat().getContainer())) {
mimeType = "audio/ogg";
} else {
Expand Down Expand Up @@ -158,7 +162,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se

final String streamId = substringBefore(substringAfterLast(requestURI, "/"), ".");

try (final InputStream stream = prepareInputStream(streamId, resp)) {
List<String> acceptedMimeTypes = Stream.of(Objects.requireNonNullElse(req.getHeader("Accept"), "").split(","))
.map(String::trim).collect(Collectors.toList());

try (final InputStream stream = prepareInputStream(streamId, resp, acceptedMimeTypes)) {
if (stream == null) {
logger.debug("Received request for invalid stream id at {}", requestURI);
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ protected ContentResponse getHttpResponse(AudioStream audioStream) throws Except
return getHttpRequest(url).send();
}

protected ContentResponse getHttpResponseWithAccept(AudioStream audioStream, String acceptHeader) throws Exception {
String url = serveStream(audioStream);
return getHttpRequest(url).header("Accept", acceptHeader).send();
}

protected String serveStream(AudioStream stream) throws Exception {
return serveStream(stream, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void audioServletProcessesByteArrayStream() throws Exception {
}

@Test
public void audioServletProcessesStreamFromWavFile() throws Exception {
public void audioServletProcessesStreamFromWavFileWithoutAcceptHeader() throws Exception {
try (BundledSoundFileHandler fileHandler = new BundledSoundFileHandler()) {
AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath()));

Expand All @@ -74,6 +74,18 @@ public void audioServletProcessesStreamFromWavFile() throws Exception {
}
}

@Test
public void audioServletProcessesStreamFromWavFileWithAcceptHeader() throws Exception {
try (BundledSoundFileHandler fileHandler = new BundledSoundFileHandler()) {
AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath()));

ContentResponse response = getHttpResponseWithAccept(audioStream, "audio/invalid, audio/x-wav");

assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.OK_200));
assertThat("The response media type was not as expected", response.getMediaType(), is("audio/x-wav"));
}
}

@Test
public void audioServletProcessesStreamFromOggContainer() throws Exception {
AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_OGG,
Expand Down