2525import java .io .OutputStream ;
2626import java .io .OutputStreamWriter ;
2727import java .io .Writer ;
28+ import java .util .HashMap ;
2829import java .util .Map ;
30+ import java .util .logging .Logger ;
2931
3032/**
3133 * Implements support for HTTP form content encoding serialization of type {@code
@@ -53,19 +55,39 @@ static void setContent(HttpRequest request, Object item) {
5355 */
5456public class UrlEncodedContent extends AbstractHttpContent {
5557
56- /** Content type. Default value is {@link UrlEncodedParser#CONTENT_TYPE}. */
58+ private static final Logger LOGGER = Logger .getLogger (UrlEncodedContent .class .getName ());
59+
60+ /**
61+ * Content type or {@code null} for none.
62+ *
63+ * <p>
64+ * Default value is {@link UrlEncodedParser#CONTENT_TYPE}.
65+ * </p>
66+ */
5767 private String contentType = UrlEncodedParser .CONTENT_TYPE ;
5868
59- /** Key name/value data or {@code null} for none . */
69+ /** Key name/value data. */
6070 private Object data ;
6171
6272 /**
63- * @param data key name/value data or {@code null} for none
73+ * <p>
74+ * Upgrade warning: prior to version 1.7, the {@code data} parameter could be {@code null} but now
75+ * {@code null} is not allowed and instead a new instance of an implementation of {@link Map} will
76+ * be created. In version 1.8 a {@link NullPointerException} will be thrown instead.
77+ * </p>
78+ *
79+ * @param data key name/value data
6480 */
6581 public UrlEncodedContent (Object data ) {
66- this . data = data ;
82+ setData ( data ) ;
6783 }
6884
85+ /**
86+ * {@inheritDoc}
87+ * <p>
88+ * Upgrade warning: overriding this method is no longer supported, and will be made final in 1.8.
89+ * </p>
90+ */
6991 public String getType () {
7092 return contentType ;
7193 }
@@ -94,7 +116,11 @@ public void writeTo(OutputStream out) throws IOException {
94116 * Sets the content type or {@code null} for none.
95117 *
96118 * <p>
97- * Defaults to {@link UrlEncodedParser#CONTENT_TYPE}.
119+ * Default value is {@link UrlEncodedParser#CONTENT_TYPE}.
120+ * </p>
121+ * <p>
122+ * Overriding is only supported for the purpose of calling the super implementation and changing
123+ * the return type, but nothing else.
98124 * </p>
99125 *
100126 * @since 1.5
@@ -107,22 +133,63 @@ public UrlEncodedContent setType(String type) {
107133 /**
108134 * Returns the key name/value data or {@code null} for none.
109135 *
136+ * <p>
137+ * Upgrade warning: prior to version 1.7 this could return {@code null} but now it always returns
138+ * a {@code non-null} value. Also overriding this method is no longer supported, and will be made
139+ * final in 1.8.
140+ * </p>
141+ *
110142 * @since 1.5
111143 */
112144 public Object getData () {
113145 return data ;
114146 }
115147
116148 /**
117- * Sets the key name/value data or {@code null} for none.
149+ * Sets the key name/value data.
150+ *
151+ * <p>
152+ * Overriding is only supported for the purpose of calling the super implementation and changing
153+ * the return type, but nothing else.
154+ * </p>
155+ * <p>
156+ * Upgrade warning: prior to version 1.7, the {@code data} parameter could be {@code null} but now
157+ * {@code null} is not allowed and instead a new instance of an implementation of {@link Map} will
158+ * be created. In version 1.8 a {@link NullPointerException} will be thrown instead.
159+ * </p>
118160 *
119161 * @since 1.5
120162 */
121163 public UrlEncodedContent setData (Object data ) {
164+ if (data == null ) {
165+ LOGGER .warning ("UrlEncodedContent.setData(null) no longer supported" );
166+ data = new HashMap <String , Object >();
167+ }
122168 this .data = data ;
123169 return this ;
124170 }
125171
172+ /**
173+ * Returns the URL-encoded content of the given HTTP request, or if none return and set as content
174+ * a new instance of {@link UrlEncodedContent} (whose {@link #getData()} is an implementation of
175+ * {@link Map}).
176+ *
177+ * @param request HTTP request
178+ * @return URL-encoded content
179+ * @throws ClassCastException if the HTTP request has a content defined that is not
180+ * {@link UrlEncodedContent}
181+ * @since 1.7
182+ */
183+ public static UrlEncodedContent getContent (HttpRequest request ) {
184+ HttpContent content = request .getContent ();
185+ if (content != null ) {
186+ return (UrlEncodedContent ) content ;
187+ }
188+ UrlEncodedContent result = new UrlEncodedContent (new HashMap <String , Object >());
189+ request .setContent (result );
190+ return result ;
191+ }
192+
126193 private static boolean appendParam (boolean first , Writer writer , String name , Object value )
127194 throws IOException {
128195 // ignore nulls
0 commit comments