Example usage for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause

List of usage examples for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause

Introduction

In this page you can find the example usage for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause.

Prototype

public IOExceptionWithCause(String message, Throwable cause) 

Source Link

Document

Constructs a new instance with the given message and cause.

Usage

From source file:org.openestate.is24.restapi.utils.ExportHandler.java

/**
 * Return publish channels for the authorized agency.
 *
 * @return//from   w w  w.j  a va  2 s .  c o  m
 * publish channels
 *
 * @throws IOException
 * if the operation failed
 */
protected PublishChannels doLoadPublishChannels() throws IOException {
    try {
        return ImportExport.PublishChannelService.get(this.client);
    } catch (RequestFailedException ex) {
        LOGGER.error("Can't get publish channels from the Webservice!");
        if (ex.requestRefNumber != null)
            LOGGER.error("> referring request: " + ex.requestRefNumber);
        logMessagesAsError(ex.responseMessages);
        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
        this.putGeneralMessage(ExportMessage.Code.PUBLISH_CHANNELS_NOT_FOUND, ex);
        return null;
    } catch (JAXBException ex) {
        //LOGGER.error( "Can't read / write XML while communicating with the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Can't read / write XML while communicating with the Webservice!", ex);
    } catch (OAuthException ex) {
        //LOGGER.error( "Can't authorize at the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Authorization failed!", ex);
    } catch (IOException ex) {
        //LOGGER.error( "Can't communicate with the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Communication failed!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportHandler.java

/**
 * Publish a real estate at the Webservice.
 *
 * @param is24ObjectId// www .  j a  va 2 s  .  c  o m
 * real estate ID by IS24
 *
 * @param externalObjectId
 * external real estate ID
 *
 * @param is24PublishChannels
 * channels, where the real estate should be published
 *
 * @throws IOException
 * if the operation failed
 */
protected void doPublishObject(long is24ObjectId, String externalObjectId, PublishChannels is24PublishChannels)
        throws IOException {
    final org.openestate.is24.restapi.xml.common.ObjectFactory commonFactory = new org.openestate.is24.restapi.xml.common.ObjectFactory();

    try {
        // derzeitige Verffentlichungen zur Immobilie ermitteln
        final List<Long> is24PublishedChannels = new ArrayList<Long>();
        try {
            PublishObjects is24Publishings = ImportExport.PublishService.get(this.client, is24ObjectId, 0);
            if (is24Publishings != null) {
                for (PublishObject is24Publishing : is24Publishings.getPublishObject()) {
                    is24PublishedChannels.add(is24Publishing.getPublishChannel().getId());
                }
            }
        } catch (RequestFailedException ex) {
            LOGGER.error("Can't get publishings of property '" + externalObjectId + "' (" + is24ObjectId
                    + ") from the Webservice!");
            if (ex.requestRefNumber != null)
                LOGGER.error("> referring request: " + ex.requestRefNumber);
            logMessagesAsError(ex.responseMessages);
            LOGGER.error("> " + ex.getLocalizedMessage(), ex);
            this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_PUBLISHINGS_NOT_FOUND, ex);
        }

        if (is24PublishChannels == null) {
            this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_PUBLISHED,
                    "No channels for publishing found!");
        } else {
            // Verffentlichungen zur Immobilie aktualisieren,
            // wenn diese zu einem Kanal noch nicht gesetzt wurde
            for (PublishChannel is24Channel : is24PublishChannels.getPublishChannel()) {
                Long is24ChannelId = is24Channel.getId();
                if (is24PublishedChannels.contains(is24ChannelId))
                    continue;

                PublishObject is24Publishing = commonFactory.createPublishObject();
                is24Publishing.setPublishChannel(is24Channel);
                is24Publishing.setRealEstate(commonFactory.createPublishObjectRealEstate());
                is24Publishing.getRealEstate().setId(is24ObjectId);

                try {
                    ImportExport.PublishService.post(this.client, is24Publishing);
                } catch (RequestFailedException ex) {
                    LOGGER.error("Can't publish property '" + externalObjectId + "' (" + is24ObjectId + ") "
                            + "in channel '" + is24Channel.getTitle() + "' (" + is24ChannelId + ")!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_PUBLISHED, ex);
                }
            }
        }
    } catch (JAXBException ex) {
        //LOGGER.error( "Can't read / write XML while communicating with the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Can't read / write XML while communicating with the Webservice!", ex);
    } catch (OAuthException ex) {
        //LOGGER.error( "Can't authorize at the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Authorization failed!", ex);
    } catch (IOException ex) {
        //LOGGER.error( "Can't communicate with the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Communication failed!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportHandler.java

/**
 * Remove a real estate object from the Webservice.
 *
 * @param externalObjectId/*  w w  w.ja v  a 2 s  .  c  om*/
 * external real estate ID
 *
 * @throws IOException
 * if the operation failed
 */
protected void doRemoveObject(String externalObjectId) throws IOException {
    try {
        // Lschung durchfhren
        try {
            ImportExport.RealEstateService.deleteByExternalId(this.client, externalObjectId);
        } catch (RequestFailedException ex) {
            LOGGER.error("Can't delete property '" + externalObjectId + "' at the Webservice!");
            if (ex.requestRefNumber != null)
                LOGGER.error("> referring request: " + ex.requestRefNumber);
            logMessagesAsError(ex.responseMessages);
            LOGGER.error("> " + ex.getLocalizedMessage(), ex);
            this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_REMOVED, ex);
        }
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't read / write XML while communicating with the Webservice!", ex);
    } catch (OAuthException ex) {
        throw new IOExceptionWithCause("Authorization failed!", ex);
    } catch (IOException ex) {
        throw new IOExceptionWithCause("Communication failed!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportHandler.java

/**
 * Remove a real estate object from the Webservice.
 *
 * @param is24ObjectId//from w  ww .j av  a 2  s.  co  m
 * real estate ID by IS24
 *
 * @param externalObjectId
 * external real estate ID
 *
 * @throws IOException
 * if the operation failed
 */
protected void doRemoveObject(long is24ObjectId, String externalObjectId) throws IOException {
    try {
        // Lschung durchfhren
        try {
            ImportExport.RealEstateService.deleteByIs24Id(this.client, is24ObjectId);
        } catch (RequestFailedException ex) {
            if (!StringUtils.isBlank(externalObjectId)) {
                LOGGER.error("Can't delete property '" + externalObjectId + "' (" + is24ObjectId
                        + ") at the Webservice!");
                if (ex.requestRefNumber != null)
                    LOGGER.error("> referring request: " + ex.requestRefNumber);
                logMessagesAsError(ex.responseMessages);
                LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_REMOVED, ex);
            } else {
                LOGGER.error("Can't delete property (" + is24ObjectId + ") at the Webservice!");
                if (ex.requestRefNumber != null)
                    LOGGER.error("> referring request: " + ex.requestRefNumber);
                logMessagesAsError(ex.responseMessages);
                LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                this.putGeneralMessage(ExportMessage.Code.OBJECT_NOT_REMOVED, ex);
            }
        }
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't read / write XML while communicating with the Webservice!", ex);
    } catch (OAuthException ex) {
        throw new IOExceptionWithCause("Authorization failed!", ex);
    } catch (IOException ex) {
        throw new IOExceptionWithCause("Communication failed!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportHandler.java

/**
 * Save a contact person to the Webservice.
 *
 * @param contact//from   w  ww.jav a  2s  . c om
 * contact to save
 *
 * @param poolContactId
 * contact ID within export pool
 *
 * @return
 * ID of the processed contact person in the Webservice or null, if the object
 * was not updated
 *
 * @throws IOException
 * if the operation failed
 */
protected Long doUpdateContact(RealtorContactDetails contact, String poolContactId) throws IOException {
    final String externalContactId = contact.getExternalId();
    try {
        // prfen, ob ein Ansprechpartner mit der externen ID bereits im Webservice existiert
        final RealtorContactDetails oldIs24Contact;
        try {
            oldIs24Contact = ImportExport.ContactAddressService.getByExternalId(this.client, externalContactId);
        } catch (RequestFailedException ex) {
            LOGGER.error("Can't get contact person '" + externalContactId + "' from the Webservice!");
            if (ex.requestRefNumber != null)
                LOGGER.error("> referring request: " + ex.requestRefNumber);
            logMessagesAsError(ex.responseMessages);
            LOGGER.error("> " + ex.getLocalizedMessage(), ex);
            this.putContactMessage(externalContactId, ExportMessage.Code.CONTACT_NOT_FOUND, ex);
            return null;
        }

        Long is24ContactId;

        // neuen Ansprechpartner erstellen
        if (oldIs24Contact == null) {
            try {
                is24ContactId = ImportExport.ContactAddressService.post(this.client, contact);
            } catch (RequestFailedException ex) {
                Resource resource = Resource.getMessageResource(ex.responseMessages);
                if (resource != null && "duplicated contactDetails".equalsIgnoreCase(resource.type)
                        && resource.id > 0) {
                    LOGGER.info("contact '" + externalContactId + "' is already "
                            + "available at the Webservice with ID " + resource.id + ".");
                    this.duplicatedContactIds.put(externalContactId, resource.id);
                    is24ContactId = resource.id;
                } else {
                    LOGGER.error("Can't add contact person '" + externalContactId + "' to the Webservice!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putContactMessage(externalContactId, ExportMessage.Code.CONTACT_NOT_SAVED, ex);
                    return null;
                }
            }
        }

        // bestehenden Ansprechpartner aktualisieren
        else {
            is24ContactId = oldIs24Contact.getId();
            contact.setId(is24ContactId);

            try {
                ImportExport.ContactAddressService.putByIs24Id(this.client, contact, is24ContactId);
            } catch (RequestFailedException ex) {
                Resource resource = Resource.getMessageResource(ex.responseMessages);
                if (resource != null && "duplicated contactDetails".equalsIgnoreCase(resource.type)
                        && resource.id > 0) {
                    LOGGER.info("contact '" + externalContactId + "' is already "
                            + "available at the Webservice with ID " + resource.id + ".");
                    this.duplicatedContactIds.put(externalContactId, resource.id);
                    return resource.id;
                } else {
                    LOGGER.error("Can't update contact person '" + externalContactId + "' at the Webservice!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putContactMessage(externalContactId, ExportMessage.Code.CONTACT_NOT_SAVED, ex);
                    return null;
                }
            }
        }

        // ID des Ansprechpartners als erfolgreich exportiert vormerken
        this.savedContactIds.add(externalContactId);

        // ID des verarbeiteten Ansprechpartners bei IS24 zurckliefern
        return is24ContactId;
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't read / write XML while communicating with the Webservice!", ex);
    } catch (OAuthException ex) {
        throw new IOExceptionWithCause("Authorization failed!", ex);
    } catch (IOException ex) {
        throw new IOExceptionWithCause("Communication failed!", ex);
    } finally {
        // Fortschritt protokollieren
        this.addProgress(this.pool.getContactSize(poolContactId, true));
    }
}

From source file:org.openestate.is24.restapi.utils.ExportHandler.java

/**
 * Save a real estate to the Webservice.
 *
 * @param object//from   w  ww .j  av  a2  s .  co m
 * real estate to save
 *
 * @param poolObjectId
 * real estate ID within export pool
 *
 * @return
 * ID of the processed real estate in the Webservice or null, if the object
 * was not updated
 *
 * @throws IOException
 * if the operation failed
 */
protected Long doUpdateObject(RealEstate object, String poolObjectId) throws IOException {
    final String externalObjectId = object.getExternalId();
    final org.openestate.is24.restapi.xml.realestates.ObjectFactory realEstatesFactory = new org.openestate.is24.restapi.xml.realestates.ObjectFactory();
    final org.openestate.is24.restapi.xml.attachmentsorder.ObjectFactory attachmentsorderFactory = new org.openestate.is24.restapi.xml.attachmentsorder.ObjectFactory();

    object.setRealEstateState(RealEstateState.ACTIVE);

    // Ansprechpartner zuweisen, wenn nicht bereits explizit eine intene
    // Ansprechpartner-ID hinterlegt wurde
    Long is24ContactId = (object.getContact() != null) ? object.getContact().getId() : null;
    if (is24ContactId == null || is24ContactId < 1) {
        String externalContactId = (object.getContact() != null)
                ? StringUtils.trimToNull(object.getContact().getExternalId())
                : null;

        // Duplikat des Ansprechpartners verwenden
        if (externalContactId != null && this.duplicatedContactIds.containsKey(externalContactId)) {
            is24ContactId = this.duplicatedContactIds.get(externalContactId);
            if (object.getContact() == null)
                object.setContact(realEstatesFactory.createRealEstateContact());
            object.getContact().setId(is24ContactId);
            object.getContact().setExternalId(null);
        }

        // sicherstellen, dass der Ansprechpartner vorher erfolgreich whrend des Transports exportiert wurde
        else if (externalContactId != null && !this.savedContactIds.contains(externalContactId)) {
            this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_WITHOUT_CONTACT,
                    "The contact '" + externalContactId + "' was not saved during the export process.");
            object.setContact(null);
        }
    }

    try {
        // prfen, ob eine Immobilie mit der externen ID bereits im Webservice existiert
        RealEstate oldIs24Object;
        try {
            oldIs24Object = ImportExport.RealEstateService.getByExternalId(this.client, externalObjectId);
        } catch (RequestFailedException ex) {
            LOGGER.error("Can't get property '" + externalObjectId + "' from the Webservice!");
            if (ex.requestRefNumber != null)
                LOGGER.error("> referring request: " + ex.requestRefNumber);
            logMessagesAsError(ex.responseMessages);
            LOGGER.error("> " + ex.getLocalizedMessage(), ex);
            this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_FOUND, ex);

            // Fortschritt protokollieren
            this.addProgress(this.pool.getObjectSize(poolObjectId, true));

            return null;
        }

        //Messages responseMessages;
        final Long is24ObjectId;

        // Immobilie im Portal lschen,
        // wenn diese bereits im Portal existiert
        // und einer anderen Rubrik zugewiesen ist
        if (oldIs24Object != null && !oldIs24Object.getClass().getName().equals(object.getClass().getName())) {
            //LOGGER.debug( "RUBRIK GENDERT" );
            //LOGGER.debug( "> fr Immobilie #" + oldIs24Object.getId() );
            //LOGGER.debug( "> alte Rubrik " + oldIs24Object.getClass().getName() );
            //LOGGER.debug( "> neue Rubrik " + object.getClass().getName() );
            try {
                ImportExport.RealEstateService.deleteByIs24Id(this.client, oldIs24Object.getId());
                oldIs24Object = null;
            } catch (RequestFailedException ex) {
                LOGGER.error("Can't delete property '" + externalObjectId + "' (" + oldIs24Object.getId()
                        + ") at the Webservice!");
                if (ex.requestRefNumber != null)
                    LOGGER.error("> referring request: " + ex.requestRefNumber);
                logMessagesAsError(ex.responseMessages);
                LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_SAVED, ex);

                // Fortschritt protokollieren
                this.addProgress(this.pool.getObjectSize(poolObjectId, true));

                return null;
            }
        }

        // neue Immobilie erstellen
        if (oldIs24Object == null) {
            try {
                is24ObjectId = ImportExport.RealEstateService.post(this.client, object,
                        this.isUseNewEnergySourceEnev2014Values());
                //LOGGER.debug( "created object with IS24-ID #" + is24ObjectId );
            } catch (RequestFailedException ex) {
                LOGGER.error("Can't add property '" + externalObjectId + "' to the Webservice!");
                if (ex.requestRefNumber != null)
                    LOGGER.error("> referring request: " + ex.requestRefNumber);
                logMessagesAsError(ex.responseMessages);
                LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_SAVED, ex);

                // Fortschritt protokollieren
                this.addProgress(this.pool.getObjectSize(poolObjectId, true));

                return null;
            }
        }

        // bestehende Immobilie aktualisieren
        else {
            is24ObjectId = oldIs24Object.getId();
            object.setId(is24ObjectId);

            try {
                ImportExport.RealEstateService.putByIs24Id(this.client, object, is24ObjectId,
                        this.isUseNewEnergySourceEnev2014Values());
                //LOGGER.debug( "updated object with IS24-ID #" + is24ObjectId );
            } catch (RequestFailedException ex) {
                LOGGER.error("Can't update property '" + externalObjectId + "' (" + is24ObjectId
                        + ") at the Webservice!");
                if (ex.requestRefNumber != null)
                    LOGGER.error("> referring request: " + ex.requestRefNumber);
                logMessagesAsError(ex.responseMessages);
                LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_NOT_SAVED, ex);

                // Fortschritt protokollieren
                this.addProgress(this.pool.getObjectSize(poolObjectId, true));

                return null;
            }
        }

        // Fortschritt protokollieren
        this.addProgress(this.pool.getObjectSize(poolObjectId, false));

        // bestehende Anhnge / Web-Links ermitteln
        final Map<String, Attachment> oldIs24Attachments = new HashMap<String, Attachment>();
        boolean ignoreAttachments = false;
        try {
            Attachments attachments = ImportExport.AttachmentService.getAll(this.client, externalObjectId);
            if (attachments != null && !attachments.getAttachment().isEmpty()) {
                for (Attachment attachment : attachments.getAttachment()) {
                    Long is24AttachmentId = attachment.getId();
                    String externalAttachmentId = StringUtils.trimToNull(attachment.getExternalId());

                    // Anhang bernehmen, wenn ein Hashwert hinterlegt ist
                    // und dieser noch nicht bernommen wurde
                    if (externalAttachmentId != null && !oldIs24Attachments.containsKey(externalAttachmentId)) {
                        //LOGGER.debug( "> found old attachment #" + is24AttachmentId + " / " + externalAttachmentId + " / " + externalAttachmentId.length() );
                        oldIs24Attachments.put(externalAttachmentId, attachment);
                        continue;
                    }

                    // alten Anhang entfernen
                    try {
                        //LOGGER.debug( "> removing old attachment #" + is24AttachmentId + " without external id" );
                        ImportExport.AttachmentService.deleteById(this.client, externalObjectId,
                                is24AttachmentId);
                    } catch (RequestFailedException ex) {
                        LOGGER.error("Can't remove old attachment (" + is24AttachmentId + ") " + "of property '"
                                + externalObjectId + "' (" + is24ObjectId + ") from the Webservice!");
                        if (ex.requestRefNumber != null)
                            LOGGER.error("> referring request: " + ex.requestRefNumber);
                        logMessagesAsError(ex.responseMessages);
                        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                        this.putObjectMessage(externalObjectId,
                                ExportMessage.Code.OBJECT_OLD_ATTACHMENT_NOT_REMOVED, ex);
                    }
                }
            }
        } catch (RequestFailedException ex) {
            LOGGER.error("Can't get attachments of property '" + externalObjectId + "' (" + is24ObjectId
                    + ") from the Webservice!");
            if (ex.requestRefNumber != null)
                LOGGER.error("> referring request: " + ex.requestRefNumber);
            logMessagesAsError(ex.responseMessages);
            LOGGER.error("> " + ex.getLocalizedMessage(), ex);
            this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_WITHOUT_ATTACHMENTS, ex);
            ignoreAttachments = true;
        }

        if (ignoreAttachments) {
            // Fortschritt protokollieren
            long totalAttachmentSize = this.pool.getObjectSize(poolObjectId, true)
                    - this.pool.getObjectSize(poolObjectId, false);
            this.addProgress(totalAttachmentSize);
        }

        else {
            // Anhnge zur bertragung ermitteln und zugehrige Hash-Werte berechnen
            List<String> attachmentHashes = new ArrayList<String>();
            Map<String, Attachment> attachments = new TreeMap<String, Attachment>(new AlphanumComparator());
            Map<String, File> attachmentFiles = new HashMap<String, File>();
            for (String attachmentKey : this.pool.getObjectAttachmentIds(poolObjectId)) {
                Attachment is24Attachment = this.pool.getObjectAttachment(poolObjectId, attachmentKey);
                if (is24Attachment == null) {
                    LOGGER.error("Can't read the XML file for attachment!");
                    this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_ATTACHMENT_NOT_SAVED,
                            "Can't read the XML file for attachment!");

                    // Fortschritt protokollieren
                    this.addProgress(this.pool.getObjectAttachmentSize(poolObjectId, attachmentKey));
                    continue;
                }

                // Anhang als Web-Link verarbeiten
                if (is24Attachment instanceof Link) {
                    Link link = (Link) is24Attachment;

                    // Hashwert zur Identifizierung des Anhangs errechnen
                    URL url = link.getUrl();
                    String externalAttachmentId = (url != null)
                            ? DigestUtils.sha1Hex(is24ObjectId + "-" + url.toString())
                            : DigestUtils.sha1Hex(is24ObjectId + "-" + attachmentKey);

                    // Sicherstellen, dass der gleiche Anhang nicht mehrfach hochgeladen wird
                    if (attachmentHashes.contains(externalAttachmentId))
                        continue;
                    attachmentHashes.add(externalAttachmentId);

                    link.setExternalId(externalAttachmentId);
                }

                // Anhang als Datei verarbeiten
                else {
                    // Datei ermitteln
                    File attachFile = this.pool.getObjectAttachmentFile(poolObjectId, is24Attachment);

                    // ggf. Datei herunterladen, wenn noch nicht im Pool hinterlegt
                    if (attachFile == null) {
                        URL attachUrl = this.pool.getObjectAttachmentURL(poolObjectId, attachmentKey);
                        if (attachUrl != null) {
                            try {
                                attachFile = doDownloadFile(attachUrl);
                            } catch (Exception ex) {
                                LOGGER.warn("Can't download file from URL: " + attachUrl);
                                LOGGER.warn("> " + ex.getLocalizedMessage(), ex);
                            }
                        }
                    }

                    if (attachFile == null || !attachFile.isFile()) {
                        LOGGER.error("Can't find file for attachment!");
                        this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_ATTACHMENT_NOT_SAVED,
                                "Can't find file for attachment!");

                        // Fortschritt protokollieren
                        this.addProgress(this.pool.getObjectAttachmentSize(poolObjectId, attachmentKey));

                        continue;
                    }

                    // Datei des Anhangs vormerken
                    attachmentFiles.put(attachmentKey, attachFile.getAbsoluteFile());

                    // Hashwert zur Identifizierung des Anhangs errechnen
                    final String externalAttachmentId;
                    InputStream input = null;
                    try {
                        input = new FileInputStream(attachFile);
                        String attachFileHash = DigestUtils.sha1Hex(input);
                        externalAttachmentId = DigestUtils.sha1Hex(is24ObjectId + "-" + attachFileHash);

                        // Sicherstellen, dass der gleiche Anhang nicht mehrfach hochgeladen wird
                        if (attachmentHashes.contains(externalAttachmentId))
                            continue;
                        attachmentHashes.add(externalAttachmentId);

                        is24Attachment.setExternalId(externalAttachmentId);
                    } finally {
                        IOUtils.closeQuietly(input);
                    }
                }

                attachments.put(attachmentKey, is24Attachment);
            }

            // alte Anhnge entfernen
            String[] oldIs24AttachmentIds = oldIs24Attachments.keySet()
                    .toArray(new String[oldIs24Attachments.size()]);
            for (String oldIs24AttachmentId : oldIs24AttachmentIds) {
                if (attachmentHashes.contains(oldIs24AttachmentId))
                    continue;
                Attachment is24Attachment = oldIs24Attachments.remove(oldIs24AttachmentId);
                Long is24AttachmentId = is24Attachment.getId();
                try {
                    //LOGGER.debug( "> removing old attachment #" + is24AttachmentId );
                    ImportExport.AttachmentService.deleteById(this.client, externalObjectId, is24AttachmentId);
                } catch (RequestFailedException ex) {
                    LOGGER.error("Can't remove old attachment (" + is24AttachmentId + ") " + "of property '"
                            + externalObjectId + "' (" + is24ObjectId + ") from the Webservice!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putObjectMessage(externalObjectId,
                            ExportMessage.Code.OBJECT_OLD_ATTACHMENT_NOT_REMOVED, ex);
                }
            }

            // Anhnge aus dem Exportverzeichnis der Immobilie ermitteln
            Map<Integer, Long> attachmentsOrder = new TreeMap<Integer, Long>();
            for (Map.Entry<String, Attachment> entry : attachments.entrySet()) {
                final String attachmentKey = entry.getKey();
                final Attachment is24Attachment = entry.getValue();
                final String externalAttachmentId = is24Attachment.getExternalId();

                int pos;
                try {
                    pos = Math.abs(Integer.parseInt(attachmentKey));
                } catch (NumberFormatException ex) {
                    LOGGER.warn("Can't read attachment position!");
                    LOGGER.warn("> " + ex.getLocalizedMessage(), ex);
                    pos = 999;
                }

                // Anhang als Web-Link verarbeiten
                if (is24Attachment instanceof Link) {
                    Link link = (Link) is24Attachment;
                    try {
                        // zuvor gespeicherten Web-Link mit gleichem Hashwert aktualisieren
                        if (oldIs24Attachments.containsKey(externalAttachmentId)) {
                            Attachment oldAttachment = oldIs24Attachments.get(externalAttachmentId);
                            long is24AttachmentId = oldAttachment.getId();
                            //LOGGER.debug( "> updating attached link #" + is24AttachmentId );
                            //LOGGER.debug( ">> " + externalAttachmentId + " / " + externalAttachmentId.length() );
                            ImportExport.AttachmentService.putById(client, is24ObjectId, is24AttachmentId,
                                    link);
                            oldIs24Attachments.remove(externalAttachmentId);
                        }
                        // neuen Web-Link erzeugen
                        else {
                            //LOGGER.debug( "> adding attached link" );
                            //LOGGER.debug( ">> " + externalAttachmentId + " / " + externalAttachmentId.length() );
                            ImportExport.AttachmentService.post(this.client, externalObjectId, is24Attachment,
                                    null, null, null);
                        }
                    } catch (RequestFailedException ex) {
                        LOGGER.error("Can't save attachment of property '" + externalObjectId + "' ("
                                + is24ObjectId + ") to the Webservice!");
                        if (ex.requestRefNumber != null)
                            LOGGER.error("> referring request: " + ex.requestRefNumber);
                        logMessagesAsError(ex.responseMessages);
                        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                        this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_ATTACHMENT_NOT_SAVED,
                                ex);
                    }

                    // Fortschritt protokollieren
                    this.addProgress(this.pool.getObjectAttachmentSize(poolObjectId, attachmentKey));

                    continue;
                }

                // Datei ermitteln
                File attachFile = attachmentFiles.get(attachmentKey);
                if (attachFile == null || !attachFile.isFile()) {
                    LOGGER.error("Can't find file for attachment!");
                    this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_ATTACHMENT_NOT_SAVED,
                            "Can't find file for attachment!");

                    // Fortschritt protokollieren
                    this.addProgress(this.pool.getObjectAttachmentSize(poolObjectId, attachmentKey));

                    continue;
                }

                // Name und Gre des Dateianhangs ermitteln
                final String attachFileName = attachFile.getName();
                final long attachFileSize = attachFile.length();

                InputStream attachFileInput = null;
                try {
                    // zuvor gespeicherten Anhang mit gleichem Hashwert aktualisieren
                    if (oldIs24Attachments.containsKey(externalAttachmentId)) {
                        Attachment oldAttachment = oldIs24Attachments.get(externalAttachmentId);
                        long is24AttachmentId = oldAttachment.getId();
                        //LOGGER.debug( "> updating attached file #" + is24AttachmentId );
                        //LOGGER.debug( ">> " + externalAttachmentId + " / " + externalAttachmentId.length() );
                        ImportExport.AttachmentService.putById(client, is24ObjectId, is24AttachmentId,
                                is24Attachment);
                        oldIs24Attachments.remove(externalAttachmentId);

                        // Sortierung des Anhangs vormerken
                        if (!StreamingVideo.class.isInstance(is24Attachment)) {
                            while (attachmentsOrder.containsKey(pos)) {
                                pos++;
                            }
                            attachmentsOrder.put(pos, is24AttachmentId);
                            //LOGGER.debug( "untouched attachment #" + is24AttachmentId + " (" + StringUtils.trimToEmpty( is24Attachment.getTitle() ) + ") at " + pos );
                        }
                    }
                    // neuen Anhang erzeugen
                    else {
                        //LOGGER.debug( "> adding attached file" );
                        //LOGGER.debug( ">> " + externalAttachmentId + " / " + externalAttachmentId.length() );

                        // MIME-Type des Dateianhangs ermitteln
                        final String attachFileMimeType;
                        if (is24Attachment instanceof PDFDocument) {
                            attachFileMimeType = "application/pdf";
                        } else if (is24Attachment instanceof Picture) {
                            if (attachFileName.toLowerCase().endsWith(".png"))
                                attachFileMimeType = "image/png";
                            else if (attachFileName.toLowerCase().endsWith(".gif"))
                                attachFileMimeType = "image/gif";
                            else
                                attachFileMimeType = "image/jpeg";
                        }
                        //else if (is24Attachment instanceof VideoFile)
                        //{
                        //  mimeType = "application/octet-stream";
                        //}
                        else {
                            //mimeType = "application/octet-stream";
                            attachFileMimeType = null;
                        }

                        attachFileInput = new FileInputStream(attachFile);

                        // Video auf separaten Webservice bertragen
                        if (is24Attachment instanceof StreamingVideo) {
                            // Videodatei via UploadService bertragen
                            //LOGGER.debug( "UPLOAD STREAMING VIDEO '" + attachFileName + "'" );
                            String videoId = ImportExport.VideoUploadService.doVideoUpload(this.client,
                                    attachFileInput, attachFileName, attachFileSize);

                            // Anhang mit ID des bertragenen Videos zum Webservice senden
                            //LOGGER.debug( "POST STREAMING VIDEO WITH ID '" + videoId + "'" );
                            StreamingVideo streamingVideo = (StreamingVideo) is24Attachment;
                            streamingVideo.setVideoId(videoId);
                            ImportExport.AttachmentService.post(this.client, externalObjectId, streamingVideo,
                                    null, null, null);
                        }

                        // Anhang direkt bertragen
                        else {
                            long is24AttachmentId = ImportExport.AttachmentService.post(this.client,
                                    externalObjectId, is24Attachment, attachFileInput, attachFileName,
                                    attachFileMimeType);

                            // Sortierung des Anhangs vormerken
                            while (attachmentsOrder.containsKey(pos)) {
                                pos++;
                            }
                            attachmentsOrder.put(pos, is24AttachmentId);
                            //LOGGER.debug( "new attachment #" + is24AttachmentId + " (" + StringUtils.trimToEmpty( is24Attachment.getTitle() ) + ") at " + pos );
                        }
                    }
                } catch (RequestFailedException ex) {
                    LOGGER.error("Can't save attachment of property '" + externalObjectId + "' (" + is24ObjectId
                            + ") to the Webservice!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_ATTACHMENT_NOT_SAVED, ex);
                } finally {
                    IOUtils.closeQuietly(attachFileInput);

                    // Fortschritt protokollieren
                    this.addProgress(
                            this.pool.getObjectAttachmentSize(poolObjectId, attachmentKey) + attachFileSize);
                }
            }

            // nicht aktualisierte Anhnge entfernen
            for (Attachment is24Attachment : oldIs24Attachments.values()) {
                Long is24AttachmentId = is24Attachment.getId();
                try {
                    //LOGGER.debug( "> removing untouched attachment #" + is24AttachmentId );
                    ImportExport.AttachmentService.deleteById(this.client, externalObjectId, is24AttachmentId);
                } catch (RequestFailedException ex) {
                    LOGGER.error(
                            "Can't remove untouched attachment (" + is24AttachmentId + ") " + "of property '"
                                    + externalObjectId + "' (" + is24ObjectId + ") from the Webservice!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putObjectMessage(externalObjectId,
                            ExportMessage.Code.OBJECT_OLD_ATTACHMENT_NOT_REMOVED, ex);
                }
            }

            // Reihenfolge der Bild-Anhnge setzen
            if (!attachmentsOrder.isEmpty()) {
                //LOGGER.debug( "update attachment order for property '" + externalObjectId + "' (" + is24ObjectId + ")" );
                //LOGGER.debug( "> " + StringUtils.join( attachmentsOrder.values(), ", " ) );
                org.openestate.is24.restapi.xml.attachmentsorder.List list = attachmentsorderFactory
                        .createList();
                for (Long is24AttachmentId : attachmentsOrder.values()) {
                    list.getAttachmentId().add(is24AttachmentId);
                }
                try {
                    ImportExport.AttachmentsOrderService.put(this.client, externalObjectId, list);
                } catch (RequestFailedException ex) {
                    LOGGER.error("Can't order attachments of property '" + externalObjectId + "' ("
                            + is24ObjectId + ")!");
                    if (ex.requestRefNumber != null)
                        LOGGER.error("> referring request: " + ex.requestRefNumber);
                    logMessagesAsError(ex.responseMessages);
                    LOGGER.error("> " + ex.getLocalizedMessage(), ex);
                    this.putObjectMessage(externalObjectId, ExportMessage.Code.OBJECT_UNORDERED_ATTACHMENTS,
                            ex);
                }
            }
        }

        return is24ObjectId;
    } catch (JAXBException ex) {
        //LOGGER.error( "Can't read / write XML while communicating with the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Can't read / write XML while communicating with the Webservice!", ex);
    } catch (OAuthException ex) {
        //LOGGER.error( "Can't authorize at the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Authorization failed!", ex);
    } catch (IOException ex) {
        //LOGGER.error( "Can't communicate with the Webservice!" );
        //LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
        throw new IOExceptionWithCause("Communication failed!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportPool.java

/**
 * Return a pooled contact.//from ww  w.ja va 2 s  .  c  o  m
 *
 * @param pooledContactId
 * ID of the contact within the pool
 *
 * @return
 * contact
 *
 * @throws IOException
 * if object is not readable from local directory
 */
public RealtorContactDetails getContact(String pooledContactId) throws IOException {
    if (StringUtils.isBlank(pooledContactId))
        return null;

    final File xmlFile = new File(new File(this.contactsDir, pooledContactId), "contact.xml");
    if (!xmlFile.isFile())
        return null;

    final Unmarshaller unmarshaller;
    try {
        unmarshaller = org.openestate.is24.restapi.utils.XmlUtils.createUnmarshaller();
        if (unmarshaller == null) {
            throw new IOException("Can't init XML parser for IS24-REST!");
        }
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't init XML parser for IS24-REST!", ex);
    }

    try {
        JAXBElement<RealtorContactDetails> xml = (JAXBElement<RealtorContactDetails>) unmarshaller
                .unmarshal(xmlFile);
        RealtorContactDetails contact = xml.getValue();
        if (contact == null) {
            throw new IOException("Can't read XML file for contact '" + pooledContactId + "'!");
        }
        return contact;
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't read XML file for contact '" + pooledContactId + "'!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportPool.java

/**
 * Return a pooled real estate.//from   w w w. j  a  va2  s  . c o  m
 *
 * @param pooledObjectId
 * ID of the real estate within the pool
 *
 * @return
 * real estate
 *
 * @throws IOException
 * if object is not readable from local directory
 */
public RealEstate getObject(String pooledObjectId) throws IOException {
    if (StringUtils.isBlank(pooledObjectId))
        return null;

    final File xmlFile = new File(new File(this.objectsDir, pooledObjectId), "object.xml");
    if (!xmlFile.isFile())
        return null;

    final Unmarshaller unmarshaller;
    try {
        unmarshaller = org.openestate.is24.restapi.utils.XmlUtils.createUnmarshaller();
        if (unmarshaller == null) {
            throw new IOException("Can't init XML parser for IS24-REST!");
        }
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't init XML parser for IS24-REST!", ex);
    }

    try {
        JAXBElement<RealEstate> xml = (JAXBElement<RealEstate>) unmarshaller.unmarshal(xmlFile);
        RealEstate object = xml.getValue();
        if (object == null) {
            throw new IOException("Can't read XML file for object '" + pooledObjectId + "'!");
        }
        return object;
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't read XML file for object '" + pooledObjectId + "'!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportPool.java

/**
 * Return a pooled attachment for a real estate.
 *
 * @param pooledObjectId//from  www  . j  av  a  2  s .  c o m
 * ID of the real estate within the pool
 *
 * @param attachmentId
 * ID of the attachment
 *
 * @return
 * attachment
 *
 * @throws IOException
 * if object is not readable from local directory
 */
public Attachment getObjectAttachment(String pooledObjectId, String attachmentId) throws IOException {
    if (StringUtils.isBlank(pooledObjectId) || StringUtils.isBlank(attachmentId))
        return null;

    final File xmlFile = new File(new File(this.objectsDir, pooledObjectId),
            "attachment." + attachmentId + ".xml");
    if (!xmlFile.isFile())
        return null;

    final Unmarshaller unmarshaller;
    try {
        unmarshaller = org.openestate.is24.restapi.utils.XmlUtils.createUnmarshaller();
        if (unmarshaller == null) {
            throw new IOException("Can't init XML parser for IS24-REST!");
        }
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't init XML parser for IS24-REST!", ex);
    }

    try {
        JAXBElement<Attachment> xml = (JAXBElement<Attachment>) unmarshaller.unmarshal(xmlFile);
        Attachment attachment = xml.getValue();
        if (attachment == null) {
            throw new IOException("Can't read XML file for object '" + pooledObjectId + "'!");
        }
        //attachment.setExternalId( pooledObjectId );
        return attachment;
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't read XML file for object '" + pooledObjectId + "'!", ex);
    }
}

From source file:org.openestate.is24.restapi.utils.ExportPool.java

/**
 * Add a contact to export pool./*from www  . ja  v a 2s .co  m*/
 *
 * @param contact
 * contact
 *
 * @param pooledContactId
 * ID of the contact within the pool
 *
 * @throws IOException
 * if pooling failed
 */
public synchronized void putContact(RealtorContactDetails contact, String pooledContactId) throws IOException {
    if (contact == null)
        return;
    if (pooledContactId == null)
        pooledContactId = contact.getExternalId();
    if (StringUtils.isBlank(pooledContactId))
        throw new IOException("No pool ID was provided for the object!");
    if (StringUtils.isBlank(contact.getExternalId()))
        contact.setExternalId(pooledContactId);

    final File contactDir = new File(this.contactsDir, pooledContactId);
    if (contactDir.exists())
        FileUtils.deleteDirectory(contactDir);
    if (!contactDir.mkdirs())
        throw new IOException("Can't create folder at '" + contactDir.getAbsolutePath() + "'!");

    OutputStream output = null;
    try {
        output = new FileOutputStream(new File(contactDir, "contact.xml"));
        XmlUtils.writeXml(contact, output);
    } catch (JAXBException ex) {
        throw new IOExceptionWithCause("Can't write XML for contact '" + pooledContactId + "'!", ex);
    } finally {
        IOUtils.closeQuietly(output);
    }
}