/** Send a request to a locally running Cougar container via REST as per the passed parameters. */ public HttpResponseBean makeRestCougarHTTPCall( HttpCallBean httpCallBean, HttpUriRequest method, CougarMessageProtocolRequestTypeEnum protocolRequestType, CougarMessageContentTypeEnum responseContentTypeEnum, CougarMessageContentTypeEnum requestContentTypeEnum) { Map<String, String> headerParams = httpCallBean.getHeaderParams(); String authority = httpCallBean.getAuthority(); Map<String, String> authCredentials = httpCallBean.getAuthCredentials(); Map<String, String> acceptProtocols = httpCallBean.getAcceptProtocols(); String ipAddress = httpCallBean.getIpAddress(); String altUrl = httpCallBean.getAlternativeURL(); Object postQueryObject = httpCallBean.getPostQueryObjectsByEnum(protocolRequestType); String postQuery; if (postQueryObject == null) { postQuery = null; } else { postQuery = (String) postQueryObject; } InputStream inputStream = null; try { completeRestMethodBuild( method, responseContentTypeEnum, requestContentTypeEnum, postQuery, headerParams, authority, authCredentials, altUrl, acceptProtocols, ipAddress); if (logger.isDebugEnabled()) { logger.debug("Request"); logger.debug("======="); logger.debug("URI: '" + method.getURI() + "'"); Header[] headers = method.getAllHeaders(); for (Header h : headers) { logger.debug("Header: '" + h.getName() + " = " + h.getValue() + "'"); } logger.debug("Body: '" + postQuery + "'"); } Date requestTime = new Date(); final HttpResponse httpResponse = cougarDAO.executeHttpMethodBaseCall(method); inputStream = httpResponse.getEntity().getContent(); String response = buildResponseString(inputStream); if (logger.isDebugEnabled()) { logger.debug("Response"); logger.debug("========"); logger.debug(String.valueOf(httpResponse.getStatusLine())); Header[] headers = httpResponse.getAllHeaders(); for (Header h : headers) { logger.debug("Header: '" + h.getName() + " = " + h.getValue() + "'"); } logger.debug("Body: '" + response + "'"); } Date responseTime = new Date(); return buildHttpResponseBean(httpResponse, response, requestTime, responseTime); } catch (IOException e) { throw new RuntimeException(e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { /*ignore*/ } } } }
/* * Create and return a HttpMethodBase for a Rest request based on the passed * HttpCallBean and CougarMessageProtocolRequestTypeEnum. * * @param httpCallBean * @param protocolRequestType * @return */ public HttpUriRequest getRestMethod( HttpCallBean httpCallBean, CougarMessageProtocolRequestTypeEnum protocolRequestType) { Object postQueryObject = httpCallBean.getPostQueryObjectsByEnum(protocolRequestType); String postQuery; if (postQueryObject == null) { postQuery = null; } else { postQuery = (String) postQueryObject; } String serviceExtension = httpCallBean.getServiceExtension(); String version = httpCallBean.getVersion(); Map<String, String> queryParams = httpCallBean.getQueryParams(); String host = httpCallBean.getHost(); String port = httpCallBean.getPort(); String path = httpCallBean.getPath(); String altURL = httpCallBean .getAlternativeURL(); // Will be "" for standard URL, or "/www" for alternative URL boolean batchedQuery = httpCallBean.getJSONRPC(); String fullPath = httpCallBean.getFullPath(); String methodURL = ""; if (batchedQuery) { // If request is a batched JSON request set the URL to the appropriate // baseline URL postQuery = createBatchedPostString( httpCallBean .getBatchedRequests()); // Build the post string out of the list of requests to // batch methodURL = "http://" + host + ":" + port + "/json-rpc"; } else { // else build the request URL String queryParamString = ""; if (queryParams != null) { int counter = 0; StringBuilder queryBuff = new StringBuilder(); for (Map.Entry<String, String> entry : queryParams.entrySet()) { String value = entry.getValue(); String key = entry.getKey(); if (counter == 0) { String firstQuery = "?" + key + "=" + value; queryBuff.append(firstQuery); } else { String nextQuery = "&" + key + "=" + value; queryBuff.append(nextQuery); } counter++; } queryParamString = queryBuff.toString(); } if (fullPath != null) { methodURL = "http://" + host + ":" + port + fullPath + queryParamString; } else { methodURL = "http://" + host + ":" + port + altURL + "/" + serviceExtension + "/" + version + "/" + path + queryParamString; } } // if (logger.isDebugEnabled()) { // } if ((postQuery != null) && (!postQuery.equalsIgnoreCase(""))) { HttpPost method = new HttpPost(methodURL); try { method.setEntity(new StringEntity(postQuery, null, UTF8)); return method; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else { return new HttpGet(methodURL); } }