Skip to content

Commit 1d2f52f

Browse files
committed
add post util and iroiro
1 parent cb88a81 commit 1d2f52f

File tree

9 files changed

+295
-52
lines changed

9 files changed

+295
-52
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package dev.felnull.fnjl;
22

33
public class FNJLBuildIn {
4-
protected static final String VERSION = "1.49";
4+
protected static final String VERSION = "1.50";
55
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package dev.felnull.fnjl.io;
2+
3+
import org.jetbrains.annotations.Contract;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.io.BufferedOutputStream;
8+
import java.io.IOException;
9+
import java.io.OutputStream;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.Collections;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.function.Consumer;
15+
16+
public class PostRequest {
17+
@NotNull
18+
private final Consumer<OutputStream> body;
19+
@NotNull
20+
private final Map<String, String> headers;
21+
22+
public PostRequest(@NotNull Consumer<OutputStream> body, @NotNull Map<String, String> headers, long length) {
23+
this.body = body;
24+
if (length >= 0) {
25+
headers = new HashMap<>(headers);
26+
headers.put("Content-Length", String.valueOf(length));
27+
headers = Collections.unmodifiableMap(headers);
28+
}
29+
this.headers = headers;
30+
}
31+
32+
public PostRequest(@NotNull Consumer<OutputStream> body, @NotNull Map<String, String> headers) {
33+
this(body, headers, -1);
34+
}
35+
36+
@NotNull
37+
public Consumer<OutputStream> getBody() {
38+
return body;
39+
}
40+
41+
@NotNull
42+
public Map<String, String> getHeaders() {
43+
return headers;
44+
}
45+
46+
@Contract("_, _, _ -> new")
47+
public static @NotNull PostRequest newRequest(Consumer<OutputStream> body, Map<String, String> headers, long length) {
48+
return new PostRequest(body, headers, length);
49+
}
50+
51+
@Contract("_, _ -> new")
52+
public static @NotNull PostRequest newRequest(@NotNull Consumer<OutputStream> body, Map<String, String> headers) {
53+
return new PostRequest(body, headers);
54+
}
55+
56+
@Contract("_, _ -> new")
57+
public static @NotNull PostRequest newRequest(@NotNull String bodyText, @NotNull Map<String, String> headers) {
58+
return newRequest(bodyText.getBytes(StandardCharsets.UTF_8), headers);
59+
}
60+
61+
@Contract("_, _ -> new")
62+
public static @NotNull PostRequest newRequest(byte[] bodyByte, @NotNull Map<String, String> headers) {
63+
return newRequest(out -> {
64+
try (BufferedOutputStream outst = new BufferedOutputStream(out)) {
65+
outst.write(bodyByte);
66+
} catch (IOException e) {
67+
throw new RuntimeException(e);
68+
}
69+
}, headers, bodyByte.length);
70+
}
71+
72+
public static class HeaderBuilder {
73+
@NotNull
74+
private final Map<String, String> headerMap = new HashMap<>();
75+
76+
@Contract(" -> new")
77+
public static @NotNull HeaderBuilder newBuilder() {
78+
return new HeaderBuilder();
79+
}
80+
81+
public Map<String, String> build() {
82+
return Collections.unmodifiableMap(headerMap);
83+
}
84+
85+
public HeaderBuilder addHeader(@NotNull String key, @NotNull String entry) {
86+
headerMap.put(key, entry);
87+
return this;
88+
}
89+
90+
public HeaderBuilder addHeader(@NotNull Map<String, String> headers) {
91+
this.headerMap.putAll(headers);
92+
return this;
93+
}
94+
95+
public HeaderBuilder setLanguage(@NotNull String language) {
96+
addHeader("Accept-Language", language);
97+
return this;
98+
}
99+
100+
public HeaderBuilder setContentType(@NotNull String contentType) {
101+
setContentType(contentType, null);
102+
return this;
103+
}
104+
105+
public HeaderBuilder setContentType(@NotNull String contentType, @Nullable String charset) {
106+
addHeader("Content-Type", String.format("%s; ", contentType) + (charset != null ? String.format("charset=%s", charset) : ""));
107+
return this;
108+
}
109+
}
110+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.felnull.fnjl.io;
2+
3+
import java.io.UnsupportedEncodingException;
4+
5+
public class PostResponse {
6+
private final byte[] response;
7+
private final int code;
8+
private final String encoding;
9+
10+
11+
public PostResponse(byte[] response, int code, String encoding) {
12+
this.response = response;
13+
this.code = code;
14+
this.encoding = encoding;
15+
}
16+
17+
public byte[] getResponse() {
18+
return response;
19+
}
20+
21+
public int getCode() {
22+
return code;
23+
}
24+
25+
public String getEncoding() {
26+
return encoding;
27+
}
28+
29+
public String getResponseString() {
30+
try {
31+
return new String(response, encoding);
32+
} catch (UnsupportedEncodingException e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
}

common/src/main/java/dev/felnull/fnjl/util/DiscordWebHookBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ public String createContent() {
4545
}
4646

4747
public int send() throws IOException {
48-
return FNURLUtil.getResponseByPOST(new URL(url), createContent(), "jp", "application/JSON").getEntry();
48+
return FNURLUtil.getResponseByPOST(new URL(url), createContent(), "jp", "application/JSON").getCode();
4949
}
5050

5151
public CompletableFuture<Void> sendAsync(Consumer<Integer> response) throws IOException {
5252
return FNURLUtil.getResponseByPOSTAsync(new URL(url), createContent(), "jp", "application/JSON", n -> {
5353
if (response != null)
54-
response.accept(n.getRight());
54+
response.accept(n.getCode());
5555
});
5656
}
57+
58+
public static DiscordWebHookBuilder newBuilder(String url, String content) {
59+
return new DiscordWebHookBuilder(url, content);
60+
}
5761
}

common/src/main/java/dev/felnull/fnjl/util/FNDataUtil.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.io.*;
77
import java.net.HttpURLConnection;
88
import java.net.URL;
9+
import java.nio.charset.Charset;
10+
import java.nio.charset.StandardCharsets;
911
import java.nio.file.Path;
1012
import java.nio.file.WatchEvent;
1113
import java.security.MessageDigest;
@@ -25,6 +27,52 @@
2527
* @since 1.0
2628
*/
2729
public class FNDataUtil {
30+
/**
31+
* InputStreamを文字列へ変換
32+
*
33+
* @param stream 対象ストリーム
34+
* @return 変換後
35+
* @throws IOException 例外
36+
*/
37+
public static String readAllString(InputStream stream) throws IOException {
38+
return readAllString(stream, StandardCharsets.UTF_8);
39+
}
40+
41+
/**
42+
* InputStreamを文字列へ変換
43+
*
44+
* @param stream 対象ストリーム
45+
* @param cs 文字コード
46+
* @return 変換後
47+
* @throws IOException 例外
48+
*/
49+
public static String readAllString(InputStream stream, Charset cs) throws IOException {
50+
try (Reader reader = new InputStreamReader(stream, cs)) {
51+
return readAllString(reader);
52+
}
53+
}
54+
55+
/**
56+
* Readerを文字列へ変換
57+
*
58+
* @param reader  リーダー
59+
* @return 変換後
60+
* @throws IOException 例外
61+
*/
62+
public static String readAllString(Reader reader) throws IOException {
63+
StringBuilder sb = new StringBuilder();
64+
boolean flg = false;
65+
try (BufferedReader breader = new BufferedReader(reader)) {
66+
String next;
67+
while ((next = breader.readLine()) != null) {
68+
if (flg)
69+
sb.append('\n');
70+
sb.append(next);
71+
flg = true;
72+
}
73+
}
74+
return sb.toString();
75+
}
2876

2977
/**
3078
* バッファー付きストリームをバイト配列へ変換

0 commit comments

Comments
 (0)