/* ------------------- Support methods for readToFile ------------------- */
  private File remoteReproject(
      WcsReaderRequest request, File containingDirectory, String baseFilename)
      throws NoSuchAuthorityCodeException, FactoryException, IOException {
    InputStream input = null;
    try {
      BoundWcsRequest requestNegotiatedFormat = negotiateFormat(request.bind(_wcsUrl));
      BoundWcsRequest requestNegotiatedFormatCrs = negotiateRequestCRS(requestNegotiatedFormat);
      BoundWcsRequest requestNegotiatedFormatCrs2 =
          negotiateResponseCRS(requestNegotiatedFormatCrs);
      requestNegotiatedFormatCrs2.assertLegalSize(_maxCoverageExtractionSize);

      input = requestNegotiatedFormatCrs2.getCoverage();

      // file = new File (new File("/tmp/"),
      // baseFilename+"."+request.fileExtension());
      File file = null;
      file = new File(containingDirectory, baseFilename + "." + request.fileExtension());
      LOG.debug("Writing GridCoverage obtained from " + _wcsUrl + " to file " + file);

      convertFormat(baseFilename, input, file, request, requestNegotiatedFormatCrs2);

      transformCoverage(file, file, request, requestNegotiatedFormatCrs2, false);

      return file;
    } finally {
      if (input != null) IOUtils.closeQuietly(input);
    }
  }
  private BoundWcsRequest negotiateFormat(BoundWcsRequest request) throws IOException {
    final Set<String> formats = request.getSupportedFormats();
    if (formats.isEmpty()) return request;
    if (!formats.contains(request.format)) {
      for (String format : Formats.preferredFormats) {
        if (formats.contains(format)) return request.withFormat(format);
      }

      // Note: If none of the preferred formats are offered by server
      // then we download a format and see if we can convert it to the
      // desired format
      return request.withFormat(formats.iterator().next());
    }
    return request;
  }
 private BoundWcsRequest negotiateRequestCRS(BoundWcsRequest request)
     throws IOException, FactoryException {
   Set<String> crss = request.getSupportedRequestCRSs();
   String requestCrs =
       "EPSG:" + CRS.lookupEpsgCode(request.requestBbox.getCoordinateReferenceSystem(), true);
   if (crss.isEmpty()) return request;
   if (!crss.contains(requestCrs)) {
     ReferencedEnvelope newBBox = null;
     for (String crs : crss) {
       try {
         newBBox = request.requestBbox.transform(CRS.decode(crs), true, 10);
       } catch (Exception e) {
         // try next crs
       }
     }
     if (newBBox != null && !crss.isEmpty()) {
       return request.withRequestBBox(newBBox);
     }
   }
   return request;
 }
  private File localReproject(
      WcsReaderRequest request, File containingDirectory, String baseFilename)
      throws NoSuchAuthorityCodeException, FactoryException, IOException {
    InputStream input = null;
    File tmpFile = null;
    try {
      BoundWcsRequest geotiffRequest = request.bind(_wcsUrl).withFormat("geotiff");
      BoundWcsRequest requestNativeFormat =
          geotiffRequest.withCRS(geotiffRequest.getNativeCRSs().iterator().next());
      requestNativeFormat.assertLegalSize(_maxCoverageExtractionSize);

      input = requestNativeFormat.getCoverage();

      File file = null;
      file = new File(containingDirectory, baseFilename + "." + request.fileExtension());
      LOG.debug("Writing GridCoverage obtained from " + _wcsUrl + " to file " + file);

      tmpFile = File.createTempFile(baseFilename, ".tif");
      writeToFile(tmpFile, input);

      transformCoverage(tmpFile, file, request, requestNativeFormat, true);
      return file;
    } finally {
      if (tmpFile != null) FileUtils.delete(tmpFile);

      if (input != null) IOUtils.closeQuietly(input);
    }
  }
  private BoundWcsRequest negotiateResponseCRS(BoundWcsRequest request) throws IOException {

    Set<String> crss = request.getSupportedResponseCRSs();
    // Hack. It will work so long as backing servers
    // are GeoServers
    if (crss.isEmpty() && request.getNativeCRSs().isEmpty()) return request;
    if (!crss.contains(request.getResponseEpsgCode())) {
      String newCrs = "EPSG:4326";
      if (request.getNativeCRSs().isEmpty()) {
        Iterator<String> crsIter = crss.iterator();
        if (!crss.contains(newCrs) && crsIter.hasNext()) {
          newCrs = crsIter.next();
        } else {

        }
      } else {
        newCrs = request.getNativeCRSs().iterator().next();
      }
      return request.withCRS(newCrs);
    }
    return request;
  }
  @Test
  public void BoundWcsRequestTest() throws Throwable {
    WcsReaderRequest rq =
        new WcsReaderRequestFactory()
            .create(
                "1.0",
                "myCov",
                0,
                0,
                1,
                1,
                CRS.decode("EPSG:4326"),
                CRS.decode("EPSG:2154"),
                1,
                "GeoTiff",
                true,
                true,
                true,
                "scott",
                "tiger");

    BoundWcsRequest bwr = new BoundWcsRequest(new URL("http://localhost/"), rq);

    HttpClient mockClient = getMockHttpClient();
    bwr.setHttpClient(mockClient);

    // DescribeCoverage related tests
    setOutputDocument(describeCoverageSample);
    bwr.getCoverage();

    // supported formats
    Set<String> fmts = bwr.getSupportedFormats();
    String[] expectedFmts = {"jpg", "geotiff", "tif", "jpeg", "png", "gif", "tiff"};
    assertArrayEquals(expectedFmts, fmts.toArray());

    // supported response CRS
    Set<String> supportedResponseCrs = bwr.getSupportedResponseCRSs();
    String[] expectedResponseCrs = {"EPSG:4326"};
    assertArrayEquals(expectedResponseCrs, supportedResponseCrs.toArray());

    // num bands
    int numBands = bwr.numBands();
    assertEquals(numBands, 1);

    // supported request CRS
    Set<String> supportedRequestCrs = bwr.getSupportedRequestCRSs();
    String[] expectedRequestCrs = {"EPSG:4326"};
    assertArrayEquals(expectedRequestCrs, supportedRequestCrs.toArray());

    // Native CRS
    Set<String> nativeCRSs = bwr.getNativeCRSs();
    String[] expectedNativeCRSs = {"EPSG:4326"};
    assertArrayEquals(expectedNativeCRSs, nativeCRSs.toArray());

    // just to recall getDescribeCoverage(), should be defined as for now
    assertTrue(bwr.getDescribeCoverage() instanceof String);

    // Same
    assertTrue(bwr.getSupportedFormats() instanceof Set<?>);
    assertTrue(bwr.getSupportedResponseCRSs() instanceof Set<?>);
    assertTrue(bwr.numBands() == 1);
    assertTrue(bwr.getSupportedRequestCRSs() instanceof Set<?>);
    assertTrue(bwr.getNativeCRSs() instanceof Set<?>);

    // GetCapabilities related tests
    setOutputDocument(getCapabilitiesSample);

    String getCap = bwr.getCapabilities();
    assertTrue(getCap instanceof String);
    // another time
    assertTrue(bwr.getCapabilities() instanceof String);
  }