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.lang.annotation.*;
016import java.lang.reflect.*;
017import java.util.*;
018import java.util.concurrent.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.parser.*;
023import org.apache.juneau.utils.*;
024
025/**
026 * Parses a MessagePack stream into a POJO model.
027 *
028 * <h5 class='topic'>Media types</h5>
029 *
030 * Handles <c>Content-Type</c> types:  <bc>octal/msgpack</bc>
031 *
032 * <h5 class='section'>Notes:</h5><ul>
033 *    <li class='note'>This class is thread safe and reusable.
034 * </ul>
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.MsgPackDetails">MessagePack Details</a>
038
039 * </ul>
040 */
041public class MsgPackParser extends InputStreamParser implements MsgPackMetaProvider {
042
043   //-------------------------------------------------------------------------------------------------------------------
044   // Static
045   //-------------------------------------------------------------------------------------------------------------------
046
047   /** Default parser, all default settings.*/
048   public static final MsgPackParser DEFAULT = new MsgPackParser(create());
049
050   /** Default parser, all default settings, string input encoded as spaced-hex.*/
051   public static final MsgPackParser DEFAULT_SPACED_HEX = new SpacedHex(create());
052
053   /** Default parser, all default settings, string input encoded as BASE64.*/
054   public static final MsgPackParser DEFAULT_BASE64 = new Base64(create());
055
056   /**
057    * Creates a new builder for this object.
058    *
059    * @return A new builder.
060    */
061   public static Builder create() {
062      return new Builder();
063   }
064
065   //-------------------------------------------------------------------------------------------------------------------
066   // Static subclasses
067   //-------------------------------------------------------------------------------------------------------------------
068
069   /** Default parser, string input encoded as spaced-hex. */
070   public static class SpacedHex extends MsgPackParser {
071
072      /**
073       * Constructor.
074       *
075       * @param builder The builder for this object.
076       */
077      public SpacedHex(Builder builder) {
078         super(
079            builder.binaryFormat(BinaryFormat.SPACED_HEX)
080         );
081      }
082   }
083
084   /** Default parser, string input encoded as BASE64. */
085   public static class Base64 extends MsgPackParser {
086
087      /**
088       * Constructor.
089       *
090       * @param builder The builder for this object.
091       */
092      public Base64(Builder builder) {
093         super(
094            builder.binaryFormat(BinaryFormat.BASE64)
095         );
096      }
097   }
098
099   //-------------------------------------------------------------------------------------------------------------------
100   // Builder
101   //-------------------------------------------------------------------------------------------------------------------
102
103   /**
104    * Builder class.
105    */
106   @FluentSetters
107   public static class Builder extends InputStreamParser.Builder {
108
109      private static final Cache<HashKey,MsgPackParser> CACHE = Cache.of(HashKey.class, MsgPackParser.class).build();
110
111      /**
112       * Constructor, default settings.
113       */
114      protected Builder() {
115         super();
116         consumes("octal/msgpack");
117      }
118
119      /**
120       * Copy constructor.
121       *
122       * @param copyFrom The bean to copy from.
123       */
124      protected Builder(MsgPackParser copyFrom) {
125         super(copyFrom);
126      }
127
128      /**
129       * Copy constructor.
130       *
131       * @param copyFrom The builder to copy from.
132       */
133      protected Builder(Builder copyFrom) {
134         super(copyFrom);
135      }
136
137      @Override /* Context.Builder */
138      public Builder copy() {
139         return new Builder(this);
140      }
141
142      @Override /* Context.Builder */
143      public MsgPackParser build() {
144         return cache(CACHE).build(MsgPackParser.class);
145      }
146
147      @Override /* Context.Builder */
148      public HashKey hashKey() {
149         return super.hashKey();
150      }
151      //-----------------------------------------------------------------------------------------------------------------
152      // Properties
153      //-----------------------------------------------------------------------------------------------------------------
154
155      // <FluentSetters>
156
157      @Override /* GENERATED - org.apache.juneau.Context.Builder */
158      public Builder annotations(Annotation...values) {
159         super.annotations(values);
160         return this;
161      }
162
163      @Override /* GENERATED - org.apache.juneau.Context.Builder */
164      public Builder apply(AnnotationWorkList work) {
165         super.apply(work);
166         return this;
167      }
168
169      @Override /* GENERATED - org.apache.juneau.Context.Builder */
170      public Builder applyAnnotations(java.lang.Class<?>...fromClasses) {
171         super.applyAnnotations(fromClasses);
172         return this;
173      }
174
175      @Override /* GENERATED - org.apache.juneau.Context.Builder */
176      public Builder applyAnnotations(Method...fromMethods) {
177         super.applyAnnotations(fromMethods);
178         return this;
179      }
180
181      @Override /* GENERATED - org.apache.juneau.Context.Builder */
182      public Builder cache(Cache<HashKey,? extends org.apache.juneau.Context> value) {
183         super.cache(value);
184         return this;
185      }
186
187      @Override /* GENERATED - org.apache.juneau.Context.Builder */
188      public Builder debug() {
189         super.debug();
190         return this;
191      }
192
193      @Override /* GENERATED - org.apache.juneau.Context.Builder */
194      public Builder debug(boolean value) {
195         super.debug(value);
196         return this;
197      }
198
199      @Override /* GENERATED - org.apache.juneau.Context.Builder */
200      public Builder impl(Context value) {
201         super.impl(value);
202         return this;
203      }
204
205      @Override /* GENERATED - org.apache.juneau.Context.Builder */
206      public Builder type(Class<? extends org.apache.juneau.Context> value) {
207         super.type(value);
208         return this;
209      }
210
211      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
212      public Builder beanClassVisibility(Visibility value) {
213         super.beanClassVisibility(value);
214         return this;
215      }
216
217      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
218      public Builder beanConstructorVisibility(Visibility value) {
219         super.beanConstructorVisibility(value);
220         return this;
221      }
222
223      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
224      public Builder beanContext(BeanContext value) {
225         super.beanContext(value);
226         return this;
227      }
228
229      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
230      public Builder beanContext(BeanContext.Builder value) {
231         super.beanContext(value);
232         return this;
233      }
234
235      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
236      public Builder beanDictionary(java.lang.Class<?>...values) {
237         super.beanDictionary(values);
238         return this;
239      }
240
241      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
242      public Builder beanFieldVisibility(Visibility value) {
243         super.beanFieldVisibility(value);
244         return this;
245      }
246
247      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
248      public Builder beanInterceptor(Class<?> on, Class<? extends org.apache.juneau.swap.BeanInterceptor<?>> value) {
249         super.beanInterceptor(on, value);
250         return this;
251      }
252
253      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
254      public Builder beanMapPutReturnsOldValue() {
255         super.beanMapPutReturnsOldValue();
256         return this;
257      }
258
259      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
260      public Builder beanMethodVisibility(Visibility value) {
261         super.beanMethodVisibility(value);
262         return this;
263      }
264
265      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
266      public Builder beanProperties(Map<String,Object> values) {
267         super.beanProperties(values);
268         return this;
269      }
270
271      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
272      public Builder beanProperties(Class<?> beanClass, String properties) {
273         super.beanProperties(beanClass, properties);
274         return this;
275      }
276
277      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
278      public Builder beanProperties(String beanClassName, String properties) {
279         super.beanProperties(beanClassName, properties);
280         return this;
281      }
282
283      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
284      public Builder beanPropertiesExcludes(Map<String,Object> values) {
285         super.beanPropertiesExcludes(values);
286         return this;
287      }
288
289      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
290      public Builder beanPropertiesExcludes(Class<?> beanClass, String properties) {
291         super.beanPropertiesExcludes(beanClass, properties);
292         return this;
293      }
294
295      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
296      public Builder beanPropertiesExcludes(String beanClassName, String properties) {
297         super.beanPropertiesExcludes(beanClassName, properties);
298         return this;
299      }
300
301      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
302      public Builder beanPropertiesReadOnly(Map<String,Object> values) {
303         super.beanPropertiesReadOnly(values);
304         return this;
305      }
306
307      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
308      public Builder beanPropertiesReadOnly(Class<?> beanClass, String properties) {
309         super.beanPropertiesReadOnly(beanClass, properties);
310         return this;
311      }
312
313      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
314      public Builder beanPropertiesReadOnly(String beanClassName, String properties) {
315         super.beanPropertiesReadOnly(beanClassName, properties);
316         return this;
317      }
318
319      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
320      public Builder beanPropertiesWriteOnly(Map<String,Object> values) {
321         super.beanPropertiesWriteOnly(values);
322         return this;
323      }
324
325      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
326      public Builder beanPropertiesWriteOnly(Class<?> beanClass, String properties) {
327         super.beanPropertiesWriteOnly(beanClass, properties);
328         return this;
329      }
330
331      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
332      public Builder beanPropertiesWriteOnly(String beanClassName, String properties) {
333         super.beanPropertiesWriteOnly(beanClassName, properties);
334         return this;
335      }
336
337      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
338      public Builder beansRequireDefaultConstructor() {
339         super.beansRequireDefaultConstructor();
340         return this;
341      }
342
343      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
344      public Builder beansRequireSerializable() {
345         super.beansRequireSerializable();
346         return this;
347      }
348
349      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
350      public Builder beansRequireSettersForGetters() {
351         super.beansRequireSettersForGetters();
352         return this;
353      }
354
355      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
356      public Builder dictionaryOn(Class<?> on, java.lang.Class<?>...values) {
357         super.dictionaryOn(on, values);
358         return this;
359      }
360
361      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
362      public Builder disableBeansRequireSomeProperties() {
363         super.disableBeansRequireSomeProperties();
364         return this;
365      }
366
367      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
368      public Builder disableIgnoreMissingSetters() {
369         super.disableIgnoreMissingSetters();
370         return this;
371      }
372
373      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
374      public Builder disableIgnoreTransientFields() {
375         super.disableIgnoreTransientFields();
376         return this;
377      }
378
379      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
380      public Builder disableIgnoreUnknownNullBeanProperties() {
381         super.disableIgnoreUnknownNullBeanProperties();
382         return this;
383      }
384
385      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
386      public Builder disableInterfaceProxies() {
387         super.disableInterfaceProxies();
388         return this;
389      }
390
391      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
392      public <T> Builder example(Class<T> pojoClass, T o) {
393         super.example(pojoClass, o);
394         return this;
395      }
396
397      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
398      public <T> Builder example(Class<T> pojoClass, String json) {
399         super.example(pojoClass, json);
400         return this;
401      }
402
403      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
404      public Builder findFluentSetters() {
405         super.findFluentSetters();
406         return this;
407      }
408
409      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
410      public Builder findFluentSetters(Class<?> on) {
411         super.findFluentSetters(on);
412         return this;
413      }
414
415      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
416      public Builder ignoreInvocationExceptionsOnGetters() {
417         super.ignoreInvocationExceptionsOnGetters();
418         return this;
419      }
420
421      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
422      public Builder ignoreInvocationExceptionsOnSetters() {
423         super.ignoreInvocationExceptionsOnSetters();
424         return this;
425      }
426
427      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
428      public Builder ignoreUnknownBeanProperties() {
429         super.ignoreUnknownBeanProperties();
430         return this;
431      }
432
433      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
434      public Builder ignoreUnknownEnumValues() {
435         super.ignoreUnknownEnumValues();
436         return this;
437      }
438
439      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
440      public Builder implClass(Class<?> interfaceClass, Class<?> implClass) {
441         super.implClass(interfaceClass, implClass);
442         return this;
443      }
444
445      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
446      public Builder implClasses(Map<Class<?>,Class<?>> values) {
447         super.implClasses(values);
448         return this;
449      }
450
451      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
452      public Builder interfaceClass(Class<?> on, Class<?> value) {
453         super.interfaceClass(on, value);
454         return this;
455      }
456
457      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
458      public Builder interfaces(java.lang.Class<?>...value) {
459         super.interfaces(value);
460         return this;
461      }
462
463      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
464      public Builder locale(Locale value) {
465         super.locale(value);
466         return this;
467      }
468
469      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
470      public Builder mediaType(MediaType value) {
471         super.mediaType(value);
472         return this;
473      }
474
475      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
476      public Builder notBeanClasses(java.lang.Class<?>...values) {
477         super.notBeanClasses(values);
478         return this;
479      }
480
481      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
482      public Builder notBeanPackages(String...values) {
483         super.notBeanPackages(values);
484         return this;
485      }
486
487      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
488      public Builder propertyNamer(Class<? extends org.apache.juneau.PropertyNamer> value) {
489         super.propertyNamer(value);
490         return this;
491      }
492
493      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
494      public Builder propertyNamer(Class<?> on, Class<? extends org.apache.juneau.PropertyNamer> value) {
495         super.propertyNamer(on, value);
496         return this;
497      }
498
499      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
500      public Builder sortProperties() {
501         super.sortProperties();
502         return this;
503      }
504
505      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
506      public Builder sortProperties(java.lang.Class<?>...on) {
507         super.sortProperties(on);
508         return this;
509      }
510
511      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
512      public Builder stopClass(Class<?> on, Class<?> value) {
513         super.stopClass(on, value);
514         return this;
515      }
516
517      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
518      public <T, S> Builder swap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction) {
519         super.swap(normalClass, swappedClass, swapFunction);
520         return this;
521      }
522
523      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
524      public <T, S> Builder swap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction, ThrowingFunction<S,T> unswapFunction) {
525         super.swap(normalClass, swappedClass, swapFunction, unswapFunction);
526         return this;
527      }
528
529      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
530      public Builder swaps(java.lang.Class<?>...values) {
531         super.swaps(values);
532         return this;
533      }
534
535      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
536      public Builder timeZone(TimeZone value) {
537         super.timeZone(value);
538         return this;
539      }
540
541      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
542      public Builder typeName(Class<?> on, String value) {
543         super.typeName(on, value);
544         return this;
545      }
546
547      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
548      public Builder typePropertyName(String value) {
549         super.typePropertyName(value);
550         return this;
551      }
552
553      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
554      public Builder typePropertyName(Class<?> on, String value) {
555         super.typePropertyName(on, value);
556         return this;
557      }
558
559      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
560      public Builder useEnumNames() {
561         super.useEnumNames();
562         return this;
563      }
564
565      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
566      public Builder useJavaBeanIntrospector() {
567         super.useJavaBeanIntrospector();
568         return this;
569      }
570
571      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
572      public Builder autoCloseStreams() {
573         super.autoCloseStreams();
574         return this;
575      }
576
577      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
578      public Builder autoCloseStreams(boolean value) {
579         super.autoCloseStreams(value);
580         return this;
581      }
582
583      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
584      public Builder consumes(String value) {
585         super.consumes(value);
586         return this;
587      }
588
589      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
590      public Builder debugOutputLines(int value) {
591         super.debugOutputLines(value);
592         return this;
593      }
594
595      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
596      public Builder listener(Class<? extends org.apache.juneau.parser.ParserListener> value) {
597         super.listener(value);
598         return this;
599      }
600
601      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
602      public Builder strict() {
603         super.strict();
604         return this;
605      }
606
607      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
608      public Builder strict(boolean value) {
609         super.strict(value);
610         return this;
611      }
612
613      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
614      public Builder trimStrings() {
615         super.trimStrings();
616         return this;
617      }
618
619      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
620      public Builder trimStrings(boolean value) {
621         super.trimStrings(value);
622         return this;
623      }
624
625      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
626      public Builder unbuffered() {
627         super.unbuffered();
628         return this;
629      }
630
631      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
632      public Builder unbuffered(boolean value) {
633         super.unbuffered(value);
634         return this;
635      }
636
637      @Override /* GENERATED - org.apache.juneau.parser.InputStreamParser.Builder */
638      public Builder binaryFormat(BinaryFormat value) {
639         super.binaryFormat(value);
640         return this;
641      }
642
643      // </FluentSetters>
644   }
645
646   //-------------------------------------------------------------------------------------------------------------------
647   // Instance
648   //-------------------------------------------------------------------------------------------------------------------
649
650   private final Map<ClassMeta<?>,MsgPackClassMeta> msgPackClassMetas = new ConcurrentHashMap<>();
651   private final Map<BeanPropertyMeta,MsgPackBeanPropertyMeta> msgPackBeanPropertyMetas = new ConcurrentHashMap<>();
652
653   /**
654    * Constructor.
655    *
656    * @param builder The builder for this object.
657    */
658   public MsgPackParser(Builder builder) {
659      super(builder);
660   }
661
662   @Override /* Context */
663   public Builder copy() {
664      return new Builder(this);
665   }
666
667   @Override /* Context */
668   public MsgPackParserSession.Builder createSession() {
669      return MsgPackParserSession.create(this);
670   }
671
672   @Override /* Context */
673   public MsgPackParserSession getSession() {
674      return createSession().build();
675   }
676
677   //-----------------------------------------------------------------------------------------------------------------
678   // Extended metadata
679   //-----------------------------------------------------------------------------------------------------------------
680
681   @Override /* MsgPackMetaProvider */
682   public MsgPackClassMeta getMsgPackClassMeta(ClassMeta<?> cm) {
683      MsgPackClassMeta m = msgPackClassMetas.get(cm);
684      if (m == null) {
685         m = new MsgPackClassMeta(cm, this);
686         msgPackClassMetas.put(cm, m);
687      }
688      return m;
689   }
690
691   @Override /* MsgPackMetaProvider */
692   public MsgPackBeanPropertyMeta getMsgPackBeanPropertyMeta(BeanPropertyMeta bpm) {
693      if (bpm == null)
694         return MsgPackBeanPropertyMeta.DEFAULT;
695      MsgPackBeanPropertyMeta m = msgPackBeanPropertyMetas.get(bpm);
696      if (m == null) {
697         m = new MsgPackBeanPropertyMeta(bpm.getDelegateFor(), this);
698         msgPackBeanPropertyMetas.put(bpm, m);
699      }
700      return m;
701   }
702}