+ * If there is a content string, that will be returned. + * Otherwise, will get content from input stream + *
+ * + * @return the content string + */ + public String getContent() { + if (content != null) { + return content; + } + + if (stream != null) { + Scanner scanner = new Scanner(stream, "UTF-8").useDelimiter("\\A"); + + if (!scanner.hasNext()) { + return ""; + } + + content = scanner.next(); + scanner.close(); + + return content; + } + + return ""; + } + + /** + * Get response data as stream. + * + * @return the response data as a stream + */ + public InputStream getStream() { + if (stream != null) { + return stream; + } + try { + return new ByteArrayInputStream(content.getBytes("utf-8")); + } catch (final UnsupportedEncodingException e) { + throw new ApiException("UTF-8 encoding not supported", e); + } + } + + public int getStatusCode() { + return statusCode; + } + + public Header[] getHeaders() { + return headers; + } +} diff --git a/src/main/java/com/sendgrid/http/auth/AuthStrategy.java b/src/main/java/com/sendgrid/http/auth/AuthStrategy.java new file mode 100644 index 00000000..800f3740 --- /dev/null +++ b/src/main/java/com/sendgrid/http/auth/AuthStrategy.java @@ -0,0 +1,7 @@ +package com.sendgrid.http.auth; + +import com.sendgrid.http.Request; + +public interface AuthStrategy { + void applyAuth(Request request); +} diff --git a/src/main/java/com/sendgrid/http/auth/BasicAuthStrategy.java b/src/main/java/com/sendgrid/http/auth/BasicAuthStrategy.java new file mode 100644 index 00000000..32ea0ccb --- /dev/null +++ b/src/main/java/com/sendgrid/http/auth/BasicAuthStrategy.java @@ -0,0 +1,18 @@ +package com.sendgrid.http.auth; + +import com.sendgrid.http.Request; +import lombok.RequiredArgsConstructor; + +import java.util.Base64; + +@RequiredArgsConstructor +public class BasicAuthStrategy implements AuthStrategy { + private final String username; + private final String password; + + @Override + public void applyAuth(Request request) { + String basicAuthValue = Base64.getEncoder().encodeToString((username + ":" + password).getBytes()); + //requestBuilder.addHeader("Authorization", "Basic " + basicAuthValue); + } +} diff --git a/src/main/java/com/sendgrid/http/auth/TokenStrategy.java b/src/main/java/com/sendgrid/http/auth/TokenStrategy.java new file mode 100644 index 00000000..9c365e5e --- /dev/null +++ b/src/main/java/com/sendgrid/http/auth/TokenStrategy.java @@ -0,0 +1,13 @@ +package com.sendgrid.http.auth; + +import com.sendgrid.http.Request; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class TokenStrategy implements AuthStrategy { + private final String token; + @Override + public void applyAuth(Request request) { + request.addHeader("Authorization", "Bearer " + token); + } +} diff --git a/src/main/java/com/sendgrid/http/httpclient/ApiKeyHttpClient.java b/src/main/java/com/sendgrid/http/httpclient/ApiKeyHttpClient.java new file mode 100644 index 00000000..e7e406a8 --- /dev/null +++ b/src/main/java/com/sendgrid/http/httpclient/ApiKeyHttpClient.java @@ -0,0 +1,158 @@ +package com.sendgrid.http.httpclient; + +import com.sendgrid.constant.Config; +import com.sendgrid.constant.EnumConstants; +import com.sendgrid.http.HttpMethod; +import com.sendgrid.http.Request; +import com.sendgrid.http.Response; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; +import java.util.Map; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; +import org.apache.http.HttpVersion; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.RequestBuilder; +import org.apache.http.client.utils.HttpClientUtils; +import org.apache.http.config.SocketConfig; +import org.apache.http.entity.BufferedHttpEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicHeader; + +public class ApiKeyHttpClient extends HttpClient { + + protected final org.apache.http.client.HttpClient client; + + private boolean isCustomClient; + + /** + * Create a new HTTP Client. + */ + public ApiKeyHttpClient() { + this(DEFAULT_REQUEST_CONFIG); + } + + /** + * Create a new HTTP Client with a custom request config. + * + * @param requestConfig a RequestConfig. + */ + public ApiKeyHttpClient(final RequestConfig requestConfig) { + this(requestConfig, DEFAULT_SOCKET_CONFIG); + } + + /** + * Create a new HTTP Client with a custom request and socket config. + * + * @param requestConfig a RequestConfig. + * @param socketConfig a SocketConfig. + */ + public ApiKeyHttpClient(final RequestConfig requestConfig, final SocketConfig socketConfig) { + Collection
+ * This class provides methods to convert Java objects to JSON strings and vice versa.
+ * It includes a default {@link ObjectMapper} instance for common use cases,
+ * while also allowing custom {@link ObjectMapper} instances for specific configurations.
+ **/
+public class JsonUtil {
+ private static final ObjectMapper defaultObjectMapper = createDefaultObjectMapper();
+
+ private static ObjectMapper createDefaultObjectMapper() {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.registerModule(new JavaTimeModule());
+ return mapper;
+ }
+
+ public static String toJson(Object object) {
+ return toJson(object, defaultObjectMapper);
+ }
+
+ /**
+ * Converts an object to a JSON string using Jackson ObjectMapper.
+ *
+ * @param object the object to be converted to JSON
+ * @param mapper the ObjectMapper instance
+ * @return the JSON string representation of the object
+ * @throws ApiException if there is a JSON processing or mapping error
+ */
+ public static String toJson(Object object, ObjectMapper mapper) {
+ try {
+ return mapper.writeValueAsString(object);
+ } catch (JsonMappingException e) {
+ throw new ApiException(e.getMessage(), e);
+ } catch (JsonProcessingException e) {
+ throw new ApiException(e.getMessage(), e);
+ }
+ }
+
+ public static