private RpcEnvelope.Request readRpcEnvelope(ServletInputStream in) throws Exception { byte chunkSize[] = new byte[4]; in.read(chunkSize); int size = Ints.fromByteArray(chunkSize); if (size <= 0 || size > ProtobufUtil.MAX_HEADER_CHUNK_SIZE) { String message = "Invalid header chunk size: " + size; throw new RpcReadException(chunkSize, in, message); } byte headerData[] = readyFully(in, size); RpcEnvelope.Request rpcRequest = RpcEnvelope.Request.parseFrom(headerData); return rpcRequest; }
public void testBodyReading(String first, String second) throws IOException { ServletInputStream x = (ServletInputStream) new RpcHandlerTest_InputStream(second); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Map<String, Set<String>> headers = new TreeMap<>(); when(request.getHeaderNames()) .thenReturn( new RpcReadExceptionTest.RpcHandlerTest_IteratorEnumeration<>(headers.keySet().iterator()) ); when(request.getInputStream()).thenReturn(x); when(request.getRequestURL()) .thenReturn(new StringBuffer("http://fizz.buzz")); RpcReadException rpcReadException = new RpcReadException(first.getBytes(), x, "i am a message"); String json = rpcReadException.toJson(request); try { JsonElement root = new JsonParser().parse(json); JsonObject jsob = root.getAsJsonObject(); JsonElement b = jsob.get("request_body"); Assert.assertNotNull(b); Assert.assertEquals(first+second, this.decode(b.getAsString())); } catch (Exception ex) { Assert.fail(ex.toString()); } }
private Message readRpcBody(ServletInputStream in, Class<? extends Message> requestClass) throws Exception { byte chunkSize[] = new byte[4]; in.read(chunkSize); int size = Ints.fromByteArray(chunkSize); if (size == 0) { return ProtobufUtil.newEmptyMessage(requestClass); } if (size > ProtobufUtil.MAX_BODY_CHUNK_SIZE) { String message = "Invalid body chunk size: " + size; throw new RpcReadException(chunkSize, in, message); } byte bodyData[] = readyFully(in, size); Message pbRequest = ProtobufUtil.byteArrayToProtobuf(bodyData, requestClass); return pbRequest; }
Boolean caught = false; try { throw new RpcReadException("beg".getBytes(),request.getInputStream(),"i am a message"); } catch (RpcReadException ex) { json = ex.toJson(request);