Exemplo n.º 1
0
  /*
   * TODO Replace this version with a method that uses LaxValidationCases and provides means to access the errors after validating the document
   */
  public static boolean validateDocument(final XmlObject doc) throws OwsExceptionReport {
    // Create an XmlOptions instance and set the error listener.
    final LinkedList<XmlError> validationErrors = new LinkedList<XmlError>();
    final XmlOptions validationOptions =
        new XmlOptions()
            .setErrorListener(validationErrors)
            .setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT);

    // Validate the GetCapabilitiesRequest XML document
    final boolean isValid = doc.validate(validationOptions);

    // Create Exception with error message if the xml document is invalid
    if (!isValid) {

      String message = null;

      // getValidation error and throw service exception for the first
      // error
      final Iterator<XmlError> iter = validationErrors.iterator();
      final List<XmlError> shouldPassErrors = new LinkedList<XmlError>();
      final List<XmlError> errors = new LinkedList<XmlError>();
      while (iter.hasNext()) {
        final XmlError error = iter.next();
        boolean shouldPass = false;
        if (error instanceof XmlValidationError) {
          for (final LaxValidationCase lvc : LaxValidationCase.values()) {
            if (lvc.shouldPass((XmlValidationError) error)) {
              shouldPass = true;
              LOGGER.debug("Lax validation case found for XML validation error: {}", error);
              break;
            }
          }
        }
        if (shouldPass) {
          shouldPassErrors.add(error);
        } else {
          errors.add(error);
        }
      }
      final CompositeOwsException exceptions = new CompositeOwsException();
      for (final XmlError error : errors) {

        // get name of the missing or invalid parameter
        message = error.getMessage();
        if (message != null) {

          exceptions.add(
              new InvalidRequestException()
                  .at(message)
                  .withMessage("[XmlBeans validation error:] %s", message));

          /*
           * TODO check if code can be used for validation of SOS
           * 1.0.0 requests // check, if parameter is missing or value
           * of parameter // is // invalid to ensure, that correct //
           * exceptioncode in exception response is used
           *
           * // invalid parameter value if
           * (message.startsWith("The value")) { exCode =
           * OwsExceptionCode.InvalidParameterValue;
           *
           * // split message string to get attribute name String[]
           * messAndAttribute = message.split("attribute '"); if
           * (messAndAttribute.length == 2) { parameterName =
           * messAndAttribute[1].replace("'", ""); } }
           *
           * // invalid enumeration value --> InvalidParameterValue
           * else if
           * (message.contains("not a valid enumeration value")) {
           * exCode = OwsExceptionCode.InvalidParameterValue;
           *
           * // get attribute name String[] messAndAttribute =
           * message.split(" "); parameterName = messAndAttribute[10];
           * }
           *
           * // mandatory attribute is missing --> //
           * missingParameterValue else if
           * (message.startsWith("Expected attribute")) { exCode =
           * OwsExceptionCode.MissingParameterValue;
           *
           * // get attribute name String[] messAndAttribute =
           * message.split("attribute: "); if (messAndAttribute.length
           * == 2) { String[] attrAndRest =
           * messAndAttribute[1].split(" in"); if (attrAndRest.length
           * == 2) { parameterName = attrAndRest[0]; } } }
           *
           * // mandatory element is missing --> //
           * missingParameterValue else if
           * (message.startsWith("Expected element")) { exCode =
           * SwesExceptionCode.InvalidRequest;
           *
           * // get element name String[] messAndElements =
           * message.split(" '"); if (messAndElements.length >= 2) {
           * String elements = messAndElements[1]; if
           * (elements.contains("offering")) { parameterName =
           * "offering"; } else if
           * (elements.contains("observedProperty")) { parameterName =
           * "observedProperty"; } else if
           * (elements.contains("responseFormat")) { parameterName =
           * "responseFormat"; } else if
           * (elements.contains("procedure")) { parameterName =
           * "procedure"; } else if
           * (elements.contains("featureOfInterest")) { parameterName
           * = "featureOfInterest"; } else { // TODO check if other
           * elements are invalid } } } // invalidParameterValue else
           * if (message.startsWith("Element")) { exCode =
           * OwsExceptionCode.InvalidParameterValue;
           *
           * // get element name String[] messAndElements =
           * message.split(" '"); if (messAndElements.length >= 2) {
           * String elements = messAndElements[1]; if
           * (elements.contains("offering")) { parameterName =
           * "offering"; } else if
           * (elements.contains("observedProperty")) { parameterName =
           * "observedProperty"; } else if
           * (elements.contains("responseFormat")) { parameterName =
           * "responseFormat"; } else if
           * (elements.contains("procedure")) { parameterName =
           * "procedure"; } else if
           * (elements.contains("featureOfInterest")) { parameterName
           * = "featureOfInterest"; } else { // TODO check if other
           * elements are invalid } } } else { // create service
           * exception OwsExceptionReport se = new
           * OwsExceptionReport();
           * se.addCodedException(SwesExceptionCode.InvalidRequest,
           * message, "[XmlBeans validation error:] " + message);
           * LOGGER.error("The request is invalid!", se); throw se; }
           *
           * // create service exception OwsExceptionReport se = new
           * OwsExceptionReport(); se.addCodedException(exCode,
           * message, "[XmlBeans validation error:] " + message);
           * LOGGER.error("The request is invalid!", se); throw se;
           */

        }
      }
      exceptions.throwIfNotEmpty();
    }
    return isValid;
  }
Exemplo n.º 2
0
  /**
   * parses the String representing the getCapabilities request and creates a SosGetCapabilities
   * request
   *
   * @param decoderKeyType the matched <code>KvpDecoderKeyType</code>
   * @param element String with getCapabilities parameters
   * @return Returns SosGetCapabilitiesRequest representing the request
   * @throws OwsExceptionReport If parsing the String failed
   */
  @Override
  public GetCapabilitiesRequest decode(Map<String, String> element) throws OwsExceptionReport {

    GetCapabilitiesRequest request = new GetCapabilitiesRequest();
    CompositeOwsException exceptions = new CompositeOwsException();

    for (String parameterName : element.keySet()) {
      String parameterValues = element.get(parameterName);
      try {
        if (!parseDefaultParameter(request, parameterValues, parameterName)) {
          //                    // service (mandatory SOS 1.0.0, SOS 2.0 default)
          //                    if
          // (parameterName.equalsIgnoreCase(OWSConstants.RequestParams.service.name())) {
          //
          // request.setService(KvpHelper.checkParameterSingleValue(parameterValues,
          // parameterName));
          //                    } // request (mandatory)
          //                    else if
          // (parameterName.equalsIgnoreCase(OWSConstants.RequestParams.request.name())) {
          //                        KvpHelper.checkParameterSingleValue(parameterValues,
          // parameterName);
          //                    } // acceptVersions (optional)
          //                    else
          if (parameterName.equalsIgnoreCase(
              SosConstants.GetCapabilitiesParams.AcceptVersions.name())) {
            if (!parameterValues.isEmpty()) {
              request.setAcceptVersions(
                  Arrays.asList(parameterValues.split(Constants.COMMA_STRING)));
            } else {
              exceptions.add(new MissingParameterValueException(parameterName));
            }
          }
          // acceptFormats (optional)
          else if (parameterName.equalsIgnoreCase(
              SosConstants.GetCapabilitiesParams.AcceptFormats.name())) {
            request.setAcceptFormats(
                KvpHelper.checkParameterMultipleValues(parameterValues, parameterName));
          }
          // updateSequence (optional)
          else if (parameterName.equalsIgnoreCase(
              SosConstants.GetCapabilitiesParams.updateSequence.name())) {
            request.setUpdateSequence(
                KvpHelper.checkParameterSingleValue(parameterValues, parameterName));
          }
          // sections (optional)
          else if (parameterName.equalsIgnoreCase(
              SosConstants.GetCapabilitiesParams.Sections.name())) {
            request.setSections(
                KvpHelper.checkParameterMultipleValues(parameterValues, parameterName));
          }
          // capabilitiesId (optional; non-standard)
          else if (parameterName.equalsIgnoreCase(
              SosConstants.GetCapabilitiesParams.CapabilitiesId.name())) {
            request.setCapabilitiesId(
                KvpHelper.checkParameterSingleValue(parameterValues, parameterName));
          } else {
            exceptions.add(new ParameterNotSupportedException(parameterName));
          }
        }
      } catch (OwsExceptionReport owse) {
        exceptions.add(owse);
      }
    }
    exceptions.throwIfNotEmpty();

    return request;
  }