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.common.internal.ThrowableUtils.*;
016
017import org.apache.http.*;
018
019/**
020 * Represents an instance of an HTTP part.
021 *
022 * <p>
023 * Can be used to represent both request and response parts.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
027 * </ul>
028 */
029public class HttpPart implements NameValuePair {
030   private final String name;
031   private final Object opart;
032   private final String spart;
033   private final HttpPartType partType;
034   private final HttpPartSchema schema;
035   private final HttpPartSerializerSession serializer;
036
037   /**
038    * Constructor.
039    *
040    * <p>
041    * Used when the part is in POJO form and needs to be converted to a String.
042    *
043    * @param name The HTTP part name (e.g. the header name).
044    * @param partType The HTTP part type.
045    * @param schema Schema information about the part.
046    * @param serializer The part serializer to use to serialize the part.
047    * @param part The part POJO being serialized.
048    */
049   public HttpPart(String name, HttpPartType partType, HttpPartSchema schema, HttpPartSerializerSession serializer, Object part) {
050      this.name = name;
051      this.partType = partType;
052      this.schema = schema;
053      this.serializer = serializer;
054      this.opart = part;
055      this.spart = null;
056   }
057
058   @Override /* NameValuePair */
059   public String getName() {
060      return name;
061   }
062
063   @Override /* NameValuePair */
064   public String getValue() {
065      if (spart != null)
066         return spart;
067      try {
068         return serializer.serialize(partType, schema, opart);
069      } catch (Exception e) {
070         throw asRuntimeException(e);
071      }
072   }
073}