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