Search in sources :

Example 16 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project camel by apache.

the class Olingo2AppImpl method patch.

@Override
public <T> void patch(Edm edm, String resourcePath, Object data, Olingo2ResponseHandler<T> responseHandler) {
    final UriInfoWithType uriInfo = parseUri(edm, resourcePath, null);
    writeContent(edm, new HttpPatch(createUri(resourcePath, null)), uriInfo, data, responseHandler);
}
Also used : HttpPatch(org.apache.http.client.methods.HttpPatch)

Example 17 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project spark by perwendel.

the class SparkTestUtil method getHttpRequest.

private HttpUriRequest getHttpRequest(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) {
    try {
        String protocol = secureConnection ? "https" : "http";
        String uri = protocol + "://localhost:" + port + path;
        if (requestMethod.equals("GET")) {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpGet);
            return httpGet;
        }
        if (requestMethod.equals("POST")) {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPost);
            httpPost.setEntity(new StringEntity(body));
            return httpPost;
        }
        if (requestMethod.equals("PATCH")) {
            HttpPatch httpPatch = new HttpPatch(uri);
            httpPatch.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPatch);
            httpPatch.setEntity(new StringEntity(body));
            return httpPatch;
        }
        if (requestMethod.equals("DELETE")) {
            HttpDelete httpDelete = new HttpDelete(uri);
            addHeaders(reqHeaders, httpDelete);
            httpDelete.setHeader("Accept", acceptType);
            return httpDelete;
        }
        if (requestMethod.equals("PUT")) {
            HttpPut httpPut = new HttpPut(uri);
            httpPut.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPut);
            httpPut.setEntity(new StringEntity(body));
            return httpPut;
        }
        if (requestMethod.equals("HEAD")) {
            HttpHead httpHead = new HttpHead(uri);
            addHeaders(reqHeaders, httpHead);
            return httpHead;
        }
        if (requestMethod.equals("TRACE")) {
            HttpTrace httpTrace = new HttpTrace(uri);
            addHeaders(reqHeaders, httpTrace);
            return httpTrace;
        }
        if (requestMethod.equals("OPTIONS")) {
            HttpOptions httpOptions = new HttpOptions(uri);
            addHeaders(reqHeaders, httpOptions);
            return httpOptions;
        }
        if (requestMethod.equals("LOCK")) {
            HttpLock httpLock = new HttpLock(uri);
            addHeaders(reqHeaders, httpLock);
            return httpLock;
        }
        throw new IllegalArgumentException("Unknown method " + requestMethod);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) StringEntity(org.apache.http.entity.StringEntity) HttpTrace(org.apache.http.client.methods.HttpTrace)

Example 18 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project HongsCORE by ihongs.

the class Remote method request.

/**
 * 简单远程请求
 *
 * @param type
 * @param url
 * @param data
 * @param head
 * @param multipart
 * @return
 * @throws HongsException
 */
