Java Code Examples for org.apache.commons.httpclient.HttpMethodBase#setQueryString()

The following examples show how to use org.apache.commons.httpclient.HttpMethodBase#setQueryString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static Response makeRequest(HttpMethod httpMethod, String address, Map<String, String> requestHeaders, String requestBody,
		List<NameValuePair> queryParams, boolean authenticate) throws HttpException, IOException, HMACSecurityException {
	logger.debug("httpMethod = " + httpMethod);
	logger.debug("address = " + address);
	logger.debug("requestHeaders = " + requestHeaders);
	logger.debug("requestBody = " + requestBody);

	HttpMethodBase method = getMethod(httpMethod, address);
	if (requestHeaders != null) {
		for (Entry<String, String> entry : requestHeaders.entrySet()) {
			method.addRequestHeader(entry.getKey(), entry.getValue());
		}
	}
	if (queryParams != null) {
		// add uri query params to provided query params present in query
		List<NameValuePair> addressPairs = getAddressPairs(address);
		List<NameValuePair> totalPairs = new ArrayList<NameValuePair>(addressPairs);
		totalPairs.addAll(queryParams);
		method.setQueryString(totalPairs.toArray(new NameValuePair[queryParams.size()]));
	}
	if (method instanceof EntityEnclosingMethod) {
		EntityEnclosingMethod eem = (EntityEnclosingMethod) method;
		// charset of request currently not used
		eem.setRequestBody(requestBody);
	}

	if (authenticate) {
		String hmacKey = SpagoBIUtilities.getHmacKey();
		if (hmacKey != null && !hmacKey.isEmpty()) {
			logger.debug("HMAC key found with value [" + hmacKey + "]. Requests will be authenticated.");
			HMACFilterAuthenticationProvider authenticationProvider = new HMACFilterAuthenticationProvider(hmacKey);
			authenticationProvider.provideAuthentication(method, requestBody);
		} else {
			throw new SpagoBIRuntimeException("The request need to be authenticated, but hmacKey wasn't found.");
		}
	}

	try {
		HttpClient client = getHttpClient(address);
		int statusCode = client.executeMethod(method);
		Header[] headers = method.getResponseHeaders();
		String res = method.getResponseBodyAsString();
		return new Response(res, statusCode, headers);
	} finally {
		method.releaseConnection();
	}
}
 
Example 2
Source File: RestUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static InputStream makeRequestGetStream(HttpMethod httpMethod, String address, Map<String, String> requestHeaders, String requestBody,
		List<NameValuePair> queryParams, boolean authenticate) throws HttpException, IOException, HMACSecurityException {
	final HttpMethodBase method = getMethod(httpMethod, address);
	if (requestHeaders != null) {
		for (Entry<String, String> entry : requestHeaders.entrySet()) {
			method.addRequestHeader(entry.getKey(), entry.getValue());
		}
	}
	if (queryParams != null) {
		// add uri query params to provided query params present in query
		List<NameValuePair> addressPairs = getAddressPairs(address);
		List<NameValuePair> totalPairs = new ArrayList<NameValuePair>(addressPairs);
		totalPairs.addAll(queryParams);
		method.setQueryString(totalPairs.toArray(new NameValuePair[queryParams.size()]));
	}
	if (method instanceof EntityEnclosingMethod) {
		EntityEnclosingMethod eem = (EntityEnclosingMethod) method;
		// charset of request currently not used
		eem.setRequestBody(requestBody);
	}

	if (authenticate) {
		String hmacKey = SpagoBIUtilities.getHmacKey();
		if (hmacKey != null && !hmacKey.isEmpty()) {
			logger.debug("HMAC key found with value [" + hmacKey + "]. Requests will be authenticated.");
			HMACFilterAuthenticationProvider authenticationProvider = new HMACFilterAuthenticationProvider(hmacKey);
			authenticationProvider.provideAuthentication(method, requestBody);
		} else {
			throw new SpagoBIRuntimeException("The request need to be authenticated, but hmacKey wasn't found.");
		}
	}

	HttpClient client = getHttpClient(address);
	int statusCode = client.executeMethod(method);
	logger.debug("Status code " + statusCode);
	Header[] headers = method.getResponseHeaders();
	logger.debug("Response header " + headers);
	Asserts.check(statusCode == HttpStatus.SC_OK, "Response not OK.\nStatus code: " + statusCode);

	return new FilterInputStream(method.getResponseBodyAsStream()) {
		@Override
		public void close() throws IOException {
			try {
				super.close();
			} finally {
				method.releaseConnection();
			}
		}
	};
}