Example usage for org.apache.solr.common.util ContentStream getSize

List of usage examples for org.apache.solr.common.util ContentStream getSize

Introduction

In this page you can find the example usage for org.apache.solr.common.util ContentStream getSize.

Prototype

Long getSize();

Source Link

Usage

From source file:lux.solr.XQueryComponent.java

License:Mozilla Public License

private void handleContentStreams(LinkedTreeBuilder builder, SolrQueryRequest req, ArrayList<XdmItem> result,
        Evaluator evaluator) throws XPathException {
    // parts//from  ww  w  .jav  a 2 s  . c o  m
    int i = 0;
    for (ContentStream stream : req.getContentStreams()) {
        String contentType = stream.getContentType();
        //String name = stream.getName();
        byte[] partBytes = null;
        try {
            partBytes = IOUtils.toByteArray(stream.getStream(), stream.getSize());
        } catch (IOException e) {
            throw new LuxException(e);
        }
        String charset = ContentStreamBase.getCharsetFromContentType(contentType);
        if (charset == null) {
            charset = "utf-8";
        }
        if (!isText(contentType)) {
            logger.warn("Binary values not supported; treating " + contentType + " as xml, or text");
        }
        XdmItem part = null;
        if (isXML(contentType) || !isText(contentType)) {
            try {
                part = evaluator.build(new ByteArrayInputStream(partBytes), "#part" + i);
            } catch (LuxException e) {
                // failed to parse
                logger.warn("Caught an exception while parsing XML: " + e.getMessage()
                        + ", treating it as plain text");
                contentType = "text/plain; charset=" + charset;
            }
        }
        if (part == null) {
            String text;
            try {
                text = new String(partBytes, charset);
            } catch (UnsupportedEncodingException e1) {
                throw new LuxException(e1);
            }
            if (isHTML(contentType)) {
                HtmlParser parser = new HtmlParser();
                //Parser parser = new Parser();
                SAXSource source = new SAXSource(parser, new InputSource(new StringReader(text)));
                try {
                    part = evaluator.getDocBuilder().build(source);
                } catch (SaxonApiException e) {
                    e.printStackTrace();
                    logger.warn("failed to parse HTML; treating as plain text: " + e.getMessage());
                }
            }
            if (part == null) {
                TextFragmentValue node = new TextFragmentValue(text, "#part" + i);
                node.setConfiguration(builder.getConfiguration());
                part = new XdmNode(node);
            }
        }
        result.add(part);
        builder.startElement(fQNameFor("http", EXPATH_HTTP_NS, "body"), BuiltInAtomicType.UNTYPED_ATOMIC, 0, 0);
        addAttribute(builder, "position", "1");
        addAttribute(builder, "content-type", contentType);
        builder.startContent();
        builder.endElement();
    }
}

From source file:org.vootoo.client.netty.util.ProtobufUtil.java

License:Apache License

public static List<SolrProtocol.ContentStream> toProtobufContentStreams(
        Collection<ContentStream> contentStreams) {
    return Lists.transform(Lists.newArrayList(contentStreams),
            new Function<ContentStream, SolrProtocol.ContentStream>() {
                @Override//from ww  w .  j a  v a 2  s .  co m
                public SolrProtocol.ContentStream apply(ContentStream input) {
                    SolrProtocol.ContentStream.Builder stream = SolrProtocol.ContentStream.newBuilder();

                    if (input.getName() != null) {
                        stream.setName(input.getName());
                    }
                    String contentType = input.getContentType();
                    if (contentType == null) {
                        // default javabin
                        contentType = BinaryResponseParser.BINARY_CONTENT_TYPE;
                    }
                    stream.setContentType(contentType);
                    stream.setSourceInfo(input.getSourceInfo());
                    stream.setSize(input.getSize());
                    try {
                        //TODO zero copy byte
                        stream.setStream(ByteString.readFrom(input.getStream()));
                    } catch (IOException e) {
                        //TODO dealing input.getStream() exception
                        throw new RuntimeException("ContentStream.getStream() exception", e);
                    }

                    return stream.build();
                }
            });
}