/**
  * Checks whether the object with the given PID has a datastream with the given identifier
  * (dsName).
  *
  * @param pid the fedora persistent identifier for the object whose datastreams are being queried
  * @param dsName the identifier for the datastream
  * @return true if the datastream exists, false otherwise
  * @throws IOException if an error occurs while accessing fedora
  * @throws FedoraException
  */
 public boolean hasDatastream(String pid, String dsName) throws IOException, FedoraException {
   GetMethod get =
       new GetMethod(this.fedoraBaseUrl + "/objects/" + pid + "/datastreams?format=xml");
   this.client.executeMethod(get);
   return (readStream(get.getResponseBodyAsStream(), get.getResponseCharSet())
           .indexOf("dsid=\"" + dsName + "\"")
       != -1);
 }
  /*
   * Tests the <tt>withBody(String)</tt> method in connection with the default content type
   * (which is set to text/html; charset=UTF-8). Checks the content type was set to the stub response
   * and the body is readable to the http client.
   */
  @Test
  public void withDefaultContentType() throws IOException {
    onRequest().respond().withBody(STRING_WITH_DIACRITICS);

    final GetMethod method = new GetMethod("http://localhost:" + port());

    client.executeMethod(method);

    // the content type header set to the default value
    assertThat(method.getResponseHeader("Content-Type").getValue(), is(UTF_8_TYPE));

    // the http client was able to retrieve the charset portion of the content type header
    assertThat(method.getResponseCharSet(), is(UTF_8_CHARSET.name()));

    // since the body was encoded in UTF-8 and content type charset was set to UTF-8,
    // the http client should be able to read it correctly
    assertThat(method.getResponseBodyAsString(), is(STRING_WITH_DIACRITICS));
  }
  public GeoRSSReader createReader(final URL url, final String username, final String password)
      throws IOException {

    if (log.isDebugEnabled()) {
      log.debug(
          "Creating GeoRSS reader for URL " + url.toExternalForm() + " with user " + username);
    }

    HttpClientBuilder builder = new HttpClientBuilder();
    builder.setHttpCredentials(username, password, url);
    builder.setBackendTimeout(120);

    HttpClient httpClient = builder.buildClient();

    GetMethod getMethod = new GetMethod(url.toString());
    getMethod.setRequestHeader("Connection", "close");
    if (builder.isDoAuthentication()) {
      getMethod.setDoAuthentication(true);
      httpClient.getParams().setAuthenticationPreemptive(true);
    }

    if (log.isDebugEnabled()) {
      log.debug("Executing HTTP GET requesr for feed URL " + url.toExternalForm());
    }
    httpClient.executeMethod(getMethod);

    if (log.isDebugEnabled()) {
      log.debug("Building GeoRSS reader out of URL response");
    }
    String contentEncoding = getMethod.getResponseCharSet();
    if (contentEncoding == null) {
      contentEncoding = "UTF-8";
    }

    InputStream in = getMethod.getResponseBodyAsStream();
    Reader reader = new BufferedReader(new InputStreamReader(in, contentEncoding));
    if (log.isDebugEnabled()) {
      log.debug("GeoRSS reader created, returning.");
    }
    return createReader(reader);
  }
  /*
   * Tests a mismatch between body encoding and the encoding stated in the content type header.
   * Body is encoded using ISO-8859-2, however the content type header states it's encoded
   * using UTF-8.
   */
  @Test
  public void withContentTypeEncodingMismatch() throws IOException {
    onRequest()
        .respond()
        .withEncoding(ISO_8859_2_CHARSET)
        .withContentType(UTF_8_TYPE)
        .withBody(STRING_WITH_DIACRITICS);

    final GetMethod method = new GetMethod("http://localhost:" + port());

    client.executeMethod(method);

    // the content type header set to the specified value
    assertThat(method.getResponseHeader("Content-Type").getValue(), is(UTF_8_TYPE));

    // the http client was able to retrieve the charset portion of the content type header
    assertThat(method.getResponseCharSet(), is(UTF_8_CHARSET.name()));

    // however the applied encoding is ISO-8859-2
    final byte[] body = IOUtils.toByteArray(method.getResponseBodyAsStream());
    assertThat(body, is(ISO_8859_2_REPRESENTATION));
  }