Skip to content
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
10 changes: 0 additions & 10 deletions src/main/java/com/mindee/input/InputSourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mindee.MindeeException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBuffer;
import org.apache.pdfbox.pdmodel.PDDocument;
Expand Down Expand Up @@ -79,15 +78,6 @@ public static boolean isPdf(byte[] fileBytes) {
return true;
}

/**
* Ensures the URL can be sent to the Mindee server.
*/
public static void validateUrl(URL inputUrl) {
if (!"https".equalsIgnoreCase(inputUrl.getProtocol())) {
throw new MindeeException("Only HTTPS source URLs are allowed");
}
}

/**
* Returns true if the source PDF has source text inside. Returns false for images.
*
Expand Down
58 changes: 41 additions & 17 deletions src/main/java/com/mindee/input/URLInputSource.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.mindee.input;

import com.mindee.MindeeException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -19,7 +21,7 @@
*/
public class URLInputSource {
@Getter
private final String url;
private final URL url;
private final String username;
private final String password;
@Getter
Expand All @@ -43,20 +45,21 @@ public class URLInputSource {
* @param url URL to fetch the file from.
* @return An instance of {@link URLInputSource}.
*/
public static Builder builder(String url) {
return new Builder(url);
public static Builder builder(String url) throws MalformedURLException {
return new Builder(new URL(url));
}

private HttpURLConnection prepareConnection() throws IOException {
HttpURLConnection connection = createConnection(url);
connection = handleRedirects(connection);
public static Builder builder(URL url) {
return new Builder(url);
}

int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Failed to fetch file: " + responseCode);
/**
* Ensures the URL can be sent to the Mindee server.
*/
public void validateSecure() {
if (!"https".equalsIgnoreCase(this.url.getProtocol())) {
throw new MindeeException("Only HTTPS source URLs are allowed");
}

return connection;
}

/**
Expand All @@ -72,8 +75,20 @@ public void fetchFile() throws IOException {
}
}

protected HttpURLConnection createConnection(String urlString) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
private HttpURLConnection prepareConnection() throws IOException {
HttpURLConnection connection = createConnection(url);
connection = handleRedirects(connection);

int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Failed to fetch file: " + responseCode);
}

return connection;
}

protected HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);

if (username != null && password != null) {
Expand Down Expand Up @@ -101,7 +116,7 @@ private HttpURLConnection handleRedirects(HttpURLConnection connection) throws I
String newUrl = connection.getHeaderField("Location");
connection.disconnect();

HttpURLConnection newConnection = createConnection(newUrl);
HttpURLConnection newConnection = createConnection(new URL(newUrl));
return handleRedirects(newConnection); // Recursive call to handle multiple redirects
}
return connection;
Expand Down Expand Up @@ -189,18 +204,27 @@ public void cleanup() {
* Builder class for an URLInputSource.
*/
public static class Builder {
private final String url;
private final URL url;
private String username;
private String password;
private String localFilename;
private String token;

/**
* Default constructor.
* String constructor.
*
* @param url Remote URL resource.
*/
public Builder(String url) throws MalformedURLException {
this.url = new URL(url);
}

/**
* URL constructor.
*
* @param url Remote URL resource.
*/
public Builder(String url) {
public Builder(URL url) {
this.url = url;
}

Expand Down
29 changes: 14 additions & 15 deletions src/main/java/com/mindee/v1/MindeeClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mindee.v1;

import com.mindee.MindeeException;
import com.mindee.input.InputSourceUtils;
import com.mindee.input.LocalInputSource;
import com.mindee.input.PageOptions;
import com.mindee.input.URLInputSource;
import com.mindee.pdf.PDFBoxApi;
import com.mindee.pdf.PDFOperation;
import com.mindee.v1.clientOptions.PollingOptions;
Expand Down Expand Up @@ -203,7 +203,6 @@ public <T extends Inference> AsyncPredictResponse<T> enqueue(
Class<T> type,
URL sourceUrl
) throws IOException {
InputSourceUtils.validateUrl(sourceUrl);
return this.enqueue(type, new Endpoint(type), null, null, null, sourceUrl);
}

Expand All @@ -222,7 +221,6 @@ public <T extends Inference> AsyncPredictResponse<T> enqueue(
URL sourceUrl,
PredictOptions predictOptions
) throws IOException {
InputSourceUtils.validateUrl(sourceUrl);
return this.enqueue(type, new Endpoint(type), null, null, predictOptions, sourceUrl);
}

Expand All @@ -232,8 +230,12 @@ private <T extends Inference> AsyncPredictResponse<T> enqueue(
byte[] file,
String filename,
PredictOptions predictOptions,
URL urlInputSource
URL url
) throws IOException {
URLInputSource urlInputSource = null;
if (url != null) {
urlInputSource = new URLInputSource.Builder(url).build();
}
RequestParameters params = RequestParameters
.builder()
.file(file)
Expand Down Expand Up @@ -402,7 +404,6 @@ public <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
Class<T> type,
URL sourceUrl
) throws IOException, InterruptedException {
InputSourceUtils.validateUrl(sourceUrl);
return this.enqueueAndParse(type, new Endpoint(type), null, null, null, null, sourceUrl);
}

Expand Down Expand Up @@ -441,11 +442,12 @@ private <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
byte[] file,
String filename,
PredictOptions predictOptions,
URL urlInputSource
URL url
) throws IOException, InterruptedException {
if (pollingOptions == null) {
pollingOptions = PollingOptions.builder().build();
}

this.validateAsyncParams(pollingOptions);
final int initialDelaySec = (int) (pollingOptions.getInitialDelaySec() * 1000);
final int intervalSec = (int) (pollingOptions.getIntervalSec() * 1000);
Expand All @@ -456,7 +458,7 @@ private <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
file,
filename,
predictOptions,
urlInputSource
url
);

String jobId = enqueueResponse.getJob().getId();
Expand Down Expand Up @@ -648,7 +650,6 @@ public <T extends Inference> PredictResponse<T> parse(
Class<T> type,
URL urlInputSource
) throws IOException {
InputSourceUtils.validateUrl(urlInputSource);
return this.parse(type, new Endpoint(type), null, null, null, urlInputSource);
}

Expand All @@ -667,7 +668,6 @@ public <T extends Inference> PredictResponse<T> parse(
URL urlInputSource,
PredictOptions predictOptions
) throws IOException {
InputSourceUtils.validateUrl(urlInputSource);
return this.parse(type, new Endpoint(type), null, null, predictOptions, urlInputSource);
}

Expand All @@ -677,8 +677,12 @@ private <T extends Inference> PredictResponse<T> parse(
byte[] file,
String filename,
PredictOptions predictOptions,
URL urlInputSource
URL url
) throws IOException {
URLInputSource urlInputSource = null;
if (url != null) {
urlInputSource = new URLInputSource.Builder(url).build();
}
RequestParameters params = RequestParameters
.builder()
.file(file)
Expand Down Expand Up @@ -760,7 +764,6 @@ public <T extends GeneratedV1> AsyncPredictResponse<T> enqueue(
Endpoint endpoint,
URL sourceUrl
) throws IOException {
InputSourceUtils.validateUrl(sourceUrl);
return this.enqueue(type, endpoint, null, null, null, sourceUrl);
}

Expand All @@ -781,7 +784,6 @@ public <T extends GeneratedV1> AsyncPredictResponse<T> enqueue(
URL sourceUrl,
PredictOptions predictOptions
) throws IOException {
InputSourceUtils.validateUrl(sourceUrl);
return this.enqueue(type, endpoint, null, null, predictOptions, sourceUrl);
}

Expand Down Expand Up @@ -893,7 +895,6 @@ public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
Endpoint endpoint,
URL sourceUrl
) throws IOException, InterruptedException {
InputSourceUtils.validateUrl(sourceUrl);
return this.enqueueAndParse(type, endpoint, null, null, null, null, sourceUrl);
}

Expand Down Expand Up @@ -1024,7 +1025,6 @@ public <T extends GeneratedV1> PredictResponse<T> parse(
Endpoint endpoint,
URL documentUrl
) throws IOException {
InputSourceUtils.validateUrl(documentUrl);
return this.parse(type, endpoint, null, null, null, documentUrl);
}

Expand All @@ -1045,7 +1045,6 @@ public <T extends GeneratedV1> PredictResponse<T> parse(
URL documentUrl,
PredictOptions predictOptions
) throws IOException {
InputSourceUtils.validateUrl(documentUrl);
return this.parse(type, endpoint, null, null, predictOptions, documentUrl);
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/mindee/v1/http/RequestParameters.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mindee.v1.http;

import com.mindee.input.URLInputSource;
import com.mindee.v1.clientOptions.PredictOptions;
import com.mindee.v1.clientOptions.WorkflowOptions;
import java.net.URL;
Expand All @@ -20,7 +21,7 @@ public class RequestParameters {

@Builder
private RequestParameters(
URL urlInputSource,
URLInputSource urlInputSource,
byte[] file,
PredictOptions predictOptions,
WorkflowOptions workflowOptions,
Expand All @@ -39,7 +40,12 @@ private RequestParameters(
} else {
this.workflowOptions = workflowOptions;
}
this.fileUrl = urlInputSource;
if (urlInputSource != null) {
urlInputSource.validateSecure();
this.fileUrl = urlInputSource.getUrl();
} else {
this.fileUrl = null;
}
this.file = file;
this.fileName = fileName;
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/mindee/v2/MindeeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public JobResponse enqueue(
* @param params The parameters to send along with the file.
*/
public JobResponse enqueue(URLInputSource inputSource, BaseParameters params) throws IOException {
inputSource.validateSecure();
return mindeeApi.reqPostEnqueue(inputSource, params);
}

Expand Down Expand Up @@ -141,8 +142,12 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
URLInputSource inputSource,
BaseParameters params
) throws IOException, InterruptedException {
JobResponse job = enqueue(inputSource, params);
return pollAndFetch(responseClass, job, PollingOptions.builder().build());
return enqueueAndGetResult(
responseClass,
inputSource,
params,
PollingOptions.builder().build()
);
}

/**
Expand All @@ -162,6 +167,7 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
BaseParameters params,
PollingOptions pollingOptions
) throws IOException, InterruptedException {
inputSource.validateSecure();
JobResponse job = enqueue(inputSource, params);
return pollAndFetch(responseClass, job, pollingOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseParameters opt

var builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.EXTENDED);
builder.addTextBody("url", inputSource.getUrl());
builder.addTextBody("url", inputSource.getUrl().toString());
post.setEntity(options.buildHttpBody(builder).build());
return executeEnqueue(post);
}
Expand Down
Loading
Loading