Example usage for org.apache.http.client.methods HttpPut METHOD_NAME

List of usage examples for org.apache.http.client.methods HttpPut METHOD_NAME

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPut METHOD_NAME.

Prototype

String METHOD_NAME

To view the source code for org.apache.http.client.methods HttpPut METHOD_NAME.

Click Source Link

Usage

From source file:com.microsoft.windowsazure.mobileservices.http.MobileServiceHttpClient.java

/**
 * Makes a request over HTTP//from   w  ww.j a va  2s  .  c  o m
 *
 * @param path           The path of the request URI
 * @param content        The byte array to send as the request body
 * @param httpMethod     The HTTP Method used to invoke the API
 * @param requestHeaders The extra headers to send in the request
 * @param parameters     The query string parameters sent in the request
 * @param features       The features used in the request
 */
public ListenableFuture<ServiceFilterResponse> request(String path, byte[] content, String httpMethod,
        List<Pair<String, String>> requestHeaders, List<Pair<String, String>> parameters,
        EnumSet<MobileServiceFeatures> features) {
    final SettableFuture<ServiceFilterResponse> future = SettableFuture.create();

    if (path == null || path.trim().equals("")) {
        future.setException(new IllegalArgumentException("request path cannot be null"));
        return future;
    }

    if (httpMethod == null || httpMethod.trim().equals("")) {
        future.setException(new IllegalArgumentException("httpMethod cannot be null"));
        return future;
    }

    Uri.Builder uriBuilder = Uri.parse(mClient.getAppUrl().toString()).buildUpon();
    uriBuilder.path(path);

    if (parameters != null && parameters.size() > 0) {
        for (Pair<String, String> parameter : parameters) {
            uriBuilder.appendQueryParameter(parameter.first, parameter.second);
        }
    }

    ServiceFilterRequestImpl request;
    String url = uriBuilder.build().toString();

    if (httpMethod.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpGet(url), mClient.getAndroidHttpClientFactory());
    } else if (httpMethod.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpPost(url), mClient.getAndroidHttpClientFactory());
    } else if (httpMethod.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpPut(url), mClient.getAndroidHttpClientFactory());
    } else if (httpMethod.equalsIgnoreCase(HttpPatch.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpPatch(url), mClient.getAndroidHttpClientFactory());
    } else if (httpMethod.equalsIgnoreCase(HttpDelete.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpDelete(url), mClient.getAndroidHttpClientFactory());
    } else {
        future.setException(new IllegalArgumentException("httpMethod not supported"));
        return future;
    }

    String featuresHeader = MobileServiceFeatures.featuresToString(features);
    if (featuresHeader != null) {
        if (requestHeaders == null) {
            requestHeaders = new ArrayList<Pair<String, String>>();
        }

        boolean containsFeatures = false;
        for (Pair<String, String> header : requestHeaders) {
            if (header.first.equals(X_ZUMO_FEATURES)) {
                containsFeatures = true;
                break;
            }
        }

        if (!containsFeatures) {
            // Clone header list to prevent changing user's list
            requestHeaders = new ArrayList<Pair<String, String>>(requestHeaders);
            requestHeaders.add(new Pair<String, String>(X_ZUMO_FEATURES, featuresHeader));
        }
    }

    if (requestHeaders != null && requestHeaders.size() > 0) {
        for (Pair<String, String> header : requestHeaders) {
            request.addHeader(header.first, header.second);
        }
    }

    if (content != null) {
        try {
            request.setContent(content);
        } catch (Exception e) {
            future.setException(e);
            return future;
        }
    }

    MobileServiceConnection conn = mClient.createConnection();

    new RequestAsyncTask(request, conn) {
        @Override
        protected void onPostExecute(ServiceFilterResponse response) {
            if (mTaskException != null) {
                future.setException(mTaskException);
            } else {
                future.set(response);
            }
        }
    }.executeTask();

    return future;
}

From source file:code.google.restclient.core.Hitter.java

/**
 * Method to make POST or PUT request by sending http entity (as body)
 *//*www . jav  a  2s.c  o m*/
public void hit(String url, String methodName, HttpHandler handler, Map<String, String> requestHeaders)
        throws Exception {

    if (DEBUG_ENABLED)
        LOG.debug("hit() - method => " + methodName + ", url => " + url);

    if (HttpGet.METHOD_NAME.equals(methodName)) {
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> GET " + url);
        hit(url, new HttpGet(url), handler, requestHeaders);
    } else if (HttpHead.METHOD_NAME.equals(methodName)) {
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> HEAD " + url);
        hit(url, new HttpHead(url), handler, requestHeaders);
    } else if (HttpDelete.METHOD_NAME.equals(methodName)) {
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> DELETE " + url);
        hit(url, new HttpDelete(url), handler, requestHeaders);
    } else if (HttpOptions.METHOD_NAME.equals(methodName)) {
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> OPTIONS " + url);
        hit(url, new HttpOptions(url), handler, requestHeaders);
    } else if (HttpTrace.METHOD_NAME.equals(methodName)) {
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> TRACE " + url);
        hit(url, new HttpTrace(url), handler, requestHeaders);
    } else if (HttpPost.METHOD_NAME.equals(methodName)) { // POST
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> POST " + url);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(handler.getReqBodyEntity());
        hit(url, httpPost, handler, requestHeaders);
    } else if (HttpPut.METHOD_NAME.equals(methodName)) { // PUT
        if (DEBUG_ENABLED)
            LOG.debug("hit() - ===> PUT " + url);
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(handler.getReqBodyEntity());
        hit(url, httpPut, handler, requestHeaders);
    } else {
        throw new IllegalArgumentException("hit(): Unsupported method => " + methodName);
    }
}

From source file:com.uploader.Vimeo.java

public VimeoResponse addEmbedPreset(String videoEndPoint, String presetId) throws IOException {
    return apiRequest(new StringBuffer(videoEndPoint).append("/presets/").append(presetId).toString(),
            HttpPut.METHOD_NAME, null, null);
}

From source file:org.apache.edgent.connectors.http.HttpStreams.java

/**
 * Make an HTTP PUT request with JsonObject. <br>
 * /*from   w  ww.j  av a  2s .c  om*/
 * Method specifically works with JsonObjects. For each JsonObject in the
 * stream, HTTP PUT request is executed on provided uri. Request body is
 * filled using HttpEntity provided by body function. As a result, Response
 * is added to the response TStream.<br>
 * 
 * Sample usage:<br>
 * 
 * <pre>
 * {@code
 *     DirectProvider ep = new DirectProvider();
 *     Topology topology = ep.newTopology();
 *     final String url = "http://httpbin.org/put";
 * 
 *     JsonObject body = new JsonObject();
 *     body.addProperty("foo", "abc");
 *     body.addProperty("bar", 42);
 * 
 *     TStream<JsonObject> stream = topology.collection(Arrays.asList(body));
 *     TStream<JsonObject> rc = HttpStreams.putJson(stream,
 *             HttpClients::noAuthentication, t -> url, t -> t);
 * }
 * </pre>
 * 
 * <br>
 * See HttpTest for example. <br>
 * 
 * @param stream - JsonObject TStream.
 * @param clientCreator - CloseableHttpClient supplier preferably created using {@link HttpClients}
 * @param uri - URI function which returns URI string
 * @param body - Function that returns JsonObject which will be set as a body for the request.
 * @return TStream of JsonObject which contains responses of PUT requests
 * 
 * @see HttpStreams#requestsWithBody(TStream, Supplier, Function, Function, Function, BiFunction)
 */
