
simplehttp.FormParameters Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2011-2019, simple-http committers
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package simplehttp;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
public final class FormParameters implements MessageContent {
private final Map parameters = new HashMap<>();
public static FormParameters params(String... values) {
return new FormParameters(values);
}
private FormParameters(String... values) {
if (values.length % 2 != 0)
throw new IllegalArgumentException("should be a even number of arguments, received" + values.length);
toMap(values);
}
private void toMap(String[] values) {
for (int i = 0; i < values.length; i += 2)
parameters.put(values[i], values[i + 1]);
}
public List transform(Transform, T> transform) {
List pairs = new ArrayList<>();
for (Map.Entry parameter : parameters.entrySet())
pairs.add(transform.call(parameter));
return pairs;
}
@Override
public String asString() {
StringBuilder builder = new StringBuilder();
Iterator> iterator = parameters.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry parameter = iterator.next();
builder.append(encode(parameter.getKey())).append("=").append(encode(parameter.getValue()));
if (iterator.hasNext())
builder.append("&");
}
return builder.toString();
}
private String encode(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HttpException(e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FormParameters that = (FormParameters) o;
return Objects.equals(parameters, that.parameters);
}
@Override
public int hashCode() {
return Objects.hash(parameters);
}
@Override
public String toString() {
return asString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy