/****************************************************************************** * Copyright © 2013-2016 The Nxt Core Developers. * * * * See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at * * the top-level directory of this distribution for the individual copyright * * holder information and the developer policies on copyright and licensing. * * * * Unless otherwise agreed in a custom licensing agreement, no part of the * * Nxt software, including this file, may be copied, modified, propagated, * * or distributed except according to the terms contained in the LICENSE.txt * * file. * * * * Removal or modification of this copyright notice is prohibited. * * * ******************************************************************************/ package nxt.http; import nxt.CurrencyBuyOffer; import nxt.db.DbIterator; import nxt.db.DbUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONStreamAware; import javax.servlet.http.HttpServletRequest; public final class GetBuyOffers extends APIServlet.APIRequestHandler { static final GetBuyOffers instance = new GetBuyOffers(); private GetBuyOffers() { super(new APITag[] {APITag.MS}, "currency", "account", "availableOnly", "firstIndex", "lastIndex"); } @Override JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException { long currencyId = ParameterParser.getUnsignedLong(req, "currency", false); long accountId = ParameterParser.getAccountId(req, false); if (currencyId == 0 && accountId == 0) { return JSONResponses.MISSING_CURRENCY_ACCOUNT; } boolean availableOnly = "true".equalsIgnoreCase(req.getParameter("availableOnly")); int firstIndex = ParameterParser.getFirstIndex(req); int lastIndex = ParameterParser.getLastIndex(req); JSONObject response = new JSONObject(); JSONArray offerData = new JSONArray(); response.put("offers", offerData); DbIterator<CurrencyBuyOffer> offers= null; try { if (accountId == 0) { offers = CurrencyBuyOffer.getCurrencyOffers(currencyId, availableOnly, firstIndex, lastIndex); } else if (currencyId == 0) { offers = CurrencyBuyOffer.getAccountOffers(accountId, availableOnly, firstIndex, lastIndex); } else { CurrencyBuyOffer offer = CurrencyBuyOffer.getOffer(currencyId, accountId); if (offer != null) { offerData.add(JSONData.offer(offer)); } return response; } while (offers.hasNext()) { offerData.add(JSONData.offer(offers.next())); } } finally { DbUtils.close(offers); } return response; } }