|
| 1 | +package org.javawebstack.httpclient; |
| 2 | + |
| 3 | +import org.javawebstack.querystring.QueryString; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.net.HttpURLConnection; |
| 10 | +import java.net.URL; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public class HTTPRequest { |
| 16 | + |
| 17 | + private final HTTPClient client; |
| 18 | + private final String path; |
| 19 | + private final String method; |
| 20 | + private final QueryString query = new QueryString(); |
| 21 | + private final Map<String, String> requestHeaders = new HashMap<>(); |
| 22 | + private byte[] requestBody; |
| 23 | + private Map<String, String> responseHeaders = new HashMap<>(); |
| 24 | + private byte[] responseBody; |
| 25 | + private int status; |
| 26 | + |
| 27 | + public HTTPRequest(HTTPClient client, String method, String path){ |
| 28 | + this.client = client; |
| 29 | + this.method = method; |
| 30 | + this.path = path; |
| 31 | + } |
| 32 | + |
| 33 | + public HTTPRequest header(String key, String value){ |
| 34 | + requestHeaders.put(key, value); |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + public HTTPRequest query(String key, String value){ |
| 39 | + query.set(key, value); |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + public HTTPRequest query(String key1, String key2, String value){ |
| 44 | + return query(key1 + "[" + key2 + "]", value); |
| 45 | + } |
| 46 | + |
| 47 | + public HTTPRequest query(String key1, String key2, String key3, String value){ |
| 48 | + return query(key1 + "[" + key2 + "]" + "[" + key3 + "]", value); |
| 49 | + } |
| 50 | + |
| 51 | + public HTTPRequest body(byte[] body){ |
| 52 | + this.requestBody = body; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public HTTPRequest body(String body){ |
| 57 | + return body(body.getBytes(StandardCharsets.UTF_8)); |
| 58 | + } |
| 59 | + |
| 60 | + public HTTPRequest contentType(String contentType){ |
| 61 | + return header("Content-Type", contentType); |
| 62 | + } |
| 63 | + |
| 64 | + public HTTPRequest authorization(String type, String value){ |
| 65 | + return header("Authorization", type + " " + value); |
| 66 | + } |
| 67 | + |
| 68 | + public HTTPRequest bearer(String token){ |
| 69 | + return authorization("Bearer", token); |
| 70 | + } |
| 71 | + |
| 72 | + public HTTPRequest jsonBody(Object object){ |
| 73 | + return body(client.getGson().toJson(object)).contentType("application/json"); |
| 74 | + } |
| 75 | + |
| 76 | + public int status(){ |
| 77 | + return status; |
| 78 | + } |
| 79 | + |
| 80 | + public byte[] bytes(){ |
| 81 | + return responseBody; |
| 82 | + } |
| 83 | + |
| 84 | + public String string(){ |
| 85 | + return new String(responseBody, StandardCharsets.UTF_8); |
| 86 | + } |
| 87 | + |
| 88 | + public <T> T json(Class<T> type){ |
| 89 | + if(type == null) |
| 90 | + return null; |
| 91 | + if(type.equals(byte[].class)) |
| 92 | + return (T) responseBody; |
| 93 | + if(type.equals(String.class)) |
| 94 | + return (T) string(); |
| 95 | + return client.getGson().fromJson(string(), type); |
| 96 | + } |
| 97 | + |
| 98 | + public String header(String key){ |
| 99 | + return responseHeaders.get(key); |
| 100 | + } |
| 101 | + |
| 102 | + public HTTPRequest execute(){ |
| 103 | + HttpURLConnection conn = null; |
| 104 | + try{ |
| 105 | + URL theUrl = new URL(client.getBaseUrl() + (path.startsWith("/") ? "" : "/") + path + (query.size() > 0 ? "?" + query.toString() : "")); |
| 106 | + conn = (HttpURLConnection) theUrl.openConnection(); |
| 107 | + conn.setReadTimeout(client.getTimeout()); |
| 108 | + conn.setConnectTimeout(5000); |
| 109 | + conn.setRequestMethod(method); |
| 110 | + conn.setDoInput(true); |
| 111 | + for(String k : requestHeaders.keySet()){ |
| 112 | + conn.setRequestProperty(k, requestHeaders.get(k)); |
| 113 | + } |
| 114 | + if(requestBody!=null){ |
| 115 | + conn.setDoOutput(true); |
| 116 | + OutputStream os = conn.getOutputStream(); |
| 117 | + os.write(requestBody); |
| 118 | + os.flush(); |
| 119 | + os.close(); |
| 120 | + } |
| 121 | + status = conn.getResponseCode(); |
| 122 | + conn.getHeaderFields().forEach((k,v) -> { |
| 123 | + if(v.size()>0) |
| 124 | + responseHeaders.put(k, v.get(v.size()-1)); |
| 125 | + }); |
| 126 | + if(status>299){ |
| 127 | + this.responseBody = readAll(conn.getErrorStream()); |
| 128 | + }else{ |
| 129 | + this.responseBody = readAll(conn.getInputStream()); |
| 130 | + } |
| 131 | + }catch(Exception e){ |
| 132 | + try { |
| 133 | + this.responseBody = readAll(conn.getErrorStream()); |
| 134 | + }catch(IOException | NullPointerException ex){} |
| 135 | + } |
| 136 | + this.responseBody = new byte[0]; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + private static byte[] readAll(InputStream is) throws IOException { |
| 141 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 142 | + byte[] data = new byte[1024]; |
| 143 | + int r = 0; |
| 144 | + while (r != -1){ |
| 145 | + r = is.read(data); |
| 146 | + if(r != -1) |
| 147 | + baos.write(data, 0, r); |
| 148 | + } |
| 149 | + is.close(); |
| 150 | + return baos.toByteArray(); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments