This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} | |
} | |
} |
サーバ側はSinatraのview (erb)で書いたらこんな感じ。
<!doctype html> <html> <head> <meta charset="utf-8"> <title>post /</title> </head> <body> <div>post /</div> <div>Content-Type: <%= request.content_type%></div> <div>Content-Encoding: <%= request.env["HTTP_CONTENT_ENCODING"]%></div> <textarea cols="80" rows="20" readonly><%= if "gzip" == request.env["HTTP_CONTENT_ENCODING"] begin Zlib::Inflate.inflate(request.body.read) rescue => e e.pretty_inspect end else request.body.read end %></textarea> </body> </html>