Example usage for org.apache.commons.httpclient.methods.multipart PartSource getFileName

List of usage examples for org.apache.commons.httpclient.methods.multipart PartSource getFileName

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods.multipart PartSource getFileName.

Prototype

public abstract String getFileName();

Source Link

Usage

From source file:org.safecreative.api.RegisterWork.java

/**
* @param fileName File name/*from  w  ww .  j  a  va  2  s  .com*/
* @param workFile PartSource data to register
* @param work Work containing register parameters to override those of defined profile,
* optional if license is defined    
* @param checksum 
* @return registration code
* @throws Exception  
 */
public String registerWork(String fileName, PartSource workFile, Work work, String checksum) throws Exception {
    if (StringUtils.isEmpty(getProfile()) && work == null) { // ERROR
        throw new IllegalArgumentException("No profile or parameters defined");
    }
    String baseUrl = api.getBaseUrl();
    ////////////////////////////////////////////////////////////////////
    //Look up:
    log.debug("registerWork Lookup upload server");
    Map params = api.createParams("component", "work.upload.lookup");

    params.put("authkey", api.getAuthKey());
    if (workFile != null) {
        String name = fileName == null ? workFile.getFileName() : fileName;
        if (name != null) {
            params.put("filename", name);
        }
    }
    if (postUpload) {
        params.put("bypost", "true");
    }
    String response = api.callSigned(params, api.getPrivateAuthKey(), true, false);
    checkError(params, response);
    String uploadURL = api.evalXml(response, "/workuploadlookup/uploadurl");
    URL url = new URL(uploadURL);
    log.debug("Upload URL: {}", url);
    String uploadID = api.evalXml(response, "/workuploadlookup/uploadid");
    log.debug("Upload id: {}", uploadID);
    String uploadTicket = null;
    if (workFile != null) {
        if (postUpload) {
            ////////////////////////////////////////////////////////////////////
            //POST Upload:
            log.info("registerWork upload by post file: {}", workFile);
            params = api.createParams("uploadid", uploadID);
            uploadTicket = postFile(uploadURL, params, workFile);
            log.info("Successfully uploaded file: {}", workFile);
        } else {
            ////////////////////////////////////////////////////////////////////
            //Begin:
            uploadTicket = uploadFile(uploadURL, uploadID, workFile, checksum);
        }
        log.debug("uploadTicket {}", uploadTicket);
    }
    ////////////////////////////////////////////////////////////////////
    //Work registration:
    log.info("REGISTRATION");
    api.setBaseUrl(baseUrl);
    params = api.createParams("component", "work.register");
    params.put("authkey", api.getAuthKey());
    if (uploadTicket != null) {
        params.put("uploadticket", uploadTicket);
    }

    params.put("final", registryFinal ? "1" : "0");
    if (StringUtils.isNotBlank(code)) {
        params.put("code", getCode());
    }

    if (!StringUtils.isEmpty(getProfile())) {
        params.put("profile", getProfile());
    }
    if (work != null) {
        params = ParamsBuilder.buildWorkParams(params, work);
    }

    String result = api.callSigned(params, api.getPrivateAuthKey(), true, true);
    checkError(params, result);
    log.debug("work: {}", result);
    return api.evalXml(result, "/workregistry/code");
}

From source file:org.safecreative.api.RegisterWork.java

/**
 * Post data using an instance of <code>PartSource</code>
 * @param uploadUrl/*from www  .j  a v  a2  s.  co m*/
 * @param params
 * @param file <code>PartSource</code> to upload
 * @return uploadticket
 */
public String postFile(String uploadUrl, Map<String, String> params, final PartSource file) {
    PostMethod post = null;
    try {
        log.debug(String.format("api request by post: \n%s/%s\n", uploadUrl, file.getFileName()));
        if (client == null) {
            client = new HttpClient();
        }
        post = new PostMethod(uploadUrl);
        Part[] parts = new Part[params.size() + 1];
        int i = 0;
        for (Map.Entry<String, String> param : params.entrySet()) {
            parts[i++] = new StringPart(param.getKey(), param.getValue(), SafeCreativeAPI.DEFAULT_ENCODING);
        }
        parts[i] = new FilePart("file", file, "application/octet-stream", SafeCreativeAPI.DEFAULT_ENCODING) {

            @Override
            protected void sendData(OutputStream out) throws IOException {
                if (uploadProgressListener == null) {
                    super.sendData(out);
                } else {
                    super.sendData(new PostUploadProgressListenerOutputStream(out, uploadProgressListener,
                            file.getLength()));
                }
            }

        };
        MultipartRequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
        post.setRequestEntity(entity);
        int status = client.executeMethod(post);
        if (status != HttpURLConnection.HTTP_OK) {
            throw new IOException("Response error " + status);
        }

        return post.getResponseBodyAsString();

    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }
}

