Search in sources :

Example 51 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project cloudstack by apache.

the class VeeamClient method post.

private HttpResponse post(final String path, final Object obj) throws IOException {
    String xml = null;
    if (obj != null) {
        XmlMapper xmlMapper = new XmlMapper();
        xml = xmlMapper.writer().with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION).writeValueAsString(obj);
        // Remove invalid/empty xmlns
        xml = xml.replace(" xmlns=\"\"", "");
    }
    String url = apiURI.toString() + path;
    final HttpPost request = new HttpPost(url);
    request.setHeader(SESSION_HEADER, veeamSessionId);
    request.setHeader("Content-type", "application/xml");
    if (StringUtils.isNotBlank(xml)) {
        request.setEntity(new StringEntity(xml));
    }
    final HttpResponse response = httpClient.execute(request);
    checkAuthFailure(response);
    LOG.debug(String.format("Response received in POST request with body [%s] is: [%s] for URL [%s].", xml, response.toString(), url));
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 52 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project cloudstack by apache.

the class VeeamClient method removeVMFromVeeamJob.

public boolean removeVMFromVeeamJob(final String jobId, final String vmwareInstanceName, final String vmwareDcName) {
    LOG.debug("Trying to remove VM from backup offering that is a Veeam job: " + jobId);
    try {
        final String hierarchyId = findDCHierarchy(vmwareDcName);
        final String veeamVmRefId = lookupVM(hierarchyId, vmwareInstanceName);
        final HttpResponse response = get(String.format("/jobs/%s/includes", jobId));
        checkResponseOK(response);
        final ObjectMapper objectMapper = new XmlMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        final ObjectsInJob jobObjects = objectMapper.readValue(response.getEntity().getContent(), ObjectsInJob.class);
        if (jobObjects == null || jobObjects.getObjects() == null) {
            LOG.warn("No objects found in the Veeam job " + jobId);
            return false;
        }
        for (final ObjectInJob jobObject : jobObjects.getObjects()) {
            if (jobObject.getName().equals(vmwareInstanceName) && jobObject.getHierarchyObjRef().equals(veeamVmRefId)) {
                final HttpResponse deleteResponse = delete(String.format("/jobs/%s/includes/%s", jobId, jobObject.getObjectInJobId()));
                return checkTaskStatus(deleteResponse);
            }
        }
        LOG.warn(vmwareInstanceName + " VM was not found to be attached to Veaam job (backup offering): " + jobId);
        return false;
    } catch (final IOException e) {
        LOG.error("Failed to list Veeam jobs due to:", e);
        checkResponseTimeOut(e);
    }
    return false;
}
Also used : ObjectsInJob(org.apache.cloudstack.backup.veeam.api.ObjectsInJob) HttpResponse(org.apache.http.HttpResponse) ObjectInJob(org.apache.cloudstack.backup.veeam.api.ObjectInJob) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 53 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project cloudstack by apache.

the class VeeamClient method listJob.

public Job listJob(final String jobId) {
    LOG.debug("Trying to list veeam job id: " + jobId);
    try {
        final HttpResponse response = get(String.format("/jobs/%s?format=Entity", jobId.replace("urn:veeam:Job:", "")));
        checkResponseOK(response);
        final ObjectMapper objectMapper = new XmlMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper.readValue(response.getEntity().getContent(), Job.class);
    } catch (final IOException e) {
        LOG.error("Failed to list Veeam jobs due to:", e);
        checkResponseTimeOut(e);
    } catch (final ServerApiException e) {
        LOG.error(e);
    }
    return null;
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 54 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project cloudstack by apache.

the class VeeamClient method parseTaskResponse.

private Task parseTaskResponse(HttpResponse response) throws IOException {
    checkResponseOK(response);
    final ObjectMapper objectMapper = new XmlMapper();
    return objectMapper.readValue(response.getEntity().getContent(), Task.class);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 55 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project cloudstack by apache.

the class VeeamClient method listBackupRepository.

// //////////////////////////////////////////////////////
// ////////////// Public Veeam APIs /////////////////////
// //////////////////////////////////////////////////////
public Ref listBackupRepository(final String backupServerId, final String backupName) {
    LOG.debug(String.format("Trying to list backup repository for backup job [name: %s] in server [id: %s].", backupName, backupServerId));
    try {
        String repositoryName = getRepositoryNameFromJob(backupName);
        final HttpResponse response = get(String.format("/backupServers/%s/repositories", backupServerId));
        checkResponseOK(response);
        final ObjectMapper objectMapper = new XmlMapper();
        final EntityReferences references = objectMapper.readValue(response.getEntity().getContent(), EntityReferences.class);
        for (final Ref ref : references.getRefs()) {
            if (ref.getType().equals("RepositoryReference") && ref.getName().equals(repositoryName)) {
                return ref;
            }
        }
    } catch (final IOException e) {
        LOG.error(String.format("Failed to list Veeam backup repository used by backup job [name: %s] due to: [%s].", backupName, e.getMessage()), e);
        checkResponseTimeOut(e);
    }
    return null;
}
Also used : EntityReferences(org.apache.cloudstack.backup.veeam.api.EntityReferences) Ref(org.apache.cloudstack.backup.veeam.api.Ref) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)62 IOException (java.io.IOException)19 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Test (org.junit.Test)15 InputStream (java.io.InputStream)9 File (java.io.File)8 HttpResponse (org.apache.http.HttpResponse)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.jupiter.api.Test)5 BadRequestException (ninja.exceptions.BadRequestException)4 EntityReferences (org.apache.cloudstack.backup.veeam.api.EntityReferences)4 Ref (org.apache.cloudstack.backup.veeam.api.Ref)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)2 FileInputStream (java.io.FileInputStream)2