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