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 org.apache.juneau.parser.*; 016import org.apache.juneau.serializer.*; 017 018/** 019 * Represents an instance of an HTTP part. 020 * 021 * <p> 022 * Can be used to represent both request and response parts. 023 */ 024public class HttpPart { 025 private final String name; 026 private final Object opart; 027 private final String spart; 028 private final HttpPartType partType; 029 private final HttpPartSchema schema; 030 private final HttpPartSerializer serializer; 031 private final HttpPartParser parser; 032 private final SerializerSessionArgs sargs; 033 private final ParserSessionArgs pargs; 034 035 /** 036 * Constructor. 037 * 038 * <p> 039 * Used when the part is in POJO form and needs to be converted to a String. 040 * 041 * @param name The HTTP part name (e.g. the header name). 042 * @param partType The HTTP part type. 043 * @param schema Schema information about the part. 044 * @param serializer The part serializer to use to serialize the part. 045 * @param args Session arguments to pass to the serializer. 046 * @param part The part POJO being serialized. 047 */ 048 public HttpPart(String name, HttpPartType partType, HttpPartSchema schema, HttpPartSerializer serializer, SerializerSessionArgs args, Object part) { 049 this.name = name; 050 this.partType = partType; 051 this.schema = schema; 052 this.serializer = serializer; 053 this.sargs = args; 054 this.opart = part; 055 this.spart = null; 056 this.parser = null; 057 this.pargs = null; 058 } 059 060 /** 061 * Constructor. 062 * 063 * <p> 064 * Used when the part is in String form and needs to be converted to a POJO. 065 * 066 * @param name The HTTP part name (e.g. the header name). 067 * @param partType The HTTP part type. 068 * @param schema Schema information about the part. 069 * @param parser The part parser to use to parse the part. 070 * @param args Session arguments to pass to the parser. 071 * @param part The part string being parsed. 072 */ 073 public HttpPart(String name, HttpPartType partType, HttpPartSchema schema, HttpPartParser parser, ParserSessionArgs args, String part) { 074 this.name = name; 075 this.partType = partType; 076 this.schema = schema; 077 this.parser = parser; 078 this.pargs = args; 079 this.spart = part; 080 this.serializer = null; 081 this.sargs = null; 082 this.opart = null; 083 } 084 085 /** 086 * Returns the name of the part. 087 * 088 * @return The name of the part. 089 */ 090 public String getName() { 091 return name; 092 } 093 094 /** 095 * Returns the value of the part converted to a string. 096 * 097 * @return The value of the part converted to a string. 098 * @throws SchemaValidationException HTTP part failed schema validation. 099 * @throws SerializeException HTTP part could not be serialized. 100 */ 101 public String asString() throws SchemaValidationException, SerializeException { 102 if (spart != null) 103 return spart; 104 return serializer.createPartSession(sargs).serialize(partType, schema, opart); 105 } 106 107 /** 108 * Returns the value of the part converted to a string. 109 * 110 * @param c The type to convert to. 111 * @return The value of the part converted to a string. 112 * @throws SchemaValidationException HTTP part failed schema validation. 113 * @throws ParseException Malformed input encountered. 114 */ 115 public <T> T asType(Class<T> c) throws SchemaValidationException, ParseException { 116 return parser.createPartSession(pargs).parse(partType, schema, spart, c); 117 } 118}