Search in sources :

Example 6 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project android-instagram by markchang.

the class TakePictureActivity method doUpload.

// fixme: this doesn't need to be a Map return
public Map<String, String> doUpload() {
    Log.i(TAG, "Upload");
    Long timeInMilliseconds = System.currentTimeMillis() / 1000;
    String timeInSeconds = timeInMilliseconds.toString();
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    Map returnMap = new HashMap<String, String>();
    // check for cookies
    if (httpClient.getCookieStore() == null) {
        returnMap.put("result", "Not logged in");
        return returnMap;
    }
    try {
        // create multipart data
        File imageFile = new File(processedImageUri.getPath());
        FileBody partFile = new FileBody(imageFile);
        StringBody partTime = new StringBody(timeInSeconds);
        multipartEntity.addPart("photo", partFile);
        multipartEntity.addPart("device_timestamp", partTime);
    } catch (Exception e) {
        Log.e(TAG, "Error creating mulitpart form: " + e.toString());
        returnMap.put("result", "Error creating mulitpart form: " + e.toString());
        return returnMap;
    }
    // upload
    try {
        HttpPost httpPost = new HttpPost(Utils.UPLOAD_URL);
        httpPost.setEntity(multipartEntity);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        Log.i(TAG, "Upload status: " + httpResponse.getStatusLine());
        // test result code
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            Log.e(TAG, "Login HTTP status fail: " + httpResponse.getStatusLine().getStatusCode());
            returnMap.put("result", "HTTP status error: " + httpResponse.getStatusLine().getStatusCode());
            return returnMap;
        }
        /*
            {"status": "ok"}
            */
        if (httpEntity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"));
            String json = reader.readLine();
            JSONTokener jsonTokener = new JSONTokener(json);
            JSONObject jsonObject = new JSONObject(jsonTokener);
            Log.i(TAG, "JSON: " + jsonObject.toString());
            String loginStatus = jsonObject.getString("status");
            if (!loginStatus.equals("ok")) {
                Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
                returnMap.put("result", "JSON status not ok: " + jsonObject.getString("status"));
                return returnMap;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "HttpPost exception: " + e.toString());
        returnMap.put("result", "HttpPost exception: " + e.toString());
        return returnMap;
    }
    // configure / comment
    try {
        HttpPost httpPost = new HttpPost(Utils.CONFIGURE_URL);
        String partComment = txtCaption.getText().toString();
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("device_timestamp", timeInSeconds));
        postParams.add(new BasicNameValuePair("caption", partComment));
        httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        // test result code
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            Log.e(TAG, "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
            returnMap.put("result", "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
            return returnMap;
        }
        returnMap.put("result", "ok");
        return returnMap;
    } catch (Exception e) {
        Log.e(TAG, "HttpPost comment error: " + e.toString());
        returnMap.put("result", "HttpPost comment error: " + e.toString());
        return returnMap;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project felix by apache.

the class ITScriptConsolePlugin method execute.

private void execute(ContentBody code) throws Exception {
    RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
    final MultipartEntity entity = new MultipartEntity();
    // Add Sling POST options
    entity.addPart("lang", new StringBody("groovy"));
    entity.addPart("code", code);
    executor.execute(rb.buildPostRequest("/system/console/sc").withEntity(entity).withCredentials("admin", "admin")).assertStatus(200);
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody)

Example 8 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project sling by apache.

the class WebconsoleClient method installBundle.

/** Install a bundle using the Felix webconsole HTTP interface, with a specific start level */
public void installBundle(File f, boolean startBundle, int startLevel) throws Exception {
    // Setup request for Felix Webconsole bundle install
    final MultipartEntity entity = new MultipartEntity();
    entity.addPart("action", new StringBody("install"));
    if (startBundle) {
        entity.addPart("bundlestart", new StringBody("true"));
    }
    entity.addPart("bundlefile", new FileBody(f));
    if (startLevel > 0) {
        entity.addPart("bundlestartlevel", new StringBody(String.valueOf(startLevel)));
        log.info("Installing bundle {} at start level {}", f.getName(), startLevel);
    } else {
        log.info("Installing bundle {} at default start level", f.getName());
    }
    // Console returns a 302 on success (and in a POST this
    // is not handled automatically as per HTTP spec)
    executor.execute(builder.buildPostRequest(CONSOLE_BUNDLES_PATH).withCredentials(username, password).withEntity(entity)).assertStatus(302);
}
Also used : FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody)

Example 9 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project sling by apache.

the class WebconsoleClient method uninstallBundle.

public void uninstallBundle(String symbolicName, File f) throws Exception {
    final long bundleId = getBundleId(symbolicName);
    log.info("Uninstalling bundle {} with bundleId {}", symbolicName, bundleId);
    final MultipartEntity entity = new MultipartEntity();
    entity.addPart("action", new StringBody("uninstall"));
    executor.execute(builder.buildPostRequest(CONSOLE_BUNDLES_PATH + "/" + bundleId).withCredentials(username, password).withEntity(entity)).assertStatus(200);
}
Also used : MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody)

Example 10 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project sling by apache.

the class SlingClient method createNode.

/** Create a node at specified path, with optional properties
     * @param path Used in POST request to Sling server
     * @param properties If not null, properties are added to the created node
     * @return The actual path of the node that was created
     */
public String createNode(String path, Map<String, Object> properties) throws UnsupportedEncodingException, IOException {
    String actualPath = null;
    final MultipartEntity entity = new MultipartEntity();
    // Add Sling POST options 
    entity.addPart(":redirect", new StringBody("*"));
    entity.addPart(":displayExtension", new StringBody(""));
    // Add user properties
    if (properties != null) {
        for (Map.Entry<String, Object> e : properties.entrySet()) {
            entity.addPart(e.getKey(), new StringBody(e.getValue().toString()));
        }
    }
    final HttpResponse response = executor.execute(builder.buildPostRequest(path).withEntity(entity).withCredentials(username, password)).assertStatus(302).getResponse();
    final Header location = response.getFirstHeader(LOCATION_HEADER);
    assertNotNull("Expecting " + LOCATION_HEADER + " in response", location);
    actualPath = locationToPath(location.getValue());
    return actualPath;
}
Also used : Header(org.apache.http.Header) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MultipartEntity (org.apache.http.entity.mime.MultipartEntity)62 HttpPost (org.apache.http.client.methods.HttpPost)41 StringBody (org.apache.http.entity.mime.content.StringBody)40 FileBody (org.apache.http.entity.mime.content.FileBody)37 File (java.io.File)31 HttpResponse (org.apache.http.HttpResponse)28 Test (org.junit.Test)24 TestHttpClient (io.undertow.testutils.TestHttpClient)13 IOException (java.io.IOException)13 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 JSONObject (org.json.JSONObject)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)5 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)5 JSONException (org.json.JSONException)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 InputStream (java.io.InputStream)4