private static HTTPRequestResponse performHTTPRequest()

in taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/HTTPRequestHandler.java [240:325]


	private static HTTPRequestResponse performHTTPRequest(
			ClientConnectionManager connectionManager, HttpRequestBase httpRequest,
			RESTActivityConfigurationBean configBean,
			Map<String, String> urlParameters, CredentialsProvider credentialsProvider) {
		// headers are set identically for all HTTP methods, therefore can do
		// centrally - here

		// If the user wants to set MIME type for the 'Accepts' header
		String acceptsHeaderValue = configBean.getAcceptsHeaderValue();
		if ((acceptsHeaderValue != null) && !acceptsHeaderValue.isEmpty()) {
			httpRequest.setHeader(ACCEPT_HEADER_NAME,
					URISignatureHandler.generateCompleteURI(acceptsHeaderValue, urlParameters, configBean.getEscapeParameters()));
		}

		// See if user wanted to set any other HTTP headers
		ArrayList<ArrayList<String>> otherHTTPHeaders = configBean.getOtherHTTPHeaders();
		if (!otherHTTPHeaders.isEmpty())
			for (ArrayList<String> httpHeaderNameValuePair : otherHTTPHeaders)
				if (httpHeaderNameValuePair.get(0) != null
						&& !httpHeaderNameValuePair.get(0).isEmpty()) {
					String headerParameterizedValue = httpHeaderNameValuePair.get(1);
					String headerValue = URISignatureHandler.generateCompleteURI(headerParameterizedValue, urlParameters, configBean.getEscapeParameters());
					httpRequest.setHeader(httpHeaderNameValuePair.get(0), headerValue);
				}

		try {
			HTTPRequestResponse requestResponse = new HTTPRequestResponse();
			DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, null);
			((DefaultHttpClient) httpClient).setCredentialsProvider(credentialsProvider);
			HttpContext localContext = new BasicHttpContext();

			// Set the proxy settings, if any
			if (System.getProperty(PROXY_HOST) != null
					&& !System.getProperty(PROXY_HOST).isEmpty()) {
				// Instruct HttpClient to use the standard
				// JRE proxy selector to obtain proxy information
				ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(httpClient
						.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
				httpClient.setRoutePlanner(routePlanner);
				// Do we need to authenticate the user to the proxy?
				if (System.getProperty(PROXY_USERNAME) != null
						&& !System.getProperty(PROXY_USERNAME).isEmpty())
					// Add the proxy username and password to the list of
					// credentials
					httpClient.getCredentialsProvider().setCredentials(
							new AuthScope(System.getProperty(PROXY_HOST), Integer.parseInt(System
									.getProperty(PROXY_PORT))),
							new UsernamePasswordCredentials(System.getProperty(PROXY_USERNAME),
									System.getProperty(PROXY_PASSWORD)));
			}

			// execute the request
			HttpResponse response = httpClient.execute(httpRequest, localContext);

			// record response code
			requestResponse.setStatusCode(response.getStatusLine().getStatusCode());
			requestResponse.setReasonPhrase(response.getStatusLine().getReasonPhrase());

			// record header values for Content-Type of the response
			requestResponse.setResponseContentTypes(response.getHeaders(CONTENT_TYPE_HEADER_NAME));

			// track where did the final redirect go to (if there was any)
			HttpHost targetHost = (HttpHost) localContext
					.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
			HttpUriRequest targetRequest = (HttpUriRequest) localContext
					.getAttribute(ExecutionContext.HTTP_REQUEST);
			requestResponse.setRedirectionURL("" + targetHost + targetRequest.getURI());
			requestResponse.setRedirectionHTTPMethod(targetRequest.getMethod());
			requestResponse.setHeaders(response.getAllHeaders());

			/* read and store response body
			 (check there is some content - negative length of content means
			 unknown length;
			 zero definitely means no content...)*/
			// TODO - make sure that this test is sufficient to determine if
			// there is no response entity
			if (response.getEntity() != null && response.getEntity().getContentLength() != 0)
				requestResponse.setResponseBody(readResponseBody(response.getEntity()));

			// release resources (e.g. connection pool, etc)
			httpClient.getConnectionManager().shutdown();
			return requestResponse;
		} catch (Exception ex) {
			return new HTTPRequestResponse(ex);
		}
	}