Java Code Examples for org.elasticsearch.common.xcontent.XContentFactory#xContentType()

The following examples show how to use org.elasticsearch.common.xcontent.XContentFactory#xContentType() . 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: RestSearchScrollAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    String scrollId = request.param("scroll_id");
    SearchScrollRequest searchScrollRequest = new SearchScrollRequest();
    searchScrollRequest.scrollId(scrollId);
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }

    if (RestActions.hasBodyContent(request)) {
        XContentType type = XContentFactory.xContentType(RestActions.getRestContent(request));
        if (type == null) {
            if (scrollId == null) {
                scrollId = RestActions.getRestContent(request).toUtf8();
                searchScrollRequest.scrollId(scrollId);
            }
        } else {
            // NOTE: if rest request with xcontent body has request parameters, these parameters override xcontent values
            buildFromContent(RestActions.getRestContent(request), searchScrollRequest);
        }
    }
    client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<SearchResponse>(channel));
}
 
Example 2
Source File: CsvXContentGenerator.java    From elasticsearch-rest-command with The Unlicense 6 votes vote down vote up
@Override
public final void writeRawField(String fieldName, BytesReference content, OutputStream bos) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType != null) {
        writeObjectRaw(fieldName, content, bos);
    } else {
        writeFieldName(fieldName);
        // we could potentially optimize this to not rely on exception logic...
        String sValue = content.toUtf8();
        try {
            writeNumber(Long.parseLong(sValue));
        } catch (NumberFormatException e) {
            try {
                writeNumber(Double.parseDouble(sValue));
            } catch (NumberFormatException e1) {
                writeString(sValue);
            }
        }
    }
}
 
Example 3
Source File: AggregationBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(getName());

    if (this.metaData != null) {
        builder.field("meta", this.metaData);
    }
    builder.field(type);
    internalXContent(builder, params);

    if (aggregations != null || aggregationsBinary != null) {

        if (aggregations != null) {
            builder.startObject("aggregations");
            for (AbstractAggregationBuilder subAgg : aggregations) {
                subAgg.toXContent(builder, params);
            }
            builder.endObject();
        }

        if (aggregationsBinary != null) {
            if (XContentFactory.xContentType(aggregationsBinary) == builder.contentType()) {
                builder.rawField("aggregations", aggregationsBinary);
            } else {
                builder.field("aggregations_binary", aggregationsBinary);
            }
        }

    }

    return builder.endObject();
}
 
Example 4
Source File: RestChannel.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public XContentBuilder newBuilder(@Nullable BytesReference autoDetectSource, boolean useFiltering) throws IOException {
    XContentType contentType = XContentType.fromRestContentType(request.param("format", request.header("Content-Type")));
    if (contentType == null) {
        // try and guess it from the auto detect source
        if (autoDetectSource != null) {
            contentType = XContentFactory.xContentType(autoDetectSource);
        }
    }
    if (contentType == null) {
        // default to JSON
        contentType = XContentType.JSON;
    }

    String[] filters = useFiltering ? request.paramAsStringArrayOrEmptyIfAll("filter_path") :  null;
    XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), bytesOutput(), filters);
    if (request.paramAsBoolean("pretty", false)) {
        builder.prettyPrint().lfAtEnd();
    }

    builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable()));

    String casing = request.param("case");
    if (casing != null) {
        String msg = "Parameter 'case' has been deprecated, all responses will use underscore casing in the future";
        DEPRECATION_LOGGER.deprecated(msg);
    }
    if (casing != null && "camelCase".equals(casing)) {
        builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE);
    } else {
        // we expect all REST interfaces to write results in underscore casing, so
        // no need for double casing
        builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE);
    }
    return builder;
}
 
Example 5
Source File: QuerySourceBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void innerToXContent(XContentBuilder builder, Params params) throws IOException {
    if (queryBuilder != null) {
        builder.field("query");
        queryBuilder.toXContent(builder, params);
    }

    if (queryBinary != null) {
        if (XContentFactory.xContentType(queryBinary) == builder.contentType()) {
            builder.rawField("query", queryBinary);
        } else {
            builder.field("query_binary", queryBinary);
        }
    }
}
 
Example 6
Source File: ContentBuilderUtil.java    From ElasticsearchSink2 with Apache License 2.0 5 votes vote down vote up
public static void appendField(XContentBuilder builder, String field,
    byte[] data) throws IOException {
  XContentType contentType = XContentFactory.xContentType(data);
  if (contentType == null) {
    addSimpleField(builder, field, data);
  } else {
    addComplexField(builder, field, contentType, data);
  }
}
 
