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