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.util.*; 016import java.util.concurrent.*; 017 018import org.apache.juneau.*; 019import org.apache.juneau.annotation.*; 020import org.apache.juneau.parser.*; 021import org.apache.juneau.transform.*; 022 023/** 024 * Parsers HTTP plain text request bodies into Group 5 POJOs. 025 * 026 * <p> 027 * See {@doc PojoCategories}. 028 * 029 * <h5 class='topic'>Media types</h5> 030 * 031 * Handles <c>Accept</c> types: <bc>text/plain</bc> 032 * <p> 033 * Produces <c>Content-Type</c> types: <bc>text/plain</bc> 034 * 035 * <h5 class='topic'>Description</h5> 036 * 037 * Essentially just converts plain text to POJOs via static <c>fromString()</c> or <c>valueOf()</c>, or 038 * through constructors that take a single string argument. 039 * 040 * <p> 041 * Also parses objects using a transform if the object class has an {@link PojoSwap PojoSwap<?,String>} transform 042 * defined on it. 043 */ 044@ConfigurableContext 045public class PlainTextParser extends ReaderParser implements PlainTextMetaProvider, PlainTextCommon { 046 047 //------------------------------------------------------------------------------------------------------------------- 048 // Configurable properties 049 //------------------------------------------------------------------------------------------------------------------- 050 051 static final String PREFIX = "PlainTextParser"; 052 053 //------------------------------------------------------------------------------------------------------------------- 054 // Predefined subclasses 055 //------------------------------------------------------------------------------------------------------------------- 056 057 /** Default parser, all default settings.*/ 058 public static final PlainTextParser DEFAULT = new PlainTextParser(PropertyStore.DEFAULT); 059 060 061 //------------------------------------------------------------------------------------------------------------------- 062 // Instance 063 //------------------------------------------------------------------------------------------------------------------- 064 065 private final Map<ClassMeta<?>,PlainTextClassMeta> plainTextClassMetas = new ConcurrentHashMap<>(); 066 private final Map<BeanPropertyMeta,PlainTextBeanPropertyMeta> plainTextBeanPropertyMetas = new ConcurrentHashMap<>(); 067 068 /** 069 * Constructor. 070 * 071 * @param ps The property store containing all the settings for this object. 072 */ 073 public PlainTextParser(PropertyStore ps) { 074 this(ps, "text/plain"); 075 } 076 077 /** 078 * Constructor. 079 * 080 * @param ps The property store containing all the settings for this object. 081 * @param consumes The media types that this parser consumes. 082 * <p> 083 * Can contain meta-characters per the <c>media-type</c> specification of {@doc RFC2616.section14.1} 084 */ 085 public PlainTextParser(PropertyStore ps, String...consumes) { 086 super(ps, consumes); 087 } 088 089 @Override /* Context */ 090 public PlainTextParserBuilder builder() { 091 return new PlainTextParserBuilder(getPropertyStore()); 092 } 093 094 /** 095 * Instantiates a new clean-slate {@link PlainTextParserBuilder} object. 096 * 097 * <p> 098 * This is equivalent to simply calling <code><jk>new</jk> PlainTextParserBuilder()</code>. 099 * 100 * <p> 101 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 102 * the settings of the object called on. 103 * 104 * @return A new {@link PlainTextParserBuilder} object. 105 */ 106 public static PlainTextParserBuilder create() { 107 return new PlainTextParserBuilder(); 108 } 109 110 @Override /* Parser */ 111 public PlainTextParserSession createSession() { 112 return createSession(createDefaultSessionArgs()); 113 } 114 115 @Override /* Parser */ 116 public PlainTextParserSession createSession(ParserSessionArgs args) { 117 return new PlainTextParserSession(this, args); 118 } 119 120 //----------------------------------------------------------------------------------------------------------------- 121 // Extended metadata 122 //----------------------------------------------------------------------------------------------------------------- 123 124 @Override /* PlainTextMetaProvider */ 125 public PlainTextClassMeta getPlainTextClassMeta(ClassMeta<?> cm) { 126 PlainTextClassMeta m = plainTextClassMetas.get(cm); 127 if (m == null) { 128 m = new PlainTextClassMeta(cm, this); 129 plainTextClassMetas.put(cm, m); 130 } 131 return m; 132 } 133 134 @Override /* PlainTextMetaProvider */ 135 public PlainTextBeanPropertyMeta getPlainTextBeanPropertyMeta(BeanPropertyMeta bpm) { 136 if (bpm == null) 137 return PlainTextBeanPropertyMeta.DEFAULT; 138 PlainTextBeanPropertyMeta m = plainTextBeanPropertyMetas.get(bpm); 139 if (m == null) { 140 m = new PlainTextBeanPropertyMeta(bpm.getDelegateFor(), this); 141 plainTextBeanPropertyMetas.put(bpm, m); 142 } 143 return m; 144 } 145 146 //----------------------------------------------------------------------------------------------------------------- 147 // Other methods 148 //----------------------------------------------------------------------------------------------------------------- 149 150 @Override /* Context */ 151 public ObjectMap toMap() { 152 return super.toMap() 153 .append("PlainTextParser", new DefaultFilteringObjectMap() 154 ); 155 } 156}