@@ -410,77 +410,18 @@ private void addClaim(String name, Object value) {
410410 }
411411
412412 private String signJsonEncode (Schema schemaForHeader , Schema schemaForPayload ) throws Exception {
413- /*Schema schemaForHeader = SchemaBuilder
414- .record("record").namespace("namespace")
415- .fields()
416- .name("alg").type().stringType().noDefault()
417- .name("typ").type().stringType().noDefault()
418- .endRecord();*/
419- /*String schema = "{" +
420- "\"type\":\"record\"," +
421- "\"namespace\":\"foo\"," +
422- "\"name\":\"Person\"," +
423- "\"fields\":" +
424- "{" +
425- "\"alg\":\"string\"," +
426- "\"typ\":\"string\"" +
427- "}" +
428- "}";*/
429413 byte [] header = jsonToAvro (headerJson , schemaForHeader .toString ());
430414 schemaToHeaderAndPayloadByteArray .put (schemaForHeader , header );
431- System .out .println (avroToJson (header , schemaForHeader ));
432- //.name("aud").type().array().items().stringType().noDefault()
433- /*Schema schemaForPayload = SchemaBuilder
434- .record("record").namespace("namespace")
435- .fields()
436- .name("sub").type().array().items().stringType().noDefault()
437- .name("iss").type().array().items().stringType().noDefault()
438- .name("aud").type().stringType().noDefault()
439- .name("iat").type().intType().noDefault()
440- .endRecord();*/
441415 byte [] payload = jsonToAvro (payloadJson , schemaForPayload .toString ());
442416 schemaToHeaderAndPayloadByteArray .put (schemaForPayload , payload );
443- System .out .println (avroToJson (payload , schemaForPayload ));
444417 String content = String .format ("%s.%s" , new String (header ), new String (payload ));
445418
446419 byte [] signatureBytes = algorithm .sign (content .getBytes (StandardCharsets .UTF_8 ));
447420 String signature = Base64 .encodeBase64URLSafeString (signatureBytes );
448- System .out .println (signature );
449- System .out .println (Base64 .decodeBase64 (signature ));
450421
451- return String .format ("%s.%s" , content , signature ); //for now
422+ return String .format ("%s.%s" , content , signature );
452423 }
453424
454- /*public static String avroToJson(byte[] avro, String schemaStr) throws IOException {
455- boolean pretty = false;
456- GenericDatumReader<GenericRecord> reader = null;
457- JsonEncoder encoder = null;
458- ByteArrayOutputStream output = null;
459- try {
460- Schema schema = new Schema.Parser().parse(schemaStr);
461- reader = new GenericDatumReader<GenericRecord>(schema);
462- InputStream input = new ByteArrayInputStream(avro);
463- output = new ByteArrayOutputStream();
464- DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
465- encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);
466- Decoder decoder = DecoderFactory.get().binaryDecoder(input, null);
467- GenericRecord datum;
468- while (true) {
469- try {
470- datum = reader.read(null, decoder);
471- } catch (EOFException eofe) {
472- break;
473- }
474- writer.write(datum, encoder);
475- }
476- encoder.flush();
477- output.flush();
478- return new String(output.toByteArray());
479- } finally {
480- try { if (output != null) output.close(); } catch (Exception e) { }
481- }
482- }*/
483-
484425 public static String avroToJson (byte [] avro , Schema schema ) throws IOException {
485426 boolean pretty = false ;
486427 GenericDatumReader <Object > reader = new GenericDatumReader <>(schema );
@@ -495,30 +436,6 @@ public static String avroToJson(byte[] avro, Schema schema) throws IOException {
495436 return new String (output .toByteArray (), "UTF-8" );
496437 }
497438
498- public static String avroToJson (byte [] avro ) throws IOException {
499- boolean pretty = false ;
500- GenericDatumReader <GenericRecord > reader = null ;
501- JsonEncoder encoder = null ;
502- ByteArrayOutputStream output = null ;
503- try {
504- reader = new GenericDatumReader <GenericRecord >();
505- InputStream input = new ByteArrayInputStream (avro );
506- DataFileStream <GenericRecord > streamReader = new DataFileStream <GenericRecord >(input , reader );
507- output = new ByteArrayOutputStream ();
508- Schema schema = streamReader .getSchema ();
509- DatumWriter <GenericRecord > writer = new GenericDatumWriter <GenericRecord >(schema );
510- encoder = EncoderFactory .get ().jsonEncoder (schema , output , pretty );
511- for (GenericRecord datum : streamReader ) {
512- writer .write (datum , encoder );
513- }
514- encoder .flush ();
515- output .flush ();
516- return new String (output .toByteArray ());
517- } finally {
518- try { if (output != null ) output .close (); } catch (Exception e ) { }
519- }
520- }
521-
522439 public static byte [] jsonToAvro (String json , String schemaStr ) throws Exception {
523440 InputStream input = null ;
524441 GenericDatumWriter <GenericRecord > writer = null ;
0 commit comments