for (Field f : PROTOCOL.getType("TestRecord").getFields()) fields.add(new Field(f.name(), f.schema(), null, null)); fields.add(new Field("extra", Schema.create(Schema.Type.BOOLEAN), null, true)); Schema record = Schema.createRecord("TestRecord", null, "org.apache.avro.test", false); record.setFields(fields); Protocol protocol = new Protocol("Simple", "org.apache.avro.test"); List<Field> params = new ArrayList<>(); params.add(new Field("record", record, null, null)); protocol.createMessage("echo", null, Schema.createRecord(params), record, Schema.createUnion(new ArrayList<>())); try { GenericRequestor r = new GenericRequestor(protocol, t); GenericRecord args = new GenericData.Record(message.getRequest()); GenericRecord rec = new GenericData.Record(record); rec.put("name", new Utf8("foo")); rec.put("kind", new GenericData.EnumSymbol (PROTOCOL.getType("Kind"), "BAR")); rec.put("hash", new GenericData.Fixed (PROTOCOL.getType("MD5"), new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5}));
@Override public Object respond(Message message, Object request) throws Exception { int numParams = message.getRequest().getFields().size(); Object[] params = new Object[numParams]; Class[] paramTypes = new Class[numParams]; int i = 0; try { for (Schema.Field param: message.getRequest().getFields()) { params[i] = ((GenericRecord)request).get(param.name()); paramTypes[i] = getSpecificData().getClass(param.schema()); i++; } Method method = impl.getClass().getMethod(message.getName(), paramTypes); method.setAccessible(true); return method.invoke(impl, params); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Exception) { throw (Exception) e.getTargetException(); } else { throw new Exception(e.getTargetException()); } } catch (NoSuchMethodException e) { throw new AvroRuntimeException(e); } catch (IllegalAccessException e) { throw new AvroRuntimeException(e); } }
@Test public void testP0() throws Exception { Protocol p0 = ReflectData.get().getProtocol(P0.class); Protocol.Message message = p0.getMessages().get("foo"); // check response schema is union Schema response = message.getResponse(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.NULL, response.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(1).getType()); // check request schema is union Schema request = message.getRequest(); Field field = request.getField("s"); assertNotNull("field 's' should not be null", field); Schema param = field.schema(); assertEquals(Schema.Type.UNION, param.getType()); assertEquals(Schema.Type.NULL, param.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, param.getTypes().get(1).getType()); // check union erasure assertEquals(String.class, ReflectData.get().getClass(response)); assertEquals(String.class, ReflectData.get().getClass(param)); }
private Message parseMessage(String messageName, JsonNode json) { String doc = parseDocNode(json); if (fieldDocNode != null) fieldDoc = fieldDocNode.textValue(); Field newField = new Field(name, Schema.parse(fieldTypeNode,types), fieldDoc, field.get("default")); Set<String> aliases = Schema.parseAliases(field); if (aliases != null) { // add aliases for (String alias : aliases) newField.addAlias(alias); String prop = i.next(); if (!FIELD_RESERVED.contains(prop)) // ignore reserved newField.addProp(prop, field.get(prop)); Schema request = Schema.createRecord(fields); && Schema.parse(responseNode, types).getType() != Schema.Type.NULL) throw new SchemaParseException("One way response must be null: "+json); return new Message(messageName, doc, mProps, request);
@Test /** Construct and use a different protocol whose "hello" method has an extra argument to check that schema is sent to parse request. */ public void testHandshake() throws IOException { Protocol protocol = new Protocol("Simple", "org.apache.avro.test"); List<Field> fields = new ArrayList<>(); fields.add(new Schema.Field("extra", Schema.create(Schema.Type.BOOLEAN), null, null)); fields.add(new Schema.Field("greeting", Schema.create(Schema.Type.STRING), null, null)); Protocol.Message message = protocol.createMessage("hello", null /* doc */, Schema.createRecord(fields), Schema.create(Schema.Type.STRING), Schema.createUnion(new ArrayList<>())); protocol.getMessages().put("hello", message); Transceiver t = new SocketTransceiver(new InetSocketAddress(server.getPort())); try { GenericRequestor r = new GenericRequestor(protocol, t); GenericRecord params = new GenericData.Record(message.getRequest()); params.put("extra", Boolean.TRUE); params.put("greeting", new Utf8("bob")); Utf8 response = (Utf8)r.request("hello", params); assertEquals(new Utf8("goodbye"), response); } finally { t.close(); } }
@Override public String getProperty(String key) throws Exception { if(key == null) { throw new NullPointerException("key"); } Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_META).getRequest(); GenericRecord req = new GenericData.Record(schema); req.put("src", _sourceUtf8); req.put("opt", ProtocolConstants.OPT_GET_PROPERTY_UTF8); req.put("key", new Utf8(key)); Utf8 res = (Utf8)send(ProtocolConstants.MSG_META, req); return res == null ? null : res.toString(); }
public Object respond(Message message, Object request) throws AvroRemoteException { GenericRecord params = (GenericRecord)request; if ("hello".equals(message.getName())) { LOG.info("hello: "+params.get("greeting")); return new Utf8("goodbye"); } if ("echo".equals(message.getName())) { Object record = params.get("record"); LOG.info("echo: "+record); return record; } if ("echoBytes".equals(message.getName())) { Object data = params.get("data"); LOG.info("echoBytes: "+data); return data; } if ("error".equals(message.getName())) { if (throwUndeclaredError) throw new RuntimeException("foo"); GenericRecord error = new GenericData.Record(PROTOCOL.getType("TestError")); error.put("message", new Utf8("an error")); throw new AvroRemoteException(error); } throw new AvroRuntimeException("unexpected message: "+message.getName()); }
@Test public void testHello() throws IOException { GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("hello").getRequest()); params.put("greeting", new Utf8("bob")); Utf8 response = (Utf8)requestor.request("hello", params); assertEquals(new Utf8("goodbye"), response); }
@Test public void testP4() throws Exception { Protocol p = ReflectData.get().getProtocol(P4.class); Protocol.Message message = p.getMessages().get("foo"); assertEquals(Schema.Type.INT, message.getResponse().getType()); Field field = message.getRequest().getField("x"); assertEquals(Schema.Type.INT, field.schema().getType()); }
@Test public void testEcho() throws IOException { GenericRecord record = new GenericData.Record(PROTOCOL.getType("TestRecord")); record.put("name", new Utf8("foo")); record.put("kind", new GenericData.EnumSymbol (PROTOCOL.getType("Kind"), "BAR")); record.put("hash", new GenericData.Fixed (PROTOCOL.getType("MD5"), new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("echo").getRequest()); params.put("record", record); Object echoed = requestor.request("echo", params); assertEquals(record, echoed); }
Object request = readRequest(rm.getRequest(), m.getRequest(), in); if ((m.isOneWay() != rm.isOneWay()) && wasConnected) throw new AvroRuntimeException("Not both one-way: "+messageName); if (m.isOneWay() && wasConnected) // no response data return null; writeResponse(m.getResponse(), response, out); else try { writeError(m.getErrors(), error, out); } catch (UnresolvedUnionException e) { // unexpected error throw error; out = EncoderFactory.get().binaryEncoder(bbo, null); out.writeBoolean(true); writeError(Protocol.SYSTEM_ERRORS, new Utf8(e.toString()), out); if (null == handshake) { handshake = new ByteBufferOutputStream().getBufferList();
@Override public int drainTo(OutputStream target) throws IOException { int written; if (getPartial() != null) { written = (int) IoUtils.copy(getPartial(), target); } else { Schema reqSchema = message.getRequest(); CountingOutputStream outputStream = new CountingOutputStream(target); BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(outputStream, null); int i = 0; for (Schema.Field param : reqSchema.getFields()) { new SpecificDatumWriter<>(param.schema()).write(args[i++], out); } out.flush(); args = null; written = outputStream.getWrittenCount(); } return written; } }
@Test public void testEchoBytes() throws IOException { Random random = new Random(); int length = random.nextInt(1024*16); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("echoBytes").getRequest()); ByteBuffer data = ByteBuffer.allocate(length); random.nextBytes(data.array()); data.flip(); params.put("data", data); Object echoed = requestor.request("echoBytes", params); assertEquals(data, echoed); }
@Test public void testMessageFieldAliases() throws IOException{ Protocol protocol = getSimpleProtocol(); final Message msg = protocol.getMessages().get("hello"); assertNotNull(msg); final Schema.Field field = msg.getRequest().getField("greeting"); assertNotNull(field); assertTrue(field.aliases().contains("salute")); }
@Test public void testError() throws IOException { GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("error").getRequest()); AvroRemoteException error = null; try { requestor.request("error", params); } catch (AvroRemoteException e) { error = e; } assertNotNull(error); assertEquals("an error", ((GenericRecord)error.getValue()).get("message").toString()); }
args = new String[] { "7002", "7003" }; Protocol protocol = Protocol.parse("{\"protocol\": \"sleepy\", " + "\"messages\": { \"sleep\": {" + " \"request\": [{\"name\": \"millis\", \"type\": \"long\"}," + GenericRecord params = new GenericData.Record(protocol.getMessages().get( "sleep").getRequest()); Random rand = new Random(); params.put("millis", Math.abs(rand.nextLong()) % 1000); int payloadSize = Math.abs(rand.nextInt()) % 10000; byte[] payload = new byte[payloadSize]; rand.nextBytes(payload); params.put("data", ByteBuffer.wrap(payload)); req.request("sleep", params);
private int writeResponse(OutputStream target) throws IOException { int written; if (message.isOneWay()) { written = 0; } else if (response instanceof Exception) { try { out.writeBoolean(true); new SpecificDatumWriter(message.getErrors()).write(response, out); } catch (Exception e) { bao = new ByteArrayOutputStream(); out = ENCODER_FACTORY.binaryEncoder(bao, null); out.writeBoolean(true); new SpecificDatumWriter(Protocol.SYSTEM_ERRORS).write(new Utf8(e.toString()), out); BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(outputStream, null); out.writeBoolean(false); new SpecificDatumWriter(message.getResponse()).write(response, out); out.flush(); written = outputStream.getWrittenCount();
throw new ParseException(); for (Schema s : importProtocol.getTypes()) names.put(s.getFullName(), s); p.getMessages().putAll(importProtocol.getMessages()); break; case SCHEMA: case TICK: message = MessageDeclaration(p, props); p.getMessages().put(message.getName(), message); break; default:
private Protocol addStringType(Protocol p) { if (stringType != StringType.String) return p; Protocol newP = new Protocol(p.getName(), p.getDoc(), p.getNamespace()); Map<Schema,Schema> types = new LinkedHashMap<>(); for (Map.Entry<String, Object> a : p.getObjectProps().entrySet()) { newP.addProp(a.getKey(), a.getValue()); } // annotate types Collection<Schema> namedTypes = new LinkedHashSet<>(); for (Schema s : p.getTypes()) namedTypes.add(addStringType(s, types)); newP.setTypes(namedTypes); // annotate messages Map<String,Message> newM = newP.getMessages(); for (Message m : p.getMessages().values()) newM.put(m.getName(), m.isOneWay() ? newP.createMessage(m, addStringType(m.getRequest(), types)) : newP.createMessage(m, addStringType(m.getRequest(), types), addStringType(m.getResponse(), types), addStringType(m.getErrors(), types))); return newP; }