1

I'm building a prototype of xAPI LRS and use Java/Jersey to create a test xAPI REST service implementation, recent Jersey version is using MOXy for XML and JSON processing.

Now I'm facing the issue, according to the specification "statements" resource for POST can accept a single JSON statement or a list of statements.

I cannot handle that, because of lack of my MOXy knowledge. I tried different approaches and did not find a solution.

I found similair question here dated 2014, unfortunately it was not answered so far...

Can anyone propose a workaround I would like to keep use MOXy?

1 Answers1

0

I've managed some workaround via ReaderInterceptor

@ArrayWrapper
public class ArrayWrapperInterceptor implements ReaderInterceptor {
    public ArrayWrapperInterceptor() {
        openingCurlyBrace = Pattern.compile("\\A\\s*\\{");
        closingCurlyBrace = Pattern.compile("\\}\\s*\\Z");
    }
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
        InputStream is = context.getInputStream();
        String content = "";
        while (is.available() != 0) {
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            content = content + new String(bytes);
        }
        if (content.length() > 0 && openingCurlyBrace.matcher(content).find() && closingCurlyBrace.matcher(content).find()) {
            content = "[" + content + "]";
        }
        context.setInputStream(new ByteArrayInputStream(content.getBytes()));       
        return context.proceed();
    }
    private Pattern openingCurlyBrace;
    private Pattern closingCurlyBrace;
}

I defined this annotation

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface ArrayWrapper {}

and put it in both places (at interceptor and at my POST resource method).

In order to make the interceptor work only with @ArrayWrapper annotation, I've added register(ArrayWrapperInterceptor.class) to my application class. Without it Jersey does not aware of it and with @Provider annotation my interceptor is global.

Maybe it's not the best solution but now it looks like it's the only one solution available to me.

Later I'll try to investigate a possibility to use some dynamic object in my resource method (like JSON object) instead of interceptor use.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65