Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.solr.util;
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 
Facilitates testing Solr's REST API via a provided embedded Jetty
 
 public class RestTestHarness extends BaseTestHarness {
   private HttpClient httpClient = HttpClientUtil.createClient(new
       ModifiableSolrParams());
   
   public RestTestHarness(RESTfulServerProvider serverProvider) {
     this. = serverProvider;
   }
   
   public String getBaseURL() {
     return .getBaseURL();
   }
  
  
Validates an XML "query" response against an array of XPath test strings

Parameters:
request the Query to process
Returns:
null if all good, otherwise the first test that fails.
Throws:
java.lang.Exception any exception in the response.
java.io.IOException if there is a problem writing the XML
 
   public String validateQuery(String requestString... teststhrows Exception {
 
     String res = query(request);
     return validateXPath(restests);
   }


  
Validates an XML PUT response against an array of XPath test strings

Parameters:
request the PUT request to process
content the content to send with the PUT request
tests the validating XPath tests
Returns:
null if all good, otherwise the first test that fails.
Throws:
java.lang.Exception any exception in the response.
java.io.IOException if there is a problem writing the XML
 
   public String validatePut(String requestString contentString... teststhrows Exception {
 
     String res = put(requestcontent);
     return validateXPath(restests);
   }


  
Processes a "query" using a URL path (with no context path) + optional query params, e.g. "/schema/fields?indent=on"

Parameters:
request the URL path and optional query params
Returns:
The response to the query
Throws:
java.lang.Exception any exception in the response.
 
   public String query(String requestthrows Exception {
     return getResponse(new HttpGet(getBaseURL() + request));
   }

  
Processes a PUT request using a URL path (with no context path) + optional query params, e.g. "/schema/fields/newfield", PUTs the given content, and returns the response content.

Parameters:
request The URL path and optional query params
content The content to include with the PUT request
Returns:
The response to the PUT request
  public String put(String requestString contentthrows IOException {
    HttpPut httpPut = new HttpPut(getBaseURL() + request);
    httpPut.setEntity(new StringEntity(content, ContentType.create(
        "application/json".)));
    
    return getResponse(httpPut);
  }

  
Processes a DELETE request using a URL path (with no context path) + optional query params, e.g. "/schema/analysis/protwords/english", and returns the response content.

Parameters:
request the URL path and optional query params
Returns:
The response to the DELETE request
  public String delete(String requestthrows IOException {
    HttpDelete httpDelete = new HttpDelete(getBaseURL() + request);
    return getResponse(httpDelete);
  }

  
Processes a POST request using a URL path (with no context path) + optional query params, e.g. "/schema/fields/newfield", PUTs the given content, and returns the response content.

Parameters:
request The URL path and optional query params
content The content to include with the POST request
Returns:
The response to the POST request
  public String post(String requestString contentthrows IOException {
    HttpPost httpPost = new HttpPost(getBaseURL() + request);
    httpPost.setEntity(new StringEntity(content, ContentType.create(
        "application/json".)));
    
    return getResponse(httpPost);
  }
  public String checkResponseStatus(String xmlString codethrows Exception {
    try {
      String response = query(xml);
      String valid = validateXPath(response"//int[@name='status']="+code );
      return (null == valid) ? null : response;
    } catch (XPathExpressionException e) {
      throw new RuntimeException("?!? static xpath has bug?"e);
    }
  }


  
Reloads the first core listed in the response to the core admin handler STATUS command
  public void reload() throws Exception {
    String coreName = (String)evaluateXPath
        (query("/admin/cores?action=STATUS"),
         "//lst[@name='status']/lst[1]/str[@name='name']",
         .);
    String xml = checkResponseStatus("/admin/cores?action=RELOAD&core=" + coreName"0");
    if (null != xml) {
      throw new RuntimeException("RELOAD failed:\n" + xml);
    }
  }

  
Processes an "update" (add, commit or optimize) and returns the response as a String.

Parameters:
xml The XML of the update
Returns:
The XML response to the update
  public String update(String xml) {
    try {
      return query("/update?stream.body=" + URLEncoder.encode(xml"UTF-8"));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  
Executes the given request and returns the response.
  private String getResponse(HttpUriRequest requestthrows IOException {
    HttpEntity entity = null;
    try {
      entity = .execute(request).getEntity();
      return EntityUtils.toString(entity.);
    } finally {
      EntityUtils.consumeQuietly(entity);
    }
  }
New to GrepCode? Check out our FAQ X