use of org.apache.http.client.methods.HttpPatch in project microservice_framework by CJSCommonPlatform.
the class DefaultUsersUserIdResourceIT method patchRequestFor.
private HttpPatch patchRequestFor(final String uri, final String json, final String contentType, final String accept) throws UnsupportedEncodingException {
final HttpPatch request = patchRequestFor(uri, json, contentType);
request.setHeader("Accept", accept);
return request;
}
use of org.apache.http.client.methods.HttpPatch in project carbon-apimgt by wso2.
the class SolaceAdminApis method patchClientIdForApplication.
/**
* Patch client ID for Solace application
*
* @param organization name of the Organization
* @param application Application object to be renamed
* @param consumerKey Consumer key to be used when patching
* @param consumerSecret Consumer secret to be used when patching
* @return CloseableHttpResponse of the PATCH call
*/
public CloseableHttpResponse patchClientIdForApplication(String organization, Application application, String consumerKey, String consumerSecret) {
URL serviceEndpointURL = new URL(baseUrl);
HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
HttpPatch httpPatch = new HttpPatch(baseUrl + "/" + organization + "/developers/" + developerUserName + "/apps/" + application.getUUID());
httpPatch.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedCredentials());
httpPatch.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
org.json.JSONObject requestBody = buildRequestBodyForClientIdPatch(application, consumerKey, consumerSecret);
StringEntity params = null;
try {
params = new StringEntity(requestBody.toString());
httpPatch.setEntity(params);
return APIUtil.executeHTTPRequest(httpPatch, httpClient);
} catch (IOException | APIManagementException e) {
log.error(e.getMessage());
}
return null;
}
use of org.apache.http.client.methods.HttpPatch in project carbon-apimgt by wso2.
the class SolaceAdminApis method applicationPatchRemoveSubscription.
/**
* Remove subscriptions to application in Solace and update the application
*
* @param organization name of the Organization
* @param application Application to be checked in solace
* @param apiProductsToRemove List of API products to remove from subscriptions
* @return CloseableHttpResponse of the PATCH call
*/
public CloseableHttpResponse applicationPatchRemoveSubscription(String organization, Application application, List<String> apiProductsToRemove) {
URL serviceEndpointURL = new URL(baseUrl);
HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
HttpPatch httpPatch = new HttpPatch(baseUrl + "/" + organization + "/developers/" + developerUserName + "/apps/" + application.getUUID());
httpPatch.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedCredentials());
httpPatch.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
// retrieve existing API products in the app
ArrayList<String> apiProducts = new ArrayList<>();
try {
apiProducts = retrieveApiProductsInAnApplication(applicationGet(organization, application.getUUID(), "default"), apiProducts);
} catch (IOException e) {
log.error(e.getMessage());
}
// remove API product from arrayList
apiProducts.removeAll(apiProductsToRemove);
org.json.JSONObject requestBody = buildRequestBodyForApplicationPatchSubscriptions(apiProducts);
StringEntity params = null;
try {
params = new StringEntity(requestBody.toString());
httpPatch.setEntity(params);
return APIUtil.executeHTTPRequest(httpPatch, httpClient);
} catch (IOException | APIManagementException e) {
log.error(e.getMessage());
}
return null;
}
use of org.apache.http.client.methods.HttpPatch in project HongsCORE by ihongs.
the class Remote method request.
/**
* 简单请求
*
* @param type
* @param kind
* @param url
* @param data
* @param head
* @param con
* @throws HongsException
* @throws StatusException
* @throws SimpleException
*/
public static void request(METHOD type, FORMAT kind, String url, Map<String, Object> data, Map<String, String> head, Consumer<HttpResponse> con) throws HongsException, StatusException, SimpleException {
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 (null != kind)
switch(kind) {
case JSON:
htte.setEntity(buildJson(data));
break;
case PART:
htte.setEntity(buildPart(data));
break;
default:
htte.setEntity(buildPost(data));
break;
}
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);
}
if (sta >= 400 || sta <= 199) {
HttpEntity t = rsp.getEntity();
String txt = EntityUtils.toString(t, "UTF-8");
throw new StatusException(sta, url, txt);
}
con.accept(rsp);
} catch (URISyntaxException | IOException ex) {
throw new SimpleException(url, ex);
} finally {
if (http != null) {
http.releaseConnection();
}
}
}
use of org.apache.http.client.methods.HttpPatch in project open-kilda by telstra.
the class RestClientManager method invoke.
/**
* Invoke.
*
* @param apiRequestDto the api request dto
* @return the http response
*/
public HttpResponse invoke(final ApiRequestDto apiRequestDto) {
HttpResponse httpResponse = null;
String url = apiRequestDto.getUrl();
String headers = apiRequestDto.getHeader();
HttpMethod httpMethod = apiRequestDto.getHttpMethod();
String payload = apiRequestDto.getPayload();
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509CertChain, authType) -> true).build();
CloseableHttpClient client = HttpClientBuilder.create().setSSLContext(sslContext).setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)).build())).build();
HttpUriRequest httpUriRequest = null;
HttpEntityEnclosingRequestBase httpEntityEnclosingRequest = null;
// Initializing Request
if (HttpMethod.POST.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPost(url);
} else if (HttpMethod.PUT.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPut(url);
} else if (HttpMethod.DELETE.equals(httpMethod)) {
httpUriRequest = new HttpDelete(url);
} else if (HttpMethod.PATCH.equals(httpMethod)) {
httpUriRequest = new HttpPatch(url);
} else {
httpUriRequest = new HttpGet(url);
}
Map<String, String> headersMap = new HashMap<String, String>();
if (!HttpMethod.POST.equals(httpMethod) && !HttpMethod.PUT.equals(httpMethod)) {
if (!StringUtil.isNullOrEmpty(headers)) {
for (String header : headers.split("\n")) {
getHeaders(headersMap, header);
for (Entry<String, String> headerEntrySet : headersMap.entrySet()) {
httpUriRequest.setHeader(headerEntrySet.getKey(), headerEntrySet.getValue());
}
}
}
}
if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod)) {
LOGGER.info("[invoke] Executing POST/ PUT request : httpEntityEnclosingRequest : " + httpEntityEnclosingRequest);
if (!StringUtil.isNullOrEmpty(headers)) {
for (String header : headers.split("\n")) {
getHeaders(headersMap, header);
for (Entry<String, String> headerEntrySet : headersMap.entrySet()) {
httpEntityEnclosingRequest.setHeader(headerEntrySet.getKey(), headerEntrySet.getValue());
}
}
}
// Setting request payload
httpEntityEnclosingRequest.setEntity(new StringEntity(payload));
httpResponse = client.execute(httpEntityEnclosingRequest);
LOGGER.debug("[invoke] Call executed successfully");
} else {
LOGGER.info("[invoke] Executing : httpUriRequest : " + httpUriRequest);
httpResponse = client.execute(httpUriRequest);
LOGGER.info("[invoke] Call executed successfully");
}
} catch (Exception e) {
LOGGER.error("Error occurred while trying to communicate third party service provider", e);
throw new RestCallFailedException(e);
}
return httpResponse;
}
Aggregations