private void parseResponseBody() throws IOException {
   if (body != null) {
     String contentType = response.getHeader("Content-Type");
     XContentType xContentType = XContentType.fromMediaTypeOrFormat(contentType);
     // skip parsing if we got text back (e.g. if we called _cat apis)
     if (xContentType == XContentType.JSON || xContentType == XContentType.YAML) {
       this.parsedResponse = ObjectPath.createFromXContent(xContentType.xContent(), body);
     }
   }
 }
  public void testFromXContent() throws IOException {
    SearchSortValues sortValues = createTestItem();
    XContentType xcontentType = randomFrom(XContentType.values());
    XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
    if (randomBoolean()) {
      builder.prettyPrint();
    }
    builder
        .startObject(); // we need to wrap xContent output in proper object to create a parser for
    // it
    builder = sortValues.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();

    XContentParser parser = createParser(builder);
    parser
        .nextToken(); // skip to the elements field name token, fromXContent advances from there if
    // called from ourside
    parser.nextToken();
    if (sortValues.sortValues().length > 0) {
      SearchSortValues parsed = SearchSortValues.fromXContent(parser);
      assertToXContentEquivalent(
          builder.bytes(), toXContent(parsed, xcontentType, true), xcontentType);
      parser.nextToken();
    }
    assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
    assertNull(parser.nextToken());
  }
  @Override
  public XContentBuilder newBuilder(@Nullable BytesReference autoDetectSource, boolean useFiltering)
      throws IOException {
    XContentType contentType = XContentType.fromMediaTypeOrFormat(format);
    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;
    }

    Set<String> includes = Collections.emptySet();
    Set<String> excludes = Collections.emptySet();
    if (useFiltering) {
      Set<String> filters = Strings.splitStringByCommaToSet(filterPath);
      includes = filters.stream().filter(INCLUDE_FILTER).collect(toSet());
      excludes = filters.stream().filter(EXCLUDE_FILTER).map(f -> f.substring(1)).collect(toSet());
    }

    XContentBuilder builder =
        new XContentBuilder(
            XContentFactory.xContent(contentType), bytesOutput(), includes, excludes);
    if (pretty) {
      builder.prettyPrint().lfAtEnd();
    }

    builder.humanReadable(human);
    return builder;
  }
  @Inject
  public LocalGatewayMetaState(
      Settings settings,
      ThreadPool threadPool,
      NodeEnvironment nodeEnv,
      TransportNodesListGatewayMetaState nodesListGatewayMetaState,
      LocalAllocateDangledIndices allocateDangledIndices,
      NodeIndexDeletedAction nodeIndexDeletedAction)
      throws Exception {
    super(settings);
    this.nodeEnv = nodeEnv;
    this.threadPool = threadPool;
    this.format = XContentType.fromRestContentType(settings.get("format", "smile"));
    this.allocateDangledIndices = allocateDangledIndices;
    this.nodeIndexDeletedAction = nodeIndexDeletedAction;
    nodesListGatewayMetaState.init(this);

    if (this.format == XContentType.SMILE) {
      Map<String, String> params = Maps.newHashMap();
      params.put("binary", "true");
      formatParams = new ToXContent.MapParams(params);
      Map<String, String> globalOnlyParams = Maps.newHashMap();
      globalOnlyParams.put("binary", "true");
      globalOnlyParams.put(MetaData.PERSISTENT_ONLY_PARAM, "true");
      globalOnlyParams.put(MetaData.GLOBAL_ONLY_PARAM, "true");
      globalOnlyFormatParams = new ToXContent.MapParams(globalOnlyParams);
    } else {
      formatParams = ToXContent.EMPTY_PARAMS;
      Map<String, String> globalOnlyParams = Maps.newHashMap();
      globalOnlyParams.put(MetaData.PERSISTENT_ONLY_PARAM, "true");
      globalOnlyParams.put(MetaData.GLOBAL_ONLY_PARAM, "true");
      globalOnlyFormatParams = new ToXContent.MapParams(globalOnlyParams);
    }

    this.autoImportDangled =
        AutoImportDangledState.fromString(
            settings.get(
                "gateway.local.auto_import_dangled", AutoImportDangledState.YES.toString()));
    this.danglingTimeout =
        settings.getAsTime("gateway.local.dangling_timeout", TimeValue.timeValueHours(2));

    logger.debug(
        "using gateway.local.auto_import_dangled [{}], with gateway.local.dangling_timeout [{}]",
        this.autoImportDangled,
        this.danglingTimeout);

    if (DiscoveryNode.masterNode(settings)) {
      try {
        pre019Upgrade();
        long start = System.currentTimeMillis();
        loadState();
        logger.debug(
            "took {} to load state", TimeValue.timeValueMillis(System.currentTimeMillis() - start));
      } catch (Exception e) {
        logger.error("failed to read local state, exiting...", e);
        throw e;
      }
    }
  }
 // https://github.com/elasticsearch/elasticsearch/issues/7240
 public void testEmptyBooleanQuery() throws Exception {
   XContentBuilder contentBuilder =
       XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   BytesReference query =
       contentBuilder.startObject().startObject("bool").endObject().endObject().bytes();
   Query parsedQuery = parseQuery(query).toQuery(createShardContext());
   assertThat(parsedQuery, Matchers.instanceOf(MatchAllDocsQuery.class));
 }
Ejemplo n.º 6
0
 public static RestResponse buildResponse(Table table, RestRequest request, RestChannel channel)
     throws Exception {
   XContentType xContentType =
       XContentType.fromRestContentType(request.param("format", request.header("Content-Type")));
   if (xContentType != null) {
     return buildXContentBuilder(table, request, channel);
   }
   return buildTextPlainResponse(table, request, channel);
 }
Ejemplo n.º 7
0
 public static RestResponse buildResponse(Table table, RestChannel channel) throws Exception {
   RestRequest request = channel.request();
   XContentType xContentType =
       XContentType.fromMediaTypeOrFormat(request.param("format", request.header("Accept")));
   if (xContentType != null) {
     return buildXContentBuilder(table, channel);
   }
   return buildTextPlainResponse(table, channel);
 }
Ejemplo n.º 8
0
 /**
  * Returns a random source containing a random number of fields, objects and array, with maximum
  * depth 5.
  *
  * @param random Random generator
  */
 public static BytesReference randomSource(Random random) {
   // the source can be stored in any format and eventually converted when retrieved depending on
   // the format of the response
   XContentType xContentType = RandomPicks.randomFrom(random, XContentType.values());
   try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
     builder.startObject();
     addFields(random, builder, 0);
     builder.endObject();
     return builder.bytes();
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 9
0
 public void testFromXContent() throws Exception {
   SliceBuilder sliceBuilder = randomSliceBuilder();
   XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   if (randomBoolean()) {
     builder.prettyPrint();
   }
   builder.startObject();
   sliceBuilder.innerToXContent(builder);
   builder.endObject();
   XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes());
   QueryParseContext context =
       new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT);
   SliceBuilder secondSliceBuilder = SliceBuilder.fromXContent(context);
   assertNotSame(sliceBuilder, secondSliceBuilder);
   assertEquals(sliceBuilder, secondSliceBuilder);
   assertEquals(sliceBuilder.hashCode(), secondSliceBuilder.hashCode());
 }
 /**
  * Test that creates new smoothing model from a random test smoothing model and checks both for
  * equality
  */
 public void testFromXContent() throws IOException {
   SmoothingModel testModel = createTestModel();
   XContentBuilder contentBuilder =
       XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   if (randomBoolean()) {
     contentBuilder.prettyPrint();
   }
   contentBuilder.startObject();
   testModel.innerToXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
   contentBuilder.endObject();
   XContentParser parser = XContentHelper.createParser(shuffleXContent(contentBuilder).bytes());
   QueryParseContext context =
       new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT);
   parser.nextToken(); // go to start token, real parsing would do that in the outer element parser
   SmoothingModel parsedModel = fromXContent(context);
   assertNotSame(testModel, parsedModel);
   assertEquals(testModel, parsedModel);
   assertEquals(testModel.hashCode(), parsedModel.hashCode());
 }
  /**
   * creates random suggestion builder, renders it to xContent and back to new instance that should
   * be equal to original
   */
  public void testFromXContent() throws IOException {
    Suggesters suggesters = new Suggesters(Collections.emptyMap());
    QueryParseContext context = new QueryParseContext(null);
    context.parseFieldMatcher(new ParseFieldMatcher(Settings.EMPTY));
    for (int runs = 0; runs < NUMBER_OF_RUNS; runs++) {
      SuggestBuilder suggestBuilder = createTestModel();
      XContentBuilder xContentBuilder =
          XContentFactory.contentBuilder(randomFrom(XContentType.values()));
      if (randomBoolean()) {
        xContentBuilder.prettyPrint();
      }
      suggestBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
      XContentParser parser = XContentHelper.createParser(xContentBuilder.bytes());
      context.reset(parser);

      SuggestBuilder secondSuggestBuilder = SuggestBuilder.fromXContent(context, suggesters);
      assertNotSame(suggestBuilder, secondSuggestBuilder);
      assertEquals(suggestBuilder, secondSuggestBuilder);
      assertEquals(suggestBuilder.hashCode(), secondSuggestBuilder.hashCode());
    }
  }
 /**
  * Generic test that creates new AggregatorFactory from the test AggregatorFactory and checks both
  * for equality and asserts equality on the two queries.
  */
 public void testFromXContent() throws IOException {
   AF testAgg = createTestAggregatorFactory();
   AggregatorFactories.Builder factoriesBuilder =
       AggregatorFactories.builder().skipResolveOrder().addPipelineAggregator(testAgg);
   logger.info("Content string: {}", factoriesBuilder);
   XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   if (randomBoolean()) {
     builder.prettyPrint();
   }
   factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
   XContentBuilder shuffled = shuffleXContent(builder);
   XContentParser parser =
       XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes());
   QueryParseContext parseContext =
       new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
   String contentString = factoriesBuilder.toString();
   logger.info("Content string: {}", contentString);
   assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
   assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken());
   assertEquals(testAgg.getName(), parser.currentName());
   assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
   assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken());
   assertEquals(testAgg.type(), parser.currentName());
   assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
   PipelineAggregationBuilder newAgg =
       aggParsers
           .pipelineParser(testAgg.getWriteableName(), ParseFieldMatcher.STRICT)
           .parse(testAgg.getName(), parseContext);
   assertSame(XContentParser.Token.END_OBJECT, parser.currentToken());
   assertSame(XContentParser.Token.END_OBJECT, parser.nextToken());
   assertSame(XContentParser.Token.END_OBJECT, parser.nextToken());
   assertNull(parser.nextToken());
   assertNotNull(newAgg);
   assertNotSame(newAgg, testAgg);
   assertEquals(testAgg, newAgg);
   assertEquals(testAgg.hashCode(), newAgg.hashCode());
 }