From source file:org.safecreative.api.wrapper.SafeCreativeAPIWrapper.java

/**
 * Uploads a file part source for register or update a work
 * //from w  ww  .  j  a va  2s  . c  o m
 * @param fileSource <code>PartSource</code> to upload 
 * @param byPost true - upload file using POST, false - upload by API
* @param uploadListener Upload progress listener to notify if not <code>null</code>* 
* @param checkSum Optional SHA-1 checksum if available, otherwise it will be calculated again for the file part source
 * @return upload ticket or null if fails
 * @throws ApiException
 */
public String uploadFileSource(PartSource fileSource, boolean byPost, UploadProgressListener uploadListener,
        String... checkSum) throws ApiException {
    if (fileSource == null) {
        return null;
    } else {
        // Look up
        Map<String, String> params = api.createParams("component", "work.upload.lookup");
        params.put("authkey", authKey.getAuthkey());
        params.put("filename", fileSource.getFileName());
        if (byPost) {
            params.put("bypost", "true");
        }
        String response = api.callSigned(params, authKey.getPrivatekey(), true, false);
        checkError(response);

        String uploadURL = api.evalXml(response, "/workuploadlookup/uploadurl");
        log.debug("Upload URL: {}", uploadURL);
        String uploadID = api.evalXml(response, "/workuploadlookup/uploadid");
        log.debug("Upload id: {}", uploadID);

        // Upload
        String uploadTicket = null;
        RegisterWork registerer = new RegisterWork(api);
        registerer.setUploadProgressListener(uploadListener);

        api.setBaseUrl(uploadURL);
        if (byPost) {
            ////////////////////////////////////////////////////////////////////
            //POST Upload:
            log.info("registerWork upload by post file: {}", fileSource.getFileName());
            params = api.createParams("uploadid", uploadID);
            uploadTicket = registerer.postFile(uploadURL, params, fileSource);
            log.info("Successfully uploaded file: {}", fileSource);
        } else {
            ////////////////////////////////////////////////////////////////////
            //API Upload:
            try {
                log.info("registerWork upload by API file: {}", fileSource.getFileName());
                String sha1CheckSum = null;
                if (checkSum != null && checkSum.length == 1) {
                    sha1CheckSum = checkSum[0];
                }
                if (StringUtils.isBlank(sha1CheckSum)) {
                    log.info("Building SHA-1 checksum");
                    sha1CheckSum = Digest.getHexDigest(fileSource.createInputStream(), Digest.SHA1);
                }
                uploadTicket = registerer.uploadFile(uploadURL, uploadID, fileSource, sha1CheckSum);
                log.info("Successfully uploaded file: {}", fileSource.getFileName());
            } catch (Exception ex) {
                throw ApiException.wrap(ex);
            }
        }
        log.debug("uploadTicket {}", uploadTicket);
        return uploadTicket;
    }
}

From source file:org.scohen.juploadr.event.MonitorFilePart.java

/**
 * @param arg0//from   w w w  . ja  v a 2 s .  co  m
 * @param arg1
 */
public MonitorFilePart(String arg0, PartSource arg1, ImageAttributes img) {
    super(arg0, arg1);
    uploadFile = new File(arg1.getFileName());
    this.image = img;
}

From source file:org.scohen.juploadr.event.MonitorFilePart.java

/**
 * @param arg0//w  ww  .  j a v a  2 s .com
 * @param arg1
 * @param arg2
 * @param arg3
 */
public MonitorFilePart(String arg0, PartSource arg1, String arg2, String arg3, ImageAttributes img) {
    super(arg0, arg1, arg2, arg3);
    uploadFile = new File(arg1.getFileName());
    this.image = img;
}