forked from Viber/viber-bot-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.java
More file actions
121 lines (103 loc) · 3.81 KB
/
Copy pathRequest.java
File metadata and controls
121 lines (103 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.viber.bot;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
import com.viber.bot.event.incoming.IncomingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.WillClose;
import javax.annotation.WillNotClose;
import javax.annotation.concurrent.ThreadSafe;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static com.google.common.base.Preconditions.checkNotNull;
@ThreadSafe
public class Request implements Closeable {
private static final Logger logger = LoggerFactory.getLogger(Request.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final String EMPTY_JSON_OBJECT = "{}";
private final AtomicBoolean isClosed = new AtomicBoolean(false);
private final AtomicReference<InputStream> responseInputStream = new AtomicReference<>();
private final IncomingEvent event;
private Request(final String json) {
logger.debug("Reading json request: {}", json);
event = readJson(json, IncomingEvent.class);
}
/**
* Creates a Request object from an InputStream.
* Be noted that the InputStream will be closed by this method.
*
* @param inputStream the InputStream
* @return Request
*/
public static Request fromInputStream(final @WillClose @Nonnull InputStream inputStream) {
final String json = new String(readInputStream(inputStream), Charsets.UTF_8);
Closeables.closeQuietly(inputStream);
return Request.fromJsonString(json);
}
/**
* Creates a Request object from a json string literal.
*
* @param json string
* @return Request
*/
public static Request fromJsonString(final @Nullable String json) {
return new Request(MoreObjects.firstNonNull(Strings.nullToEmpty(json), EMPTY_JSON_OBJECT));
}
private static byte[] readInputStream(final @Nonnull InputStream inputStream) {
final byte[] data;
try {
data = ByteStreams.toByteArray(inputStream);
} catch (final IOException e) {
logger.error("Could not read input stream", e);
throw new RuntimeException(e);
}
return data;
}
private static <T> T readJson(final String json, final Class<T> clazz) {
final T event;
try {
event = objectMapper.readValue(json, clazz);
} catch (final Exception e) {
logger.error("Could not read incoming event", e);
throw new RuntimeException(e);
}
return event;
}
public IncomingEvent getEvent() {
return event;
}
public boolean isClosed() {
return isClosed.get();
}
public void setResponse(final String response) {
setResponse(new ByteArrayInputStream(response.getBytes(Charsets.UTF_16)));
}
public void setResponse(final @Nonnull @WillNotClose InputStream inputStream) {
responseInputStream.set(checkNotNull(inputStream));
}
@Nullable
public InputStream getResponseInputStream() {
return responseInputStream.get();
}
@Override
public void close() throws IOException {
synchronized (this) {
if (!isClosed.compareAndSet(false, true)) return;
try {
notify();
} catch (final IllegalMonitorStateException e) {
logger.error("Could not notify object", e);
}
}
}
}