aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-core/src/main/java/com/yahoo/feedapi/Feeder.java
blob: 2b67df00e1211ae6c69c93ecc6870c17b9a7a008 (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
106
107
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.feedapi;

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.List;

import javax.xml.stream.XMLStreamException;

import com.yahoo.document.DocumentTypeManager;
import com.yahoo.vespaxmlparser.FeedOperation;
import com.yahoo.vespaxmlparser.FeedReader;

/**
 * Base class for unpacking document operation streams and pushing to feed
 * access points.
 *
 * @author Thomas Gundersen
 * @author steinar
 */
public abstract class Feeder {

    protected final InputStream stream;
    protected final DocumentTypeManager docMan;
    protected List<String> errors = new LinkedList<>();
    private boolean doAbort = true;
    private boolean createIfNonExistent = false;
    private final VespaFeedSender sender;
    private static final int MAX_ERRORS = 10;

    protected Feeder(DocumentTypeManager docMan, VespaFeedSender sender, InputStream stream) {
        this.docMan = docMan;
        this.sender = sender;
        this.stream = stream;
    }

    public void setAbortOnDocumentError(boolean doAbort) {
        this.doAbort = doAbort;
    }

    public void setCreateIfNonExistent(boolean value) {
        this.createIfNonExistent = value;
    }

    private void addException(Exception e) {
        String message;
        if (e.getMessage() != null) {
            message = e.getMessage().replaceAll("\"", "'");
        } else {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            message = "(no message) " + sw;
        }
        addError("ERROR: " + message);
    }

    private void addError(String error) {
        if (errors.size() < MAX_ERRORS) {
            errors.add(error);
        } else if (errors.size() == MAX_ERRORS) {
            errors.add("Reached maximum limit of errors (" + MAX_ERRORS + "). Not collecting any more.");
        }
    }

    protected abstract FeedReader createReader() throws Exception;

    public List<String> parse() {
        FeedReader reader;

        try {
            reader = createReader();
        } catch (Exception e) {
            addError("ERROR: " + e.getClass().toString() + ": " + e.getMessage().replaceAll("\"", "'"));
            return errors;
        }

        while (!sender.isAborted()) {
            try {
                FeedOperation op = reader.read();
                if (createIfNonExistent) {
                    if (op.getDocumentUpdate() != null) {
                        op.getDocumentUpdate().setCreateIfNonExistent(true);
                    }
                    if (op.getDocumentPut() != null) {
                        op.getDocumentPut().setCreateIfNonExistent(true);
                    }
                }
                if (op.getType() == FeedOperation.Type.INVALID) break; // Done feeding
                sender.sendOperation(op);
            } catch (XMLStreamException | NullPointerException e) {
                addException(e);
                break;
            } catch (Exception e) {
                addException(e);
                if (doAbort) {
                    break;
                }
            }
        }

        return errors;
    }

}