Example usage for org.apache.commons.httpclient.methods.multipart FilePart DEFAULT_CONTENT_TYPE

List of usage examples for org.apache.commons.httpclient.methods.multipart FilePart DEFAULT_CONTENT_TYPE

Introduction

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

Prototype

String DEFAULT_CONTENT_TYPE

To view the source code for org.apache.commons.httpclient.methods.multipart FilePart DEFAULT_CONTENT_TYPE.

Click Source Link

Usage

From source file:PublisherUtil.java

/**
 * Publishes a list of files and a datasource to the server with basic authentication to the server
 * //from w  w  w .j  a  v a 2s  .  com
 * @param publishURL
 *          The URL of the Pentaho server
 * @param publishPath
 *          The path in the solution to place the files
 * @param publishFiles
 *          Array of File objects to post to the server
 * @param dataSource
 *          The datasource to publish to the server
 * @param publishPassword
 *          The publishing password for the server
 * @param serverUserid
 *          The userid to authenticate to the server
 * @param serverPassword
 *          The password to authenticate with the server
 * @param overwrite
 *          Whether the server should overwrite the file if it exists already
 * @param mkdirs
 *          Whether the server should create any missing folders on the publish path
 * @return Server response as a string
* @throws Exception 
 */
public static String publish(final String publishURL, final String publishPath, final File publishFiles[],
        final String publishPassword, final String serverUserid, final String serverPassword,
        final boolean overwrite, final boolean mkdirs) throws Exception {
    int status = -1;
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); //$NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "warn");//$NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "warn");//$NON-NLS-1$ //$NON-NLS-2$

    String fullURL = null;
    try {
        fullURL = publishURL + "?publishPath=" + URLEncoder.encode(publishPath, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        fullURL = publishURL + "?publishPath=" + publishPath;
        System.out.println("Erro de publicao na classe publisher!" + e);
        // throw new Exception("Erro de publicao na classe publisher!" + e);
    }
    if (publishPassword == null) {
        throw new IllegalArgumentException("PUBLISHERUTIL.ERROR_0001_PUBLISH_PASSWORD_REQUIRED"); //$NON-NLS-1$
        // throw new IllegalArgumentException(Messages.getErrorString("PUBLISHERUTIL.ERROR_0001_PUBLISH_PASSWORD_REQUIRED")); //$NON-NLS-1$
    }

    fullURL += "&publishKey=" + PublisherUtil.getPasswordKey(publishPassword); //$NON-NLS-1$
    fullURL += "&overwrite=" + overwrite; //$NON-NLS-1$
    fullURL += "&mkdirs=" + mkdirs; //$NON-NLS-1$

    PostMethod filePost = new PostMethod(fullURL);

    Part[] parts = new Part[publishFiles.length];
    for (int i = 0; i < publishFiles.length; i++) {
        try {
            File file = publishFiles[i];
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            IOUtils.copy(in, out);
            String reportNameEncoded = (URLEncoder.encode(file.getName(), "UTF-8"));
            ByteArrayPartSource source = new ByteArrayPartSource(reportNameEncoded, out.toByteArray());
            parts[i] = new FilePart(reportNameEncoded, source, FilePart.DEFAULT_CONTENT_TYPE, "UTF-8");
        } catch (Exception e) {
            PublisherUtil.logger.error(null, e);
            throw new Exception("Erro de publicao na classe publisher!" + e);
        }
    }

    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
    HttpClient client = new HttpClient();

    try {
        if ((serverUserid != null) && (serverUserid.length() > 0) && (serverPassword != null)
                && (serverPassword.length() > 0)) {
            Credentials creds = new UsernamePasswordCredentials(serverUserid, serverPassword);
            client.getState().setCredentials(AuthScope.ANY, creds);
            client.getParams().setAuthenticationPreemptive(true);
        }
        status = client.executeMethod(filePost);

        if (status == HttpStatus.SC_OK) {
            String postResult = filePost.getResponseBodyAsString();

            if (postResult != null) {
                try {
                    return postResult.trim(); // +" - "+Integer.parseInt(postResult.trim());
                } catch (NumberFormatException e) {
                    PublisherUtil.logger.error(null, e);
                    // throw new Exception("Erro de publicao na classe publisher!" + e + " " +PublisherUtil.FILE_ADD_INVALID_USER_CREDENTIALS);
                    return "" + PublisherUtil.FILE_ADD_INVALID_USER_CREDENTIALS;
                }
            }
        } else if (status == HttpStatus.SC_UNAUTHORIZED) {
            return "" + PublisherUtil.FILE_ADD_INVALID_USER_CREDENTIALS;
        }
    } catch (HttpException e) {
        PublisherUtil.logger.error(null, e);
        // throw new Exception("Erro de publicao na classe publisher!" + e );
    } catch (IOException e) {
        PublisherUtil.logger.error(null, e);
        // throw new Exception("Erro de publicao na classe publisher!" + e );
    }
    // return Messages.getString("REPOSITORYFILEPUBLISHER.USER_PUBLISHER_FAILED"); //$NON-NLS-1$
    return "" + PublisherUtil.FILE_ADD_FAILED;
}

