2011年4月11日月曜日

Apache HttpClientでJSONをgzipしてPOSTする

/* 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;
}
}
}
view raw JSONEntity.java hosted with ❤ by GitHub

サーバ側はSinatraview (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>

2011年4月8日金曜日

AndroidでSystem.outとSystem.errをLogに転送する

Androidでは、System.outとSystem.errがLogに転送されているけれど、
任意のプライオリティと任意のタグでLogに転送するコードを作ってみた。

ついでに、はじめてGistを使ってみた。

Gistで保存するコードの先頭には、コピーライトとか書かない方がいいのかもなぁ。

/* RuntimeInit.java
* My favorite initialization code for Android applications.
*
* 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.PrintStream;
import android.util.Log;
/**
* The class for runtime initialization.
*
* <code>
* class YourActivity extends Activity {
* static {
* RuntimeInit.setOut(android.util.Log.INFO, "Kei");
* RuntimeInit.setErr(android.util.Log.ERROR, "Kei");
* }
* }
* </code>
*/
public class RuntimeInit {
/**
* Redirect 'System.out' to android.util.Log with any priority and tag you want.
*
* @param priority from {@link android.util.Log}
* @param tag to log
*/
public static void setOut(int priority, String tag) {
try {
System.setOut(getAndroidPrintStream(priority, tag));
} catch (Exception e) {
Log.e(tag, "RuntimeInit.setOut", e);
}
}
/**
* Redirect 'System.err' to android.util.Log with any priority and tag you want.
*
* @param priority from {@link android.util.Log}
* @param tag to log
*/
public static void setErr(int priority, String tag) {
try {
System.setErr(getAndroidPrintStream(priority, tag));
} catch (Exception e) {
Log.e(tag, "RuntimeInit.setErr", e);
}
}
/**
* Create a new logging print stream.
*
* @param priority from {@link android.util.Log}
* @param tag to log
* @return A print stream which logs output line by line.
* @throws Exception thrown when the com.android.internal.os.AndroidPrintStream was not found.
*/
public static PrintStream getAndroidPrintStream(int priority, String tag) throws Exception {
return (PrintStream) (Class
.forName("com.android.internal.os.AndroidPrintStream")
.getConstructor(int.class, String.class)
.newInstance(priority, tag));
}
}
view raw RutimeInit.java hosted with ❤ by GitHub


おもいっきりプライベートなAPI使ってるから良くない子に分類されるだろう。