001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.marshall.httppart; 018 019import static org.apache.juneau.commons.utils.ThrowableUtils.*; 020 021import org.apache.juneau.commons.http.*; 022import org.apache.juneau.commons.httppart.*; 023 024/** 025 * Represents an instance of an HTTP part. 026 * 027 * <p> 028 * Can be used to represent both request and response parts. 029 * 030 * <h5 class='section'>See Also:</h5><ul> 031 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a> 032 * </ul> 033 */ 034public class HttpPart implements NameValuePair { 035 private final String name; 036 private final Object opart; 037 private final HttpPartType partType; 038 private final HttpPartSchema schema; 039 private final HttpPartSerializerSession serializer; 040 041 /** 042 * Constructor. 043 * 044 * <p> 045 * Used when the part is in POJO form and needs to be converted to a String. 046 * 047 * @param name The HTTP part name (e.g. the header name). 048 * @param partType The HTTP part type. 049 * @param schema Schema information about the part. 050 * @param serializer The part serializer to use to serialize the part. 051 * @param part The part POJO being serialized. 052 */ 053 public HttpPart(String name, HttpPartType partType, HttpPartSchema schema, HttpPartSerializerSession serializer, Object part) { 054 this.name = name; 055 this.partType = partType; 056 this.schema = schema; 057 this.serializer = serializer; 058 this.opart = part; 059 } 060 061 @Override /* Overridden from NameValuePair */ 062 public String getName() { return name; } 063 064 @Override /* Overridden from NameValuePair */ 065 public String getValue() { 066 try { 067 return serializer.write(partType, schema, opart); 068 } catch (Exception e) { 069 throw toRex(e); 070 } 071 } 072}