aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/notification/MultiPartEventForwarder.java
blob: 6227c66cceed37bbd1cde6ab52209c622f655478 (plain)
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
package org.traccar.notification;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.traccar.Context;
import org.traccar.model.Event;
import org.traccar.model.Position;

import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.multipart.StringPart;

public class MultiPartEventForwarder extends EventForwarder {

    private final String payloadParamName;
    private final String additionalParams;

    public MultiPartEventForwarder() {
        payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload");
        additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams");
    }

    @Override
    protected String getContentType() {
        return "multipart/form-data";
    }

    @Override
    protected void setContent(Event event, Position position, Set<Long> users, BoundRequestBuilder requestBuilder) {

        if (additionalParams != null && !additionalParams.isEmpty()) {
            Map<String, List<String>> paramsToAdd = splitIntoKeyValues(additionalParams, "=");

            for (Entry<String, List<String>> param : paramsToAdd.entrySet()) {
                for (String singleParamValue : param.getValue()) {
                    requestBuilder.addBodyPart(new StringPart(param.getKey(), singleParamValue, null,
                            StandardCharsets.UTF_8));
                }
            }
        }
        requestBuilder.addBodyPart(new StringPart(payloadParamName,
                prepareJsonPayload(event, position, users), "application/json", StandardCharsets.UTF_8));
    }
}