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.
  */
 
 
 import java.net.URL;
Facilitates testing Solr's REST API via a provided embedded Jetty
 
 public class RestTestHarness extends BaseTestHarness {
   
   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 {
     URL url = new URL(getBaseURL() + request);
     HttpURLConnection connection = (HttpURLConnection)url.openConnection();
     InputStream inputStream = null;
     StringWriter strWriter;
     try {
       try {
         inputStream = connection.getInputStream();
       } catch (IOException e) {
         inputStream = connection.getErrorStream();
       }
       strWriter = new StringWriter();
       IOUtils.copy(new InputStreamReader(inputStream"UTF-8"), strWriter);
     } finally {
       IOUtils.closeQuietly(inputStream);
    }
    return strWriter.toString();
  }

  
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 {
    URL url = new URL(getBaseURL() + request);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT");
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    out.write(content);
    out.close();
    InputStream inputStream = null;
    StringWriter stringWriter;
    try {
      try {
        inputStream = connection.getInputStream();
      } catch (IOException e) {
        inputStream = connection.getErrorStream();
      }
      stringWriter = new StringWriter();
      IOUtils.copy(new InputStreamReader(inputStream"UTF-8"), stringWriter);
    } finally {
      IOUtils.closeQuietly(inputStream);
    }
    return stringWriter.toString();
  }

  
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 {
    URL url = new URL(getBaseURL() + request);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type""application/json; charset=utf-8");
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    out.write(content);
    out.close();
    InputStream inputStream = null;
    StringWriter stringWriter;
    try {
      try {
        inputStream = connection.getInputStream();
      } catch (IOException e) {
        inputStream = connection.getErrorStream();
      }
      stringWriter = new StringWriter();
      IOUtils.copy(new InputStreamReader(inputStream"UTF-8"), stringWriter);
    } finally {
      IOUtils.closeQuietly(inputStream);
    }
    return stringWriter.toString();
  }
  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);
    }
  }
  
  public void reload() throws Exception {
    String xml = checkResponseStatus("/admin/cores?action=RELOAD""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);
    }
  }
New to GrepCode? Check out our FAQ X