@@ -105,7 +105,8 @@ public class VeeamClient {
105105 private static final String REPOSITORY_REFERENCE = "RepositoryReference" ;
106106 private static final String RESTORE_POINT_REFERENCE = "RestorePointReference" ;
107107 private static final String BACKUP_FILE_REFERENCE = "BackupFileReference" ;
108- private static final SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss" );
108+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss" );
109+ private static final ObjectMapper OBJECT_MAPPER = new XmlMapper ();
109110
110111 private String veeamServerIp ;
111112 private final Integer veeamServerVersion ;
@@ -124,6 +125,8 @@ public VeeamClient(final String url, final Integer version, final String usernam
124125 this .taskPollInterval = taskPollInterval ;
125126 this .taskPollMaxRetry = taskPollMaxRetry ;
126127
128+ OBJECT_MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
129+
127130 final RequestConfig config = RequestConfig .custom ()
128131 .setConnectTimeout (timeout * 1000 )
129132 .setConnectionRequestTimeout (timeout * 1000 )
@@ -233,8 +236,7 @@ protected HttpResponse get(final String path) throws IOException {
233236 private HttpResponse post (final String path , final Object obj ) throws IOException {
234237 String xml = null ;
235238 if (obj != null ) {
236- XmlMapper xmlMapper = new XmlMapper ();
237- xml = xmlMapper .writer ()
239+ xml = OBJECT_MAPPER .writer ()
238240 .with (ToXmlGenerator .Feature .WRITE_XML_DECLARATION )
239241 .writeValueAsString (obj );
240242 // Remove invalid/empty xmlns
@@ -277,8 +279,7 @@ private String findDCHierarchy(final String vmwareDcName) {
277279 try {
278280 final HttpResponse response = get ("/hierarchyRoots" );
279281 checkResponseOK (response );
280- final ObjectMapper objectMapper = new XmlMapper ();
281- final EntityReferences references = objectMapper .readValue (response .getEntity ().getContent (), EntityReferences .class );
282+ final EntityReferences references = OBJECT_MAPPER .readValue (response .getEntity ().getContent (), EntityReferences .class );
282283 for (final Ref ref : references .getRefs ()) {
283284 if (ref .getName ().equals (vmwareDcName ) && ref .getType ().equals (HIERARCHY_ROOT_REFERENCE )) {
284285 return ref .getUid ();
@@ -297,8 +298,7 @@ private String lookupVM(final String hierarchyId, final String vmName) {
297298 try {
298299 final HttpResponse response = get (String .format ("/lookup?host=%s&type=Vm&name=%s" , hierarchyId , vmName ));
299300 checkResponseOK (response );
300- final ObjectMapper objectMapper = new XmlMapper ();
301- final HierarchyItems items = objectMapper .readValue (response .getEntity ().getContent (), HierarchyItems .class );
301+ final HierarchyItems items = OBJECT_MAPPER .readValue (response .getEntity ().getContent (), HierarchyItems .class );
302302 if (items == null || items .getItems () == null || items .getItems ().isEmpty ()) {
303303 throw new CloudRuntimeException ("Could not find VM " + vmName + " in Veeam, please ask administrator to check Veeam B&R manager" );
304304 }
@@ -316,14 +316,12 @@ private String lookupVM(final String hierarchyId, final String vmName) {
316316
317317 private Task parseTaskResponse (HttpResponse response ) throws IOException {
318318 checkResponseOK (response );
319- final ObjectMapper objectMapper = new XmlMapper ();
320- return objectMapper .readValue (response .getEntity ().getContent (), Task .class );
319+ return OBJECT_MAPPER .readValue (response .getEntity ().getContent (), Task .class );
321320 }
322321
323322 protected RestoreSession parseRestoreSessionResponse (HttpResponse response ) throws IOException {
324323 checkResponseOK (response );
325- final ObjectMapper objectMapper = new XmlMapper ();
326- return objectMapper .readValue (response .getEntity ().getContent (), RestoreSession .class );
324+ return OBJECT_MAPPER .readValue (response .getEntity ().getContent (), RestoreSession .class );
327325 }
328326
329327 private boolean checkTaskStatus (final HttpResponse response ) throws IOException {
@@ -410,8 +408,7 @@ public Ref listBackupRepository(final String backupServerId, final String backup
410408 String repositoryName = getRepositoryNameFromJob (backupName );
411409 final HttpResponse response = get (String .format ("/backupServers/%s/repositories" , backupServerId ));
412410 checkResponseOK (response );
413- final ObjectMapper objectMapper = new XmlMapper ();
414- final EntityReferences references = objectMapper .readValue (response .getEntity ().getContent (), EntityReferences .class );
411+ final EntityReferences references = OBJECT_MAPPER .readValue (response .getEntity ().getContent (), EntityReferences .class );
415412 for (final Ref ref : references .getRefs ()) {
416413 if (ref .getType ().equals (REPOSITORY_REFERENCE ) && ref .getName ().equals (repositoryName )) {
417414 return ref ;
@@ -447,8 +444,7 @@ public void listAllBackups() {
447444 try {
448445 final HttpResponse response = get ("/backups" );
449446 checkResponseOK (response );
450- final ObjectMapper objectMapper = new XmlMapper ();
451- final EntityReferences entityReferences = objectMapper .readValue (response .getEntity ().getContent (), EntityReferences .class );
447+ final EntityReferences entityReferences = OBJECT_MAPPER .readValue (response .getEntity ().getContent (), EntityReferences .class );
452448 for (final Ref ref : entityReferences .getRefs ()) {
453449 logger .debug ("Veeam Backup found, name: " + ref .getName () + ", uid: " + ref .getUid () + ", type: " + ref .getType ());
454450 }
@@ -463,8 +459,7 @@ public List<BackupOffering> listJobs() {
463459 try {
464460 final HttpResponse response = get ("/jobs" );
465461 checkResponseOK (response );
466- final ObjectMapper objectMapper = new XmlMapper ();
467- final EntityReferences entityReferences = objectMapper .readValue (response .getEntity ().getContent (), EntityReferences .class );
462+ final EntityReferences entityReferences = OBJECT_MAPPER .readValue (response .getEntity ().getContent (), EntityReferences .class );
468463 final List <BackupOffering > policies = new ArrayList <>();
469464 if (entityReferences == null || entityReferences .getRefs () == null ) {
470465 return policies ;
@@ -486,9 +481,7 @@ public Job listJob(final String jobId) {
486481 final HttpResponse response = get (String .format ("/jobs/%s?format=Entity" ,
487482 jobId .replace ("urn:veeam:Job:" , "" )));
488483 checkResponseOK (response );
489- final ObjectMapper objectMapper = new XmlMapper ();
490- objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
491- return objectMapper .readValue (response .getEntity ().getContent (), Job .class );
484+ return OBJECT_MAPPER .readValue (response .getEntity ().getContent (), Job .class );
492485 } catch (final IOException e ) {
493486 logger .error ("Failed to list Veeam jobs due to:" , e );
494487 checkResponseTimeOut (e );
@@ -568,9 +561,7 @@ public boolean removeVMFromVeeamJob(final String jobId, final String vmwareInsta
568561 final String veeamVmRefId = lookupVM (hierarchyId , vmwareInstanceName );
569562 final HttpResponse response = get (String .format ("/jobs/%s/includes" , jobId ));
570563 checkResponseOK (response );
571- final ObjectMapper objectMapper = new XmlMapper ();
572- objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
573- final ObjectsInJob jobObjects = objectMapper .readValue (response .getEntity ().getContent (), ObjectsInJob .class );
564+ final ObjectsInJob jobObjects = OBJECT_MAPPER .readValue (response .getEntity ().getContent (), ObjectsInJob .class );
574565 if (jobObjects == null || jobObjects .getObjects () == null ) {
575566 logger .warn ("No objects found in the Veeam job " + jobId );
576567 return false ;
@@ -710,8 +701,7 @@ public Map<String, Backup.Metric> getBackupMetricsViaVeeamAPI() {
710701 protected Map <String , Backup .Metric > processHttpResponseForBackupMetrics (final InputStream content ) {
711702 Map <String , Backup .Metric > metrics = new HashMap <>();
712703 try {
713- final ObjectMapper objectMapper = new XmlMapper ();
714- final BackupFiles backupFiles = objectMapper .readValue (content , BackupFiles .class );
704+ final BackupFiles backupFiles = OBJECT_MAPPER .readValue (content , BackupFiles .class );
715705 if (backupFiles == null || CollectionUtils .isEmpty (backupFiles .getBackupFiles ())) {
716706 throw new CloudRuntimeException ("Could not get backup metrics via Veeam B&R API" );
717707 }
@@ -855,8 +845,7 @@ public List<Backup.RestorePoint> listVmRestorePointsViaVeeamAPI(String vmwareDcN
855845 public List <Backup .RestorePoint > processHttpResponseForVmRestorePoints (InputStream content , String vmwareDcName , String vmInternalName , Map <String , Backup .Metric > metricsMap ) {
856846 List <Backup .RestorePoint > vmRestorePointList = new ArrayList <>();
857847 try {
858- final ObjectMapper objectMapper = new XmlMapper ();
859- final VmRestorePoints vmRestorePoints = objectMapper .readValue (content , VmRestorePoints .class );
848+ final VmRestorePoints vmRestorePoints = OBJECT_MAPPER .readValue (content , VmRestorePoints .class );
860849 final String hierarchyId = findDCHierarchy (vmwareDcName );
861850 final String hierarchyUuid = StringUtils .substringAfterLast (hierarchyId , ":" );
862851 if (vmRestorePoints == null ) {
@@ -907,7 +896,7 @@ public List<Backup.RestorePoint> processHttpResponseForVmRestorePoints(InputStre
907896 }
908897
909898 private Date formatDate (String date ) throws ParseException {
910- return dateFormat .parse (StringUtils .substring (date , 0 , 19 ));
899+ return DATE_FORMAT .parse (StringUtils .substring (date , 0 , 19 ));
911900 }
912901
913902 public Pair <Boolean , String > restoreVMToDifferentLocation (String restorePointId , String restoreLocation , String hostIp , String dataStoreUuid ) {
0 commit comments