001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons.domain;
019
020import static javax.ws.rs.core.Variant.mediaTypes;
021import static org.apache.jena.riot.WebContent.contentTypeJSONLD;
022import static org.apache.jena.riot.WebContent.contentTypeN3;
023import static org.apache.jena.riot.WebContent.contentTypeN3Alt2;
024import static org.apache.jena.riot.WebContent.contentTypeNTriples;
025import static org.apache.jena.riot.WebContent.contentTypeRDFXML;
026import static org.apache.jena.riot.WebContent.contentTypeTurtle;
027import static org.apache.jena.riot.WebContent.contentTypeTurtleAlt2;
028
029import java.util.List;
030
031import javax.ws.rs.core.MediaType;
032import javax.ws.rs.core.Variant;
033
034/**
035 * This is a convenience class carrying the various RDF content-type values as
036 * Strings and MediaTypes, after the fashion of the constants available on
037 * javax.ws.rs.core.MediaType
038 *
039 * @author ba2213
040 */
041public abstract class RDFMediaType extends MediaType {
042
043    private static final String CHARSET_UTF8 = ";charset=utf-8";
044
045    public static final String N3 = contentTypeN3;
046
047    public static final MediaType N3_TYPE = typeFromString(N3);
048
049    public static final String N3_WITH_CHARSET = N3 + CHARSET_UTF8;
050
051    public static final String N3_ALT2 = contentTypeN3Alt2;
052
053    public static final MediaType N3_ALT2_TYPE = typeFromString(N3_ALT2);
054
055    public static final String N3_ALT2_WITH_CHARSET = N3_ALT2 + CHARSET_UTF8;
056
057    public static final String TURTLE = contentTypeTurtle;
058
059    public static final MediaType TURTLE_TYPE = typeFromString(TURTLE);
060
061    public static final String TURTLE_WITH_CHARSET = TURTLE + CHARSET_UTF8;
062
063    public static final String TURTLE_X = contentTypeTurtleAlt2;
064
065    public static final MediaType TURTLE_X_TYPE = typeFromString(TURTLE_X);
066
067    public static final String RDF_XML = contentTypeRDFXML;
068
069    public static final MediaType RDF_XML_TYPE = typeFromString(RDF_XML);
070
071    public static final String NTRIPLES = contentTypeNTriples;
072
073    public static final MediaType NTRIPLES_TYPE = typeFromString(NTRIPLES);
074
075    public final static String JSON_LD = contentTypeJSONLD;
076
077    public final static MediaType JSON_LD_TYPE = typeFromString(JSON_LD);
078
079    public final static String TEXT_PLAIN_WITH_CHARSET = TEXT_PLAIN + CHARSET_UTF8;
080
081    public final static String TEXT_HTML_WITH_CHARSET = TEXT_HTML + CHARSET_UTF8;
082
083    public static final List<Variant> POSSIBLE_RDF_VARIANTS = mediaTypes(
084            RDF_XML_TYPE, TURTLE_TYPE, N3_TYPE, N3_ALT2_TYPE, NTRIPLES_TYPE,
085            TEXT_PLAIN_TYPE, TURTLE_X_TYPE, JSON_LD_TYPE).add().build();
086
087    public static final String POSSIBLE_RDF_RESPONSE_VARIANTS_STRING[] = {
088        TURTLE_WITH_CHARSET, N3_WITH_CHARSET, N3_ALT2_WITH_CHARSET, RDF_XML, NTRIPLES,
089        TEXT_PLAIN_WITH_CHARSET, TURTLE_X, JSON_LD };
090
091    private static MediaType typeFromString(final String type) {
092        return new MediaType(type.split("/")[0], type.split("/")[1]);
093    }
094
095}