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.httppart; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.parser.*; 019import org.apache.juneau.uon.*; 020 021/** 022 * Parses HTTP headers, query/form-data parameters, and path variables into POJOs. 023 * 024 * <p> 025 * This parser expects UON notation for all parts by default. 026 */ 027@SuppressWarnings({ "unchecked" }) 028public class UonPartParser extends UonParser implements HttpPartParser { 029 030 //------------------------------------------------------------------------------------------------------------------- 031 // Predefined instances 032 //------------------------------------------------------------------------------------------------------------------- 033 034 /** Reusable instance of {@link UonPartParser}. */ 035 public static final UonPartParser DEFAULT = new UonPartParser(PropertyStore.DEFAULT); 036 037 038 //------------------------------------------------------------------------------------------------------------------- 039 // Instance 040 //------------------------------------------------------------------------------------------------------------------- 041 042 /** 043 * Constructor. 044 * 045 * @param ps The property store containing all the settings for this object. 046 */ 047 public UonPartParser(PropertyStore ps) { 048 super( 049 ps.builder() 050 .build(), 051 "application/x-www-form-urlencoded" 052 ); 053 } 054 055 @Override /* Context */ 056 public UonPartParserBuilder builder() { 057 return new UonPartParserBuilder(getPropertyStore()); 058 } 059 060 /** 061 * Instantiates a new clean-slate {@link UonPartParserBuilder} object. 062 * 063 * <p> 064 * This is equivalent to simply calling <code><jk>new</jk> UonPartParserBuilder()</code>. 065 * 066 * <p> 067 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 068 * the settings of the object called on. 069 * 070 * @return A new {@link UonPartParserBuilder} object. 071 */ 072 public static UonPartParserBuilder create() { 073 return new UonPartParserBuilder(); 074 } 075 076 077 @Override /* HttpPartParser */ 078 public <T> T parse(HttpPartType partType, String in, ClassMeta<T> type) throws ParseException { 079 if (in == null) 080 return null; 081 if (type.isString() && in.length() > 0) { 082 // Shortcut - If we're returning a string and the value doesn't start with "'" or is "null", then 083 // just return the string since it's a plain value. 084 // This allows us to bypass the creation of a UonParserSession object. 085 char x = firstNonWhitespaceChar(in); 086 if (x != '\'' && x != 'n' && in.indexOf('~') == -1) 087 return (T)in; 088 if (x == 'n' && "null".equals(in)) 089 return null; 090 } 091 UonParserSession session = createParameterSession(); 092 try (ParserPipe pipe = session.createPipe(in)) { 093 try (UonReader r = session.getUonReader(pipe, false)) { 094 return session.parseAnything(type, r, null, true, null); 095 } 096 } catch (ParseException e) { 097 throw e; 098 } catch (Exception e) { 099 throw new ParseException(e); 100 } 101 } 102}