From source file:org.pentaho.platform.util.client.PublisherUtil.java

/**
 * Publishes a list of files and a datasource to the server with basic authentication to the server
 * /*  w  ww . jav a  2  s  . com*/
 * @param publishURL
 *          The URL of the Pentaho server
 * @param publishPath
 *          The path in the solution to place the files
 * @param publishFiles
 *          Array of File objects to post to the server
 * @param dataSource
 *          The datasource to publish to the server
 * @param publishPassword
 *          The publishing password for the server
 * @param serverUserid
 *          The userid to authenticate to the server
 * @param serverPassword
 *          The password to authenticate with the server
 * @param overwrite
 *          Whether the server should overwrite the file if it exists already
 * @param mkdirs
 *          Whether the server should create any missing folders on the publish path
 * @return Server response as a string
 */
public static int publish(final String publishURL, final String publishPath, final File[] publishFiles,
        final String publishPassword, final String serverUserid, final String serverPassword,
        final boolean overwrite, final boolean mkdirs) {
    int status = -1;
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); //$NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "warn"); //$NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "warn"); //$NON-NLS-1$ //$NON-NLS-2$

    String fullURL = null;
    try {
        fullURL = publishURL + "?publishPath=" + URLEncoder.encode(publishPath, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        fullURL = publishURL + "?publishPath=" + publishPath;
    }
    if (publishPassword == null) {
        throw new IllegalArgumentException(
                Messages.getInstance().getErrorString("PUBLISHERUTIL.ERROR_0001_PUBLISH_PASSWORD_REQUIRED")); //$NON-NLS-1$
    }

    fullURL += "&publishKey=" + PublisherUtil.getPasswordKey(publishPassword); //$NON-NLS-1$
    fullURL += "&overwrite=" + overwrite; //$NON-NLS-1$
    fullURL += "&mkdirs=" + mkdirs; //$NON-NLS-1$

    PostMethod filePost = new PostMethod(fullURL);
    Part[] parts = new Part[publishFiles.length];
    for (int i = 0; i < publishFiles.length; i++) {
        try {
            File file = publishFiles[i];
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            IOUtils.copy(in, out);
            String reportNameEncoded = (URLEncoder.encode(file.getName(), "UTF-8"));
            ByteArrayPartSource source = new ByteArrayPartSource(reportNameEncoded, out.toByteArray());
            parts[i] = new FilePart(reportNameEncoded, source, FilePart.DEFAULT_CONTENT_TYPE, "UTF-8");
        } catch (Exception e) {
            PublisherUtil.logger.error(null, e);
        }
    }
    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
    HttpClient client = new HttpClient();
    try {
        // If server userid/password was supplied, use basic authentication to
        // authenticate with the server.
        if ((serverUserid != null) && (serverUserid.length() > 0) && (serverPassword != null)
                && (serverPassword.length() > 0)) {
            Credentials creds = new UsernamePasswordCredentials(serverUserid, serverPassword);
            client.getState().setCredentials(AuthScope.ANY, creds);
            client.getParams().setAuthenticationPreemptive(true);
        }
        status = client.executeMethod(filePost);

        if (status == HttpStatus.SC_OK) {
            String postResult = filePost.getResponseBodyAsString();

            if (postResult != null) {
                try {
                    return Integer.parseInt(postResult.trim());
                } catch (NumberFormatException e) {
                    PublisherUtil.logger.error(null, e);
                    return PublisherUtil.FILE_ADD_INVALID_USER_CREDENTIALS;
                }
            }
        } else if (status == HttpStatus.SC_UNAUTHORIZED) {
            return PublisherUtil.FILE_ADD_INVALID_USER_CREDENTIALS;
        }
    } catch (HttpException e) {
        PublisherUtil.logger.error(null, e);
    } catch (IOException e) {
        PublisherUtil.logger.error(null, e);
    }
    // return Messages.getString("REPOSITORYFILEPUBLISHER.USER_PUBLISHER_FAILED"); //$NON-NLS-1$
    return PublisherUtil.FILE_ADD_FAILED;
}