jose4j: how to set full serialization input? - jwe

Is there a way to set a JWE full serialization input with jose4j? For example, what goes in the TODO below?
public String decryptJWE(PrivateKey privateKey, String payload, boolean compact) throws JoseException {
JsonWebEncryption jwe = new JsonWebEncryption();
if (compact) {
jwe.setCompactSerialization(payload);
} else {
// TODO: what goes here? expecting something like jwe.setFullSerialization(payload)
}
jwe.setKey(privateKey);
return jwe.getPayload();
}

No, only the JWE compact serialization is supported. The general and flattened JWE JSON serializations aren't directly supported.

Related

Why does my apache camel split/aggregate route return no results?

I'm trying to read a binary file, convert it into a pojo format and then output as CSV. The unmarshalling (and marshalling) seems to be fine, but I'm having trouble optimising the converting the relevant records to Foo.class. The attempt below returns no results.
from(String.format("file://%s?move=%s", INPUT_DIRECTORY, MOVE_DIRECTORY))
.unmarshal(unmarshaller)
.split(bodyAs(Iterator.class), new ListAggregationStrategy())
.choice()
.when(not(predicate)).stop()
.otherwise().convertBodyTo(Foo.class)
.end()
.end()
.marshal(csv)
.to(String.format("file://%s?fileName=${header.CamelFileName}.csv", OUTPUT_DIRECTORY));
I was able to get it to work like this, but it feels like there has to be a better way - This will be need to be efficient, and having a 1s timeout feels like it goes against that, which is why I was attempting to use the built in split aggregation. Alternatively some way of using completionFromBatchConsumer, but I was struggling to make that work either!.
from(String.format("file://%s?move=%s", INPUT_DIRECTORY, MOVE_DIRECTORY))
.unmarshal(unmarshaller)
.split(bodyAs(Iterator.class))
.streaming()
.filter(predicate)
.convertBodyTo(Foo.class)
.aggregate(header("CamelFileName"), new ListAggregationStrategy())
.completionTimeout(1000)
.marshal(csv)
.to(String.format("file://%s?fileName=${header.CamelFileName}.csv", OUTPUT_DIRECTORY));
You could create your own AggregationStrategy in your first solution.
Instead of calling stop() in your choice statement, put a simple header like "skipMerge" to true.
In your strategy, test if this header exists and if so, skip it.
class ArrayListAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object newBody = newExchange.getIn().getBody();
Boolean skipMerge = newExchange.getIn().getHeader("skipMerge", Boolean.class);
if (!skipMerge) { return oldExchange; }
ArrayList<Object> list = null;
if (oldExchange == null) {
list = new ArrayList<Object>();
list.add(newBody);
newExchange.getIn().setBody(list);
return newExchange;
} else {
list = oldExchange.getIn().getBody(ArrayList.class);
list.add(newBody);
return oldExchange;
}
}
}
Currently, your code never goes to marshal(csv) because the aggregator does not receive all the splitted parts.

I can't unmarshal basic string from akka http stream

I am trying to consume a stream of json objects in akka-http. ( akka http version "10.0.9", akka-http-play-json version 1.10.1)
I follow examples on web but, for String I am getting:
could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromByteStringUnmarshaller[String]
and for my user defined Foo case class (for which I provided the json protocol):
could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromByteStringUnmarshaller[server.Foo]
This is the code that is simplified. I provide a EntityStreamingSupport.json() and for the Foo object a Json Format. I don't think I need one for String. If I don't put the asSourceOf and read a simple String object or a Foo case class object the code works. What am I missing?
package server
import akka.http.scaladsl.common.EntityStreamingSupport
import akka.http.scaladsl.server.{ Directives, Route }
import akka.http.scaladsl.model.StatusCodes
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._
import play.api.libs.json._
case class Foo(bar: String)
class StreamingMarketDataUpload extends Directives {
implicit val jsonStreamingSupport = EntityStreamingSupport.json()
implicit val jsonFooFormat = Json.format[Foo]
lazy val routes: Route =
path("upload1") {
post {
entity(as[String]) { input =>
complete(StatusCodes.OK)
}
}
} ~ path("upload2") {
post {
// Compile error here
entity(asSourceOf[String]) { marks =>
complete(StatusCodes.OK)
}
}
} ~ path("upload3") {
post {
entity(as[Foo]) { input =>
complete(StatusCodes.OK)
}
}
} ~ path("upload4") {
post {
// Compile error here
entity(asSourceOf[Foo]) { marks =>
complete(StatusCodes.OK)
}
}
}
}
asSourceOf[T] tries to consume the incoming data as a Source[T, _]. As its method signature indicates, asSourceOf[T] takes an implicit FromByteStringUnmarshaller[T] parameter. The de.heikoseeberger.akkahttpplayjson.PlayJsonSupport utility doesn't provide an implementation of this unmarshaller (as of version 1.19.0), but it does provide the unmarshallers necessary for consuming a simple String or Foo. This is why your code works when it's not using asSourceOf.
The examples in the documentation use SprayJsonSupport, which is shipped as part of Akka HTTP. If you don't want to use SprayJsonSupport, you'll have to implement a FromByteStringUnmarshaller to use asSourceOf: the linked source code can give you an idea of how to do this.

TextEncodings.Base64Url.Decode vs Convert.FromBase64String

I was working on creating a method that would generate a JWT token. Part of the method reads a value from my web.config that services as the "secret" used to generate the hash used to create the signature for the JWT token.
<add key="MySecret" value="j39djak49H893hsk297353jG73gs72HJ3tdM37Vk397" />
Initially I tried using the following to convert the "secret" value to a byte array.
byte[] key = Convert.FromBase64String(ConfigurationManager.AppSettings["MySecret"]);
However, an exception was thrown when this line was reached ...
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
So I looked into the OAuth code and so another method being used to change a base64 string into a byte array
byte[] key = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["MySecret"]);
This method worked without issue. To me it looks like they are doing the same thing. Changing a Base64 text value into an array of bytes. However, I must be missing something. Why does Convert.FromBase64String fail and TextEncodings.Base64Url.Decode work?
I came across the same thing when I migrated our authentication service to .NET Core. I had a look at the source code for the libraries we used in our previous implementation, and the difference is actually in the name itself.
The TextEncodings class has two types of text encoders, Base64TextEncoder and Base64UrlEncoder. The latter one modifies the string slightly so the base64 string can be used in an url.
My understanding is that it is quite common to replace + and / with - and _. As a matter of fact we have been doing the same with our handshake tokens. Additionally the padding character(s) at the end can also be removed. This leaves us with the following implementation (this is from the source code):
public class Base64UrlTextEncoder : ITextEncoder
{
public string Encode(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
return Convert.ToBase64String(data).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
public byte[] Decode(string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
return Convert.FromBase64String(Pad(text.Replace('-', '+').Replace('_', '/')));
}
private static string Pad(string text)
{
var padding = 3 - ((text.Length + 3) % 4);
if (padding == 0)
{
return text;
}
return text + new string('=', padding);
}
}

Converting HL7 v2 to JSON

I am looking to convert HL7 v2 (older EDI format) messages to JSON, so I could make them processable under Apache Drill and compressible under Parquet.
I looked into HAPI, but I am not having luck finding utility for non-XML HL7 to JSON conversion.
Does anyone have a suggestion or a reference to a library?
Just use HAPI to convert to XML. The code below requires Saxon, because the XML-to-JSON requires XSLT 2.0, but if you already have a method to convert XML to JSON, then you just need the first two lines, which are entirely HAPI. You should download the XSLT locally for production, of course. :-)
String convertHL7ToJson(Message message) {
try {
DefaultXMLParser xmlParser = new DefaultXMLParser(new CanonicalModelClassFactory("2.6"));
String xml = xmlParser.encode(message);
Transformer xmlTransformer = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null).newTransformer(
new StreamSource(new StringReader(readFileFromURL("https://github.com/bramstein/xsltjson/raw/master/conf/xml-to-json.xsl")))
);
StringWriter result = new StringWriter();
xmlTransformer.transform(
new StreamSource(new StringReader(xml)), new StreamResult(result)
);
return result.toString();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
String readFileFromURL(String url) {
InputStream is = null;
try {
return new Scanner(is = new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next();
} catch (Throwable t) {
t.printStackTrace();
} finally {
if(is != null)
try {
is.close();
} catch (Throwable ignored){}
}
return null;
}
This creates output like this:
"ORM_O01":{"MSH":{"MSH.1":"|","MSH.2":"^~\\&","MSH.3":{"HD.1":"TEST"},"MSH.4":{"HD.1":"TEST000","HD.2":"BL"},...
If there is a way to convert the HL7 to XML, you can query the XML natively 1 with Drill and then ultimately convert that to parquet.

How to read byte by byte from appengine datastore Entity Object

In a nutshell, since GAE cannot write to a filesystem, I have decided to persist my data into the datastore (using JDO). Now, I will like to retrieve the data byte by byte and pass it to the client as an input stream. There's code from the gwtupload library(http://code.google.com/p/gwtupload/) (see below) which breaks on GAE because it writes to the system filesystem. I'll like to be able to provide a GAE ported solution.
public static void copyFromInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[100000];
while (true) {
synchronized (buffer) {
int amountRead = in.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
in.close();
out.flush();
out.close();
}
One work around I have tried (didn't work) is to retrieve the data from the datastore as a resource like this:
InputStream resourceAsStream = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Query q = pm.newQuery(ImageFile.class);
lf = q.execute();
resourceAsStream = getServletContext().getResourceAsStream((String) pm.getObjectById(lf));
} finally {
pm.close();
}
if (lf != null) {
response.setContentType(receivedContentTypes.get(fieldName));
copyFromInputStreamToOutputStream(resourceAsStream, response.getOutputStream());
}
I welcome your suggestions.
Regards
Store data in a byte array, and use a ByteArrayInputStream or ByteArrayOutputStream to pass it to libraries that expect streams.
If by 'client' you mean 'HTTP client' or browser, though, there's no reason to do this - just deal with regular byte arrays on your end and send them to/from the user as you would any other data. The only reason to mess around with streams like this is if you have some library that expects them.

Resources