/****************************************************************************** * 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.Block; import nxt.Nxt; import nxt.util.Convert; import org.json.simple.JSONStreamAware; import javax.servlet.http.HttpServletRequest; import static nxt.http.JSONResponses.INCORRECT_BLOCK; import static nxt.http.JSONResponses.INCORRECT_HEIGHT; import static nxt.http.JSONResponses.INCORRECT_TIMESTAMP; import static nxt.http.JSONResponses.UNKNOWN_BLOCK; public final class GetBlock extends APIServlet.APIRequestHandler { static final GetBlock instance = new GetBlock(); private GetBlock() { super(new APITag[] {APITag.BLOCKS}, "block", "height", "timestamp", "includeTransactions", "includeExecutedPhased"); } @Override JSONStreamAware processRequest(HttpServletRequest req) { Block blockData; String blockValue = Convert.emptyToNull(req.getParameter("block")); String heightValue = Convert.emptyToNull(req.getParameter("height")); String timestampValue = Convert.emptyToNull(req.getParameter("timestamp")); if (blockValue != null) { try { blockData = Nxt.getBlockchain().getBlock(Convert.parseUnsignedLong(blockValue)); } catch (RuntimeException e) { return INCORRECT_BLOCK; } } else if (heightValue != null) { try { int height = Integer.parseInt(heightValue); if (height < 0 || height > Nxt.getBlockchain().getHeight()) { return INCORRECT_HEIGHT; } blockData = Nxt.getBlockchain().getBlockAtHeight(height); } catch (RuntimeException e) { return INCORRECT_HEIGHT; } } else if (timestampValue != null) { try { int timestamp = Integer.parseInt(timestampValue); if (timestamp < 0) { return INCORRECT_TIMESTAMP; } blockData = Nxt.getBlockchain().getLastBlock(timestamp); } catch (RuntimeException e) { return INCORRECT_TIMESTAMP; } } else { blockData = Nxt.getBlockchain().getLastBlock(); } if (blockData == null) { return UNKNOWN_BLOCK; } boolean includeTransactions = "true".equalsIgnoreCase(req.getParameter("includeTransactions")); boolean includeExecutedPhased = "true".equalsIgnoreCase(req.getParameter("includeExecutedPhased")); return JSONData.block(blockData, includeTransactions, includeExecutedPhased); } }