/**
   * Looks up a {@link LayerInfo} or {@link LayerGroupInfo} named after the {@code <layer>} in the
   * requested resource {@code <layer>.kml} name
   *
   * @see org.restlet.Finder#findTarget(org.restlet.data.Request, org.restlet.data.Response)
   */
  @Override
  public Resource findTarget(final Request request, Response response) {
    if (!Method.GET.equals(request.getMethod())) {
      response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
      return null;
    }
    final String name = RESTUtils.getAttribute(request, "layer");
    if (name == null) {
      throw new RestletException("No layer name specified", Status.CLIENT_ERROR_BAD_REQUEST);
    }
    final Catalog catalog = geoserver.getCatalog();
    CatalogInfo layer = catalog.getLayerByName(name);
    MetadataMap mdmap;
    if (layer == null) {
      layer = catalog.getLayerGroupByName(name);
      if (layer == null) {
        throw new RestletException("Layer " + name + " not found", Status.CLIENT_ERROR_NOT_FOUND);
      }
      mdmap = ((LayerGroupInfo) layer).getMetadata();
    } else {
      mdmap = ((LayerInfo) layer).getMetadata();
    }

    Boolean enabled = mdmap.get(Properties.INDEXING_ENABLED, Boolean.class);
    if (enabled == null || !enabled.booleanValue()) {
      throw new RestletException("Layer " + name + " not found", Status.CLIENT_ERROR_NOT_FOUND);
    }
    final Context context = getContext();
    return new GeoSearchLayer(context, request, response, layer, geoserver);
  }
Beispiel #2
0
 public static Optional<String> getRepositoryName(Request request) {
   final String repo = RESTUtils.getAttribute(request, "repository");
   if (repo != null && !repo.contains(":")) {
     throw new IllegalArgumentException(
         "Repository name should be of the form <workspace>:<datastore>: " + repo);
   }
   return Optional.fromNullable(repo);
 }
 /**
  * Executes the mapping to move uploaded file from temporary folder to REST upload root Creates
  * the sidecar file
  */
 public String uploadDone(String uploadId) throws IOException {
   ResumableUploadResource resource = getResource(uploadId);
   Map<String, String> storeParams = new HashMap<String, String>();
   String destinationPath = getDestinationPath(uploadId);
   StringBuilder remappingPath = new StringBuilder(destinationPath);
   String tempFile = resource.getFile().getCanonicalPath();
   RESTUtils.remapping(
       null, FilenameUtils.getBaseName(destinationPath), remappingPath, tempFile, storeParams);
   // Move file to remapped path
   Resource destinationFile = Resources.fromPath(remappingPath.toString());
   // Fill file
   IOUtils.copyStream(new FileInputStream(resource.getFile()), destinationFile.out(), true, true);
   resource.delete();
   // Add temporary sidecar file to mark upload completion, it will be cleared after
   // expirationThreshold
   getSideCarFile(uploadId).createNewFile();
   return destinationPath.toString();
 }
  private File readMosaic() throws NoSuchAuthorityCodeException, FactoryException, IOException {
    // Select the zip file containing the mosaic
    URL mosaicZip = getClass().getResource("test-data/watertemp2.zip");
    File zipFile = DataUtilities.urlToFile(mosaicZip);

    // Creation of another zip file which is a copy of the one before
    File newZip = new File(zipFile.getParentFile(), "watertemp2_temp.zip");
    // Copy the content of the first zip to the second
    FileUtils.copyFile(zipFile, newZip);

    File mosaic = new File(zipFile.getParentFile(), "mosaic");
    if (mosaic.exists()) {
      FileUtils.deleteDirectory(mosaic);
    }
    assertTrue(mosaic.mkdirs());

    RESTUtils.unzipFile(newZip, mosaic);
    return mosaic;
  }
  @Test
  public void testHarvestImageMosaicWithDirectory() throws Exception {
    // Upload of the Mosaic via REST
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;
    byte[] bytes;
    try {
      is = zip.openStream();
      bytes = IOUtils.toByteArray(is);
    } finally {
      IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response =
        putAsServletResponse(
            "/rest/workspaces/gs/coveragestores/watertemp3/file.imagemosaic",
            bytes,
            "application/zip");
    assertEquals(201, response.getStatusCode());

    // check the response contents
    String content = response.getOutputStreamContent();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));

    XMLAssert.assertXpathEvaluatesTo("watertemp3", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

    // check the coverage is actually there
    CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp3");
    assertNotNull(storeInfo);
    CoverageInfo ci = getCatalog().getCoverageByName("watertemp3");
    assertNotNull(ci);
    assertEquals(storeInfo, ci.getStore());

    // Harvesting of the Mosaic
    URL zipHarvest = getClass().getResource("test-data/harvesting.zip");
    File zipFile = DataUtilities.urlToFile(zipHarvest);
    // Creation of another zip file which is a copy of the one before
    File newZip = new File(zipFile.getParentFile(), "harvesting2.zip");
    // Copy the content of the first zip to the second
    FileUtils.copyFile(zipFile, newZip);
    File outputDirectory = new File(zipFile.getParentFile(), "harvesting");
    outputDirectory.mkdir();
    RESTUtils.unzipFile(newZip, outputDirectory);
    // Create the POST request
    MockHttpServletRequest request =
        createRequest("/rest/workspaces/gs/coveragestores/watertemp3/external.imagemosaic");
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setBodyContent("file:///" + outputDirectory.getAbsolutePath());
    request.setHeader("Content-type", "text/plain");
    // Get The response
    response = dispatch(request);
    // Get the Mosaic Reader
    GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints());
    // Test if all the TIME DOMAINS are present
    String[] metadataNames = reader.getMetadataNames();
    assertNotNull(metadataNames);
    assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN"));
    assertEquals(
        "2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z",
        reader.getMetadataValue(metadataNames[0]));
    // Removal of the temporary directory
    FileUtils.deleteDirectory(outputDirectory);
  }