001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.oapi; 014 015import org.apache.juneau.*; 016import org.apache.juneau.annotation.*; 017import org.apache.juneau.httppart.*; 018import org.apache.juneau.serializer.*; 019import org.apache.juneau.uon.*; 020 021/** 022 * Serializes POJOs to values suitable for transmission as HTTP headers, query/form-data parameters, and path variables. 023 * 024 * <ul class='seealso'> 025 * <li class='link'>{@doc juneau-marshall.OpenApiDetails.Serializers} 026 * </ul> 027 */ 028@ConfigurableContext 029public class OpenApiSerializer extends UonSerializer { 030 031 //------------------------------------------------------------------------------------------------------------------- 032 // Configurable properties 033 //------------------------------------------------------------------------------------------------------------------- 034 035 static final String PREFIX = "OpenApiSerializer"; 036 037 //------------------------------------------------------------------------------------------------------------------- 038 // Predefined instances 039 //------------------------------------------------------------------------------------------------------------------- 040 041 /** Reusable instance of {@link OpenApiSerializer}, all default settings. */ 042 public static final OpenApiSerializer DEFAULT = new OpenApiSerializer(PropertyStore.DEFAULT); 043 044 045 //------------------------------------------------------------------------------------------------------------------- 046 // Instance 047 //------------------------------------------------------------------------------------------------------------------- 048 049 /** 050 * Constructor. 051 * 052 * @param ps 053 * The property store containing all the settings for this object. 054 * @param produces 055 * The media type that this serializer produces. 056 * @param accept 057 * The accept media types that the serializer can handle. 058 * <p> 059 * Can contain meta-characters per the <c>media-type</c> specification of {@doc RFC2616.section14.1} 060 * <p> 061 * If empty, then assumes the only media type supported is <c>produces</c>. 062 * <p> 063 * For example, if this serializer produces <js>"application/json"</js> but should handle media types of 064 * <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be: 065 * <p class='bcode w800'> 066 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>); 067 * </p> 068 * <br>...or... 069 * <p class='bcode w800'> 070 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*​/json"</js>); 071 * </p> 072 * <p> 073 * The accept value can also contain q-values. 074 */ 075 public OpenApiSerializer(PropertyStore ps, String produces, String accept) { 076 super( 077 ps.builder() 078 .set(UON_encoding, false) 079 .build(), 080 produces, 081 accept 082 ); 083 } 084 085 /** 086 * Constructor. 087 * 088 * @param ps 089 * The property store containing all the settings for this object. 090 */ 091 public OpenApiSerializer(PropertyStore ps) { 092 this(ps, "text/openapi", null); 093 } 094 095 @Override /* Context */ 096 public OpenApiSerializerBuilder builder() { 097 return new OpenApiSerializerBuilder(getPropertyStore()); 098 } 099 100 /** 101 * Instantiates a new clean-slate {@link OpenApiSerializerBuilder} object. 102 * 103 * <p> 104 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 105 * the settings of the object called on. 106 * 107 * @return A new {@link OpenApiSerializerBuilder} object. 108 */ 109 public static OpenApiSerializerBuilder create() { 110 return new OpenApiSerializerBuilder(); 111 } 112 113 //----------------------------------------------------------------------------------------------------------------- 114 // Entry point methods 115 //----------------------------------------------------------------------------------------------------------------- 116 117 @Override /* Context */ 118 public OpenApiSerializerSession createSession() { 119 return createSession(createDefaultSessionArgs()); 120 } 121 122 @Override /* Serializer */ 123 public OpenApiSerializerSession createSession(SerializerSessionArgs args) { 124 return new OpenApiSerializerSession(this, args); 125 } 126 127 @Override /* HttpPartSerializer */ 128 public OpenApiSerializerSession createPartSession() { 129 return createPartSession(null); 130 } 131 132 @Override /* HttpPartSerializer */ 133 public OpenApiSerializerSession createPartSession(SerializerSessionArgs args) { 134 return new OpenApiSerializerSession(this, args); 135 } 136 137 @Override /* HttpPartSerializer */ 138 public String serialize(HttpPartType partType, HttpPartSchema schema, Object value) throws SchemaValidationException, SerializeException { 139 return createPartSession().serialize(partType, schema, value); 140 } 141 142 @Override /* HttpPartSerializer */ 143 public String serialize(HttpPartSchema schema, Object value) throws SchemaValidationException, SerializeException { 144 return createPartSession().serialize(null, schema, value); 145 } 146 147 //----------------------------------------------------------------------------------------------------------------- 148 // Other methods 149 //----------------------------------------------------------------------------------------------------------------- 150 151 @Override /* Context */ 152 public ObjectMap toMap() { 153 return super.toMap() 154 .append("OpenApiSerializer", new DefaultFilteringObjectMap() 155 ); 156 } 157}