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.httppart.*; 017import org.apache.juneau.serializer.*; 018import org.apache.juneau.uon.*; 019 020/** 021 * Serializes POJOs to values suitable for transmission as HTTP headers, query/form-data parameters, and path variables. 022 * 023 * <h5 class='section'>See Also:</h5> 024 * <ul> 025 * <li class='link'>{@doc juneau-marshall.OpenApiDetails.Serializers} 026 * </ul> 027 */ 028public class OpenApiSerializer extends UonSerializer { 029 030 //------------------------------------------------------------------------------------------------------------------- 031 // Predefined instances 032 //------------------------------------------------------------------------------------------------------------------- 033 034 /** Reusable instance of {@link OpenApiSerializer}, all default settings. */ 035 public static final OpenApiSerializer DEFAULT = new OpenApiSerializer(PropertyStore.DEFAULT); 036 037 038 //------------------------------------------------------------------------------------------------------------------- 039 // Instance 040 //------------------------------------------------------------------------------------------------------------------- 041 042 /** 043 * Constructor. 044 * 045 * @param ps 046 * The property store containing all the settings for this object. 047 * @param produces 048 * The media type that this serializer produces. 049 * @param accept 050 * The accept media types that the serializer can handle. 051 * <p> 052 * Can contain meta-characters per the <code>media-type</code> specification of {@doc RFC2616.section14.1} 053 * <p> 054 * If empty, then assumes the only media type supported is <code>produces</code>. 055 * <p> 056 * For example, if this serializer produces <js>"application/json"</js> but should handle media types of 057 * <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be: 058 * <p class='bcode w800'> 059 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>); 060 * </p> 061 * <br>...or... 062 * <p class='bcode w800'> 063 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*​/json"</js>); 064 * </p> 065 * <p> 066 * The accept value can also contain q-values. 067 */ 068 public OpenApiSerializer(PropertyStore ps, String produces, String accept) { 069 super( 070 ps.builder() 071 .set(UON_encoding, false) 072 .build(), 073 produces, 074 accept 075 ); 076 } 077 078 /** 079 * Constructor. 080 * 081 * @param ps 082 * The property store containing all the settings for this object. 083 */ 084 public OpenApiSerializer(PropertyStore ps) { 085 this(ps, "text/openapi", null); 086 } 087 088 @Override /* Context */ 089 public OpenApiSerializerBuilder builder() { 090 return new OpenApiSerializerBuilder(getPropertyStore()); 091 } 092 093 /** 094 * Instantiates a new clean-slate {@link OpenApiSerializerBuilder} object. 095 * 096 * <p> 097 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 098 * the settings of the object called on. 099 * 100 * @return A new {@link OpenApiSerializerBuilder} object. 101 */ 102 public static OpenApiSerializerBuilder create() { 103 return new OpenApiSerializerBuilder(); 104 } 105 106 //----------------------------------------------------------------------------------------------------------------- 107 // Entry point methods 108 //----------------------------------------------------------------------------------------------------------------- 109 110 @Override /* Context */ 111 public OpenApiSerializerSession createSession() { 112 return createSession(null); 113 } 114 115 @Override /* Serializer */ 116 public OpenApiSerializerSession createSession(SerializerSessionArgs args) { 117 return new OpenApiSerializerSession(this, args); 118 } 119 120 @Override /* HttpPartSerializer */ 121 public OpenApiSerializerSession createPartSession() { 122 return createPartSession(null); 123 } 124 125 @Override /* HttpPartSerializer */ 126 public OpenApiSerializerSession createPartSession(SerializerSessionArgs args) { 127 return new OpenApiSerializerSession(this, args); 128 } 129 130 @Override /* HttpPartSerializer */ 131 public String serialize(HttpPartType partType, HttpPartSchema schema, Object value) throws SchemaValidationException, SerializeException { 132 return createPartSession().serialize(partType, schema, value); 133 } 134 135 @Override /* HttpPartSerializer */ 136 public String serialize(HttpPartSchema schema, Object value) throws SchemaValidationException, SerializeException { 137 return createPartSession().serialize(null, schema, value); 138 } 139}