|
45 | 45 | import java.util.zip.ZipInputStream;
|
46 | 46 | import java.util.zip.ZipOutputStream;
|
47 | 47 |
|
| 48 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 49 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 50 | +import com.fasterxml.jackson.databind.JsonNode; |
| 51 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 52 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 53 | + |
48 | 54 | public class Util {
|
49 | 55 | private static Random generator = new Random();
|
50 | 56 |
|
@@ -394,7 +400,7 @@ public FileVisitResult visitFile(Path file,
|
394 | 400 | public static byte[] fileUpload(String urlString, String fieldName, String fileName, byte[] bytes) throws IOException {
|
395 | 401 | final int TIMEOUT = 90000; // 90 seconds
|
396 | 402 | String boundary = "===" + createPrivateUID() + "===";
|
397 |
| - URL url = new URL(urlString); |
| 403 | + URL url = URI.create(urlString).toURL(); |
398 | 404 | HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
399 | 405 | conn.setConnectTimeout(TIMEOUT);
|
400 | 406 | conn.setReadTimeout(TIMEOUT);
|
@@ -444,7 +450,7 @@ public static byte[] fileUpload(String urlString, String fieldName, String fileN
|
444 | 450 | public static String httpPost(String urlString, String content, String contentType) {
|
445 | 451 | StringBuilder result = new StringBuilder();
|
446 | 452 | try {
|
447 |
| - URL url = new URL(urlString); |
| 453 | + URL url = URI.create(urlString).toURL(); |
448 | 454 | HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
449 | 455 | connection.setRequestProperty("Content-Type", contentType);
|
450 | 456 | connection.setDoOutput(true);
|
@@ -528,7 +534,7 @@ public static Map<String, String> getParams(String url)
|
528 | 534 | public static boolean exists(String url) {
|
529 | 535 | boolean result = false;
|
530 | 536 | try {
|
531 |
| - HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); |
| 537 | + HttpURLConnection conn = (HttpURLConnection) URI.create(url).toURL().openConnection(); |
532 | 538 | try {
|
533 | 539 | conn.connect();
|
534 | 540 | result = conn.getHeaderField(null).contains("200");
|
@@ -592,4 +598,29 @@ private static boolean isBadWord(String word){
|
592 | 598 | public static boolean isPronouncableUID(String s) {
|
593 | 599 | return s.matches("(([aeiouy][bcdfghjklmnpqrstvwxz]){2}|([bcdfghjklmnpqrstvwxz][aeiouy]){2})(-(([aeiouy][bcdfghjklmnpqrstvwxz]){2}|([bcdfghjklmnpqrstvwxz][aeiouy]){2})){3}");
|
594 | 600 | }
|
| 601 | + |
| 602 | + private static ObjectMapper mapper = new ObjectMapper(); |
| 603 | + static { |
| 604 | + mapper.setSerializationInclusion(Include.NON_DEFAULT); |
| 605 | + } |
| 606 | + |
| 607 | + public static ObjectNode toJson(Object obj) { |
| 608 | + return (ObjectNode) mapper.convertValue(obj, JsonNode.class); |
| 609 | + } |
| 610 | + |
| 611 | + public static String toJsonString(Object obj) { |
| 612 | + try { |
| 613 | + return mapper.writeValueAsString(obj); |
| 614 | + } catch (JsonProcessingException e) { |
| 615 | + return null; |
| 616 | + } |
| 617 | + } |
| 618 | + |
| 619 | + public static ObjectNode fromJsonString(String jsonString) throws JsonProcessingException { |
| 620 | + return (ObjectNode) mapper.readTree(jsonString); |
| 621 | + } |
| 622 | + |
| 623 | + public static ObjectNode fromJsonString(byte[] jsonStringBytes) throws JsonProcessingException, IOException { |
| 624 | + return (ObjectNode) mapper.readTree(jsonStringBytes); |
| 625 | + } |
595 | 626 | }
|
0 commit comments