// 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.server;

import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.json.JsonFeedReader;
import com.yahoo.vespa.http.client.config.FeedParams;
import com.yahoo.vespaxmlparser.FeedReader;
import com.yahoo.vespaxmlparser.VespaXMLFeedReader;

import java.io.InputStream;

/**
 * Class for creating FeedReader based on dataFormat.
 * @author dybis
 */
public class FeedReaderFactory {

    /**
     * Creates FeedReader
     * @param inputStream source of feed data
     * @param docTypeManager handles the parsing of the document
     * @param dataFormat specifies the format
     * @return a feedreader
     */
    public FeedReader createReader(
            InputStream inputStream,
            DocumentTypeManager docTypeManager,
            FeedParams.DataFormat dataFormat)  {
        switch (dataFormat) {
            case XML_UTF8:
                try {
                    return new VespaXMLFeedReader(inputStream, docTypeManager);
                } catch (Exception e) {
                    throw new RuntimeException("Could not create VespaXMLFeedReader", e);
                }
            case JSON_UTF8:
                return new JsonFeedReader(inputStream, docTypeManager);
            default:
                throw new IllegalStateException("Can not create feed reader for format: " + dataFormat);
        }
    }

}

@@ -236,32 +236,60 @@
 JsonFeedReader;%0A
+import com.yahoo.text.Utf8;%0A
 import com.yahoo
@@ -419,24 +419,52 @@
 eedReader;%0A%0A
+import java.io.IOException;%0A
 import java.
@@ -1185,24 +1185,328 @@
 eption e) %7B%0A
+                    byte %5B%5D peek;%0A                    try %7B%0A                        peek = new byte%5BMath.min(200, inputStream.available())%5D;%0A                        inputStream.read(peek);%0A                    %7D catch (IOException  io) %7B%0A                        peek = new byte %5B0%5D;%0A                    %7D%0A
             
@@ -1576,17 +1576,63 @@
 edReader
-%22
+. First characters are: %22 + Utf8.toString(peek)
 , e);%0A  

Add first 200 bytes of message to xml exception