Skip to content

Instantly share code, notes, and snippets.

@takumakei
Created April 11, 2011 04:53
Show Gist options
  • Save takumakei/913067 to your computer and use it in GitHub Desktop.
Save takumakei/913067 to your computer and use it in GitHub Desktop.
An utility class to POST json(optionally gzipped) with Apache HttpClient.
/* JSONEntity.java
* An utility class to POST json(optionally gzipped) with Apache HttpClient.
*
* Copyright (C) 2011 TAKUMAKei
*
* 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.blogspot.takumakei.gist;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;
import org.apache.http.entity.AbstractHttpEntity;
import org.json.simple.JSONObject;
/**
* A self contained, repeatable entity that obtains its content from a JSONObject.
*
* <code>
* JSONObject json = new JSONObject();
* json.put("hello", "world");
*
* HttpPost httpPost = new HttpPost("http://localhost:4567/");
* httpPost.setEntity(new JSONEntity(json, true));
*
* HttpClient httpClient = new DefaultHttpClient();
* HttpResponse httpResponse = httpClient.execute(httpPost);
*
* if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
* throw new RuntimeException(httpResponse.toString());
* }
*
* IOUtil.copy(httpResponse.getEntity().getContent(), System.out);
* </code>
*/
public class JSONEntity extends AbstractHttpEntity implements Cloneable {
protected final byte[] content;
protected final int length;
public JSONEntity(JSONObject jsonObject) {
this(jsonObject, false);
}
public JSONEntity(JSONObject jsonObject, boolean deflate) {
setContentType("application/json");
final byte[] bytes = jsonObject.toJSONString().getBytes();
if (deflate) {
ByteBufferOutputStream bbos =
new ByteBufferOutputStream(Math.max(512, bytes.length / 2));
DeflaterOutputStream deflater = new DeflaterOutputStream(bbos);
try {
deflater.write(bytes);
deflater.close();
} catch (IOException e) {
content = bytes;
length = bytes.length;
return;
}
content = bbos.getBuffer();
length = bbos.size();
setContentEncoding("gzip");
} else {
content = bytes;
length = bytes.length;
}
}
@Override
public boolean isRepeatable() {
return true;
}
@Override
public long getContentLength() {
return length;
}
@Override
public InputStream getContent() throws IOException, IllegalStateException {
return new ByteArrayInputStream(content, 0, length);
}
@Override
public void writeTo(OutputStream outstream) throws IOException {
if (outstream == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
outstream.write(content);
outstream.flush();
}
@Override
public boolean isStreaming() {
return false;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
protected static class ByteBufferOutputStream extends ByteArrayOutputStream {
public ByteBufferOutputStream(int size) {
super(size);
}
public byte[] getBuffer() {
return super.buf;
}
}
}
@skauss
Copy link

skauss commented Jan 12, 2016

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment