Example usage for org.apache.http.entity StringEntity StringEntity

List of usage examples for org.apache.http.entity StringEntity StringEntity

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity StringEntity.

Prototype

public StringEntity(String str) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.nexmo.client.voice.endpoints.CreateCallMethod.java

@Override
public RequestBuilder makeRequest(Call request) throws NexmoClientException, UnsupportedEncodingException {
    return RequestBuilder.post(this.uri).setHeader("Content-Type", "application/json")
            .setEntity(new StringEntity(request.toJson()));
}

From source file:httpscheduler.LateBindingRequestHandler.java

@Override
public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {

    //System.out.println("request received");
    String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
    if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
        throw new MethodNotSupportedException(method + " method not supported");
    }//from   w  ww. j  ava2s  . c  o m

    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity httpEntity = ((HttpEntityEnclosingRequest) request).getEntity();
        String entity = EntityUtils.toString(httpEntity);

        // Parse HTTP request
        String parseResult = parseLateBindingRequest(entity, jobMap);
        StringEntity stringEntity = new StringEntity("");
        // if new job received from a client, start executing late binding policy
        if (parseResult.contains("new-job")) {
            response.setStatusCode(HttpStatus.SC_OK);
            String pieces[] = parseResult.split(":");
            taskCommExecutor.execute(
                    new LateBindingProbeThread(Integer.parseInt(pieces[1]), Integer.parseInt(pieces[2])));
            response.setEntity(new StringEntity("result:success"));
        }
        // else if probe response from worker, handle it accordingly
        else if (parseResult.contains("probe-response")) {
            //StatsLog.writeToLog("received probe-response");
            response.setStatusCode(HttpStatus.SC_OK);
            String[] pieces = parseResult.split(":");
            int jobID = Integer.parseInt(pieces[1]);

            Task task = jobMap.getTask(jobID);
            // send NOOP if task queue empty for specified job
            if (task == null) {
                response.setStatusCode(HttpStatus.SC_OK);
                try {
                    stringEntity = new StringEntity("NOOP");
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(LateBindingTaskSubmitThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("Responding with NOOP");
            }
            // else send job id, task id and task commmand to worker
            else {
                try {
                    stringEntity = new StringEntity(String.valueOf(task.getDuration()));
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(LateBindingTaskSubmitThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                //System.out.println("Responding with task duration: " + task.getDuration());
            }
            response.setEntity(stringEntity);
        }
    }
}

From source file:de.taimos.camel_cosm.CosmProducer.java

@Override
public void process(Exchange exchange) throws Exception {
    String key = this.endpoint.getApiKey();

    final String keyHeader = exchange.getIn().getHeader("CosmKey", String.class);
    if (keyHeader != null) {
        key = keyHeader;/*from   www .  jav  a  2  s. com*/
    }

    if (key == null) {
        throw new RuntimeCamelException("No ApiKey present!");
    }

    final HttpClient httpclient = new DefaultHttpClient();
    final HttpPut put = new HttpPut(this.url);
    put.setHeader("X-PachubeApiKey", key);
    put.setEntity(new StringEntity(exchange.getIn().getBody(String.class)));

    final HttpResponse response = httpclient.execute(put);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode >= 400) {
        throw new RuntimeCamelException("Error: " + response.getStatusLine().getReasonPhrase());
    }
    CosmProducer.LOG.info(response.toString());
}

From source file:org.service.ApiRest.java

/**
 * Envia la imagen codificada al servidor
 *
 * @param String encodedImage Imagen codificada con Base64
 * @throws IOException//from w w w  .  j  a va 2  s  .  c  om
 * @throws ClientProtocolException
 * @throws JSONException
 *
 */
public Boolean uploadPhoto(String encodedImage, EditText name, EditText nameFloor, EditText numberFloor)
        throws ClientProtocolException, IOException, JSONException {
    HttpClient httpclient = new DefaultHttpClient();
    //url y tipo de contenido
    HttpPost httppost = new HttpPost(HTTP_EVENT);
    httppost.addHeader("Content-Type", "application/json");
    //forma el JSON y tipo de contenido
    JSONObject jsonObject = new JSONObject();

    UUID uuid = UUID.randomUUID();
    String randomUUIDString = uuid.toString();
    jsonObject.put("idEdifice", randomUUIDString);
    jsonObject.put("NameEdifice", name.getText().toString());
    jsonObject.put("photo", encodedImage);
    jsonObject.put("idFloor", UUID.randomUUID().toString());
    jsonObject.put("NameFloor", nameFloor.getText().toString());
    jsonObject.put("NumberFloor", numberFloor.getText().toString());

    //
    StringEntity stringEntity = new StringEntity(jsonObject.toString());
    // stringEntity.setContentType((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httppost.setEntity(stringEntity);
    //ejecuta
    HttpResponse response = httpclient.execute(httppost);
    //obtiene la respuesta y transorma a objeto JSON 
    String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
    JSONObject object = new JSONObject(jsonResult);
    if (object.getString("status").equals("200")) {
        return true;
    }
    return false;
}