Skip to content

Instantly share code, notes, and snippets.

@ozzi-
Created March 5, 2025 13:24
Show Gist options
  • Save ozzi-/682f24864d8ebf7e681678a78cece159 to your computer and use it in GitHub Desktop.
Save ozzi-/682f24864d8ebf7e681678a78cece159 to your computer and use it in GitHub Desktop.
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.openhtmltopdf.extend.FSStream;
import com.openhtmltopdf.extend.FSStreamFactory;
import com.XXX.service.ImageService;
import lombok.RequiredArgsConstructor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@RequiredArgsConstructor
@Service
public class LocalAwareHttpFactory implements FSStreamFactory {
private final OkHttpClient client = new OkHttpClient();
private final ImageService imageService;
private static final String LOCALHOST = "http://localhost";
Logger logger = LoggerFactory.getLogger(LocalAwareHttpFactory.class);
@Override
public FSStream getUrl(String url) {
if (url.toLowerCase().startsWith(LOCALHOST)) {
// no HTTP is required, we can stream directly through the ImageService
try {
return handleLocalFile(url);
} catch (FileNotFoundException e) {
logger.error("Failed to get local file '{}' via ImageService",url,e);
return null;
}
} else {
return handleNwGet(url);
}
}
private FSStream handleNwGet(String url) {
try {
Request request = new Request.Builder().url(url).build();
final Response response = client.newCall(request).execute();
logger.info("Doing network call to {}", url);
return new FSStream() {
@Override
public InputStream getStream() {
return response.body().byteStream();
}
@Override
public Reader getReader() {
return response.body().charStream();
}
};
} catch (Exception e) {
logger.error("Failed to get URL {}", url, e);
}
return null;
}
private FSStream handleLocalFile(String url) throws FileNotFoundException {
int lastIndex = url.lastIndexOf('/');
String fileName = (lastIndex != -1) ? url.substring(lastIndex + 1) : url;
ResponseEntity<InputStreamResource> responseEntity = imageService.retrieve(fileName);
logger.info("Doing local call to {} -> {}", url, fileName);
return new FSStream() {
@Override
public InputStream getStream() {
try {
InputStreamResource body = responseEntity.getBody();
if (body != null) {
return body.getInputStream();
}
} catch (Exception e) {
logger.error("Failed to get URL " + url, e);
}
return null;
}
@Override
public Reader getReader() {
try {
InputStreamResource body = responseEntity.getBody();
if (body != null) {
return new InputStreamReader(body.getInputStream());
}
} catch (Exception e) {
logger.error("Failed to get URL {}", url, e);
}
return null;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment