Java tutorial
/* * Copyright 2012 Research Studios Austria Forschungsges.m.b.H. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ssoup.backend.web; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import org.apache.jena.atlas.web.ContentType; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; import org.apache.jena.riot.RDFLanguages; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import java.io.IOException; import java.util.Collection; /** * HttpMessageConverter for using jena models with Spring WebMVC. Supports all formats jena supports */ public class RdfModelConverter extends AbstractHttpMessageConverter<Model> { private static final Logger logger = LoggerFactory.getLogger(RdfModelConverter.class); public RdfModelConverter() { this(buildMediaTypeArray()); } public RdfModelConverter(MediaType supportedMediaType) { super(supportedMediaType); } public RdfModelConverter(MediaType... supportedMediaTypes) { super(supportedMediaTypes); } @Override protected boolean supports(Class<?> clazz) { return Model.class.isAssignableFrom(clazz); } @Override protected Model readInternal(Class<? extends Model> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { Model model = ModelFactory.createDefaultModel(); Lang rdfLanguage = mimeTypeToJenaLanguage(httpInputMessage.getHeaders().getContentType(), Lang.JSONLD); RDFDataMgr.read(model, httpInputMessage.getBody(), "", rdfLanguage); return model; } @Override protected void writeInternal(Model model, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException { Lang rdfLanguage = mimeTypeToJenaLanguage(httpOutputMessage.getHeaders().getContentType(), Lang.JSONLD); RDFDataMgr.write(httpOutputMessage.getBody(), model, rdfLanguage); httpOutputMessage.getBody().flush(); } private static Lang mimeTypeToJenaLanguage(MediaType mediaType, Lang defaultLanguage) { Lang lang = RDFLanguages.contentTypeToLang(mediaType.toString()); if (lang == null) return defaultLanguage; return lang; } private static MediaType[] buildMediaTypeArray() { // now register the media types this converter can handle Collection<Lang> languages = RDFLanguages.getRegisteredLanguages(); MediaType[] mediatypes = new MediaType[languages.size()]; int i = 0; for (Lang lang : languages) { ContentType ct = lang.getContentType(); logger.debug("registering converter for rdf content type {}", lang.getContentType()); MediaType mt = new MediaType(ct.getType(), ct.getSubType()); mediatypes[i++] = mt; } return mediatypes; } }