Example usage for org.apache.http.client.methods HttpUriRequest setHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest setHeader.

Prototype

void setHeader(Header header);

Source Link

Usage

From source file:co.cask.cdap.internal.app.services.http.AppFabricTestBase.java

protected static HttpResponse execute(HttpUriRequest request) throws Exception {
    DefaultHttpClient client = new DefaultHttpClient();
    request.setHeader(AUTH_HEADER);
    return client.execute(request);
}

From source file:com.arcbees.vcs.AbstractVcsApi.java

protected void setDefaultHeaders(HttpUriRequest request) {
    request.setHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, CharEncoding.UTF_8));
    request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()));
}

From source file:com.precioustech.fxtrading.oanda.restapi.streaming.OandaStreamingService.java

protected BufferedReader setUpStreamIfPossible(CloseableHttpClient httpClient) throws Exception {
    HttpUriRequest httpGet = new HttpGet(getStreamingUrl());
    httpGet.setHeader(authHeader);
    httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER);
    LOG.info(TradingUtils.executingRequestMsg(httpGet));
    HttpResponse resp = httpClient.execute(httpGet);
    HttpEntity entity = resp.getEntity();
    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK && entity != null) {
        InputStream stream = entity.getContent();
        serviceUp = true;//from ww w.ja v a2 s .  com
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        return br;
    } else {
        String responseString = EntityUtils.toString(entity, "UTF-8");
        LOG.warn(responseString);
        return null;
    }
}

From source file:com.precioustech.fxtrading.oanda.restapi.position.OandaPositionManagementProvider.java

private String getResponseAsString(String reqUrl, CloseableHttpClient httpClient) throws Exception {
    HttpUriRequest httpGet = new HttpGet(reqUrl);
    httpGet.setHeader(this.authHeader);
    LOG.info(TradingUtils.executingRequestMsg(httpGet));
    HttpResponse resp = httpClient.execute(httpGet);
    String strResp = TradingUtils.responseToString(resp);
    return strResp;
}

From source file:com.precioustech.fxtrading.oanda.restapi.instrument.OandaInstrumentDataProviderService.java

@Override
public Collection<TradeableInstrument<String>> getInstruments() {
    Collection<TradeableInstrument<String>> instrumentsList = Lists.newArrayList();
    CloseableHttpClient httpClient = getHttpClient();
    try {/*from  w w w .ja  v  a  2  s  .c  o  m*/
        HttpUriRequest httpGet = new HttpGet(getInstrumentsUrl());
        httpGet.setHeader(authHeader);
        LOG.info(TradingUtils.executingRequestMsg(httpGet));
        HttpResponse resp = httpClient.execute(httpGet);
        String strResp = TradingUtils.responseToString(resp);
        if (strResp != StringUtils.EMPTY) {
            Object obj = JSONValue.parse(strResp);
            JSONObject jsonResp = (JSONObject) obj;
            JSONArray instrumentArray = (JSONArray) jsonResp.get(instruments);

            for (Object o : instrumentArray) {
                JSONObject instrumentJson = (JSONObject) o;
                String instrument = (String) instrumentJson.get(OandaJsonKeys.instrument);
                String[] currencies = OandaUtils.splitCcyPair(instrument);
                Double pip = Double.parseDouble(instrumentJson.get(OandaJsonKeys.pip).toString());
                JSONObject interestRates = (JSONObject) instrumentJson.get(interestRate);
                if (interestRates.size() != 2) {
                    throw new IllegalArgumentException();
                }

                JSONObject currency1Json = (JSONObject) interestRates.get(currencies[0]);
                JSONObject currency2Json = (JSONObject) interestRates.get(currencies[1]);

                final double baseCurrencyBidInterestRate = ((Number) currency1Json.get(bid)).doubleValue();
                final double baseCurrencyAskInterestRate = ((Number) currency1Json.get(ask)).doubleValue();
                final double quoteCurrencyBidInterestRate = ((Number) currency2Json.get(bid)).doubleValue();
                final double quoteCurrencyAskInterestRate = ((Number) currency2Json.get(ask)).doubleValue();

                InstrumentPairInterestRate instrumentPairInterestRate = new InstrumentPairInterestRate(
                        baseCurrencyBidInterestRate, baseCurrencyAskInterestRate, quoteCurrencyBidInterestRate,
                        quoteCurrencyAskInterestRate);
                TradeableInstrument<String> tradeableInstrument = new TradeableInstrument<String>(instrument,
                        pip, instrumentPairInterestRate, null);
                instrumentsList.add(tradeableInstrument);
            }
        } else {
            TradingUtils.printErrorMsg(resp);
        }
    } catch (Exception e) {
        LOG.error("exception encountered whilst retrieving all instruments info", e);
    } finally {
        TradingUtils.closeSilently(httpClient);
    }
    return instrumentsList;
}

From source file:com.okta.sdk.framework.JsonApiClient.java

@Override
protected void setAcceptHeader(HttpUriRequest httpUriRequest) throws IOException {
    Header acceptHeader = new BasicHeader("Accept", "application/json");
    httpUriRequest.setHeader(acceptHeader);
}

From source file:com.okta.sdk.framework.JsonApiClient.java

@Override
protected void setContentTypeHeader(HttpUriRequest httpUriRequest) throws IOException {
    Header contentTypeHeader = new BasicHeader("Content-type", "application/json");
    httpUriRequest.setHeader(contentTypeHeader);
}

From source file:com.precioustech.fxtrading.oanda.restapi.account.OandaAccountDataProviderService.java

