/**
  * validates the passed <tt>DescribeCoverage</tt> against the passed <tt>WCSCapabilities</tt>
  *
  * @param capabilities
  * @param request
  * @throws InvalidParameterValueException
  */
 private static void validate(WCSCapabilities capabilities, DescribeCoverage request)
     throws InvalidParameterValueException {
   String[] coverages = request.getCoverages();
   if (coverages != null) {
     ContentMetadata cm = capabilities.getContentMetadata();
     for (int i = 0; i < coverages.length; i++) {
       if (cm.getCoverageOfferingBrief(coverages[i]) == null) {
         throw new InvalidParameterValueException(
             "Coverage: " + coverages[i] + "is not known by the WCS");
       }
     }
   }
 }
  /**
   * validates the passed <tt>GetCoverage</tt> against the passed <tt>WCSCapabilities</tt>
   *
   * @param capabilities
   * @param request
   * @throws InvalidParameterValueException
   */
  private static void validate(WCSCapabilities capabilities, GetCoverage request)
      throws InvalidParameterValueException {
    String coverage = request.getSourceCoverage();
    ContentMetadata cm = capabilities.getContentMetadata();
    // is coverage known by the WCS?
    CoverageOfferingBrief cob = cm.getCoverageOfferingBrief(coverage);
    if (cob == null) {
      throw new InvalidParameterValueException(
          "Coverage: " + coverage + " is not known by the WCS");
    }

    URL url = cob.getConfiguration();
    CoverageDescription cd = null;
    try {
      cd = CoverageDescription.createCoverageDescription(url);
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      throw new InvalidParameterValueException(e.getMessage());
    }
    CoverageOffering co = cd.getCoverageOffering(coverage);
    if (co == null) {
      throw new InvalidParameterValueException(
          "no coverage descrition " + "available for requested coverage: " + coverage);
    }
    // validate requested format
    String format = request.getOutput().getFormat().getCode();
    SupportedFormats sf = co.getSupportedFormats();
    CodeList[] codeList = sf.getFormats();
    if (!validate(codeList, null, format)) {
      throw new InvalidParameterValueException(
          "requested format: " + format + " is not known by the WCS for coverage:" + coverage);
    }
    // validate requested response CRS
    String crs = request.getOutput().getCrs().getCode();
    URI codeSpace = request.getOutput().getCrs().getCodeSpace();
    String space = null;
    if (codeSpace != null) {
      space = codeSpace.toString();
    }

    CodeList[] rrcrs = co.getSupportedCRSs().getRequestResponseSRSs();
    CodeList[] rescrs = co.getSupportedCRSs().getResponseSRSs();
    if (!validate(rrcrs, space, crs) && !validate(rescrs, space, crs)) {
      throw new InvalidParameterValueException(
          "requested response CRS: "
              + crs
              + " is not known by the WCS "
              + "for coverage:"
              + coverage);
    }
    // validate requested CRS
    crs = request.getDomainSubset().getRequestSRS().getCode();
    codeSpace = request.getDomainSubset().getRequestSRS().getCodeSpace();
    if (codeSpace != null) {
      space = codeSpace.toString();
    }
    CodeList[] reqcrs = co.getSupportedCRSs().getRequestSRSs();

    if (!validate(rrcrs, space, crs) && !validate(reqcrs, space, crs)) {
      throw new InvalidParameterValueException(
          "requested request CRS: " + crs + " is not known by the WCS for coverage:" + coverage);
    }
    // validate requested envelope
    Envelope envelope = request.getDomainSubset().getSpatialSubset().getEnvelope();
    LonLatEnvelope llEnv = cob.getLonLatEnvelope();
    Envelope[] domEnvs = co.getDomainSet().getSpatialDomain().getEnvelops();

    try {
      if (!intersects(envelope, request.getDomainSubset().getRequestSRS(), domEnvs, llEnv)) {
        throw new InvalidParameterValueException(
            "requested BBOX: doesn't intersect "
                + " the area of the requested coverage: "
                + coverage);
      }
    } catch (UnknownCRSException e) {
      throw new InvalidParameterValueException(e);
    }
  }