public void updateCreditsConfigResource(String number, String uuid, String voiceRateCredits) throws ClientProtocolException, IOException { try { String startdate = ImiDataFormatUtils.getCurrentDate("yyyy-MM-dd"); Date endDate = ImiDataFormatUtils.getDateFromString("9999-12-31", "yyyy-MM-dd"); long noOfDays = ImiDataFormatUtils.getDifferenceInDaysBetweenDates(new Date(), endDate); String resourceRequest = creditsConfigResourceRequest .replace("{number}", number) .replace("{teamuuid}", uuid) .replace("{startdate}", startdate) .replace("{noofdays}", "" + noOfDays) .replace("{credits}", voiceRateCredits); String resourceUrl = creditsResourceConfigUrl; Map<String, String> requestBody = new HashMap<String, String>(); requestBody.put("authkey", creditsResourceAuthkey); requestBody.put("key", uuid); requestBody.put("request", resourceRequest); LOG.info( "Made call to credits config" + "authkey : " + creditsResourceAuthkey + " key : " + uuid + " request : " + resourceRequest); GenericRestResponse restResponse = ImiHttpUtil.defaultHttpPostHandler( resourceUrl, requestBody, ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); ; LOG.info( "Response from Credits Resource is " + restResponse.getResponseBody() + " with response code " + restResponse.getResponseCode()); } catch (Exception e) { LOG.error(ImiDataFormatUtils.getStackTrace(e)); } }
/** public for unit test, not to be used otherwise */ public void execute( HttpUriRequest httpUriRequest, ContentType contentType, FutureCallback<HttpResponse> callback) { // add accept header when its not a form or multipart final String contentTypeString = contentType.toString(); if (!ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType.getMimeType()) && !contentType.getMimeType().startsWith(MULTIPART_MIME_TYPE)) { // otherwise accept what is being sent httpUriRequest.addHeader(HttpHeaders.ACCEPT, contentTypeString); } // is something being sent? if (httpUriRequest instanceof HttpEntityEnclosingRequestBase && httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) { httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeString); } // set user specified custom headers if (httpHeaders != null && !httpHeaders.isEmpty()) { for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpUriRequest.setHeader(entry.getKey(), entry.getValue()); } } // add client protocol version if not specified if (!httpUriRequest.containsHeader(ODataHttpHeaders.DATASERVICEVERSION)) { httpUriRequest.addHeader(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V20); } if (!httpUriRequest.containsHeader(MAX_DATA_SERVICE_VERSION)) { httpUriRequest.addHeader(MAX_DATA_SERVICE_VERSION, ODataServiceVersion.V30); } // execute request client.execute(httpUriRequest, callback); }
/* (non-Javadoc) * @see java.net.URLConnection#getInputStream() */ @Override public InputStream getInputStream() throws IOException { try { if (m_client == null) { connect(); } // Build URL int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort(); URIBuilder ub = new URIBuilder(); ub.setPort(port); ub.setScheme(m_url.getProtocol()); ub.setHost(m_url.getHost()); ub.setPath(m_url.getPath()); ub.setQuery(m_url.getQuery()); // Build Request HttpRequestBase request = null; if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) { final Content cnt = m_request.getContent(); HttpPost post = new HttpPost(ub.build()); ContentType contentType = ContentType.create(cnt.getType()); LOG.info("Processing POST request for %s", contentType); if (contentType .getMimeType() .equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData()); post.setEntity(fields.getEntity()); } else { StringEntity entity = new StringEntity(cnt.getData(), contentType); post.setEntity(entity); } request = post; } else { request = new HttpGet(ub.build()); } if (m_request != null) { // Add Custom Headers for (Header header : m_request.getHeaders()) { request.addHeader(header.getName(), header.getValue()); } } // Add User Authentication String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":"); if (userInfo != null) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo[0], userInfo[1]); request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false)); } // Get Response HttpResponse response = m_client.execute(request); return response.getEntity().getContent(); } catch (Exception e) { throw new IOException( "Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e); } }