Skip to content

Commit 35c9b83

Browse files
committed
Implemented the first logic
1 parent 0675199 commit 35c9b83

File tree

3 files changed

+306
-1
lines changed

3 files changed

+306
-1
lines changed

pom.xml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,63 @@
66

77
<groupId>org.javawebstack</groupId>
88
<artifactId>HTTP-Client</artifactId>
9-
<version>1.0</version>
9+
<version>1.0-SNAPSHOT</version>
1010

11+
<repositories>
12+
<repository>
13+
<id>javawebstack</id>
14+
<url>https://repo.javawebstack.org</url>
15+
</repository>
16+
</repositories>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.google.code.gson</groupId>
21+
<artifactId>gson</artifactId>
22+
<version>2.8.6</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.javawebstack</groupId>
26+
<artifactId>QueryString</artifactId>
27+
<version>1.0-SNAPSHOT</version>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<configuration>
37+
<source>8</source>
38+
<target>8</target>
39+
</configuration>
40+
</plugin>
41+
<plugin>
42+
<artifactId>maven-deploy-plugin</artifactId>
43+
<version>3.0.0-M1</version>
44+
<executions>
45+
<execution>
46+
<id>default-deploy</id>
47+
<phase>deploy</phase>
48+
<goals>
49+
<goal>deploy</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
<distributionManagement>
58+
<snapshotRepository>
59+
<id>javawebstack-snapshots</id>
60+
<url>https://nexus.javawebstack.org/repository/javawebstack-snapshots</url>
61+
</snapshotRepository>
62+
<repository>
63+
<id>javawebstack-releases</id>
64+
<url>https://nexus.javawebstack.org/repository/javawebstack-releases</url>
65+
</repository>
66+
</distributionManagement>
1167

1268
</project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,100 @@
11
package org.javawebstack.httpclient;
22

3+
import com.google.gson.FieldNamingPolicy;
4+
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
310
public class HTTPClient {
11+
12+
private Gson gson = new GsonBuilder()
13+
.setDateFormat("yyyy-MM-dd HH:mm:ss")
14+
.disableHtmlEscaping()
15+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
16+
.create();
17+
private int timeout = 5000;
18+
private String baseUrl = "";
19+
private Map<String, String> defaultHeaders = new HashMap<>();
20+
21+
public HTTPClient gson(Gson gson){
22+
this.gson = gson;
23+
return this;
24+
}
25+
26+
public Gson getGson() {
27+
return gson;
28+
}
29+
30+
public HTTPClient timeout(int timeout){
31+
this.timeout = timeout;
32+
return this;
33+
}
34+
35+
public int getTimeout() {
36+
return timeout;
37+
}
38+
39+
public HTTPClient header(String key, String value){
40+
defaultHeaders.put(key, value);
41+
return this;
42+
}
43+
44+
public Map<String, String> getDefaultHeaders() {
45+
return defaultHeaders;
46+
}
47+
48+
public HTTPClient headers(Map<String, String> defaultHeaders){
49+
this.defaultHeaders = defaultHeaders;
50+
return this;
51+
}
52+
53+
public HTTPClient authorization(String type, String value){
54+
return header("Authorization", type + " " + value);
55+
}
56+
57+
public HTTPClient bearer(String token){
58+
return authorization("Bearer", token);
59+
}
60+
61+
public HTTPClient setBaseUrl(String baseUrl) {
62+
if(baseUrl.endsWith("/"))
63+
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
64+
this.baseUrl = baseUrl;
65+
return this;
66+
}
67+
68+
public String getBaseUrl() {
69+
return baseUrl;
70+
}
71+
72+
public HTTPRequest request(String method, String path){
73+
return new HTTPRequest(this, method, path);
74+
}
75+
76+
public HTTPRequest get(String path){
77+
return request("GET", path);
78+
}
79+
80+
public HTTPRequest post(String path){
81+
return request("POST", path);
82+
}
83+
84+
public HTTPRequest post(String path, Object body){
85+
return post(path).jsonBody(body);
86+
}
87+
88+
public HTTPRequest put(String path){
89+
return request("PUT", path);
90+
}
91+
92+
public HTTPRequest put(String path, Object body){
93+
return put(path).jsonBody(body);
94+
}
95+
96+
public HTTPRequest delete(String path){
97+
return request("DELETE", path);
98+
}
99+
4100
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)