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