Example usage for org.apache.http.entity.mime MultipartEntity addPart

List of usage examples for org.apache.http.entity.mime MultipartEntity addPart

Introduction

In this page you can find the example usage for org.apache.http.entity.mime MultipartEntity addPart.

Prototype

public void addPart(final String name, final ContentBody contentBody) 

Source Link

Usage

From source file:bluej.collect.DataCollectorImpl.java

public static void invokeMethodSuccess(Package pkg, String code, String objName, String typeName,
        int testIdentifier, int invocationIdentifier) {
    if (!false) {
        MultipartEntity mpe = new MultipartEntity();

        mpe.addPart("event[invoke][code]", CollectUtility.toBody(code));
        mpe.addPart("event[invoke][type_name]", CollectUtility.toBody(typeName));
        mpe.addPart("event[invoke][result]", CollectUtility.toBody("success"));
        mpe.addPart("event[invoke][test_identifier]", CollectUtility.toBody(testIdentifier));
        mpe.addPart("event[invoke][invoke_identifier]", CollectUtility.toBody(invocationIdentifier));
        if (objName != null) {
            mpe.addPart("event[invoke][bench_object][class_name]", CollectUtility.toBody(typeName));
            mpe.addPart("event[invoke][bench_object][name]", CollectUtility.toBody(objName));
        }// www . j  a v a 2  s.c  om
        submitEvent(pkg.getProject(), pkg, EventName.INVOKE_METHOD, new PlainEvent(mpe));
    }
}

From source file:bluej.collect.DataCollectorImpl.java

public static void testResult(Package pkg, DebuggerTestResult lastResult) {
    MultipartEntity mpe = new MultipartEntity();

    mpe.addPart("event[class_name]", CollectUtility.toBody(lastResult.getQualifiedClassName()));
    mpe.addPart("event[method_name]", CollectUtility.toBody(lastResult.getMethodName()));
    mpe.addPart("event[run_time]", CollectUtility.toBody(lastResult.getRunTimeMs()));
    String status = "unknown";
    if (lastResult.isSuccess())
        status = "success";
    else if (lastResult.isFailure())
        status = "failure";
    else if (lastResult.isError())
        status = "error";
    mpe.addPart("event[result]", CollectUtility.toBody(status));

    if (!lastResult.isSuccess()) {
        mpe.addPart("event[exception_message]", CollectUtility.toBody(lastResult.getExceptionMessage()));
        mpe.addPart("event[exception_trace]", CollectUtility.toBody(lastResult.getTrace()));
    }//ww  w.  j a v a 2  s  . c  o m

    submitEvent(pkg.getProject(), pkg, EventName.RUN_TEST, new PlainEvent(mpe));
}

From source file:bluej.collect.DataCollectorImpl.java

public static void compiled(Project proj, Package pkg, File[] sources, List<DiagnosticWithShown> diagnostics,
        boolean success) {
    MultipartEntity mpe = new MultipartEntity();

    mpe.addPart("event[compile_success]", CollectUtility.toBody(success));

    ProjectDetails projDetails = new ProjectDetails(proj);
    for (File src : sources) {
        mpe.addPart("event[compile_input][][source_file_name]",
                CollectUtility.toBody(CollectUtility.toPath(projDetails, src)));
    }/*from  ww  w  . ja  va  2 s .c om*/

    for (DiagnosticWithShown dws : diagnostics) {
        final Diagnostic d = dws.getDiagnostic();

        mpe.addPart("event[compile_output][][is_error]",
                CollectUtility.toBody(d.getType() == Diagnostic.ERROR));
        mpe.addPart("event[compile_output][][shown]", CollectUtility.toBody(dws.wasShownToUser()));
        mpe.addPart("event[compile_output][][message]", CollectUtility.toBody(d.getMessage()));
        if (d.getFileName() != null) {
            mpe.addPart("event[compile_output][][start_line]", CollectUtility.toBody(d.getStartLine()));
            mpe.addPart("event[compile_output][][end_line]", CollectUtility.toBody(d.getEndLine()));
            mpe.addPart("event[compile_output][][start_column]", CollectUtility.toBody(d.getStartColumn()));
            mpe.addPart("event[compile_output][][end_column]", CollectUtility.toBody(d.getEndColumn()));
            // Must make file name relative for anonymisation:
            String relative = CollectUtility.toPath(projDetails, new File(d.getFileName()));
            mpe.addPart("event[compile_output][][source_file_name]", CollectUtility.toBody(relative));
        }
    }
    submitEvent(proj, pkg, EventName.COMPILE, new PlainEvent(mpe));
}

From source file:com.fhc25.percepcion.osiris.mapviewer.common.restutils.RestClient.java

/**
 * Executes the requests.//  www.  j a  va  2s.  c om
 *
 * @param method   the type of method (POST, GET, DELETE, PUT).
 * @param url      the url of the request.
 * @param headers  the headers to include.
 * @param listener a listener for callbacks.
 * @throws Exception
 */
public static void Execute(final File file, final RequestMethod method, final String url,
        final ArrayList<NameValuePair> headers, final RestListener listener) throws Exception {
    new Thread() {
        @Override
        public void run() {

            switch (method) {
            case GET:
                // Do nothing
                break;

            case POST: {

                HttpPost request = new HttpPost(url);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }

                // code file
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                Log.d(TAG, "UPLOAD: file length = " + file.length());
                Log.d(TAG, "UPLOAD: file exist = " + file.exists());
                FileBody bodyPart = new FileBody(file);
                entity.addPart("file", bodyPart);
                request.setEntity(entity);

                Log.i(TAG, "Request with File:" + request.getEntity());

                executeRequest(request, url, listener);
            }
                break;

            case PUT: {
                HttpPut request = new HttpPut(url);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }

                // code file
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                Log.d(TAG, "UPLOAD: file length = " + file.length());
                Log.d(TAG, "UPLOAD: file exist = " + file.exists());
                FileBody bodyPart = new FileBody(file);
                entity.addPart("file", bodyPart);
                request.setEntity(entity);

                Log.v(TAG, "Request " + request.toString());
                executeRequest(request, url, listener);
            }
                break;

            case DELETE:
                // Do nothing
                break;

            } // switch end
        }
    }.start();
}

