Exemplo n.º 1
0
  public static void checkBlockingBinary(
      Schema schema, Object datum, DatumWriter<Object> writer, DatumReader<Object> reader)
      throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.setSchema(schema);
    Encoder encoder = EncoderFactory.get().blockingBinaryEncoder(out, null);
    writer.write(datum, encoder);
    encoder.flush();
    byte[] data = out.toByteArray();

    reader.setSchema(schema);

    Object decoded = reader.read(null, DecoderFactory.get().binaryDecoder(data, null));

    assertEquals("Decoded data does not match.", datum, decoded);
  }
Exemplo n.º 2
0
  private static void checkJson(Schema schema, Object datum, String json) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().jsonEncoder(schema, out);
    DatumWriter<Object> writer = new GenericDatumWriter<Object>();
    writer.setSchema(schema);
    writer.write(datum, encoder);
    encoder.flush();
    byte[] data = out.toByteArray();

    String encoded = new String(data, "UTF-8");
    assertEquals("Encoded data does not match.", json, encoded);

    DatumReader<Object> reader = new GenericDatumReader<Object>();
    reader.setSchema(schema);
    Object decoded =
        reader.read(null, DecoderFactory.get().jsonDecoder(schema, new ByteArrayInputStream(data)));

    assertEquals("Decoded data does not match.", datum, decoded);
  }
Exemplo n.º 3
0
  private static void checkJson(
      Schema schema, Object datum, DatumWriter<Object> writer, DatumReader<Object> reader)
      throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().jsonEncoder(schema, out);
    writer.setSchema(schema);
    writer.write(datum, encoder);
    writer.write(datum, encoder);
    encoder.flush();
    byte[] data = out.toByteArray();

    reader.setSchema(schema);
    Decoder decoder = DecoderFactory.get().jsonDecoder(schema, new ByteArrayInputStream(data));
    Object decoded = reader.read(null, decoder);
    assertEquals("Decoded data does not match.", datum, decoded);

    decoded = reader.read(decoded, decoder);
    assertEquals("Decoded data does not match.", datum, decoded);
  }
Exemplo n.º 4
0
  /**
   * Gives the output message
   *
   * @param outputType output data type
   * @param result mapping result
   * @return the output as a String
   * @throws IOException
   */
  public String getOutputMessage(String outputType, GenericRecord result)
      throws SynapseException, IOException {

    DatumWriter<GenericRecord> writer = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Encoder encoder = new DummyEncoder(byteArrayOutputStream);
    // OMElement outMessage = null;
    String outMessage = null;

    try {

      writer = WriterRegistry.getInstance().get(outputType).newInstance();
      writer.setSchema(result.getSchema());
      writer.write(result, encoder);

      if (log.isDebugEnabled()) {
        log.debug("Output received from datum writer.." + byteArrayOutputStream.toString());
      }

    } catch (Exception e) {
      handleException("Data coversion Failed at JSONWriter..", e);
    } finally {
      encoder.flush();
    }

    /*try {
    	// Converts the result into an OMElement
    	outMessage = getOutputResult(byteArrayOutputStream.toString());
    } catch (XMLStreamException e) {
    	handleException(
    			"Failed at generating the OMElement for the JSON output received...",
    			e);
    }*/

    outMessage = byteArrayOutputStream.toString();

    return outMessage;
  }