public static String request(METHOD type, String url, Map<String, Object> data, Map<String, String> head, boolean multipart) throws HongsException {
    if (url == null) {
        throw new NullPointerException("Request url can not be null");
    }
    HttpRequestBase http = null;
    try {
        // 构建 HTTP 请求对象
        switch(type) {
            case DELETE:
                http = new HttpDelete();
                break;
            case PATCH:
                http = new HttpPatch();
                break;
            case POST:
                http = new HttpPost();
                break;
            case PUT:
                http = new HttpPut();
                break;
            default:
                http = new HttpGet();
                break;
        }
        // 设置报文
        if (data != null) {
            if (http instanceof HttpEntityEnclosingRequest) {
                HttpEntityEnclosingRequest htte = (HttpEntityEnclosingRequest) http;
                if (multipart) {
                    htte.setEntity(buildPart(data));
                } else {
                    htte.setEntity(buildPost(data));
                }
            } else {
                String qry = EntityUtils.toString(buildPost(data), "UTF-8");
                if (url.indexOf('?') == -1) {
                    url += "?" + qry;
                } else {
                    url += "&" + qry;
                }
            }
        }
        // 设置报头
        if (head != null) {
            for (Map.Entry<String, String> et : head.entrySet()) {
                http.setHeader(et.getKey(), et.getValue());
            }
        }
        // 执行请求
        http.setURI(new URI(url));
        HttpResponse rsp = HttpClients.createDefault().execute(http);
        // 判断结果
        int sta = rsp.getStatusLine().getStatusCode();
        if (sta >= 300 && sta <= 399) {
            Header hea = rsp.getFirstHeader("Location");
            String loc = hea != null ? hea.getValue() : "";
            throw new StatusException(sta, url, loc);
        } else if (sta <= 199 || sta >= 400) {
            String txt = EntityUtils.toString(rsp.getEntity(), "UTF-8").trim();
            throw new StatusException(sta, url, txt);
        } else {
            String txt = EntityUtils.toString(rsp.getEntity(), "UTF-8").trim();
            return txt;
        }
    } catch (URISyntaxException | IOException ex) {
        throw new SimpleException(url, ex);
    } finally {
        if (http != null) {
            http.releaseConnection();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project ais-sdk by huaweicloudsdk.

the class AccessServiceImpl method createRequest.

/**
 * Make a request that can be sent by the HTTP client.
 *
 * @param url
 *            specifies the API access path.
 * @param header
 *            specifies the header information to be added.
 * @param content
 *            specifies the body content to be sent in the API call.
 * @param contentLength
 *            specifies the length of the content. This parameter is optional.
 * @param httpMethod
 *            specifies the HTTP method to be used.
 * @return specifies the request that can be sent by an HTTP client.
 */
private static HttpRequestBase createRequest(URL url, Header header, InputStream content, Long contentLength, HttpMethodName httpMethod) {
    HttpRequestBase httpRequest;
    if (httpMethod == HttpMethodName.POST) {
        HttpPost postMethod = new HttpPost(url.toString());
        if (content != null) {
            InputStreamEntity entity = new InputStreamEntity(content, contentLength);
            postMethod.setEntity(entity);
        }
        httpRequest = postMethod;
    } else if (httpMethod == HttpMethodName.PUT) {
        HttpPut putMethod = new HttpPut(url.toString());
        httpRequest = putMethod;
        if (content != null) {
            InputStreamEntity entity = new InputStreamEntity(content, contentLength);
            putMethod.setEntity(entity);
        }
    } else if (httpMethod == HttpMethodName.PATCH) {
        HttpPatch patchMethod = new HttpPatch(url.toString());
        httpRequest = patchMethod;
        if (content != null) {
            InputStreamEntity entity = new InputStreamEntity(content, contentLength);
            patchMethod.setEntity(entity);
        }
    } else if (httpMethod == HttpMethodName.GET) {
        httpRequest = new HttpGet(url.toString());
    } else if (httpMethod == HttpMethodName.DELETE) {
        httpRequest = new HttpDelete(url.toString());
    } else if (httpMethod == HttpMethodName.HEAD) {
        httpRequest = new HttpHead(url.toString());
    } else {
        throw new RuntimeException("Unknown HTTP method name: " + httpMethod);
    }
    httpRequest.addHeader(header);
    return httpRequest;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpHead(org.apache.http.client.methods.HttpHead) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 20 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project ais-sdk by huaweicloudsdk.

the class AccessServiceImpl method createRequestEntity.

private static HttpRequestBase createRequestEntity(URL url, Header header, HttpEntity entity, Long contentLength, HttpMethodName httpMethod) {
    HttpRequestBase httpRequest;
    if (httpMethod == HttpMethodName.POST) {
        HttpPost postMethod = new HttpPost(url.toString());
        if (entity != null) {
            postMethod.setEntity(entity);
        }
        httpRequest = postMethod;
    } else if (httpMethod == HttpMethodName.PUT) {
        HttpPut putMethod = new HttpPut(url.toString());
        httpRequest = putMethod;
        if (entity != null) {
            putMethod.setEntity(entity);
        }
    } else if (httpMethod == HttpMethodName.PATCH) {
        HttpPatch patchMethod = new HttpPatch(url.toString());
        httpRequest = patchMethod;
        if (entity != null) {
            patchMethod.setEntity(entity);
        }
    } else if (httpMethod == HttpMethodName.GET) {
        httpRequest = new HttpGet(url.toString());
    } else if (httpMethod == HttpMethodName.DELETE) {
        httpRequest = new HttpDelete(url.toString());
    } else if (httpMethod == HttpMethodName.HEAD) {
        httpRequest = new HttpHead(url.toString());
    } else {
        throw new RuntimeException("Unknown HTTP method name: " + httpMethod);
    }
    httpRequest.addHeader(header);
    return httpRequest;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpPatch (org.apache.http.client.methods.HttpPatch)33 HttpPost (org.apache.http.client.methods.HttpPost)15 HttpPut (org.apache.http.client.methods.HttpPut)15 IOException (java.io.IOException)13 StringEntity (org.apache.http.entity.StringEntity)13 HttpGet (org.apache.http.client.methods.HttpGet)12 HttpDelete (org.apache.http.client.methods.HttpDelete)11 HttpClient (org.apache.http.client.HttpClient)7 HttpHead (org.apache.http.client.methods.HttpHead)7 URI (java.net.URI)6 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)6 HttpResponse (org.apache.http.HttpResponse)5 InputStreamEntity (org.apache.http.entity.InputStreamEntity)5 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 URL (org.apache.axis2.util.URL)4 HttpEntity (org.apache.http.HttpEntity)4 HttpOptions (org.apache.http.client.methods.HttpOptions)4 HttpTrace (org.apache.http.client.methods.HttpTrace)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4