0

Here is my function to read Model (In Apache Jena) from HttpResponse body data

1)public static Model accessModelFromResponse(HttpResponse response) throws Exception {

2)        Header contentType = response.getFirstHeader("Content-Type");
3)        String mimeType = contentType.getValue();


4)        String data = "";
5)        if(mimeType.equals("application/ld+json"))
6)        {
7)            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
8)            String json;
9)            while ((json = reader.readLine()) != null) {
10)                data += json;
11)            }
12)        }
13)        else if(mimeType.equals("application/json"))
14)        {
15)            data = (String) Services.addContextToJson(response);
16)            mimeType = "application/ld+json";
17)        }
18)        else
19)        {
20)           throw new Exception("Data is not json/jsonld type");
21)        }
22)        InputStream in = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));

23)        Model model = ModelFactory.createDefaultModel();
24)        try {
25)            model.read(in, "http://myweb.com/", mimeType);
26)        } catch (Exception ex) {
27)            throw ex;
28)        }
29)        return model;
30)    }

Everthing worked fine when I was using Jena 3.1.0 version with jdk 8 .
But later to support jdk 7 I change Jena version to 2.13.0 and things got worst.
It is throwing java.lang.Exception: com.hp.hpl.jena.shared.NoReaderForLangException: application/ld+json error which is occured from line no 27 .

I think Jena 2.13.0 doesn't support application/ld+json Language.
I found if I set third param of model.read() which is mimeType in above code to "JSONLD" it work fine.But still I have question why application/ld+json doesn't work while it works fine in Jena 3.1.0 ? Is this thing supported in later releases only?

Badman
  • 407
  • 5
  • 17

1 Answers1

0

model.read takes a name of a language, not a MIME type and in Jena 2.13.0 using the mime type wasn't provided. You might be able to look up the MIME type in RDFParserRegistry and/or read with RDFDataMgr.

AndyS
  • 16,345
  • 17
  • 21