Search in sources :

Example 1 with GeoServerRESTPublisher

use of it.geosolutions.geoserver.rest.GeoServerRESTPublisher in project sldeditor by robward-scisys.

the class GeoServerClient method deleteWorkspace.

/**
 * Delete workspace.
 *
 * @param workspaceName the workspace name
 * @return true, if successful
 */
@Override
public boolean deleteWorkspace(String workspaceName) {
    if (workspaceName == null) {
        return false;
    }
    boolean result = false;
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    GeoServerRESTPublisher publisher = manager.getPublisher();
    if (publisher != null) {
        if (isDefaultWorkspace(workspaceName)) {
            ConsoleManager.getInstance().error(this, Localisation.getString(GeoServerClient.class, "GeoServerClient.cannotDeleteDefaultWorkspace"));
        } else {
            result = publisher.removeWorkspace(workspaceName, false);
        }
    }
    return result;
}
Also used : GeoServerRESTPublisher(it.geosolutions.geoserver.rest.GeoServerRESTPublisher) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Example 2 with GeoServerRESTPublisher

use of it.geosolutions.geoserver.rest.GeoServerRESTPublisher in project sldeditor by robward-scisys.

the class GeoServerClient method deleteStyle.

/**
 * Delete style.
 *
 * @param styleToDelete the style to delete
 * @return true, if successful
 */
@Override
public boolean deleteStyle(StyleWrapper styleToDelete) {
    if (styleToDelete == null) {
        return false;
    }
    boolean result = false;
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    GeoServerRESTPublisher publisher = manager.getPublisher();
    if (publisher != null) {
        if (isDefaultWorkspace(styleToDelete.getWorkspace())) {
            result = publisher.removeStyle(styleToDelete.getStyle());
        } else {
            result = publisher.removeStyleInWorkspace(styleToDelete.getWorkspace(), styleToDelete.getStyle());
        }
    }
    return result;
}
Also used : GeoServerRESTPublisher(it.geosolutions.geoserver.rest.GeoServerRESTPublisher) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Example 3 with GeoServerRESTPublisher

use of it.geosolutions.geoserver.rest.GeoServerRESTPublisher in project coastal-hazards by USGS-CIDA.

the class GeoserverUtil method addRasterLayer.

public static Service addRasterLayer(String geoServerEndpoint, InputStream zipFileStream, String layerId, Bbox bbox, String EPSGcode) throws FileNotFoundException, IOException {
    Service rasterService = null;
    String fileId = UUID.randomUUID().toString();
    String realFileName = TempFileResource.getFileNameForId(fileId);
    // temp file must not include fileId, it should include the realFileName. We don't hand out the realFileName.
    File tempFile = new File(TempFileResource.getTempFileSubdirectory(), realFileName);
    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(tempFile);
        // this is the renamed zip file (the raster tif)
        IOUtils.copy(zipFileStream, fileOut);
    } catch (IOException ex) {
        throw new RuntimeException("Error writing zip to file '" + tempFile.getAbsolutePath() + "'.", ex);
    } finally {
        IOUtils.closeQuietly(zipFileStream);
        IOUtils.closeQuietly(fileOut);
    }
    // tempFile should now have all the data transferred to it
    // this puts it under <tomcat>/temp/cch-temp<randomkeyA>/<randomkeyB>  without the .zip ext
    log.info("Data should now have been copied to the tempFile located here:" + tempFile.getAbsoluteFile());
    log.info("The file id is: " + fileId);
    String uri = props.getProperty("coastal-hazards.base.url");
    log.info("The uri from the props is: " + uri);
    uri += DataURI.DATA_SERVICE_ENDPOINT + DataURI.TEMP_FILE_PATH + "/" + fileId;
    // String zipUrl <- create a url for the retrieval of the file  ... this is now coastal-hazards-portal/temp-file/<randommonkey>
    String unzippedFilePath = null;
    try {
        // get the security token from DynamicReadOnlyProperties
        String token = props.getProperty("gov.usgs.cida.coastalhazards.wps.fetch.and.unzip.process.token");
        // call the wps process
        unzippedFilePath = importRasterUsingWps(token, uri);
    } finally {
        // regardless of success or failure of wps proc
        tempFile.delete();
        log.info("Deleted contents of temp file");
    }
    if (unzippedFilePath == null || unzippedFilePath.isEmpty()) {
        log.error("File path to unzipped geotiff returned null. Is the Geoserver wps fetchAndUnzip call working? ");
        throw new RuntimeException("Error attempting to call wps process '" + tempFile.getAbsolutePath() + "'.");
    }
    log.info("______File path to unzipped raster on the portal is: " + unzippedFilePath);
    File unzippedFile = new File(unzippedFilePath);
    String fileName = unzippedFile.getName();
    // Publish the raster tiff as a layer on Geoserver
    GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(geoServerEndpoint, geoserverUser, geoserverPass);
    // Then use the GeoServerRESTPublisher to create the stores and layers.
    GSCoverageEncoder coverageEncoder = new GSCoverageEncoder();
    // potential todo: set coverageEncoder description here based on text from the metadata file, or from a service parameter
    // this would provide descriptive info for a user browsing our GeoServer with a generic WMS client
    // (fileName);  //BUG noted below : goeserver v2.4 requires the file name match the coverage name
    coverageEncoder.setName(fileName);
    coverageEncoder.setNativeName(fileName);
    coverageEncoder.setTitle(fileName);
    coverageEncoder.setSRS(EPSGcode);
    coverageEncoder.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.FORCE_DECLARED);
    GSLayerEncoder layerEncoder = new GSLayerEncoder();
    layerEncoder.setDefaultStyle(DEFAULT_RASTER_STYLE);
    // Geoserver Manager BUG Alert for v2.4 ...Creating a GeoTIFF coverage via REST works if the coverage's name is the same as the store name, but fails otherwise. Setting nativeName or nativeCoverageName does not help. Changing the name after creating the coverage works fine.
    // #TODO# what to do if the workspace:fileName is already in use?
    RESTCoverageStore store = publisher.publishExternalGeoTIFF(PROXY_WORKSPACE, fileName, unzippedFile, coverageEncoder, layerEncoder);
    if (null == store) {
        // if store or layer creation failed
        log.info("Error publishing GeoTiff in GeoServer.");
        rasterService = null;
    } else {
        String newLayerName = store.getWorkspaceName() + ":" + fileName;
        log.info("Published GeoTiff!!!");
        log.info("In GeoserverUtil, about to add wmsService with layer name: " + layerId);
        rasterService = wmsService(newLayerName);
        log.info("Added layer to wms service.");
    }
    return rasterService;
}
Also used : RESTCoverageStore(it.geosolutions.geoserver.rest.decoder.RESTCoverageStore) GSCoverageEncoder(it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder) FileOutputStream(java.io.FileOutputStream) Service(gov.usgs.cida.coastalhazards.model.Service) GeoServerRESTPublisher(it.geosolutions.geoserver.rest.GeoServerRESTPublisher) IOException(java.io.IOException) File(java.io.File) GSLayerEncoder(it.geosolutions.geoserver.rest.encoder.GSLayerEncoder)

