Search in sources :

Example 1 with URLCodec

use of com.helger.commons.codec.URLCodec in project ph-web by phax.

the class HttpClientHelper method createParameterEntity.

@Nullable
public static HttpEntity createParameterEntity(@Nullable final Map<String, String> aMap, @Nonnull final Charset aCharset) {
    ValueEnforcer.notNull(aCharset, "Charset");
    if (aMap == null || aMap.isEmpty())
        return null;
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(1024)) {
        final URLCodec aURLCodec = new URLCodec();
        boolean bFirst = true;
        for (final Map.Entry<String, String> aEntry : aMap.entrySet()) {
            if (bFirst)
                bFirst = false;
            else
                aBAOS.write('&');
            // Key must be present
            final String sKey = aEntry.getKey();
            aURLCodec.encode(sKey.getBytes(aCharset), aBAOS);
            // Value is optional
            final String sValue = aEntry.getValue();
            if (StringHelper.hasText(sValue)) {
                aBAOS.write('=');
                aURLCodec.encode(sValue.getBytes(aCharset), aBAOS);
            }
        }
        return new InputStreamEntity(aBAOS.getAsInputStream());
    }
}
Also used : URLCodec(com.helger.commons.codec.URLCodec) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Map(java.util.Map) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Nullable(javax.annotation.Nullable)

Aggregations

URLCodec (com.helger.commons.codec.URLCodec)1 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1