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.rest.client;
014
015import org.apache.http.*;
016import org.apache.juneau.*;
017import org.apache.juneau.httppart.*;
018import org.apache.juneau.oapi.*;
019import org.apache.juneau.serializer.*;
020import org.apache.juneau.urlencoding.*;
021
022/**
023 * Subclass of {@link NameValuePair} for serializing POJOs as URL-encoded form post entries using the
024 * {@link UrlEncodingSerializer class}.
025 *
026 * <h5 class='section'>Example:</h5>
027 * <p class='bcode w800'>
028 *    NameValuePairs params = <jk>new</jk> NameValuePairs()
029 *       .append(<jk>new</jk> SerializedNameValuePair(<js>"myPojo"</js>, pojo, UrlEncodingSerializer.<jsf>DEFAULT_SIMPLE</jsf>))
030 *       .append(<jk>new</jk> BasicNameValuePair(<js>"someOtherParam"</js>, <js>"foobar"</js>));
031 *    request.setEntity(<jk>new</jk> UrlEncodedFormEntity(params));
032 * </p>
033 */
034public final class SerializedNameValuePair implements NameValuePair {
035   private String name;
036   private Object value;
037   private HttpPartSerializer serializer;
038   private HttpPartSchema schema;
039
040   /**
041    * Constructor.
042    *
043    * @param name The parameter name.
044    * @param value The POJO to serialize to the parameter value.
045    * @param serializer
046    *    The serializer to use for serializing the value to a string value.
047    * @param schema
048    *    The schema object that defines the format of the output.
049    *    <br>If <jk>null</jk>, defaults to the schema defined on the serializer.
050    *    <br>If that's also <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}.
051    *    <br>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}).
052    */
053   public SerializedNameValuePair(String name, Object value, HttpPartSerializer serializer, HttpPartSchema schema) {
054      this.name = name;
055      this.value = value;
056      this.serializer = serializer;
057      this.schema = schema;
058   }
059
060   @Override /* NameValuePair */
061   public String getName() {
062      return name;
063   }
064
065   @Override /* NameValuePair */
066   public String getValue() {
067      try {
068         return serializer.serialize(HttpPartType.FORMDATA, schema, value);
069      } catch (SchemaValidationException e) {
070         throw new FormattedRuntimeException(e, "Validation error on request form-data parameter ''{0}''=''{1}''", name, value);
071      } catch (SerializeException e) {
072         throw new FormattedRuntimeException(e, "Serialization error on request form-data parameter ''{0}''", name);
073      }
074   }
075}