Example 4 with GeoServerRESTPublisher

use of it.geosolutions.geoserver.rest.GeoServerRESTPublisher in project sldeditor by robward-scisys.

the class GeoServerClient method updateLayerStyles.

/**
 * Update layer styles.
 *
 * @param layer the original layer
 * @return true, if successful
 */
@Override
public boolean updateLayerStyles(GeoServerLayer layer) {
    if (layer == null) {
        return false;
    }
    StyleWrapper updatedStyle = layer.getStyle();
    boolean ok = false;
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    GeoServerRESTPublisher publisher = manager.getPublisher();
    if (publisher != null) {
        GSLayerEncoder layerEncoder = new GSLayerEncoder();
        String defaultStyle;
        if (isDefaultWorkspace(updatedStyle.getWorkspace())) {
            defaultStyle = updatedStyle.getStyle();
        } else {
            defaultStyle = updatedStyle.getWorkspace() + ":" + updatedStyle.getStyle();
        }
        layerEncoder.setDefaultStyle(defaultStyle);
        ok = publisher.configureLayer(layer.getLayerWorkspace(), layer.getLayerName(), layerEncoder);
    }
    return ok;
}
Also used : StyleWrapper(com.sldeditor.common.data.StyleWrapper) GeoServerRESTPublisher(it.geosolutions.geoserver.rest.GeoServerRESTPublisher) GSLayerEncoder(it.geosolutions.geoserver.rest.encoder.GSLayerEncoder) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Example 5 with GeoServerRESTPublisher

use of it.geosolutions.geoserver.rest.GeoServerRESTPublisher in project sldeditor by robward-scisys.

the class GeoServerClient method uploadSLD.

/**
 * Upload sld.
 *
 * @param styleWrapper the style wrapper
 * @param sldBody the sld body
 * @return true, if successful
 */
@Override
public boolean uploadSLD(StyleWrapper styleWrapper, String sldBody) {
    String workspaceName = styleWrapper.getWorkspace();
    String styleName = styleWrapper.getStyle();
    if (!workspaceValid(workspaceName)) {
        return false;
    }
    boolean result = false;
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    GeoServerRESTPublisher publisher = manager.getPublisher();
    if (publisher != null) {
        if (styleExists(workspaceName, styleName)) {
            if (isDefaultWorkspace(workspaceName)) {
                result = publisher.updateStyle(sldBody, styleName, true);
            } else {
                result = publisher.updateStyleInWorkspace(workspaceName, sldBody, styleName);
            }
        } else {
            if (isDefaultWorkspace(workspaceName)) {
                result = publisher.publishStyle(sldBody, styleName, true);
            } else {
                GeoServerRESTReader reader = manager.getReader();
                if (reader != null) {
                    if (!reader.existsWorkspace(workspaceName)) {
                        if (!publisher.createWorkspace(workspaceName)) {
                            ConsoleManager.getInstance().error(this, Localisation.getField(GeoServerClient.class, "GeoServerClient.failedToCreateWorkspace") + workspaceName);
                            return false;
                        }
                    }
                } else {
                    return false;
                }
                result = publisher.publishStyleInWorkspace(workspaceName, sldBody, styleName);
            }
        }
    }
    if (result) {
        ConsoleManager.getInstance().information(this, String.format("%s : %s %s/%s", Localisation.getString(GeoServerClient.class, "GeoServerClient.styleUploaded"), connection.getConnectionName(), workspaceName, styleName));
    }
    return result;
}
Also used : GeoServerRESTReader(it.geosolutions.geoserver.rest.GeoServerRESTReader) GeoServerRESTPublisher(it.geosolutions.geoserver.rest.GeoServerRESTPublisher) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Aggregations

GeoServerRESTPublisher (it.geosolutions.geoserver.rest.GeoServerRESTPublisher)5 GeoServerRESTManager (it.geosolutions.geoserver.rest.GeoServerRESTManager)4 GSLayerEncoder (it.geosolutions.geoserver.rest.encoder.GSLayerEncoder)2 StyleWrapper (com.sldeditor.common.data.StyleWrapper)1 Service (gov.usgs.cida.coastalhazards.model.Service)1 GeoServerRESTReader (it.geosolutions.geoserver.rest.GeoServerRESTReader)1 RESTCoverageStore (it.geosolutions.geoserver.rest.decoder.RESTCoverageStore)1 GSCoverageEncoder (it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1