Skip to content

Instantly share code, notes, and snippets.

@daneko
Created January 31, 2015 03:33
Show Gist options
  • Save daneko/d00b6ba7e8610d58887d to your computer and use it in GitHub Desktop.
Save daneko/d00b6ba7e8610d58887d to your computer and use it in GitHub Desktop.
FormEncodingBuilder + set charset set media type
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed 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.
*/
/*
* Copyright (C) 2015 daneko.
*
* Licensed 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 com.squareup.okhttp;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Objects;
import com.squareup.okhttp.internal.Util;
//@formatter:off
/**
* base on {@link com.squareup.okhttp.FormEncodingBuilder}
* this is enabled to set charset and media_type
*
* sample
* <pre>
* {@code
* RequestBody sjisBody = new FormEncodingBuilderEx(Charset.formName("SJIS"))
* .add(...)
* .add(...)
* .build();
* }
* </pre>
*/
//@formatter:on
public final class FormEncodingBuilderEx {
private static final MediaType DEFAULT_CONTENT_TYPE
= MediaType.parse("application/x-www-form-urlencoded");
private final StringBuilder content = new StringBuilder();
private final Charset charset;
private final MediaType contentType;
public FormEncodingBuilderEx(final Charset charset, final MediaType mediaType) {
this.charset = charset;
if (!Objects.equals(mediaType.charset(), charset)) {
this.contentType = MediaType.parse(String.format(
"%s/%s; charset=%s",
mediaType.type(),
mediaType.subtype(),
charset.displayName().toLowerCase()));
} else {
this.contentType = mediaType;
}
}
public FormEncodingBuilderEx() {
this(Util.UTF_8, DEFAULT_CONTENT_TYPE);
}
public FormEncodingBuilderEx(final Charset charset) {
this(charset, DEFAULT_CONTENT_TYPE);
}
private String getEncodeType() {
return this.charset.displayName();
}
/**
* Add new key-value pair.
*/
public FormEncodingBuilderEx add(String name, String value) {
if (content.length() > 0) {
content.append('&');
}
try {
content.append(URLEncoder.encode(name, getEncodeType()))
.append('=')
.append(URLEncoder.encode(value, getEncodeType()));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return this;
}
public RequestBody build() {
if (content.length() == 0) {
throw new IllegalStateException("Form encoded body must have at least one part.");
}
// Convert to bytes so RequestBody.create() doesn't add a charset to the content-type.
byte[] contentBytes = content.toString().getBytes(charset);
return RequestBody.create(contentType, contentBytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment