001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2024-2026 The Enola <https://enola.dev> Authors 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * https://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 dev.enola.thing.io; 019 020import static java.util.Collections.emptySet; 021 022import com.google.common.collect.ImmutableMap; 023import com.google.common.collect.ImmutableMultimap; 024import com.google.common.collect.Multimap; 025import com.google.common.net.MediaType; 026 027import dev.enola.common.io.mediatype.MediaTypeProvider; 028import dev.enola.common.protobuf.ProtobufMediaTypes; 029import dev.enola.thing.proto.Thing; 030 031import java.nio.charset.StandardCharsets; 032import java.util.Map; 033import java.util.Set; 034 035public class ThingMediaTypes implements MediaTypeProvider { 036 037 // TODO Use vnd.enola.* like e.g. in GraphCommonsMediaType (for consistency) 038 039 public static final MediaType THING_TEXTPROTO_UTF_8 = 040 ProtobufMediaTypes.setProtoMessageFQN( 041 ProtobufMediaTypes.PROTOBUF_TEXTPROTO_UTF_8, 042 Thing.getDescriptor().getFullName()); 043 044 public static final MediaType THING_BINARYPROTO_UTF_8 = 045 ProtobufMediaTypes.setProtoMessageFQN( 046 ProtobufMediaTypes.PROTOBUF_BINARY, Thing.getDescriptor().getFullName()); 047 048 private static final String THING_SUBTYPE = "enola.dev#thing"; 049 050 public static final MediaType THING_JSON_UTF_8 = 051 MediaType.create("text", THING_SUBTYPE + "+json").withCharset(StandardCharsets.UTF_8); 052 053 public static final MediaType THING_YAML_UTF_8 = 054 MediaType.create("text", THING_SUBTYPE + "+yaml").withCharset(StandardCharsets.UTF_8); 055 056 public static final MediaType THING_HTML_UTF_8 = 057 MediaType.create("text", THING_SUBTYPE + "+html").withCharset(StandardCharsets.UTF_8); 058 059 @Override 060 public Map<MediaType, Set<MediaType>> knownTypesWithAlternatives() { 061 return ImmutableMap.of( 062 THING_TEXTPROTO_UTF_8, 063 emptySet(), 064 THING_BINARYPROTO_UTF_8, 065 emptySet(), 066 THING_JSON_UTF_8, 067 emptySet(), 068 THING_YAML_UTF_8, 069 emptySet(), 070 THING_HTML_UTF_8, 071 emptySet()); 072 } 073 074 @Override 075 public Multimap<String, MediaType> extensionsToTypes() { 076 return ImmutableMultimap.of( 077 ".thing.txtpb", 078 THING_TEXTPROTO_UTF_8, 079 ".thing.binpb", 080 THING_BINARYPROTO_UTF_8, 081 ".thing.json", 082 THING_JSON_UTF_8, 083 ".thing.yaml", 084 THING_YAML_UTF_8, 085 ".thing.html", 086 THING_HTML_UTF_8); 087 } 088}