Example 7
Source File: XmlFilter.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
@Override
public void sendResponse(RestResponse response) {
    if (!response.status().equals(RestStatus.OK)) {
        channel.sendResponse(response);
    }
    if (isXml(request)) {
        XContentParser parser = null;
        try {
            String string = response.content().toUtf8(); // takes some space ... :(
            XContentType xContentType = XContentFactory.xContentType(string);
            parser = XContentFactory.xContent(xContentType).createParser(string);
            parser.nextToken();
            XmlXContentBuilder builder = XmlXContentFactory.xmlBuilder(params);
            if (request.paramAsBoolean("pretty", false)) {
                builder.prettyPrint();
            }
            builder.copyCurrentStructure(parser);
            BytesRestResponse restResponse = new BytesRestResponse(RestStatus.OK, "text/xml; charset=UTF-8", builder.bytes());
            channel.sendResponse(restResponse);
            return;
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
            channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, e.getMessage()));
            return;
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    }
    channel.sendResponse(response);
}
 
Example 8
Source File: ContentBuilderUtil.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
public static void appendField(XContentBuilder builder, String field,
    byte[] data) throws IOException {
  XContentType contentType = XContentFactory.xContentType(data);
  if (contentType == null) {
    addSimpleField(builder, field, data);
  } else {
    addComplexField(builder, field, contentType, data);
  }
}
 
Example 9
Source File: ContentBuilderUtil.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public static void appendField(XContentBuilder builder, String field,
    byte[] data) throws IOException {
  XContentType contentType = XContentFactory.xContentType(data);
  if (contentType == null) {
    addSimpleField(builder, field, data);
  } else {
    addComplexField(builder, field, contentType, data);
  }
}
 
Example 10
Source File: IndexAuthenticator.java    From elasticsearch-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void login(final RestRequest request,
        final ActionListener<String[]> listener) {
    String username = request.param(usernameKey);
    String password = request.param(passwordKey);
    final BytesReference content = request.content();
    final XContentType xContentType = XContentFactory.xContentType(content);
    XContentParser parser = null;
    try {
        parser = XContentFactory.xContent(xContentType).createParser(
                content);
        final XContentParser.Token t = parser.nextToken();
        if (t != null) {
            final Map<String, Object> contentMap = parser.map();
            username = MapUtil.getAsString(contentMap, usernameKey,
                    username);
            password = MapUtil.getAsString(contentMap, passwordKey,
                    password);
        }
    } catch (final Exception e) {
        listener.onFailure(e);
        return;
    } finally {
        if (parser != null) {
            parser.close();
        }
    }

    if (username == null) {
        listener.onResponse(new String[0]);
        return;
    }

    processLogin(username, password, listener);

}
 
Example 11
Source File: AccountRestAction.java    From elasticsearch-auth with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleRequest(final RestRequest request,
        final RestChannel channel, final Client client) {
    final BytesReference content = request.content();
    final XContentType xContentType = XContentFactory.xContentType(content);
    XContentParser parser = null;
    String authenticator = null;
    String username = null;
    String password = null;
    String[] roles = null;
    try {
        parser = XContentFactory.xContent(xContentType).createParser(
                content);
        final XContentParser.Token t = parser.nextToken();
        if (t != null) {
            final Map<String, Object> contentMap = parser.map();
            authenticator = MapUtil.getAsString(contentMap,
                    "authenticator", null);
            username = MapUtil.getAsString(contentMap, "username", null);
            password = MapUtil.getAsString(contentMap, "password", null);
            roles = MapUtil.getAsArray(contentMap, "roles", new String[0]);
        }
    } catch (final Exception e) {
        logger.error("Could not parse the content.", e);
        ResponseUtil.send(request, channel, RestStatus.BAD_REQUEST,
                "message", "Could not parse the content.");
        return;
    } finally {
        if (parser != null) {
            parser.close();
        }
    }

    processRequest(request, channel, authenticator, username, password,
            roles);

}
 
Example 12
Source File: RestExtendedAnalyzeAction.java    From elasticsearch-extended-analyze with Apache License 2.0 5 votes vote down vote up
public static XContentType guessBodyContentType(final RestRequest request) {
    final BytesReference restContent = RestActions.getRestContent(request);
    if (restContent == null) {
        return null;
    }
    return XContentFactory.xContentType(restContent);
}