Java Code Examples for org.elasticsearch.common.xcontent.XContentHelper#convertToJson()

The following examples show how to use org.elasticsearch.common.xcontent.XContentHelper#convertToJson() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLXContentSourceParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void parseSource(BytesReference source) throws SQLParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            parse(parser);
        }
        validate();
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SQLParseException("Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 2
Source File: SQLRequestParser.java    From crate with Apache License 2.0 6 votes vote down vote up
public static SQLRequestParseContext parseSource(BytesReference source) throws IOException {
    if (source.length() == 0) {
        throw new SQLParseException("Missing request body");
    }
    XContentParser parser = null;
    try {
        SQLRequestParseContext parseContext = new SQLRequestParseContext();
        parser = XContentFactory.xContent(XContentType.JSON).createParser(
            NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(source));
        parse(parseContext, parser);
        validate(parseContext);
        return parseContext;
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, XContentType.JSON);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SQLParseException("Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 3
Source File: SearchRequestBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    if (sourceBuilder != null) {
        return sourceBuilder.toString();
    }
    if (request.source() != null) {
        try {
            return XContentHelper.convertToJson(request.source().toBytesArray(), false, true);
        } catch (Exception e) {
            return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
        }
    }
    return new SearchSourceBuilder().toString();
}
 
Example 4
Source File: ImportParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Main method of this class to parse given payload of _export action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ImportContext context, BytesReference source) throws ImportParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    ImportParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new ImportParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new ImportParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 5
Source File: JsonContent.java    From elasticsearch-dataformat with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(final SearchResponse response) {
    final String scrollId = response.getScrollId();
    final SearchHits hits = response.getHits();
    final int size = hits.getHits().length;
    currentCount += size;
    if (logger.isDebugEnabled()) {
        logger.debug("scrollId: {}, totalHits: {}, hits: {}, current: {}",
                scrollId, hits.getTotalHits(), size, currentCount);
    }
    try {
        for (final SearchHit hit : hits) {
            final String index = bulkIndex == null ? hit.getIndex()
                    : bulkIndex;
            final String operation = "{\"index\":{\"_index\":\"" + index
                    + "\",\"_id\":\"" + hit.getId() + "\"}}";
            final String source = XContentHelper.convertToJson(
                    hit.getSourceRef(), true, false, XContentType.JSON);
            writer.append(operation).append('\n');
            writer.append(source).append('\n');
        }

        if (size == 0 || scrollId == null) {
            // end
            writer.flush();
            close();
            listener.onResponse(null);
        } else {
            client.prepareSearchScroll(scrollId)
                    .setScroll(RequestUtil.getScroll(request))
                    .execute(this);
        }
    } catch (final Exception e) {
        onFailure(e);
    }
}
 
Example 6
Source File: ExportRequest.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + sSource + "]";
}
 
Example 7
Source File: TermsByQueryRequest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * String representation of the request
 */
@Override
public String toString() {
  String sSource = "_na_";
  try {
    sSource = XContentHelper.convertToJson(querySource, false);
  } catch (Exception e) {
    // ignore
  }
  return Arrays.toString(indices) + Arrays.toString(types) + "[" + field + "], querySource[" + sSource + "]";
}
 
Example 8
Source File: SearchIntoRequest.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(
            types) + ", querySource[" + sSource + "]";
}
 
Example 9
Source File: AbstractConfigurationValidator.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
private boolean checkDatatypes() throws Exception {
    String contentAsJson = XContentHelper.convertToJson(content, false, XContentType.JSON);
    try (JsonParser parser = factory.createParser(contentAsJson)) {
        JsonToken token = null;
        while ((token = parser.nextToken()) != null) {
            if (token.equals(JsonToken.FIELD_NAME)) {
                String currentName = parser.getCurrentName();
                DataType dataType = allowedKeys.get(currentName);
                if (dataType != null) {
                    JsonToken valueToken = parser.nextToken();
                    switch (dataType) {
                        case STRING:
                            if (!valueToken.equals(JsonToken.VALUE_STRING)) {
                                wrongDatatypes.put(currentName, "String expected");
                            }
                            break;
                        case ARRAY:
                            if (!valueToken.equals(JsonToken.START_ARRAY) && !valueToken.equals(JsonToken.END_ARRAY)) {
                                wrongDatatypes.put(currentName, "Array expected");
                            }
                            break;
                        case OBJECT:
                            if (!valueToken.equals(JsonToken.START_OBJECT) && !valueToken.equals(JsonToken.END_OBJECT)) {
                                wrongDatatypes.put(currentName, "Object expected");
                            }
                            break;
                    }
                }
            }
        }
        return wrongDatatypes.isEmpty();
    }
}
 
Example 10
Source File: PutIndexedScriptRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "index {[" + ScriptService.SCRIPT_INDEX + "][" + scriptLang + "][" + id + "], source[" + sSource + "]}";
}
 
Example 11
Source File: ValidateQueryRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", source[" + sSource + "], explain:" + explain + 
            ", rewrite:" + rewrite;
}
 
Example 12
Source File: CountRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", source[" + sSource + "]";
}
 
Example 13
Source File: CountRequestBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    if (sourceBuilder != null) {
        return sourceBuilder.toString();
    }
    if (request.source() != null) {
        try {
            return XContentHelper.convertToJson(request.source().toBytesArray(), false, true);
        } catch (Exception e) {
            return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
        }
    }
    return new QuerySourceBuilder().toString();
}
 
Example 14
Source File: ExistsRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", source[" + sSource + "]";
}
 
Example 15
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String sourceAsString() {
    if (source == null) {
        return null;
    }
    try {
        return XContentHelper.convertToJson(sourceRef(), false);
    } catch (IOException e) {
        throw new ElasticsearchParseException("failed to convert source to a json string");
    }
}
 
Example 16
Source File: ExportParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Main method of this class to parse given payload of _export action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ExportContext context, BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 17
Source File: Utils.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public static String convertStructuredMapToJson(Map<String, Object> structuredMap) {
    try {
        return XContentHelper.convertToJson(convertStructuredMapToBytes(structuredMap), false, XContentType.JSON);
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to convert map", e);
    }
}
 
Example 18
Source File: AbstractSearchIntoParser.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Main method of this class to parse given payload of _search_into action
 *
 * @param context
 * @param source
 * @throws org.elasticsearch.search.SearchParseException
 *
 */
public void parseSource(SearchIntoContext context,
        BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    try {
        if (source != null) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token
                    .END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = getElementParsers().get(
                            fieldName);
                    if (element == null) {
                        throw new SearchParseException(context,
                                "No parser for element [" + fieldName +
                                        "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context,
                "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 19
Source File: RestoreParser.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Main method of this class to parse given payload of _restore action
 *
 * @param context
 * @param source
 * @throws org.elasticsearch.search.SearchParseException
 */
public void parseSource(ImportContext context, BytesReference source) throws ImportParseException {
    XContentParser parser = null;
    this.setDefaults(context);
    context.settings(true);
    context.mappings(true);
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    ImportParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new ImportParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        if (context.directory() == null) {
            context.directory(DumpParser.DEFAULT_DIR);
        }
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new ImportParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 20
Source File: DumpParser.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Main method of this class to parse given payload of _dump action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ExportContext context, BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    this.setDefaults(context);
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        if (context.outputFile() == null) {
            directoryParseElement.setOutPutFile(context, DEFAULT_DIR);
            this.ensureDefaultDirectory(context);
        }
        context.mappings(true);
        context.settings(true);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}