public static TStream<JsonObject> putJson(TStream<JsonObject> stream,
        Supplier<CloseableHttpClient> clientCreator, Function<JsonObject, String> uri,
        UnaryOperator<JsonObject> body) {
    return HttpStreams.<JsonObject, JsonObject>requestsWithBody(stream, clientCreator, t -> HttpPut.METHOD_NAME,
            uri, t -> new ByteArrayEntity(body.apply(t).toString().getBytes()), HttpResponders.json());
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request createIndex(CreateIndexRequest createIndexRequest) throws IOException {
    String endpoint = endpoint(createIndexRequest.indices());
    Request request = new Request(HttpPut.METHOD_NAME, endpoint);

    Params parameters = new Params(request);
    parameters.withTimeout(createIndexRequest.timeout());
    parameters.withMasterTimeout(createIndexRequest.masterNodeTimeout());
    parameters.withWaitForActiveShards(createIndexRequest.waitForActiveShards());

    request.setEntity(createEntity(createIndexRequest, REQUEST_BODY_CONTENT_TYPE));
    return request;
}

From source file:com.evrythng.java.wrapper.util.FileUtils.java

private static HttpURLConnection getConnectionForPublicUpload(final URL url, final String contentType)
        throws IOException {

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(HttpPut.METHOD_NAME);
    connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, contentType);
    connection.setRequestProperty(X_AMZ_ACL_HEADER_NAME, X_AMZ_ACL_HEADER_VALUE_PUBLIC_READ);
    connection.setDoOutput(true);//from www  .  j av  a2s  .c o  m
    connection.connect();
    return connection;
}

From source file:com.clickntap.vimeo.Vimeo.java

public String addTextTrack(String videoEndPoint, File file, boolean active, String type, String language,
        String name) throws IOException, VimeoException {

    VimeoResponse response = null;/*from w w  w.  j a  v a  2 s  .co m*/

    Map<String, String> params = new HashMap<String, String>();
    params.put("active", active ? "true" : "false");
    params.put("type", type);
    params.put("language", language);
    params.put("name", name);

    VimeoResponse addVideoRespose = apiRequest(new StringBuffer(videoEndPoint).append("/texttracks").toString(),
            HttpPost.METHOD_NAME, params, null);

    if (addVideoRespose.getStatusCode() == 201) {
        String textTrackUploadLink = addVideoRespose.getJson().getString("link");
        response = apiRequest(textTrackUploadLink, HttpPut.METHOD_NAME, null, file);
        if (response.getStatusCode() == 200) {
            return addVideoRespose.getJson().getString("uri");
        }
    }
    throw new VimeoException(
            new StringBuffer("HTTP Status Code: ").append(response.getStatusCode()).toString());

}

From source file:com.uploader.Vimeo.java

public String addTextTrack(String videoEndPoint, File file, boolean active, String type, String language,
        String name) throws IOException, VimeoException {

    VimeoResponse response = null;//  w w  w  . ja  v a2  s.c o m

    Map<String, String> params = new HashMap<String, String>();
    params.put("active", active ? "true" : "false");
    params.put("type", type);
    params.put("language", language);
    params.put("name", name);

    VimeoResponse addVideoRespose = apiRequest(new StringBuffer(videoEndPoint).append("/texttracks").toString(),
            HttpPost.METHOD_NAME, params, null);
    try {
        if (addVideoRespose.getStatusCode() == 201) {
            String textTrackUploadLink = addVideoRespose.getJson().getString("link");
            response = apiRequest(textTrackUploadLink, HttpPut.METHOD_NAME, null, file);
            if (response.getStatusCode() == 200) {
                return addVideoRespose.getJson().getString("uri");
            }
        }
    } catch (Exception e) {
        System.out.println("Err in getStatusCode");
    }
    throw new VimeoException(
            new StringBuffer("HTTP Status Code: ").append(response.getStatusCode()).toString());

}

From source file:com.evrythng.java.wrapper.util.FileUtils.java

private static HttpURLConnection getConnectionForPrivateUpload(final URL url, final String contentType)
        throws IOException {

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(HttpPut.METHOD_NAME);

    connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, contentType);
    connection.setRequestProperty(X_AMZ_ACL_HEADER_NAME, X_AMZ_ACL_HEADER_VALUE_PRIVATE);
    connection.setDoOutput(true);/*from   w  w w  .  j a  v a 2s  .c  o  m*/
    connection.connect();
    return connection;
}