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