@Override
  public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
    if (value != null) {
      Wfs20FeatureCollection wfc = (Wfs20FeatureCollection) value;

      String schemaLoc = generateSchemaLocationFromMetacards(wfc.getMembers(), prefixToUriMapping);

      for (Entry<String, String> entry : prefixToUriMapping.entrySet()) {
        writer.addAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + entry.getKey(), entry.getValue());
      }
      writer.addAttribute(Wfs20Constants.ATTRIBUTE_SCHEMA_LOCATION, schemaLoc);

      Geometry allGeometry = getBounds(wfc.getMembers());
      if (!allGeometry.isEmpty()) {
        XmlNode.writeEnvelope(
            Wfs20Constants.GML_PREFIX + ":" + "boundedBy",
            context,
            writer,
            allGeometry.getEnvelopeInternal());
      }

      for (Metacard mc : wfc.getMembers()) {
        writer.startNode(Wfs20Constants.GML_PREFIX + ":" + FEATURE_MEMBER);
        context.convertAnother(mc);
        writer.endNode();
      }
    } else {
      LOGGER.warn("Incoming value was null");
    }
  }
 private Wfs20FeatureCollection addMetacardToFeatureCollection(
     Wfs20FeatureCollection featureCollection,
     UnmarshallingContext context,
     HierarchicalStreamReader reader) {
   featureCollection
       .getMembers()
       .add(
           (Metacard)
               context.convertAnother(
                   null, MetacardImpl.class, featureConverterMap.get(reader.getNodeName())));
   return featureCollection;
 }
Пример #3
0
  public WfsSource getWfsSource(
      final String schema,
      final FilterCapabilities filterCapabilities,
      final String srsName,
      final int numFeatures,
      final boolean throwExceptionOnDescribeFeatureType,
      boolean prefix,
      int numReturned)
      throws WfsException {

    // GetCapabilities Response
    when(mockWfs.getCapabilities(any(GetCapabilitiesRequest.class))).thenReturn(mockCapabilites);

    when(mockFeatureCollection.getMembers())
        .thenAnswer(
            new Answer<List<Metacard>>() {
              @Override
              public List<Metacard> answer(InvocationOnMock invocation) {
                // Create as many metacards as there are features
                List<Metacard> metacards = new ArrayList<Metacard>(numFeatures);
                for (int i = 0; i < numFeatures; i++) {
                  MetacardImpl mc = new MetacardImpl();
                  mc.setId("ID_" + String.valueOf(i + 1));
                  metacards.add(mc);
                }

                return metacards;
              }
            });

    if (numReturned != NULL_NUM_RETURNED) {
      when(mockFeatureCollection.getNumberReturned()).thenReturn(BigInteger.valueOf(numReturned));
    } else {
      when(mockFeatureCollection.getNumberReturned()).thenReturn(null);
    }

    when(mockWfs.getFeature(any(GetFeatureType.class))).thenReturn(mockFeatureCollection);
    mockCapabilites.setFilterCapabilities(filterCapabilities);

    when(mockAvailabilityTask.isAvailable()).thenReturn(true);

    mockCapabilites.setFeatureTypeList(new FeatureTypeListType());
    for (int ii = 0; ii < numFeatures; ii++) {
      FeatureTypeType feature = new FeatureTypeType();
      QName qName;
      if (prefix) {
        qName = new QName("http://example.com", SAMPLE_FEATURE_NAME + ii, "Prefix" + ii);
      } else {
        qName = new QName("http://example.com", SAMPLE_FEATURE_NAME + ii);
      }
      feature.setName(qName);
      feature.setDefaultCRS(Wfs20Constants.EPSG_4326_URN);
      mockCapabilites.getFeatureTypeList().getFeatureType().add(feature);
    }

    XmlSchema xmlSchema = null;
    if (StringUtils.isNotBlank(schema)) {
      XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
      WfsUriResolver wfsUriResolver = new WfsUriResolver();
      wfsUriResolver.setGmlNamespace(Wfs20Constants.GML_3_2_NAMESPACE);
      wfsUriResolver.setWfsNamespace(Wfs20Constants.WFS_2_0_NAMESPACE);
      schemaCollection.setSchemaResolver(wfsUriResolver);
      xmlSchema =
          schemaCollection.read(new StreamSource(new ByteArrayInputStream(schema.getBytes())));
    }

    if (throwExceptionOnDescribeFeatureType) {
      when(mockWfs.describeFeatureType(any(DescribeFeatureTypeRequest.class)))
          .thenThrow(new WfsException(""));

    } else {
      when(mockWfs.describeFeatureType(any(DescribeFeatureTypeRequest.class)))
          .thenReturn(xmlSchema);
    }
    when(mockWfs.getFeatureCollectionReader()).thenReturn(mockReader);

    return new WfsSource(
        mockWfs, new GeotoolsFilterAdapterImpl(), mockContext, mockAvailabilityTask);
  }