• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jhannes / logevents / #113

01 Aug 2024 12:20PM UTC coverage: 88.704% (-0.03%) from 88.735%
#113

push

jhannes
Regenerate JavaDoc

5693 of 6418 relevant lines covered (88.7%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/logevents/src/main/java/org/logevents/observers/AbstractHttpPostJsonLogEventObserver.java
1
package org.logevents.observers;
2

3
import org.logevents.config.Configuration;
4
import org.logevents.observers.batch.LogEventBatch;
5
import org.logevents.status.LogEventStatus;
6
import org.logevents.util.JsonUtil;
7
import org.logevents.util.NetUtils;
8

9
import java.io.IOException;
10
import java.net.InetSocketAddress;
11
import java.net.Proxy;
12
import java.net.URL;
13
import java.util.Map;
14
import java.util.function.Function;
15

16
/**
17
 * Convenience superclass for observers that send JSON over HTTP
18
 */
19
public abstract class AbstractHttpPostJsonLogEventObserver extends AbstractBatchingLogEventObserver {
20
    private final URL url;
21
    private Proxy proxy = Proxy.NO_PROXY;
1✔
22

23
    public AbstractHttpPostJsonLogEventObserver(URL url) {
1✔
24
        this.url = url;
1✔
25
    }
1✔
26

27
    public URL getUrl() {
28
        return url;
1✔
29
    }
30

31
    public Proxy getProxy() {
32
        return proxy;
1✔
33
    }
34

35
    public void configureProxy(Configuration configuration) {
36
        configuration.optionalString("proxy").ifPresent(proxyHost -> {
1✔
37
            int colonPos = proxyHost.lastIndexOf(':');
1✔
38
            String hostname = colonPos != -1 ? proxyHost.substring(0, colonPos) : proxyHost;
1✔
39
            int proxyPort = colonPos != -1 ? Integer.parseInt(proxyHost.substring(colonPos+1)) : 80;
1✔
40
            this.proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(hostname, proxyPort));
1✔
41
        });
1✔
42
    }
1✔
43

44
    @Override
45
    public void processBatch(LogEventBatch batch) {
46
        sendBatch(batch, this::formatBatch);
1✔
47
    }
1✔
48

49
    protected void sendBatch(LogEventBatch batch, Function<LogEventBatch, Map<String, Object>> formatter) {
50
        if (batch.isEmpty()) {
1✔
51
            return;
1✔
52
        }
53
        if (url == null) {
1✔
54
            LogEventStatus.getInstance().addInfo(this, "No url - batch discarded");
×
55
            return;
×
56
        }
57
        Map<String, Object> jsonMessage;
58
        try {
59
            jsonMessage = formatter.apply(batch);
1✔
60
        } catch (Exception e) {
1✔
61
            LogEventStatus.getInstance().addFatal(this, "Runtime error generating message", e);
1✔
62
            return;
1✔
63
        }
1✔
64
        try {
65
            String response = postJson(jsonMessage);
1✔
66
            LogEventStatus.getInstance().addTrace(this, "Sent message to " + url + ": " + response);
1✔
67
        } catch (IOException e) {
1✔
68
            LogEventStatus.getInstance().addError(this, "Failed to send message to " + url, e);
1✔
69
        }
1✔
70
    }
1✔
71

72
    /**
73
     * Override this method to customize how to execute the HTTP Post request.
74
     * For example, this is a good place to put authentication logic
75
     */
76
    protected String postJson(Map<String, Object> jsonMessage) throws IOException {
77
        return NetUtils.postJson(url, JsonUtil.toIndentedJson(jsonMessage), proxy);
1✔
78
    }
79

80
    /**
81
     * Override this method to customize how the {@link LogEventBatch} will be
82
     * formatted as JSON.
83
     */
84
    protected abstract Map<String, Object> formatBatch(LogEventBatch batch);
85

86
    @Override
87
    public String toString() {
88
        return getClass().getSimpleName() + "{url=" + url + '}';
×
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc