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.httppart.*; 017import org.apache.juneau.urlencoding.*; 018 019/** 020 * Subclass of {@link NameValuePair} for serializing POJOs as URL-encoded form post entries using the 021 * {@link UrlEncodingSerializer class}. 022 * 023 * <h5 class='section'>Example:</h5> 024 * <p class='bcode'> 025 * NameValuePairs params = <jk>new</jk> NameValuePairs() 026 * .append(<jk>new</jk> SerializedNameValuePair(<js>"myPojo"</js>, pojo, UrlEncodingSerializer.<jsf>DEFAULT_SIMPLE</jsf>)) 027 * .append(<jk>new</jk> BasicNameValuePair(<js>"someOtherParam"</js>, <js>"foobar"</js>)); 028 * request.setEntity(<jk>new</jk> UrlEncodedFormEntity(params)); 029 * </p> 030 */ 031public final class SerializedNameValuePair implements NameValuePair { 032 private String name; 033 private Object value; 034 private HttpPartSerializer serializer; 035 036 /** 037 * Constructor. 038 * 039 * @param name The parameter name. 040 * @param value The POJO to serialize to the parameter value. 041 * @param serializer The serializer to use to convert the value to a string. 042 */ 043 public SerializedNameValuePair(String name, Object value, HttpPartSerializer serializer) { 044 this.name = name; 045 this.value = value; 046 this.serializer = serializer; 047 } 048 049 @Override /* NameValuePair */ 050 public String getName() { 051 return name; 052 } 053 054 @Override /* NameValuePair */ 055 public String getValue() { 056 return serializer.serialize(HttpPartType.FORM_DATA, value); 057 } 058}