summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/FormatInputStream.java
blob: 36cdf18e102886440dedac85087f8f52124c79a9 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.runner;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.format.DataFormatDetector;
import com.fasterxml.jackson.core.format.DataFormatMatcher;
import com.fasterxml.jackson.core.format.MatchStrength;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

/**
 * @author valerijf
 */
public class FormatInputStream {

    private InputStream inputStream;
    private Format format;

    /**
     * Creates a single data input stream from either file or InputStream depending on which one is present. Preference
     * for file if both present. Additionally also detects input data format of the result stream, throws
     * IllegalArgumentException if unable to determine data format.
     *
     * @param stream              InputStream of the data if present
     * @param inputFile           Path to file to use as input
     * @param addRootElementToXml To add vespafeed root element around the input data stream
     * @throws IOException        on errors.
     */
    public FormatInputStream(InputStream stream, Optional<String> inputFile, boolean addRootElementToXml)
            throws IOException {
        final DataFormatDetector dataFormatDetector = new DataFormatDetector(new JsonFactory(), new XmlFactory());
        final DataFormatMatcher formatMatcher;

        if (inputFile.isPresent()) {
            try (FileInputStream fileInputStream = new FileInputStream(inputFile.get())) {
                formatMatcher = dataFormatDetector.findFormat(fileInputStream);
            }
            inputStream = new FileInputStream(inputFile.get());

        } else {
            if (stream.available() == 0) {
                System.out.println("No data in stream yet and no file specified, waiting for data.");
            }

            inputStream = stream.markSupported() ? stream : new BufferedInputStream(stream);
            inputStream.mark(DataFormatDetector.DEFAULT_MAX_INPUT_LOOKAHEAD);
            formatMatcher = dataFormatDetector.findFormat(inputStream);
            inputStream.reset();
        }

        if (addRootElementToXml) {
            inputStream = addVespafeedTag(inputStream);
            format = Format.XML;
            return;
        }

        if (formatMatcher.getMatchStrength() == MatchStrength.INCONCLUSIVE ||
                formatMatcher.getMatchStrength() == MatchStrength.NO_MATCH) {
            throw new IllegalArgumentException("Could not detect input format");
        }

        switch (formatMatcher.getMatchedFormatName().toLowerCase()) {
            case "json":
                format = Format.JSON;
                break;

            case "xml":
                format = Format.XML;
                break;

            default:
                throw new IllegalArgumentException("Unknown data format");
        }
    }

    private static InputStream addVespafeedTag(InputStream inputStream) {
        return new SequenceInputStream(Collections.enumeration(Arrays.asList(
                new ByteArrayInputStream("<vespafeed>".getBytes()),
                inputStream,
                new ByteArrayInputStream("</vespafeed>".getBytes())))
        );
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public Format getFormat() {
        return format;
    }

    public enum Format {
        JSON, XML
    }

}