From source file:bluej.collect.DataCollectorImpl.java

public static void teamUpdateProject(Project project, Repository repo, Collection<File> updatedFiles) {
    final ProjectDetails projDetails = new ProjectDetails(project);
    MultipartEntity mpe = DataCollectorImpl.getRepoMPE(repo);
    for (File f : updatedFiles) {
        mpe.addPart("vcs_files[][file]", CollectUtility.toBodyLocal(projDetails, f));
    }/*from   w ww  .j  av a 2  s .com*/
    submitEvent(project, null, EventName.VCS_UPDATE, new PlainEvent(mpe));
}

From source file:bluej.collect.DataCollectorImpl.java

public static void teamCommitProject(Project project, Repository repo, Collection<File> committedFiles) {
    final ProjectDetails projDetails = new ProjectDetails(project);
    MultipartEntity mpe = DataCollectorImpl.getRepoMPE(repo);
    for (File f : committedFiles) {
        mpe.addPart("vcs_files[][file]", CollectUtility.toBodyLocal(projDetails, f));
    }/*from w ww .j  a v  a 2s.com*/
    submitEvent(project, null, EventName.VCS_COMMIT, new PlainEvent(mpe));
}

From source file:bluej.collect.DataCollectorImpl.java

private static void addExtensions(MultipartEntity mpe, List<ExtensionWrapper> extensions) {
    for (ExtensionWrapper ext : extensions) {
        mpe.addPart("extensions[][name]", CollectUtility.toBody(ext.safeGetExtensionName()));
        mpe.addPart("extensions[][version]", CollectUtility.toBody(ext.safeGetExtensionVersion()));
    }/*  w w w . j  av a 2  s  .  c  om*/
}

From source file:com.mobile.natal.natalchart.NetworkUtilities.java

/**
 * Sends a {@link MultipartEntity} post with text and image files.
 *
 * @param url        the url to which to POST to.
 * @param user       the user or <code>null</code>.
 * @param pwd        the password or <code>null</code>.
 * @param stringsMap the {@link HashMap} containing the key and string pairs to send.
 * @param filesMap   the {@link HashMap} containing the key and image file paths
 *                   (jpg, png supported) pairs to send.
 * @throws Exception if something goes wrong.
 *//*from  w  ww.j a v  a 2  s . c  o m*/
public static void sentMultiPartPost(String url, String user, String pwd, HashMap<String, String> stringsMap,
        HashMap<String, File> filesMap) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httppost = new HttpPost(url);

    if (user != null && pwd != null && user.trim().length() > 0 && pwd.trim().length() > 0) {
        String ret = getB64Auth(user, pwd);
        httppost.setHeader("Authorization", ret);
    }

    MultipartEntity mpEntity = new MultipartEntity();
    Set<Entry<String, String>> stringsEntrySet = stringsMap.entrySet();
    for (Entry<String, String> stringEntry : stringsEntrySet) {
        ContentBody cbProperties = new StringBody(stringEntry.getValue());
        mpEntity.addPart(stringEntry.getKey(), cbProperties);
    }

    Set<Entry<String, File>> filesEntrySet = filesMap.entrySet();
    for (Entry<String, File> filesEntry : filesEntrySet) {
        String propName = filesEntry.getKey();
        File file = filesEntry.getValue();
        if (file.exists()) {
            String ext = file.getName().toLowerCase().endsWith("jpg") ? "jpeg" : "png";
            ContentBody cbFile = new FileBody(file, "image/" + ext);
            mpEntity.addPart(propName, cbFile);
        }
    }

    httppost.setEntity(mpEntity);
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    if (resEntity != null) {
        resEntity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();
}

From source file:bluej.collect.DataCollectorImpl.java

/**
 * Adds the given stack trace to the MPE, using the given list name.
 *//*  w w w.j  ava 2  s  .c om*/
private static void addStackTrace(MultipartEntity mpe, String listName, SourceLocation[] stack) {
    for (int i = 0; i < stack.length; i++) {
        mpe.addPart(listName + "[][entry]", CollectUtility.toBody(i));
        mpe.addPart(listName + "[][class_name]", CollectUtility.toBody(stack[i].getClassName()));
        mpe.addPart(listName + "[][class_source_name]", CollectUtility.toBody(stack[i].getFileName()));
        mpe.addPart(listName + "[][line_number]", CollectUtility.toBody(stack[i].getLineNumber()));
    }
}

From source file:bluej.collect.DataCollectorImpl.java

public static void teamStatusProject(Project project, Repository repo, Map<File, String> status) {
    final ProjectDetails projDetails = new ProjectDetails(project);
    MultipartEntity mpe = DataCollectorImpl.getRepoMPE(repo);
    for (Map.Entry<File, String> s : status.entrySet()) {
        mpe.addPart("vcs_files[][file]", CollectUtility.toBodyLocal(projDetails, s.getKey()));
        mpe.addPart("vcs_files[][status]", CollectUtility.toBody(s.getValue()));
    }//from  w w  w.j a va 2  s.com
    submitEvent(project, null, EventName.VCS_STATUS, new PlainEvent(mpe));
}