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.http.*;
016import org.apache.juneau.parser.ParseException;
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 implements NameValuePair {
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 HttpPartSerializerSession serializer;
031   private final HttpPartParserSession parser;
032
033   /**
034    * Constructor.
035    *
036    * <p>
037    * Used when the part is in POJO form and needs to be converted to a String.
038    *
039    * @param name The HTTP part name (e.g. the header name).
040    * @param partType The HTTP part type.
041    * @param schema Schema information about the part.
042    * @param serializer The part serializer to use to serialize the part.
043    * @param part The part POJO being serialized.
044    */
045   public HttpPart(String name, HttpPartType partType, HttpPartSchema schema, HttpPartSerializerSession serializer, Object part) {
046      this.name = name;
047      this.partType = partType;
048      this.schema = schema;
049      this.serializer = serializer;
050      this.opart = part;
051      this.spart = null;
052      this.parser = null;
053   }
054
055   /**
056    * Constructor.
057    *
058    * <p>
059    * Used when the part is in String form and needs to be converted to a POJO.
060    *
061    * @param name The HTTP part name (e.g. the header name).
062    * @param partType The HTTP part type.
063    * @param schema Schema information about the part.
064    * @param parser The part parser to use to parse the part.
065    * @param part The part string being parsed.
066    */
067   public HttpPart(String name, HttpPartType partType, HttpPartSchema schema, HttpPartParserSession parser, String part) {
068      this.name = name;
069      this.partType = partType;
070      this.schema = schema;
071      this.parser = parser;
072      this.spart = part;
073      this.serializer = null;
074      this.opart = null;
075   }
076
077   @Override /* NameValuePair */
078   public String getName() {
079      return name;
080   }
081
082   @Override /* NameValuePair */
083   public String getValue() {
084      if (spart != null)
085         return spart;
086      try {
087         return serializer.serialize(partType, schema, opart);
088      } catch (Exception e) {
089         throw new RuntimeException(e);
090      }
091   }
092
093   /**
094    * Returns the value of the part converted to a string.
095    *
096    * @param c The type to convert to.
097    * @return The value of the part converted to a string.
098    * @throws SchemaValidationException HTTP part failed schema validation.
099    * @throws ParseException Malformed input encountered.
100    */
101   public <T> T asType(Class<T> c) throws SchemaValidationException, ParseException {
102      return parser.parse(partType, schema, spart, c);
103   }
104}