@Override
public Collection<Account<Long>> getLatestAccountInfo() {
    CloseableHttpClient httpClient = getHttpClient();
    List<Account<Long>> accInfos = Lists.newArrayList();
    try {/*  w w  w .  j a v a  2 s. com*/
        HttpUriRequest httpGet = new HttpGet(getAllAccountsUrl());
        httpGet.setHeader(this.authHeader);

        LOG.info(TradingUtils.executingRequestMsg(httpGet));
        HttpResponse resp = httpClient.execute(httpGet);
        String strResp = TradingUtils.responseToString(resp);
        if (strResp != StringUtils.EMPTY) {
            Object jsonObject = JSONValue.parse(strResp);
            JSONObject jsonResp = (JSONObject) jsonObject;
            JSONArray accounts = (JSONArray) jsonResp.get(OandaJsonKeys.accounts);
            /*
             * We are doing a per account json request because not all information is returned in the array of results
             */
            for (Object o : accounts) {
                JSONObject account = (JSONObject) o;
                Long accountIdentifier = (Long) account.get(accountId);
                Account<Long> accountInfo = getLatestAccountInfo(accountIdentifier, httpClient);
                accInfos.add(accountInfo);
            }
        } else {
            TradingUtils.printErrorMsg(resp);
        }

    } catch (Exception e) {
        LOG.error("Exception encountered while retrieving all accounts data", e);
    } finally {
        TradingUtils.closeSilently(httpClient);
    }
    return accInfos;
}

From source file:com.precioustech.fxtrading.oanda.restapi.account.OandaAccountDataProviderService.java

private Account<Long> getLatestAccountInfo(final Long accountId, CloseableHttpClient httpClient) {
    try {//from w  w w . ja v  a2  s .  c o m
        HttpUriRequest httpGet = new HttpGet(getSingleAccountUrl(accountId));
        httpGet.setHeader(authHeader);

        LOG.info(TradingUtils.executingRequestMsg(httpGet));
        HttpResponse httpResponse = httpClient.execute(httpGet);
        String strResp = TradingUtils.responseToString(httpResponse);
        if (strResp != StringUtils.EMPTY) {
            Object obj = JSONValue.parse(strResp);
            JSONObject accountJson = (JSONObject) obj;

            /*Parse JSON response for account information*/
            final double accountBalance = ((Number) accountJson.get(balance)).doubleValue();
            final double accountUnrealizedPnl = ((Number) accountJson.get(unrealizedPl)).doubleValue();
            final double accountRealizedPnl = ((Number) accountJson.get(realizedPl)).doubleValue();
            final double accountMarginUsed = ((Number) accountJson.get(marginUsed)).doubleValue();
            final double accountMarginAvailable = ((Number) accountJson.get(marginAvail)).doubleValue();
            final Long accountOpenTrades = (Long) accountJson.get(openTrades);
            final String accountBaseCurrency = (String) accountJson.get(accountCurrency);
            final Double accountLeverage = (Double) accountJson.get(marginRate);

            Account<Long> accountInfo = new Account<Long>(accountBalance, accountUnrealizedPnl,
                    accountRealizedPnl, accountMarginUsed, accountMarginAvailable, accountOpenTrades,
                    accountBaseCurrency, accountId, accountLeverage);

            return accountInfo;
        } else {
            TradingUtils.printErrorMsg(httpResponse);
        }
    } catch (Exception e) {
        LOG.error("Exception encountered whilst getting info for account:" + accountId, e);
    }
    return null;
}

From source file:com.precioustech.fxtrading.oanda.restapi.marketdata.OandaCurrentPriceInfoProvider.java

@Override
public Map<TradeableInstrument<String>, Price<String>> getCurrentPricesForInstruments(
        Collection<TradeableInstrument<String>> instruments) {
    StringBuilder instrumentCsv = new StringBuilder();
    boolean firstTime = true;
    for (TradeableInstrument<String> instrument : instruments) {
        if (firstTime) {
            firstTime = false;//from  www.j a v  a2  s.c o m
        } else {
            instrumentCsv.append(TradingConstants.ENCODED_COMMA);
        }
        instrumentCsv.append(instrument.getInstrument());
    }

    Map<TradeableInstrument<String>, Price<String>> pricesMap = Maps.newHashMap();
    CloseableHttpClient httpClient = getHttpClient();
    try {
        HttpUriRequest httpGet = new HttpGet(
                this.url + OandaConstants.PRICES_RESOURCE + "?instruments=" + instrumentCsv.toString());
        httpGet.setHeader(this.authHeader);
        httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER);
        LOG.info(TradingUtils.executingRequestMsg(httpGet));
        HttpResponse resp = httpClient.execute(httpGet);
        String strResp = TradingUtils.responseToString(resp);
        if (strResp != StringUtils.EMPTY) {
            Object obj = JSONValue.parse(strResp);
            JSONObject jsonResp = (JSONObject) obj;
            JSONArray prices = (JSONArray) jsonResp.get(OandaJsonKeys.prices);
            for (Object price : prices) {
                JSONObject trade = (JSONObject) price;
                Long priceTime = Long.parseLong(trade.get(time).toString());
                TradeableInstrument<String> instrument = new TradeableInstrument<String>(
                        (String) trade.get(OandaJsonKeys.instrument));
                Price<String> pi = new Price<String>(instrument, ((Number) trade.get(bid)).doubleValue(),
                        ((Number) trade.get(ask)).doubleValue(),
                        new DateTime(TradingUtils.toMillisFromNanos(priceTime)));
                pricesMap.put(instrument, pi);
            }
        } else {
            TradingUtils.printErrorMsg(resp);
        }
    } catch (Exception ex) {
        LOG.error(ex);
    } finally {
        TradingUtils.closeSilently(httpClient);
    }
    return pricesMap;
}