Search in sources :

Example 6 with WorldMapToken

use of edu.harvard.iq.dataverse.worldmapauth.WorldMapToken in project dataverse by IQSS.

the class MapLayerMetadataServiceBean method deleteMapLayerFromWorldMap.

/* 
     * This method calls GeoConnect and requests that a map layer for this 
     * DataFile is deleted, from WorldMap and GeoConnect itself.
     * The use case here is when a user restricts a tabular file for which 
     * a geospatial map has already been made. 
     * This process is initiated on the Dataverse side, without any GeoConnect
     * UI in the picture. (The way a user normally deletes a layer map is by 
     * clicking 'Delete' on the GeoConnect map page). 
     * Otherwise this call follows the scheme used when accessing the 
     * /shapefile/map-it on GeoConnect - we send along a WorldMap token and a 
     * callback url for GC to download the file metadata.metadata
     * Depending on how it goes we receive a yes or no response from the server.
    */
public void deleteMapLayerFromWorldMap(DataFile dataFile, AuthenticatedUser user) throws IOException {
    if (dataFile == null) {
        logger.severe("dataFile cannot be null");
        return;
    }
    if (user == null) {
        logger.severe("user cannot be null");
        return;
    }
    // Worldmap token:
    WorldMapToken token = tokenServiceBean.getNewToken(dataFile, user);
    if (token == null) {
        logger.severe("token should NOT be null");
        return;
    }
    logger.info("-- new token id: " + token.getId());
    // Callback url for geoConnect:
    String callback_url = URLEncoder.encode(systemConfig.getDataverseSiteUrl() + WorldMapRelatedData.GET_WORLDMAP_DATAFILE_API_PATH);
    String geoConnectAddress = token.getApplication().getMapitLink();
    /* 
         * this is a bit of a hack - there should be a cleaner way to get the base 
         * geoconnect URL from the token!
        */
    geoConnectAddress = geoConnectAddress.replace("/shapefile/map-it", "");
    logger.log(Level.INFO, "callback_url: {0}", callback_url);
    // String geoConnectCommand = geoConnectAddress + GEOCONNECT_MAP_DELETE_API + token.getApplication().getMapitLink() + "/" + token.getToken() + "/?cb=" +  callback_url;
    String geoConnectCommand = geoConnectAddress + GEOCONNECT_MAP_DELETE_API + token.getToken() + "/?cb=" + callback_url;
    logger.info("-- new token id 2: " + token.getId());
    logger.info("Attempting to call GeoConnect to request that the WorldMap layer for DataFile " + dataFile.getId() + ":\n" + geoConnectCommand);
    URL geoConnectUrl = new URL(geoConnectCommand);
    HttpURLConnection geoConnectConnection = (HttpURLConnection) geoConnectUrl.openConnection();
    geoConnectConnection.setRequestMethod("GET");
    geoConnectConnection.connect();
    int code = geoConnectConnection.getResponseCode();
    logger.info("response code: " + code);
    if (code != 200) {
        throw new IOException("Failed to delete Map Layer via GeoConnect. /tabular/delete-map HTTP code response: " + code + "");
    }
    logger.info("response :" + geoConnectConnection.getContent());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WorldMapToken(edu.harvard.iq.dataverse.worldmapauth.WorldMapToken) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

WorldMapToken (edu.harvard.iq.dataverse.worldmapauth.WorldMapToken)6 DataFile (edu.harvard.iq.dataverse.DataFile)4 StringReader (java.io.StringReader)4 JsonObject (javax.json.JsonObject)4 JsonParsingException (javax.json.stream.JsonParsingException)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3 MapLayerMetadata (edu.harvard.iq.dataverse.MapLayerMetadata)2 IOException (java.io.IOException)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 Dataset (edu.harvard.iq.dataverse.Dataset)1 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)1 HttpURLConnection (java.net.HttpURLConnection)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1