|
| 1 | +package org.tron.core.services.http; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.google.common.base.Strings; |
| 6 | +import java.io.IOException; |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.eclipse.jetty.http.HttpMethod; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.tron.api.GrpcAPI.BlockMessage; |
| 14 | +import org.tron.core.Wallet; |
| 15 | +import org.tron.protos.Protocol.Block; |
| 16 | + |
| 17 | + |
| 18 | +@Component |
| 19 | +@Slf4j(topic = "API") |
| 20 | +public class GetBlockServlet extends RateLimiterServlet { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private Wallet wallet; |
| 24 | + |
| 25 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) { |
| 26 | + handle(request, response); |
| 27 | + } |
| 28 | + |
| 29 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) { |
| 30 | + handle(request, response); |
| 31 | + } |
| 32 | + |
| 33 | + private void handle(HttpServletRequest request, HttpServletResponse response) { |
| 34 | + try { |
| 35 | + PostParams params = parseParams(request); |
| 36 | + BlockMessage message = buildRequest(params.getParams(), params.isVisible()); |
| 37 | + fillResponse(params.isVisible(), message, response); |
| 38 | + } catch (Exception e) { |
| 39 | + Util.processError(e, response); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private PostParams parseParams(HttpServletRequest request) throws Exception { |
| 44 | + HttpMethod m = HttpMethod.fromString(request.getMethod()); |
| 45 | + if (HttpMethod.GET.equals(m)) { |
| 46 | + String idOrNum = request.getParameter("idOrNum"); |
| 47 | + JSONObject params = new JSONObject(); |
| 48 | + if (!Strings.isNullOrEmpty(idOrNum)) { |
| 49 | + params.put("idOrNum", idOrNum); |
| 50 | + } |
| 51 | + params.put("detail", Boolean.parseBoolean(request.getParameter("detail"))); |
| 52 | + return new PostParams(JSON.toJSONString(params), |
| 53 | + Boolean.parseBoolean(request.getParameter(Util.VISIBLE))); |
| 54 | + } |
| 55 | + if (HttpMethod.POST.equals(m)) { |
| 56 | + return PostParams.getPostParams(request); |
| 57 | + } |
| 58 | + throw new UnsupportedOperationException(); |
| 59 | + } |
| 60 | + |
| 61 | + private BlockMessage buildRequest(String params, boolean visible) |
| 62 | + throws JsonFormat.ParseException { |
| 63 | + BlockMessage.Builder build = BlockMessage.newBuilder(); |
| 64 | + if (!Strings.isNullOrEmpty(params)) { |
| 65 | + JsonFormat.merge(params, build, visible); |
| 66 | + } |
| 67 | + return build.build(); |
| 68 | + } |
| 69 | + |
| 70 | + private void fillResponse(boolean visible, BlockMessage request, HttpServletResponse response) |
| 71 | + throws IOException { |
| 72 | + try { |
| 73 | + Block reply = wallet.getBlock(request); |
| 74 | + if (reply != null) { |
| 75 | + response.getWriter().println(Util.printBlock(reply, visible)); |
| 76 | + } else { |
| 77 | + response.getWriter().println("{}"); |
| 78 | + } |
| 79 | + } catch (IllegalArgumentException e) { |
| 80 | + JSONObject jsonObject = new JSONObject(); |
| 81 | + jsonObject.put("Error", e.getMessage()); |
| 82 | + response.getWriter().println(jsonObject.toJSONString()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments