-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUtils.java
More file actions
213 lines (193 loc) · 7.82 KB
/
Utils.java
File metadata and controls
213 lines (193 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package io.imagekit.sdk.utils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import io.imagekit.sdk.ImageKit;
import io.imagekit.sdk.config.Configuration;
import io.imagekit.sdk.exceptions.BadRequestException;
import io.imagekit.sdk.exceptions.ConflictException;
import io.imagekit.sdk.exceptions.ForbiddenException;
import io.imagekit.sdk.exceptions.InternalServerException;
import io.imagekit.sdk.exceptions.NotFoundException;
import io.imagekit.sdk.exceptions.PartialSuccessException;
import io.imagekit.sdk.exceptions.TooManyRequestsException;
import io.imagekit.sdk.exceptions.UnauthorizedException;
import io.imagekit.sdk.exceptions.UnknownException;
import io.imagekit.sdk.models.ResponseMetaData;
import io.imagekit.sdk.models.results.ResultCache;
import io.imagekit.sdk.models.results.ResultException;
import okhttp3.Credentials;
import okhttp3.Response;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
public class Utils {
public static String listToString(List<String> list) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
if (null != list.get(i)) {
builder.append(list.get(i));
if (i < list.size() - 1) {
builder.append(",");
}
}
}
return builder.toString();
}
public static String fileToBase64(File file) {
String base64File = "";
try (FileInputStream imageInFile = new FileInputStream(file)) {
// Reading a file from file system
byte fileData[] = new byte[(int) file.length()];
imageInFile.read(fileData);
base64File = Base64.getEncoder().encodeToString(fileData);
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the file " + ioe);
}
return base64File;
}
public static byte[] fileToBytes(File file) {
byte[] bytes = null;
try (FileInputStream imageInFile = new FileInputStream(file)) {
// Reading a file from file system
bytes = new byte[(int) file.length()];
imageInFile.read(bytes);
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the file " + ioe);
}
return bytes;
}
public static String bytesToBase64(byte[] fileData) {
String base64File = "";
base64File = Base64.getEncoder().encodeToString(fileData);
return base64File;
}
/**
*
* @param cls is a Class name from which this method will invoke
* @return it will return object of Configuration class
* @throws IOException if config.properties file doesn't exists
*/
public static Configuration getSystemConfig(Class<?> cls) throws IOException {
Properties properties = new Properties();
String configFile = "config.properties";
InputStream is = cls.getClassLoader().getResourceAsStream(configFile);
if (null != is) {
properties.load(is);
} else {
throw new FileNotFoundException("Property file '" + configFile + "' not found in classpath");
}
Configuration config = new Configuration();
config.setUrlEndpoint(properties.getProperty("UrlEndpoint"));
config.setPublicKey(properties.getProperty("PublicKey"));
config.setPrivateKey(properties.getProperty("PrivateKey"));
config.validate();
return config;
}
public static Map<String, String> mapListOfStringToString(Map<String, List<String>> listMap) {
Map<String, String> stringMap = new HashMap<>();
Set<Entry<String, List<String>>> listMapEntries = listMap.entrySet();
for (Entry<String, List<String>> entry : listMapEntries) {
stringMap.put(entry.getKey(), entry.getValue().stream().collect(Collectors.joining(",")));
}
return stringMap;
}
public static void populateResponseMetadata(String respBody, ResponseMetaData responseMetadata, int responseCode,
Map<String, List<String>> responseHeaders) throws IOException {
responseMetadata.setRaw(respBody);
if (responseHeaders != null) {
Map<String, String> mappedHeader = Utils.mapListOfStringToString(responseHeaders);
responseMetadata.setHeaders(mappedHeader);
}
responseMetadata.setHttpStatusCode(responseCode);
}
public static Map<String, String> getHeaders(ImageKit imageKit) {
String credential = Credentials.basic(imageKit.getConfig().getPrivateKey(), "");
Map<String, String> headers = new HashMap<>();
headers.put("Accept-Encoding", "application/json");
headers.put("Content-Type", "application/json");
headers.put("Authorization", credential);
return headers;
}
public static void generalApiThrowException(Response response)
throws IOException, BadRequestException, InternalServerException, UnknownException, UnauthorizedException,
ForbiddenException, TooManyRequestsException {
ResultException result = populateResult(response);
if (response.code() == 400) {
throw new BadRequestException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else if (response.code() == 401) {
throw new UnauthorizedException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else if (response.code() == 403) {
throw new ForbiddenException(result.getMessage(), null, false, false, result.getMessage(), result.getHelp(),
result.getResponseMetaData());
} else if (response.code() == 429) {
throw new TooManyRequestsException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else if (response.code() == 500 || response.code() == 502 || response.code() == 503
|| response.code() == 504) {
throw new InternalServerException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
throw new UnknownException(result.getMessage(), null, false, false, result.getMessage(), result.getHelp(),
result.getResponseMetaData());
}
}
public static void throwOtherException(Response response)
throws IOException, PartialSuccessException, NotFoundException, UnknownException {
ResultException result = populateResult(response);
if (response.code() == 207) {
throw new PartialSuccessException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else if (response.code() == 404) {
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(), result.getHelp(),
result.getResponseMetaData());
} else {
throw new UnknownException(result.getMessage(), null, false, false, result.getMessage(), result.getHelp(),
result.getResponseMetaData());
}
}
public static ResultException populateResult(Response response) throws IOException {
String resp = response.body().string();
ResultException result = new Gson().fromJson(resp, ResultException.class);
populateResponseMetadata(resp, result.getResponseMetaData(), response.code(), response.headers().toMultimap());
return result;
}
public static BufferedImage createImage(URL url) {
try {
return ImageIO.read(url);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static boolean isReadableImage(String base64) {
byte[] bytes = Base64.getDecoder().decode(base64);
return isReadableImage(bytes);
}
public static boolean isReadableImage(byte[] bytes) {
InputStream inputStream = new ByteArrayInputStream(bytes);
try {
return ImageIO.read(inputStream) != null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static boolean isReadableImage(URL url) {
return createImage(url) != null;
}
}