Java Code Examples for org.apache.http.HttpResponse#toString()

The following examples show how to use org.apache.http.HttpResponse#toString() . 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: WS.java    From candybean with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Protected method to handle sending and receiving an http request
 *
 * @param request The Http request that is being send
 * @param headers Map of Key Value header pairs
 * @return Key Value pairs of the response
 * @throws CandybeanException If the response is null or not an acceptable HTTP code
 */
protected static Map<String, Object> handleRequest(HttpUriRequest request, Map<String, String> headers) throws CandybeanException {
	// Add the request headers and execute the request
	for (Map.Entry<String, String> header : headers.entrySet()) {
		request.addHeader(new BasicHeader(header.getKey(), header.getValue()));
	}
	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
	try {
		HttpResponse response = httpClient.execute(request);

		// Check for invalid responses or error return codes
		if (response == null) {
			throw new CandybeanException("Http Request failed: Response null");
		}

		// Cast the response into a Map and return
		JSONObject parse = (JSONObject) JSONValue.parse(new InputStreamReader(response.getEntity().getContent()));
		@SuppressWarnings("unchecked")
		Map<String, Object> mapParse = (Map<String, Object>) parse;

		int code = response.getStatusLine().getStatusCode();
		if (!ACCEPTABLE_RETURN_CODE_SET.contains(code)) {
			throw new CandybeanException("HTTP request received HTTP code: " + code + "\n"
					+ "Response: " + response.toString());
		} else if (mapParse == null) {
			throw new CandybeanException("Could not format response\n"
					+ "Response: " + response.toString());
		}

		return mapParse;
	} catch (IOException | IllegalStateException e) {
		// Cast the other possible exceptions as a CandybeanException
		throw new CandybeanException(e);
	}
}
 
Example 2
Source File: BingSpeech.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
public static synchronized boolean speak(String voice, String text, String file, String apiKey, String token, String apiEndpoint, boolean useOldUrl) {
	if ((text == null) || text.isEmpty()) {
		return false;
	}
	if (text.length() > MAX_SIZE) {
		text = text.substring(0, MAX_SIZE);
	}
	
	try {
		HttpResponse response = null;

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "Bearer " + token);
		headers.put("X-Microsoft-OutputFormat", "audio-16khz-128kbitrate-mono-mp3");
		
		if (useOldUrl) {
			response = Utils.httpPOSTReturnResponse(BING_SPEECH_URL, "application/ssml+xml", "<speak version='1.0' xml:lang='en-US'><voice name='Microsoft Server Speech Text to Speech Voice (" + voice + ")'>" + text + "</voice></speak>", headers);
		} else {
			String region = apiEndpoint.substring(apiEndpoint.indexOf("://"), apiEndpoint.indexOf('.'));
			response = Utils.httpPOSTReturnResponse("https" + region + MICROSOFT_SPEECH_URL, "application/ssml+xml", "<speak version='1.0' xml:lang='en-US'><voice name='Microsoft Server Speech Text to Speech Voice (" + voice + ")'>" + text + "</voice></speak>", headers);
		}
		if (response!=null) {
			if ((response.getStatusLine().getStatusCode() < 200) || (response.getStatusLine().getStatusCode() > 302)) {
				if (!useOldUrl) {
					return speak(voice, text, file, apiKey, token, apiEndpoint, true);
				} else {
					throw new Exception(response.toString());
				}
			}
			File path = new File(file + ".mp3");
			new File(path.getParent()).mkdirs();
			InputStream inputStream = response.getEntity().getContent();
			FileOutputStream outputStream = new FileOutputStream(path);
			int size = 0;
			byte[] readBuffer = new byte[32768];
			while ((size = inputStream.read(readBuffer)) > 0) {
				outputStream.write(readBuffer, 0, size);
			}
			outputStream.close();
			inputStream.close();
		} else {
			return false;
		}

	} catch (Exception exception) {	
		AdminDatabase.instance().log(exception);
		return false;
	}
	return true;
}