Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
Utils.java
Go to the documentation of this file.
1/*
2 * Ollama4j - Java library for interacting with Ollama server.
3 * Copyright (c) 2025 Amith Koujalgi and contributors.
4 *
5 * Licensed under the MIT License (the "License");
6 * you may not use this file except in compliance with the License.
7 *
8*/
9package io.github.ollama4j.utils;
10
11import com.fasterxml.jackson.core.JsonProcessingException;
12import com.fasterxml.jackson.databind.ObjectMapper;
13import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
14import java.io.File;
15import java.io.IOException;
16import java.net.URI;
17import java.net.http.HttpClient;
18import java.net.http.HttpRequest;
19import java.net.http.HttpResponse;
20import java.time.Duration;
21import java.util.Objects;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25public class Utils {
26 private Utils() {}
27
28 private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
29
30 private static ObjectMapper objectMapper;
31
32 public static ObjectMapper getObjectMapper() {
33 if (objectMapper == null) {
34 objectMapper = new ObjectMapper();
35 objectMapper.registerModule(new JavaTimeModule());
36 }
37 return objectMapper;
38 }
39
40 public static byte[] loadImageBytesFromUrl(
41 String imageUrl, int connectTimeoutSeconds, int readTimeoutSeconds)
42 throws IOException, InterruptedException {
43 LOG.debug(
44 "Attempting to load image from URL: {} (connectTimeout={}s, readTimeout={}s)",
45 imageUrl,
46 connectTimeoutSeconds,
47 readTimeoutSeconds);
48 HttpClient client =
49 HttpClient.newBuilder()
50 .connectTimeout(Duration.ofSeconds(connectTimeoutSeconds))
51 .build();
52 HttpRequest request =
53 HttpRequest.newBuilder()
54 .uri(URI.create(imageUrl))
55 .timeout(Duration.ofSeconds(readTimeoutSeconds))
56 .header("User-Agent", "Mozilla/5.0")
57 .GET()
58 .build();
59 LOG.debug("Sending HTTP GET request to {}", imageUrl);
60 HttpResponse<byte[]> response =
61 client.send(request, HttpResponse.BodyHandlers.ofByteArray());
62 LOG.debug("Received HTTP response with status code: {}", response.statusCode());
63 if (response.statusCode() >= 200 && response.statusCode() < 300) {
64 LOG.debug(
65 "Successfully loaded image from URL: {} ({} bytes)",
66 imageUrl,
67 response.body().length);
68 return response.body();
69 } else {
70 LOG.error(
71 "Failed to load image from URL: {}. HTTP status: {}",
72 imageUrl,
73 response.statusCode());
74 throw new IOException("Failed to load image: HTTP " + response.statusCode());
75 }
76 }
77
78 public static File getFileFromClasspath(String fileName) {
79 LOG.debug("Trying to load file from classpath: {}", fileName);
80 ClassLoader classLoader = Utils.class.getClassLoader();
81 return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
82 }
83
84 public static String toJSON(Object object) throws JsonProcessingException {
85 return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object);
86 }
87}
static byte[] loadImageBytesFromUrl(String imageUrl, int connectTimeoutSeconds, int readTimeoutSeconds)
Definition Utils.java:40
static ObjectMapper getObjectMapper()
Definition Utils.java:32
static File getFileFromClasspath(String fileName)
Definition Utils.java:78
static String toJSON(Object object)
Definition Utils.java:84