public RESTImportTaskList uploadFile ( Integer importJobId , File file , String sourceSrs ) throws Exception { LOG . debug ( "Uploading file " + file . getName ( ) + " to import job " + importJobId ) ; Response httpResponse = HttpUtil . post ( this . addEndPoint ( importJobId + "/tasks" ) , file , this . username , this . password ) ; HttpStatus responseStatus = httpResponse . getStatusCode ( ) ; if ( responseStatus == null || ! responseStatus . is2xxSuccessful ( ) ) { throw new GeoServerRESTImporterException ( "Error while uploading the file." ) ; } LOG . debug ( "Successfully uploaded the file to import job " + importJobId ) ; RESTImportTaskList importTaskList = null ; // check, if it is a list of import tasks (for multiple layers) try { importTaskList = mapper . readValue ( httpResponse . getBody ( ) , RESTImportTaskList . class ) ; LOG . debug ( "Imported file " + file . getName ( ) + " contains data for multiple layers." ) ; return importTaskList ; } catch ( Exception e ) { LOG . debug ( "Imported file " + file . getName ( ) + " likely contains data for single " + "layer. Will check this now." ) ; try { RESTImportTask importTask = mapper . readValue ( httpResponse . getBody ( ) , RESTImportTask . class ) ; if ( importTask != null ) { importTaskList = new RESTImportTaskList ( ) ; importTaskList . add ( importTask ) ; LOG . debug ( "Imported file " + file . getName ( ) + " contains data for a single layer." ) ; } return importTaskList ; } catch ( Exception ex ) { LOG . info ( "It seems that the SRS definition source file can not be interpreted by " + "GeoServer / GeoTools. Try to set SRS definition to " + sourceSrs + "." ) ; File updatedGeoTiff = null ; try { if ( ! StringUtils . isEmpty ( sourceSrs ) ) { // "First" recursion: try to add prj file to ZIP. updatedGeoTiff = addPrjFileToArchive ( file , sourceSrs ) ; } else { // At least second recursion: throw exception since SRS definition // could not be set. throw new GeoServerRESTImporterException ( "Could not set SRS definition " + "of GeoTIFF." ) ; } } catch ( ZipException ze ) { throw new GeoServerRESTImporterException ( "No valid ZIP file given containing " + "GeoTiff datasets." ) ; } if ( updatedGeoTiff != null ) { importTaskList = uploadFile ( importJobId , updatedGeoTiff , null ) ; return importTaskList ; } } } return null ; }
Upload an import file .