/*- * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.openecomp.mso.rest; import java.io.Closeable; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Set; import javax.net.ssl.SSLSocketFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.AbstractHttpMessage; import org.apache.http.util.EntityUtils; /** * Client used to send RESTFul requests. *
* Many of the methods return a reference to the 'this,' thereby allowing
* method chaining.
*
* An example of usage can be found below:
*
* RESTClient client; * try { * client = new RESTClient("http://www.openecomp.org"); * APIResponse response = client * .setHeader("Accept", "application/json") * .setHeader("Clientid", "clientid") * .setHeader("header", "value") * .httpPost("postbody"); * if (response.getStatusCode() == 200) { * System.out.println("Success!"); * } * } catch (RESTException re) { * // Handle Exception * } ** * @version 1.0 * @since 1.0 */ public class RESTClient { private final String proxyHost; private final int proxyPort; private final String URL; private final LinkedHashMap
* Does not remove any parameters with the same name, thus allowing
* duplicates.
*
* @param name name of parameter
* @param value value of parametr
* @return a reference to 'this', which can be used for method chaining
*/
public RESTClient addParameter(String name, String value) {
if (!parameters.containsKey(name)) {
parameters.put(name, new ArrayList
* Removes any parameters with the same name, thus disallowing duplicates.
*
* @param name name of parameter
* @param value value of parametr
* @return a reference to 'this', which can be used for method chaining
*/
public RESTClient setParameter(String name, String value) {
if (parameters.containsKey(name)) {
parameters.get(name).clear();
}
addParameter(name, value);
return this;
}
/**
* Adds http header to be sent during http request.
*
* Does not remove any headers with the same name, thus allowing
* duplicates.
*
* @param name name of header
* @param value value of header
* @return a reference to 'this', which can be used for method chaining
*/
public RESTClient addHeader(String name, String value) {
if (!headers.containsKey(name)) {
headers.put(name, new ArrayList
* Does not remove any headers with the same name, thus allowing
* duplicates.
*
* @param name name of header
* @param value value of header
* @return a reference to 'this', which can be used for method chaining
*/
public RESTClient setHeader(String name, String value) {
if (headers.containsKey(name)) {
headers.get(name).clear();
}
addHeader(name, value);
return this;
}
/**
* Convenience method for adding the authorization header using the
* specified OAuthToken object.
*
* @param token token to use for setting authorization
* @return a reference to 'this,' which can be used for method chaining
*/
public RESTClient addAuthorizationHeader(String token) {
this.addHeader("Authorization", token);
return this;
}
/**
* Alias for httpGet().
*
* @see RESTClient#httpGet()
*/
public APIResponse get() throws RESTException {
return httpGet();
}
/**
* Sends an http GET request using the parameters and headers previously
* set.
*
* @return api response
*
* @throws RESTException if request was unsuccessful
*/
public APIResponse httpGet() throws RESTException {
HttpResponse response = null;
try (CloseableHttpClient httpClient = createClient()) {
String query = "";
if (!buildQuery().equals("")) {
query = "?" + buildQuery();
}
HttpGet httpGet = new HttpGet(this.getURL() + query);
addInternalHeaders(httpGet);
response = httpClient.execute(httpGet);
APIResponse apiResponse = buildResponse(response);
return apiResponse;
} catch (IOException ioe) {
throw new RESTException(ioe);
} finally {
if (response != null) {
this.releaseConnection(response);
}
}
}
/**
* Alias for httpPost()
*
* @see RESTClient#httpPost()
*/
public APIResponse post() throws RESTException {
return httpPost();
}
/**
* Sends an http POST request.
*
* POST body will be set to the values set using add/setParameter()
*
* @return api response
*
* @throws RESTException if POST was unsuccessful
*/
public APIResponse httpPost() throws RESTException {
APIResponse response = httpPost(buildQuery());
return response;
}
/**
* Sends an http POST request using the specified body.
*
* @return api response
*
* @throws RESTException if POST was unsuccessful
*/
public APIResponse httpPost(String body) throws RESTException {
HttpResponse response = null;
try (CloseableHttpClient httpClient = createClient()) {
HttpPost httpPost = new HttpPost(this.getURL());
addInternalHeaders(httpPost);
if (body != null && !body.equals("")) {
httpEntity = new StringEntity(body);
httpPost.setEntity(new StringEntity(body));
}
response = httpClient.execute(httpPost);
return buildResponse(response);
} catch (IOException e) {
throw new RESTException(e);
} finally {
if (response != null) {
this.releaseConnection(response);
}
}
}
/**
*
* @param body Data to PUT
* @return API response
* @throws RESTException
*/
public APIResponse httpPut(String body) throws RESTException {
HttpResponse response = null;
try (CloseableHttpClient httpClient = createClient()) {
String query = "";
if (!buildQuery().equals("")) {
query = "?" + buildQuery();
}
HttpPut httpPut = new HttpPut(this.getURL() + query);
addInternalHeaders(httpPut);
if (body != null && !body.equals("")) {
httpEntity = new StringEntity(body);
httpPut.setEntity(httpEntity);
}
response = httpClient.execute(httpPut);
return buildResponse(response);
} catch (IOException e) {
throw new RESTException(e);
} finally {
if (response != null) {
this.releaseConnection(response);
}
}
}
/**
* Alias for httpPatch().
*
* @see RESTClient#httpPatch()
*/
public APIResponse patch(String body) throws RESTException {
return httpPatch(body);
}
/**
*
* @param body Data to PATCH
* @return API response
* @throws RESTException
*/
public APIResponse httpPatch(String body) throws RESTException {
HttpResponse response = null;
try (CloseableHttpClient httpClient = createClient()) {
String query = "";
if (!buildQuery().equals("")) {
query = "?" + buildQuery();
}
HttpPatch httpPatch = new HttpPatch(this.getURL() + query);
addInternalHeaders(httpPatch);
if (body != null && !body.equals("")) {
httpEntity = new StringEntity(body);
httpPatch.setEntity(httpEntity);
}
response = httpClient.execute(httpPatch);
return buildResponse(response);
} catch (IOException e) {
throw new RESTException(e);
} finally {
if (response != null) {
this.releaseConnection(response);
}
}
}
/**
* Alias for httpDelete().
*
* @see RESTClient#httpDelete()
*/
public APIResponse delete() throws RESTException {
return httpDelete();
}
/**
* Sends an http DELETE request using the parameters and headers previously
* set.
*
* @return api response
*
* @throws RESTException if request was unsuccessful
*/
public APIResponse httpDelete() throws RESTException {
return httpDelete(null);
}
/**
* Sends an http DELETE request with a body, using the parameters and headers
* previously set.
*
* @return api response
*
* @throws RESTException if request was unsuccessful
*/
public APIResponse httpDelete(String body) throws RESTException {
HttpResponse response = null;
try (CloseableHttpClient httpClient = createClient()){
String query = "";
if (!buildQuery().equals("")) {
query = "?" + buildQuery();
}
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(this.getURL() + query);
addInternalHeaders(httpDelete);
if (body != null && !body.equals("")) {
httpEntity = new StringEntity(body);
httpDelete.setEntity(httpEntity);
}
response = httpClient.execute(httpDelete);
APIResponse apiResponse = buildResponse(response);
return apiResponse;
} catch (IOException ioe) {
throw new RESTException(ioe);
} finally {
if (response != null) {
this.releaseConnection(response);
}
}
}
public String getURL() {
return URL;
}
public LinkedHashMap