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.msgpack; 014 015import java.util.*; 016import java.util.concurrent.*; 017 018import org.apache.juneau.*; 019import org.apache.juneau.annotation.*; 020import org.apache.juneau.collections.*; 021import org.apache.juneau.parser.*; 022 023/** 024 * Parses a MessagePack stream into a POJO model. 025 * 026 * <h5 class='topic'>Media types</h5> 027 * 028 * Handles <c>Content-Type</c> types: <bc>octal/msgpack</bc> 029 */ 030@ConfigurableContext 031public class MsgPackParser extends InputStreamParser implements MsgPackMetaProvider, MsgPackCommon { 032 033 //------------------------------------------------------------------------------------------------------------------- 034 // Configurable properties 035 //------------------------------------------------------------------------------------------------------------------- 036 037 static final String PREFIX = "MsgPackParser"; 038 039 //------------------------------------------------------------------------------------------------------------------- 040 // Predefined instances 041 //------------------------------------------------------------------------------------------------------------------- 042 043 /** Default parser, all default settings.*/ 044 public static final MsgPackParser DEFAULT = new MsgPackParser(PropertyStore.DEFAULT); 045 046 /** Default parser, all default settings, string input encoded as spaced-hex.*/ 047 public static final MsgPackParser DEFAULT_SPACED_HEX = new SpacedHex(PropertyStore.DEFAULT); 048 049 /** Default parser, all default settings, string input encoded as BASE64.*/ 050 public static final MsgPackParser DEFAULT_BASE64 = new Base64(PropertyStore.DEFAULT); 051 052 //------------------------------------------------------------------------------------------------------------------- 053 // Predefined subclasses 054 //------------------------------------------------------------------------------------------------------------------- 055 056 /** Default parser, string input encoded as spaced-hex. */ 057 public static class SpacedHex extends MsgPackParser { 058 059 /** 060 * Constructor. 061 * 062 * @param ps The property store containing all the settings for this object. 063 */ 064 public SpacedHex(PropertyStore ps) { 065 super( 066 ps.builder().setDefault(ISPARSER_binaryFormat, BinaryFormat.SPACED_HEX).build() 067 ); 068 } 069 } 070 071 /** Default parser, string input encoded as BASE64. */ 072 public static class Base64 extends MsgPackParser { 073 074 /** 075 * Constructor. 076 * 077 * @param ps The property store containing all the settings for this object. 078 */ 079 public Base64(PropertyStore ps) { 080 super( 081 ps.builder().setDefault(ISPARSER_binaryFormat, BinaryFormat.BASE64).build() 082 ); 083 } 084 } 085 086 //------------------------------------------------------------------------------------------------------------------- 087 // Instance 088 //------------------------------------------------------------------------------------------------------------------- 089 090 private final Map<ClassMeta<?>,MsgPackClassMeta> msgPackClassMetas = new ConcurrentHashMap<>(); 091 private final Map<BeanPropertyMeta,MsgPackBeanPropertyMeta> msgPackBeanPropertyMetas = new ConcurrentHashMap<>(); 092 093 /** 094 * Constructor. 095 * 096 * @param ps The property store containing all the settings for this object. 097 */ 098 public MsgPackParser(PropertyStore ps) { 099 super(ps, "octal/msgpack"); 100 } 101 102 @Override /* Context */ 103 public MsgPackParserBuilder builder() { 104 return new MsgPackParserBuilder(getPropertyStore()); 105 } 106 107 /** 108 * Instantiates a new clean-slate {@link MsgPackParserBuilder} object. 109 * 110 * <p> 111 * This is equivalent to simply calling <code><jk>new</jk> MsgPackParserBuilder()</code>. 112 * 113 * <p> 114 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 115 * the settings of the object called on. 116 * 117 * @return A new {@link MsgPackParserBuilder} object. 118 */ 119 public static MsgPackParserBuilder create() { 120 return new MsgPackParserBuilder(); 121 } 122 123 @Override /* Parser */ 124 public MsgPackParserSession createSession() { 125 return createSession(createDefaultSessionArgs()); 126 } 127 128 @Override /* Parser */ 129 public MsgPackParserSession createSession(ParserSessionArgs args) { 130 return new MsgPackParserSession(this, args); 131 } 132 133 //----------------------------------------------------------------------------------------------------------------- 134 // Extended metadata 135 //----------------------------------------------------------------------------------------------------------------- 136 137 @Override /* MsgPackMetaProvider */ 138 public MsgPackClassMeta getMsgPackClassMeta(ClassMeta<?> cm) { 139 MsgPackClassMeta m = msgPackClassMetas.get(cm); 140 if (m == null) { 141 m = new MsgPackClassMeta(cm, this); 142 msgPackClassMetas.put(cm, m); 143 } 144 return m; 145 } 146 147 @Override /* MsgPackMetaProvider */ 148 public MsgPackBeanPropertyMeta getMsgPackBeanPropertyMeta(BeanPropertyMeta bpm) { 149 if (bpm == null) 150 return MsgPackBeanPropertyMeta.DEFAULT; 151 MsgPackBeanPropertyMeta m = msgPackBeanPropertyMetas.get(bpm); 152 if (m == null) { 153 m = new MsgPackBeanPropertyMeta(bpm.getDelegateFor(), this); 154 msgPackBeanPropertyMetas.put(bpm, m); 155 } 156 return m; 157 } 158 159 //----------------------------------------------------------------------------------------------------------------- 160 // Other methods 161 //----------------------------------------------------------------------------------------------------------------- 162 163 @Override /* Context */ 164 public OMap toMap() { 165 return super.toMap() 166 .a("MsgPackParser", new DefaultFilteringOMap()); 167 } 168}