com.sfs.upload.AdobeShareFileUploadImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.upload.AdobeShareFileUploadImpl.java

Source

/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.upload;

import com.sfs.Formatter;
import com.adobe.share.api.ShareAPI;
import com.adobe.share.api.ShareAPIException;
import com.adobe.share.api.ShareAPIUser;
import com.adobe.share.api.ShareAPINode;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * The Class AdobeShareFileUploadImpl.
 *
 * @author David Harrison
 */
public class AdobeShareFileUploadImpl implements FileUpload {

    /** The logger. */
    private static Logger logger = Logger.getLogger(AdobeShareFileUploadImpl.class);

    /** The api key. */
    private String apiKey;

    /** The api shared secret. */
    private String apiSharedSecret;

    /** The user name. */
    private String userName;

    /** The password. */
    private String password;

    /**
     * The Adobe Share API key.
     *
     * @param apiKeyVal the api key
     */
    public final void setApiKey(final String apiKeyVal) {
        this.apiKey = apiKeyVal;
    }

    /**
     * The Adobe Share shared secret.
     *
     * @param apiSharedSecretVal the api shared secret
     */
    public final void setApiSharedSecret(final String apiSharedSecretVal) {
        this.apiSharedSecret = apiSharedSecretVal;
    }

    /**
     * The Adobe Share username.
     *
     * @param userNameVal the user name
     */
    public final void setUserName(final String userNameVal) {
        this.userName = userNameVal;
    }

    /**
     * The Adobe Share password.
     *
     * @param passwordVal the password
     */
    public final void setPassword(final String passwordVal) {
        this.password = passwordVal;
    }

    /**
     * Upload the identified File to the defined storage pool.
     *
     * @param file the file
     *
     * @return the file upload details
     *
     * @throws FileUploadException the file upload exception
     */
    public final FileUploadDetails upload(final File file) throws FileUploadException {
        if (file == null) {
            throw new FileUploadException("The File object cannot be null");
        }
        if (!file.exists()) {
            throw new FileUploadException("No file exists to upload");
        }
        if (StringUtils.isBlank(this.apiKey)) {
            throw new FileUploadException("An Adobe Share API key is required");
        }
        if (StringUtils.isBlank(this.apiSharedSecret)) {
            throw new FileUploadException("An Adobe Share shared secret is required");
        }
        if (StringUtils.isBlank(this.userName)) {
            throw new FileUploadException("An Adobe Share username is required");
        }
        if (StringUtils.isBlank(this.password)) {
            throw new FileUploadException("An Adobe Share password is required");
        }

        final Date currentDate = Calendar.getInstance().getTime();
        // Add a prefix to ensure that there isn't a duplicate file name issue.
        final String format = "yyyyMMddhhmm_";
        final String fileName = Formatter.numericDate(currentDate, format) + file.getName();

        ShareAPI shareAPI = new ShareAPI(this.apiKey, this.apiSharedSecret);
        ShareAPIUser shareUser = new ShareAPIUser(this.userName, this.password);
        ShareAPINode shareNode = new ShareAPINode();

        FileUploadDetails details = null;

        try {
            logger.info("Logging into Acrobat.com");
            shareAPI.login(shareUser);
            // Upload the file to Adobe Share
            logger.info("Uploading file");
            shareNode = shareAPI.addFile(shareUser, file, fileName, "", null, true);
            logger.info("Changing privileges");
            // Set this node to be public (shared)
            shareAPI.shareFile(shareUser, shareNode, new ArrayList<String>(), userName, 2);
            logger.info("Logging out");
            shareAPI.logout(shareUser);

            if (shareNode != null) {
                details = new FileUploadDetails();
                details.setFileName(file.getName());
                details.setFileSize(file.length());
                details.setUrl(shareNode.getRecipientUrl());
            }
        } catch (HttpException he) {
            throw new FileUploadException("Error performing HTTP upload: " + he.getMessage());
        } catch (IOException ioe) {
            throw new FileUploadException("Error accessing file to upload: " + ioe.getMessage());
        } catch (ShareAPIException sapie) {
            throw new FileUploadException("Error interacting with Adobe Share: " + sapie.getMessage());
        } catch (IllegalArgumentException iae) {
            throw new FileUploadException("Error uploading file: " + iae.getMessage());
        }
        return details;
    }

}