|
| 1 | +package com.github.lightlibs.simplehttpwrapper; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.net.HttpURLConnection; |
| 8 | +import java.net.URL; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +@SuppressWarnings("unused") |
| 15 | +public class HttpWrapper { |
| 16 | + |
| 17 | + |
| 18 | + // GET |
| 19 | + public static HttpResponse get(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 20 | + return request(HttpMethod.GET, urlStr, headers, null); |
| 21 | + } |
| 22 | + |
| 23 | + public static HttpResponse getOrNull(@NotNull String urlStr, @Nullable String[] headers) { |
| 24 | + return requestOrNull(HttpMethod.GET, urlStr, headers, null); |
| 25 | + } |
| 26 | + |
| 27 | + // HEAD |
| 28 | + public static HttpResponse head(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 29 | + return request(HttpMethod.HEAD, urlStr, headers, null); |
| 30 | + } |
| 31 | + |
| 32 | + public static HttpResponse headOrNull(@NotNull String urlStr, @Nullable String[] headers) { |
| 33 | + return requestOrNull(HttpMethod.HEAD, urlStr, headers, null); |
| 34 | + } |
| 35 | + |
| 36 | + // POST |
| 37 | + public static HttpResponse post(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException { |
| 38 | + return request(HttpMethod.POST, urlStr, headers, body); |
| 39 | + } |
| 40 | + |
| 41 | + public static HttpResponse postOrNull(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) { |
| 42 | + return requestOrNull(HttpMethod.POST, urlStr, headers, body); |
| 43 | + } |
| 44 | + |
| 45 | + // PUT |
| 46 | + public static HttpResponse put(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException { |
| 47 | + return request(HttpMethod.PUT, urlStr, headers, body); |
| 48 | + } |
| 49 | + |
| 50 | + public static HttpResponse putOrNull(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) { |
| 51 | + return requestOrNull(HttpMethod.PUT, urlStr, headers, body); |
| 52 | + } |
| 53 | + |
| 54 | + // DELETE |
| 55 | + public static HttpResponse delete(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 56 | + return request(HttpMethod.DELETE, urlStr, headers, null); |
| 57 | + } |
| 58 | + |
| 59 | + public static HttpResponse deleteOrNull(@NotNull String urlStr, @Nullable String[] headers) { |
| 60 | + return requestOrNull(HttpMethod.DELETE, urlStr, headers, null); |
| 61 | + } |
| 62 | + |
| 63 | + // CONNECT |
| 64 | + public static HttpResponse connect(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 65 | + return request(HttpMethod.CONNECT, urlStr, headers, null); |
| 66 | + } |
| 67 | + |
| 68 | + public static HttpResponse connectOrNull(@NotNull String urlStr, @Nullable String[] headers) { |
| 69 | + return requestOrNull(HttpMethod.CONNECT, urlStr, headers, null); |
| 70 | + } |
| 71 | + |
| 72 | + // OPTIONS |
| 73 | + public static HttpResponse options(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 74 | + return request(HttpMethod.OPTIONS, urlStr, headers, null); |
| 75 | + } |
| 76 | + |
| 77 | + public static HttpResponse optionsOrNull(@NotNull String urlStr, @Nullable String[] headers) { |
| 78 | + return requestOrNull(HttpMethod.OPTIONS, urlStr, headers, null); |
| 79 | + } |
| 80 | + |
| 81 | + // TRACE |
| 82 | + public static HttpResponse trace(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 83 | + return request(HttpMethod.TRACE, urlStr, headers, null); |
| 84 | + } |
| 85 | + |
| 86 | + public static HttpResponse traceOrNull(@NotNull String urlStr, @Nullable String[] headers) { |
| 87 | + return requestOrNull(HttpMethod.TRACE, urlStr, headers, null); |
| 88 | + } |
| 89 | + |
| 90 | + // PATCH |
| 91 | + public static HttpResponse patch(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException { |
| 92 | + return request(HttpMethod.PATCH, urlStr, headers, body); |
| 93 | + } |
| 94 | + |
| 95 | + public static HttpResponse patchOrNull(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) { |
| 96 | + return requestOrNull(HttpMethod.PATCH, urlStr, headers, body); |
| 97 | + } |
| 98 | + |
| 99 | + // ----- |
| 100 | + // UTILS |
| 101 | + // ----- |
| 102 | + |
| 103 | + public static HttpResponse request(@NotNull HttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException { |
| 104 | + return convertHeadersAndExecute(method.getMethod(), urlStr, headers, postData); |
| 105 | + } |
| 106 | + |
| 107 | + public static HttpResponse request(@NotNull HttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String postData) throws IOException { |
| 108 | + return execute(method.getMethod(), urlStr, headers, postData); |
| 109 | + } |
| 110 | + |
| 111 | + @SuppressWarnings("CallToPrintStackTrace") |
| 112 | + public static HttpResponse requestOrNull(@NotNull HttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String postData) { |
| 113 | + try { |
| 114 | + return convertHeadersAndExecute(method.getMethod(), urlStr, headers, postData); |
| 115 | + } catch (IOException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + return null; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @SuppressWarnings("CallToPrintStackTrace") |
| 122 | + public static HttpResponse requestOrNull(@NotNull HttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String postData) { |
| 123 | + try { |
| 124 | + return execute(method.getMethod(), urlStr, headers, postData); |
| 125 | + } catch (IOException e) { |
| 126 | + e.printStackTrace(); |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Use {@link #request(HttpMethod, String, String[], String)} instead. |
| 133 | + * Only use if you know what you are doing. |
| 134 | + */ |
| 135 | + @Deprecated |
| 136 | + public static HttpResponse customRequest(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException { |
| 137 | + return convertHeadersAndExecute(method, urlStr, headers, body); |
| 138 | + } |
| 139 | + |
| 140 | + @NotNull |
| 141 | + private static HttpResponse convertHeadersAndExecute(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String bodyOut) throws IOException { |
| 142 | + List<HttpHeader> convertedHeaders = convertHeaders(headers); |
| 143 | + return execute(method, urlStr, convertedHeaders, bodyOut); |
| 144 | + } |
| 145 | + |
| 146 | + @NotNull |
| 147 | + private static HttpResponse execute(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String bodyOut) throws IOException { |
| 148 | + HttpURLConnection connection = writeHttpRequest(method, urlStr, headers, bodyOut); |
| 149 | + return readHttpResponse(connection); |
| 150 | + } |
| 151 | + |
| 152 | + private static @NotNull List<HttpHeader> convertHeaders(@Nullable String[] headers) { |
| 153 | + List<HttpHeader> headersList = new ArrayList<>(); |
| 154 | + if (headers != null) { |
| 155 | + for (String header : headers) { |
| 156 | + if (header == null) continue; |
| 157 | + |
| 158 | + String[] parts = header.split(":", 2); |
| 159 | + if (parts.length != 2) continue; |
| 160 | + |
| 161 | + headersList.add(new HttpHeader(parts[0], parts[1])); |
| 162 | + } |
| 163 | + } |
| 164 | + return headersList; |
| 165 | + } |
| 166 | + |
| 167 | + private static @NotNull HttpURLConnection writeHttpRequest(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String bodyOut) throws IOException { |
| 168 | + URL url = new URL(urlStr); |
| 169 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 170 | + connection.setRequestMethod(method.getMethodId()); |
| 171 | + |
| 172 | + if (headers != null) { |
| 173 | + for (HttpHeader header : headers) { |
| 174 | + if (header == null) continue; |
| 175 | + connection.setRequestProperty(header.getKey(), header.getValue()); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (method.hasBody() && bodyOut != null) { |
| 180 | + connection.setDoOutput(true); |
| 181 | + try (OutputStream os = connection.getOutputStream()) { |
| 182 | + byte[] postDataBytes = bodyOut.getBytes(StandardCharsets.UTF_8); |
| 183 | + os.write(postDataBytes, 0, postDataBytes.length); |
| 184 | + } |
| 185 | + } |
| 186 | + return connection; |
| 187 | + } |
| 188 | + |
| 189 | + private static @NotNull HttpResponse readHttpResponse(HttpURLConnection connection) throws IOException { |
| 190 | + int responseCode = connection.getResponseCode(); |
| 191 | + |
| 192 | + Map<String, List<String>> headerFields = connection.getHeaderFields(); |
| 193 | + List<HttpHeader> headers = new ArrayList<>(); |
| 194 | + for (Map.Entry<String, List<String>> headerEntry : headerFields.entrySet()) { |
| 195 | + for (String value : headerEntry.getValue()) { |
| 196 | + headers.add(new HttpHeader(headerEntry.getKey(), value)); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + InputStream inputStream; |
| 201 | + if (responseCode >= 200 && responseCode < 300) inputStream = connection.getInputStream(); |
| 202 | + else inputStream = connection.getErrorStream(); |
| 203 | + |
| 204 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 205 | + StringBuilder bodyIn = new StringBuilder(); |
| 206 | + while (true) { |
| 207 | + String line = reader.readLine(); |
| 208 | + if (line == null) break; |
| 209 | + bodyIn.append(line); |
| 210 | + } |
| 211 | + reader.close(); |
| 212 | + connection.disconnect(); |
| 213 | + |
| 214 | + return new HttpResponse(responseCode, headers, bodyIn.toString()); |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments