@Before
  public void setUp() throws URISyntaxException, IOException {
    outputFormat = new HTMLFeatureInfoOutputFormat(getWMS());

    currentTemplate = "test_content.ftl";
    // configure template loader
    GeoServerTemplateLoader templateLoader =
        new GeoServerTemplateLoader(this.getClass(), getDataDirectory()) {

          @Override
          public Object findTemplateSource(String path) throws IOException {
            String templatePath;
            if (path.toLowerCase().contains("content")) {
              templatePath = currentTemplate;

            } else {
              templatePath = "empty.ftl";
            }
            try {
              return new File(this.getClass().getResource(templateFolder + templatePath).toURI());
            } catch (URISyntaxException e) {
              return null;
            }
          }
        };
    outputFormat.templateLoader = templateLoader;

    // test request with some parameters to use in templates
    Request request = new Request();
    parameters = new HashMap<String, Object>();
    parameters.put("LAYER", "testLayer");
    Map<String, String> env = new HashMap<String, String>();
    env.put("TEST1", "VALUE1");
    env.put("TEST2", "VALUE2");
    parameters.put("ENV", env);
    request.setKvp(parameters);

    Dispatcher.REQUEST.set(request);

    final FeatureTypeInfo featureType = getFeatureTypeInfo(MockData.PRIMITIVEGEOFEATURE);

    fcType = WfsFactory.eINSTANCE.createFeatureCollectionType();
    fcType.getFeature().add(featureType.getFeatureSource(null, null).getFeatures());

    // fake layer list
    List<MapLayerInfo> queryLayers = new ArrayList<MapLayerInfo>();
    LayerInfo layerInfo = new LayerInfoImpl();
    layerInfo.setType(PublishedType.VECTOR);
    ResourceInfo resourceInfo = new FeatureTypeInfoImpl(null);
    NamespaceInfo nameSpace = new NamespaceInfoImpl();
    nameSpace.setPrefix("topp");
    nameSpace.setURI("http://www.topp.org");
    resourceInfo.setNamespace(nameSpace);
    layerInfo.setResource(resourceInfo);
    MapLayerInfo mapLayerInfo = new MapLayerInfo(layerInfo);
    queryLayers.add(mapLayerInfo);
    getFeatureInfoRequest = new GetFeatureInfoRequest();
    getFeatureInfoRequest.setQueryLayers(queryLayers);
  }
Ejemplo n.º 2
0
 void addLayer() {
   LayerInfo l = catalog.getLayerByName(new NameImpl("sf", "states"));
   if (l == null) {
     l = catalog.getFactory().createLayer();
     l.setResource(catalog.getResourceByName("sf", "states", WMSLayerInfo.class));
     catalog.add(l);
   }
 }
Ejemplo n.º 3
0
  LayerInfo layer(JSONObject json) throws IOException {
    CatalogFactory f = importer.getCatalog().getFactory();

    if (json.has("layer")) {
      json = json.getJSONObject("layer");
    }

    ResourceInfo r = f.createFeatureType();
    if (json.has("name")) {
      r.setName(json.getString("name"));
    }
    if (json.has("nativeName")) {
      r.setNativeName(json.getString("nativeName"));
    }
    if (json.has("srs")) {
      r.setSRS(json.getString("srs"));
      try {
        r.setNativeCRS(CRS.decode(json.getString("srs")));
      } catch (Exception e) {
        // should fail later
      }
    }
    if (json.has("bbox")) {
      r.setNativeBoundingBox(bbox(json.getJSONObject("bbox")));
    }

    LayerInfo l = f.createLayer();
    l.setResource(r);
    // l.setName(); don't need to this, layer.name just forwards to name of underlying resource

    if (json.has("style")) {
      JSONObject sobj = new JSONObject();
      sobj.put("defaultStyle", json.get("style"));

      JSONObject lobj = new JSONObject();
      lobj.put("layer", sobj);

      LayerInfo tmp = fromJSON(lobj, LayerInfo.class);
      if (tmp.getDefaultStyle() != null) {
        l.setDefaultStyle(tmp.getDefaultStyle());
      } else {
        sobj = new JSONObject();
        sobj.put("style", json.get("style"));

        l.setDefaultStyle(fromJSON(sobj, StyleInfo.class));
      }
    }
    return l;
  }
Ejemplo n.º 4
0
  @Test
  public void testAddLayer() throws Exception {
    testAddFeatureType();
    testAddStyle();

    NamespaceInfo ns = dao.getNamespaceByPrefix("acme");
    FeatureTypeInfo ft = dao.getResourceByName(ns, "anvil", FeatureTypeInfo.class);
    StyleInfo s = dao.getStyleByName("blue");

    LayerInfo l = dao.getCatalog().getFactory().createLayer();
    l.setPath("/anvil");
    l.setResource(ft);
    l.setDefaultStyle(s);

    dao.add(l);

    assertEquals(l, dao.getLayerByName("anvil"));
  }
Ejemplo n.º 5
0
  @Override
  protected void handleObjectPut(Object object) throws Exception {
    String l = getAttribute("layer");
    LayerInfo original = catalog.getLayerByName(l);
    LayerInfo layer = (LayerInfo) object;

    // ensure this is not a name change
    // TODO: Uncomment this when the resource/layer split is not, now by definition
    // we cannot rename a layer, it's just not possible and it's not un-marshalled either
    //        if ( layer.getName() != null && !layer.getName().equals( original.getName() ) ) {
    //            throw new RestletException( "Can't change name of a layer",
    // Status.CLIENT_ERROR_FORBIDDEN );
    //        }
    // force in the same resource otherwise the update will simply fail as we cannot reach the name
    layer.setResource(original.getResource());

    new CatalogBuilder(catalog).updateLayer(original, layer);
    catalog.save(original);

    LOGGER.info("PUT layer " + l);
  }
Ejemplo n.º 6
0
  /**
   * Adds a raster layer to the setup.
   *
   * <p>This method configures a raster layer with the name <code>qName.getLocalPart()</code>. A
   * coverage store is created (if it doesn't already exist) with the same name. The workspace of
   * the resulting store and layer is determined by <code>qName.getPrefix()</code>.
   *
   * <p>The <tt>filename</tt> parameter defines the raster file to be loaded from the classpath and
   * copied into the data directory. The <tt>scope</tt> is used as the class from which to load the
   * file from.
   *
   * <p>In the case of adding a zipped archive that contains multiple file the <tt>filename</tt>
   * paramter should have a ".zip" extension and the <tt>extension</tt> parameter must define the
   * extension of the main raster file. The parameter is not necessary and may be null if the
   * <tt>filename</tt> does not refer to a zip file.
   *
   * <p>The <tt>props</tt> parameter is used to define custom properties for the layer. See the
   * {@link LayerProperty} class for supported properties.
   *
   * @param qName The name of the raster layer.
   * @param filename The name of the file containing the raster, to be loaded from the classpath.
   * @param extension The file extension (without a ".") of the main raster file. This parameter my
   *     be <code>null</code> only if <tt>filename</tt> does not refer to a zip file.
   * @param props Custom properties to assign to the created raster layer.
   * @param scope The class from which to load the <tt>filename</tt> resource from.
   */
  public void addRasterLayer(
      QName qName,
      String filename,
      String extension,
      Map<LayerProperty, Object> props,
      Class scope,
      Catalog catalog)
      throws IOException {

    String prefix = qName.getPrefix();
    String name = qName.getLocalPart();

    // setup the data
    File dir = new File(data, name);
    dir.mkdirs();

    File file = new File(dir, filename);
    catalog.getResourceLoader().copyFromClassPath(filename, file, scope);

    String ext = FilenameUtils.getExtension(filename);
    if ("zip".equalsIgnoreCase(ext)) {

      // unpack the archive
      IOUtils.decompress(file, dir);

      // delete archive
      file.delete();

      if (extension == null) {
        // zip with no extension, we just the directory as the file
        file = dir;
      } else {
        // files may have been top level, or one directory level deep
        file = new File(dir, FilenameUtils.getBaseName(filename) + "." + extension);
        if (!file.exists()) {
          File file2 = new File(new File(dir, dir.getName()), file.getName());
          if (file2.exists()) {
            file = file2;
          }
        }
      }

      if (!file.exists()) {
        throw new FileNotFoundException(file.getPath());
      }
    }

    // load the format/reader
    AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file);
    if (format == null) {
      throw new RuntimeException("No format for " + file.getCanonicalPath());
    }
    AbstractGridCoverage2DReader reader = null;
    try {
      reader = (AbstractGridCoverage2DReader) format.getReader(file);
      if (reader == null) {
        throw new RuntimeException(
            "No reader for " + file.getCanonicalPath() + " with format " + format.getName());
      }

      // configure workspace if it doesn;t already exist
      if (catalog.getWorkspaceByName(prefix) == null) {
        addWorkspace(prefix, qName.getNamespaceURI(), catalog);
      }
      // create the store
      CoverageStoreInfo store = catalog.getCoverageStoreByName(prefix, name);
      if (store == null) {
        store = catalog.getFactory().createCoverageStore();
      }

      store.setName(name);
      store.setWorkspace(catalog.getWorkspaceByName(prefix));
      store.setEnabled(true);
      store.setURL(DataUtilities.fileToURL(file).toString());
      store.setType(format.getName());

      if (store.getId() == null) {
        catalog.add(store);
      } else {
        catalog.save(store);
      }

      // create the coverage
      CatalogBuilder builder = new CatalogBuilder(catalog);
      builder.setStore(store);

      CoverageInfo coverage = null;

      try {

        coverage = builder.buildCoverage(reader, null);
        // coverage read params
        if (format instanceof ImageMosaicFormat) {
          //  make sure we work in immediate mode
          coverage
              .getParameters()
              .put(AbstractGridFormat.USE_JAI_IMAGEREAD.getName().getCode(), Boolean.FALSE);
        }
      } catch (Exception e) {
        throw new IOException(e);
      }

      coverage.setName(name);
      coverage.setTitle(name);
      coverage.setDescription(name);
      coverage.setEnabled(true);

      CoverageInfo cov = catalog.getCoverageByCoverageStore(store, name);
      if (cov == null) {
        catalog.add(coverage);
      } else {
        builder.updateCoverage(cov, coverage);
        catalog.save(cov);
        coverage = cov;
      }

      LayerInfo layer = catalog.getLayerByName(new NameImpl(qName));
      if (layer == null) {
        layer = catalog.getFactory().createLayer();
      }
      layer.setResource(coverage);

      layer.setDefaultStyle(
          catalog.getStyleByName(LayerProperty.STYLE.get(props, DEFAULT_RASTER_STYLE)));
      layer.setType(LayerInfo.Type.RASTER);
      layer.setEnabled(true);

      if (layer.getId() == null) {
        catalog.add(layer);
      } else {
        catalog.save(layer);
      }
    } finally {
      if (reader != null) {
        reader.dispose();
      }
    }
  }
Ejemplo n.º 7
0
  /**
   * Adds a vector layer to the catalog setup.
   *
   * <p>The layer is created within a store named <code>qName.getPrefix()</code>, creating it if it
   * does not exist. The resulting store is a {@link PropertyDataStore} that points at the directory
   * <code>getDataDirectoryRoot()/qName.getPrefix()</code>. Similarily the layer and store are
   * created within a workspace named <code>qName.getPrefix()</code>, which is created if it does
   * not already exist.
   *
   * <p>The properties data for the layer is copied from the classpath, with a file name of "<code>
   * filename</code>.properties". The <tt>scope</tt> parameter is used as the class from which to
   * load the properties file relative to.
   *
   * <p>The <tt>props</tt> parameter is used to define custom properties for the layer. See the
   * {@link LayerProperty} class for supported properties.
   */
  public void addVectorLayer(
      QName qName, Map<LayerProperty, Object> props, String filename, Class scope, Catalog catalog)
      throws IOException {
    String prefix = qName.getPrefix();
    String name = qName.getLocalPart();
    String uri = qName.getNamespaceURI();

    // configure workspace if it doesn;t already exist
    if (catalog.getWorkspaceByName(prefix) == null) {
      addWorkspace(prefix, uri, catalog);
    }

    // configure store if it doesn't already exist

    File storeDir = catalog.getResourceLoader().findOrCreateDirectory(prefix);

    DataStoreInfo store = catalog.getDataStoreByName(prefix);
    if (store == null) {
      store = catalog.getFactory().createDataStore();
      store.setName(prefix);
      store.setWorkspace(catalog.getWorkspaceByName(prefix));
      store.setEnabled(true);

      store.getConnectionParameters().put(PropertyDataStoreFactory.DIRECTORY.key, storeDir);
      store.getConnectionParameters().put(PropertyDataStoreFactory.NAMESPACE.key, uri);
      catalog.add(store);
    }

    // copy the properties file over

    catalog.getResourceLoader().copyFromClassPath(filename, new File(storeDir, filename), scope);

    // configure feature type
    FeatureTypeInfo featureType = catalog.getFactory().createFeatureType();
    featureType.setStore(store);
    featureType.setNamespace(catalog.getNamespaceByPrefix(prefix));
    featureType.setName(LayerProperty.NAME.get(props, name));
    featureType.setNativeName(FilenameUtils.getBaseName(filename));
    featureType.setTitle(name);
    featureType.setAbstract("abstract about " + name);

    Integer srs = LayerProperty.SRS.get(props, SRS.get(qName));
    if (srs == null) {
      srs = 4326;
    }
    featureType.setSRS("EPSG:" + srs);
    try {
      featureType.setNativeCRS(CRS.decode("EPSG:" + srs));
    } catch (Exception e) {
      LOGGER.warning("Failed to decode EPSG:" + srs + ", setting the native SRS to null");
    }
    featureType.setNumDecimals(8);
    featureType.getKeywords().add(new Keyword(name));
    featureType.setEnabled(true);
    featureType.setProjectionPolicy(
        LayerProperty.PROJECTION_POLICY.get(props, ProjectionPolicy.NONE));
    featureType.setLatLonBoundingBox(
        LayerProperty.LATLON_ENVELOPE.get(props, DEFAULT_LATLON_ENVELOPE));
    featureType.setNativeBoundingBox(LayerProperty.ENVELOPE.get(props, null));

    FeatureTypeInfo ft = catalog.getFeatureTypeByDataStore(store, name);
    LayerInfo layer = catalog.getLayerByName(new NameImpl(prefix, name));
    if (ft == null) {
      ft = featureType;
      catalog.add(featureType);
    } else {
      if (layer == null) {
        // handles the case of layer removed, but feature type not
        catalog.remove(ft);
        ft = featureType;
        catalog.add(featureType);
      } else {
        new CatalogBuilder(catalog).updateFeatureType(ft, featureType);
        catalog.save(ft);
      }
    }

    if (layer == null
        || !layer.getResource().getNamespace().equals(catalog.getNamespaceByPrefix(prefix))) {
      layer = catalog.getFactory().createLayer();
    }

    layer.setResource(ft);

    StyleInfo defaultStyle = null;
    if (LayerProperty.STYLE.get(props, null) != null) {
      defaultStyle = catalog.getStyleByName(LayerProperty.STYLE.get(props, null));
    } else {
      // look for a style matching the layer name
      defaultStyle = catalog.getStyleByName(name);
      if (defaultStyle == null) {
        // see if the resource exists and we just need to create it
        if (getClass().getResource(name + ".sld") != null) {
          addStyle(name, catalog);
          defaultStyle = catalog.getStyleByName(name);
        }
      }
    }

    if (defaultStyle == null) {
      defaultStyle = catalog.getStyleByName(DEFAULT_VECTOR_STYLE);
    }

    layer.getStyles().clear();
    layer.setDefaultStyle(defaultStyle);
    layer.setType(LayerInfo.Type.VECTOR);
    layer.setEnabled(true);

    if (layer.getId() == null) {
      catalog.add(layer);
    } else {
      catalog.save(layer);
    }
  }