Skip to content

Commit

Permalink
new class used for url checking
Browse files Browse the repository at this point in the history
  • Loading branch information
codingPF committed Oct 30, 2023
1 parent 09bb7a5 commit d99edf8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 95 deletions.
135 changes: 49 additions & 86 deletions src/main/java/de/mediathekview/mlib/tool/FileSizeDeterminer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,113 +8,57 @@
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Optional;

import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE;

public class FileSizeDeterminer {
private static final Logger LOG = LogManager.getLogger(FileSizeDeterminer.class);
private static final int BYTE_TO_MIB = 1024;
private static final int BYTE_TO_MB = 1000;
private static final String PROTOCOL_RTMP = "rtmp";
private static final String FILE_TYPE_M3U8 = "m3u8";
private final String url;
private final OkHttpClient client;
//
private Optional<Long> fileSizeInByte = Optional.empty();
private Optional<String> responsePath = Optional.empty();
private Optional<String> responseContentType = Optional.empty();


/*
* get the file size of the url in byte
*/
public long getFileSizeInByte() {
if (fileSizeInByte.isEmpty()) {
getFileSizeForBuilder();
}
return fileSizeInByte.orElse(-1L);
}

/*
* get the path of the response which may differ to request url for redirects
*/
public String getResponsePath() {
if (responsePath.isEmpty()) {
getFileSizeInByte();
}
return responsePath.orElse("");
}

/*
* get the content type of the reponse message
*/

public String getResponseContentType() {
if (responseContentType.isEmpty()) {
getFileSizeInByte();
}
return responseContentType.orElse("");
}




/**
* Builds the determiner with the default read- and connect timeout of 30 seconds.
*
* @param aUrl The url of the file.
*/
public FileSizeDeterminer(final String aUrl) {
this(aUrl, 30L, 30L);
public FileSizeDeterminer() {
this(30L,30L);
}

/**
* @param aUrl The url of the file.
* @param connectTimeoutInSeconds The connection timeout in seconds.
* @param readTimeoutInSeconds The read timeout in seconds.
*/
public FileSizeDeterminer(
final String aUrl, final long connectTimeoutInSeconds, final long readTimeoutInSeconds) {
url = aUrl;
final long connectTimeoutInSeconds, final long readTimeoutInSeconds) {
client =
new OkHttpClientBuilder()
.withConnectTimeout(connectTimeoutInSeconds)
.withReadTimeout(readTimeoutInSeconds)
.build();
}

/** @return The file size in bytes. */
public Long getFileSizeForBuilder() {
return getFileSizeByRequest(RequestType.HEAD);
public RespoonseInfo getRequestInfo(final String url) {
return getRequestInfo(url, RequestType.HEAD);
}

private Long getFileSizeByRequest(final RequestType requestType) {
// Cant determine the file size of rtmp and m3u8.
if (!url.startsWith(PROTOCOL_RTMP) && !url.endsWith(FILE_TYPE_M3U8)) {
public RespoonseInfo getRequestInfo(final String url, final RequestType requestType) {
try (final Response response =
client.newCall(createRequestBuilderForRequestType(requestType).build()).execute()) {
final String contentLengthHeader = response.header(CONTENT_LENGTH);
responseContentType = Optional.of(response.header(CONTENT_TYPE, ""));
responsePath = Optional.of(response.request().url().encodedPath());
fileSizeInByte = Optional.of(parseContentLength(contentLengthHeader));
return fileSizeInByte.get();
client.newCall(createRequestBuilderForRequestType(url, requestType).build()).execute()) {
RespoonseInfo respoonseInfo = new RespoonseInfo(
parseContentLength(response.header(CONTENT_LENGTH)),
response.code(),
response.header(CONTENT_TYPE, ""),
response.request().url().encodedPath()
);
return respoonseInfo;
} catch (final IOException ioException) {
LOG.error(
"Something went wrong determining the file size of \"{}\" with {} request.",
url,
requestType);
if (requestType.equals(RequestType.HEAD)) {
LOG.info("Retrying the file size determination with GET request.");
return getFileSizeByRequest(RequestType.GET);
return getRequestInfo(url, RequestType.GET);
}
}
}
return -1L;
return null;
}

@NotNull
private Request.Builder createRequestBuilderForRequestType(final RequestType requestType) {
private Request.Builder createRequestBuilderForRequestType(final String url, final RequestType requestType) {
final Request.Builder requestBuilder;
switch (requestType) {
case GET:
Expand All @@ -129,11 +73,11 @@ private Request.Builder createRequestBuilderForRequestType(final RequestType req
return requestBuilder;
}

/** @return The file size in MiB. */
public Long getFileSizeInMiB() {
return getFileSizeForBuilder() / BYTE_TO_MIB;
private enum RequestType {
GET,
HEAD
}

@NotNull
private Long parseContentLength(final String contentLengthHeader) {
try {
Expand All @@ -146,14 +90,33 @@ private Long parseContentLength(final String contentLengthHeader) {
return -1L;
}
}

/** @return The file size in MB. */
public Long getFileSizeInMB() {
return getFileSizeForBuilder() / BYTE_TO_MB;

public class RespoonseInfo {
private Long size;
private int code;
private String contentType;
private String path;

public RespoonseInfo(Long size, int code, String contentType, String path) {
super();
this.size = size;
this.code = code;
this.contentType = contentType;
this.path = path;
}

public Long getSize() {
return size;
}
public int getCode() {
return code;
}
public String getContentType() {
return contentType;
}
public String getPath() {
return path;
}
}

private enum RequestType {
GET,
HEAD
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

class FileSizeDeterminerTest {
Expand All @@ -21,7 +22,7 @@ public static void setUpWiremock() {
wireMockServer.stubFor(
head(urlEqualTo("/" + TEST_FILE_NAME))
.willReturn(
aResponse().withStatus(200).withHeader(CONTENT_LENGTH, "5643")));
aResponse().withStatus(200).withHeader(CONTENT_LENGTH, "5643").withHeader(CONTENT_TYPE, "text/html")));
}

@BeforeEach
Expand All @@ -36,20 +37,20 @@ public void stopWireMock() {

@Test
void testGetFileSize() {
assertThat(getClassUnderTest().getFileSizeForBuilder()).isEqualTo(5643L);
assertThat(getClassUnderTest().getRequestInfo(wireMockServer.baseUrl() + TEST_FILE_URL).getSize()).isEqualTo(5643L);
}

@Test
void testGetFileSizeMiB() {
assertThat(getClassUnderTest().getFileSizeInMiB()).isEqualTo(5L);
void testGetStatusCode() {
assertThat(getClassUnderTest().getRequestInfo(wireMockServer.baseUrl() + TEST_FILE_URL).getCode()).isEqualTo(200);
}

@Test
void testGetFileSizeMB() {
assertThat(getClassUnderTest().getFileSizeInMB()).isEqualTo(5L);
void testGetContentType() {
assertThat(getClassUnderTest().getRequestInfo(wireMockServer.baseUrl() + TEST_FILE_URL).getContentType()).isEqualTo("text/html");
}

private FileSizeDeterminer getClassUnderTest() {
return new FileSizeDeterminer(wireMockServer.baseUrl() + TEST_FILE_URL);
return new FileSizeDeterminer();
}
}

0 comments on commit d99edf8

Please sign in to comment.