Skip to content

Commit 6fe3ca5

Browse files
committed
Veeam: use pre-defined object mapper
1 parent 8230f04 commit 6fe3ca5

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

plugins/backup/veeam/src/main/java/org/apache/cloudstack/backup/veeam/VeeamClient.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public class VeeamClient {
107107
private static final String REPOSITORY_REFERENCE = "RepositoryReference";
108108
private static final String RESTORE_POINT_REFERENCE = "RestorePointReference";
109109
private static final String BACKUP_FILE_REFERENCE = "BackupFileReference";
110-
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
110+
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
111+
private static final ObjectMapper OBJECT_MAPPER = new XmlMapper();
111112

112113

113114
private String veeamServerIp;
@@ -127,6 +128,8 @@ public VeeamClient(final String url, final Integer version, final String usernam
127128
this.taskPollInterval = taskPollInterval;
128129
this.taskPollMaxRetry = taskPollMaxRetry;
129130

131+
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
132+
130133
final RequestConfig config = RequestConfig.custom()
131134
.setConnectTimeout(timeout * 1000)
132135
.setConnectionRequestTimeout(timeout * 1000)
@@ -236,8 +239,7 @@ protected HttpResponse get(final String path) throws IOException {
236239
private HttpResponse post(final String path, final Object obj) throws IOException {
237240
String xml = null;
238241
if (obj != null) {
239-
XmlMapper xmlMapper = new XmlMapper();
240-
xml = xmlMapper.writer()
242+
xml = OBJECT_MAPPER.writer()
241243
.with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
242244
.writeValueAsString(obj);
243245
// Remove invalid/empty xmlns
@@ -280,8 +282,7 @@ private String findDCHierarchy(final String vmwareDcName) {
280282
try {
281283
final HttpResponse response = get("/hierarchyRoots");
282284
checkResponseOK(response);
283-
final ObjectMapper objectMapper = new XmlMapper();
284-
final EntityReferences references = objectMapper.readValue(response.getEntity().getContent(), EntityReferences.class);
285+
final EntityReferences references = OBJECT_MAPPER.readValue(response.getEntity().getContent(), EntityReferences.class);
285286
for (final Ref ref : references.getRefs()) {
286287
if (ref.getName().equals(vmwareDcName) && ref.getType().equals(HIERARCHY_ROOT_REFERENCE)) {
287288
return ref.getUid();
@@ -300,8 +301,7 @@ private String lookupVM(final String hierarchyId, final String vmName) {
300301
try {
301302
final HttpResponse response = get(String.format("/lookup?host=%s&type=Vm&name=%s", hierarchyId, vmName));
302303
checkResponseOK(response);
303-
final ObjectMapper objectMapper = new XmlMapper();
304-
final HierarchyItems items = objectMapper.readValue(response.getEntity().getContent(), HierarchyItems.class);
304+
final HierarchyItems items = OBJECT_MAPPER.readValue(response.getEntity().getContent(), HierarchyItems.class);
305305
if (items == null || items.getItems() == null || items.getItems().isEmpty()) {
306306
throw new CloudRuntimeException("Could not find VM " + vmName + " in Veeam, please ask administrator to check Veeam B&R manager");
307307
}
@@ -319,14 +319,12 @@ private String lookupVM(final String hierarchyId, final String vmName) {
319319

320320
private Task parseTaskResponse(HttpResponse response) throws IOException {
321321
checkResponseOK(response);
322-
final ObjectMapper objectMapper = new XmlMapper();
323-
return objectMapper.readValue(response.getEntity().getContent(), Task.class);
322+
return OBJECT_MAPPER.readValue(response.getEntity().getContent(), Task.class);
324323
}
325324

326325
protected RestoreSession parseRestoreSessionResponse(HttpResponse response) throws IOException {
327326
checkResponseOK(response);
328-
final ObjectMapper objectMapper = new XmlMapper();
329-
return objectMapper.readValue(response.getEntity().getContent(), RestoreSession.class);
327+
return OBJECT_MAPPER.readValue(response.getEntity().getContent(), RestoreSession.class);
330328
}
331329

332330
private boolean checkTaskStatus(final HttpResponse response) throws IOException {
@@ -413,8 +411,7 @@ public Ref listBackupRepository(final String backupServerId, final String backup
413411
String repositoryName = getRepositoryNameFromJob(backupName);
414412
final HttpResponse response = get(String.format("/backupServers/%s/repositories", backupServerId));
415413
checkResponseOK(response);
416-
final ObjectMapper objectMapper = new XmlMapper();
417-
final EntityReferences references = objectMapper.readValue(response.getEntity().getContent(), EntityReferences.class);
414+
final EntityReferences references = OBJECT_MAPPER.readValue(response.getEntity().getContent(), EntityReferences.class);
418415
for (final Ref ref : references.getRefs()) {
419416
if (ref.getType().equals(REPOSITORY_REFERENCE) && ref.getName().equals(repositoryName)) {
420417
return ref;
@@ -450,8 +447,7 @@ public void listAllBackups() {
450447
try {
451448
final HttpResponse response = get("/backups");
452449
checkResponseOK(response);
453-
final ObjectMapper objectMapper = new XmlMapper();
454-
final EntityReferences entityReferences = objectMapper.readValue(response.getEntity().getContent(), EntityReferences.class);
450+
final EntityReferences entityReferences = OBJECT_MAPPER.readValue(response.getEntity().getContent(), EntityReferences.class);
455451
for (final Ref ref : entityReferences.getRefs()) {
456452
logger.debug("Veeam Backup found, name: " + ref.getName() + ", uid: " + ref.getUid() + ", type: " + ref.getType());
457453
}
@@ -466,8 +462,7 @@ public List<BackupOffering> listJobs() {
466462
try {
467463
final HttpResponse response = get("/jobs");
468464
checkResponseOK(response);
469-
final ObjectMapper objectMapper = new XmlMapper();
470-
final EntityReferences entityReferences = objectMapper.readValue(response.getEntity().getContent(), EntityReferences.class);
465+
final EntityReferences entityReferences = OBJECT_MAPPER.readValue(response.getEntity().getContent(), EntityReferences.class);
471466
final List<BackupOffering> policies = new ArrayList<>();
472467
if (entityReferences == null || entityReferences.getRefs() == null) {
473468
return policies;
@@ -489,9 +484,7 @@ public Job listJob(final String jobId) {
489484
final HttpResponse response = get(String.format("/jobs/%s?format=Entity",
490485
jobId.replace("urn:veeam:Job:", "")));
491486
checkResponseOK(response);
492-
final ObjectMapper objectMapper = new XmlMapper();
493-
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
494-
return objectMapper.readValue(response.getEntity().getContent(), Job.class);
487+
return OBJECT_MAPPER.readValue(response.getEntity().getContent(), Job.class);
495488
} catch (final IOException e) {
496489
logger.error("Failed to list Veeam jobs due to:", e);
497490
checkResponseTimeOut(e);
@@ -571,9 +564,7 @@ public boolean removeVMFromVeeamJob(final String jobId, final String vmwareInsta
571564
final String veeamVmRefId = lookupVM(hierarchyId, vmwareInstanceName);
572565
final HttpResponse response = get(String.format("/jobs/%s/includes", jobId));
573566
checkResponseOK(response);
574-
final ObjectMapper objectMapper = new XmlMapper();
575-
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
576-
final ObjectsInJob jobObjects = objectMapper.readValue(response.getEntity().getContent(), ObjectsInJob.class);
567+
final ObjectsInJob jobObjects = OBJECT_MAPPER.readValue(response.getEntity().getContent(), ObjectsInJob.class);
577568
if (jobObjects == null || jobObjects.getObjects() == null) {
578569
logger.warn("No objects found in the Veeam job " + jobId);
579570
return false;
@@ -715,8 +706,7 @@ public Map<String, Backup.Metric> getBackupMetricsViaVeeamAPI() {
715706
protected Map<String, Backup.Metric> processHttpResponseForBackupMetrics(final InputStream content) {
716707
Map<String, Backup.Metric> metrics = new HashMap<>();
717708
try {
718-
final ObjectMapper objectMapper = new XmlMapper();
719-
final BackupFiles backupFiles = objectMapper.readValue(content, BackupFiles.class);
709+
final BackupFiles backupFiles = OBJECT_MAPPER.readValue(content, BackupFiles.class);
720710
if (backupFiles == null || CollectionUtils.isEmpty(backupFiles.getBackupFiles())) {
721711
throw new CloudRuntimeException("Could not get backup metrics via Veeam B&R API");
722712
}
@@ -885,8 +875,7 @@ public List<Backup.RestorePoint> listVmRestorePointsViaVeeamAPI(String vmInterna
885875
public List<Backup.RestorePoint> processHttpResponseForVmRestorePoints(InputStream content, String vmInternalName) {
886876
List<Backup.RestorePoint> vmRestorePointList = new ArrayList<>();
887877
try {
888-
final ObjectMapper objectMapper = new XmlMapper();
889-
final VmRestorePoints vmRestorePoints = objectMapper.readValue(content, VmRestorePoints.class);
878+
final VmRestorePoints vmRestorePoints = OBJECT_MAPPER.readValue(content, VmRestorePoints.class);
890879
if (vmRestorePoints == null) {
891880
throw new CloudRuntimeException("Could not get VM restore points via Veeam B&R API");
892881
}
@@ -922,7 +911,7 @@ public List<Backup.RestorePoint> processHttpResponseForVmRestorePoints(InputStre
922911
}
923912

924913
private Date formatDate(String date) throws ParseException {
925-
return dateFormat.parse(StringUtils.substring(date, 0, 19));
914+
return DATE_FORMAT.parse(StringUtils.substring(date, 0, 19));
926915
}
927916

928917
public Pair<Boolean, String> restoreVMToDifferentLocation(String restorePointId, String hostIp, String dataStoreUuid) {

plugins/backup/veeam/src/test/java/org/apache/cloudstack/backup/veeam/VeeamClientTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ public void testListVmRestorePointsViaVeeamAPI() {
475475
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
476476
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
477477
" xmlns=\"http://www.veeam.com/ent/v1.0\">\n" +
478-
" <VmRestorePoint Href=\"https://10.0.3.142:9398/api/vmRestorePoints/f6d504cf-eafe-4cd2-8dfc-e9cfe2f1e977?format=Entity\" Type=\"VmRestorePoint\" Name=\"i-2-4-VM@2023-11-03 16:26:12.209913\" UID=\"urn:veeam:VmRestorePoint:f6d504cf-eafe-4cd2-8dfc-e9cfe2f1e977\" VmDisplayName=\"i-2-4-VM\">\n" +
478+
" <VmRestorePoint Href=\"https://10.0.3.142:9398/api/vmRestorePoints/f6d504cf-eafe-4cd2-8dfc-e9cfe2f1e977?format=Entity\"" +
479+
" Type=\"VmRestorePoint\" Name=\"i-2-4-VM@2023-11-03 16:26:12.209913\" UID=\"urn:veeam:VmRestorePoint:f6d504cf-eafe-4cd2-8dfc-e9cfe2f1e977\"" +
480+
" VmDisplayName=\"i-2-4-VM\" SqlInfo=\"SqlInfo\">\n" +
479481
" <Links>\n" +
480482
" <Link Href=\"https://10.0.3.142:9398/api/vmRestorePoints/f6d504cf-eafe-4cd2-8dfc-e9cfe2f1e977?action=restore\" Rel=\"Restore\" />\n" +
481483
" <Link Href=\"https://10.0.3.142:9398/api/backupServers/18cc2a81-1ff0-42cd-8389-62f2bbcc6b7f\" Name=\"10.0.3.142\" Type=\"BackupServerReference\" Rel=\"Up\" />\n" +

0 commit comments

Comments
 (0)