Slim3 GAE/Jで request getInputStream の中身がない。
つまり
request.getInputStream().available() が 0 の状態。

これは InputStream がすでに STREAMED だから。

下記の実装および、web.xmlの記述を行い回避できる。



BufferedServletInputStream

import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.servlet.ServletInputStream;

public class BufferedServletInputStream extends ServletInputStream {

private ByteArrayInputStream inputStream;

public BufferedServletInputStream(byte[] buffer) {
this.inputStream = new ByteArrayInputStream(buffer);
}

@Override
public int available() throws IOException {
return inputStream.available();
}

@Override
public int read() throws IOException {
return inputStream.read();
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
return inputStream.read(b, off, len);
}
}





BufferedServletRequestWrapper

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class BufferedServletRequestWrapper extends HttpServletRequestWrapper {

private byte[] buffer;

public BufferedServletRequestWrapper(HttpServletRequest request)
throws IOException {
super(request);

InputStream is = request.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buff[] = new byte[1024];
int read;
while ((read = is.read(buff)) > 0) {
baos.write(buff, 0, read);
}

this.buffer = baos.toByteArray();
}

@Override
public ServletInputStream getInputStream() throws IOException {
return new BufferedServletInputStream(this.buffer);
}

}





StreamFilter

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class StreamFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("doStreamFilter");
HttpServletRequest req = (HttpServletRequest) request;
request = new BufferedServletRequestWrapper(req);
chain.doFilter(request, response);
}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}






web.xml(フィルタ部のみ)


<filter>
<filter-name>streamFilter</filter-name>
<filter-class>package.StreamFilter</filter-class>
</filter>
<filter>
<filter-name>HotReloadingFilter</filter-name>
<filter-class>org.slim3.controller.HotReloadingFilter</filter-class>
</filter>
<filter>
<filter-name>DatastoreFilter</filter-name>
<filter-class>org.slim3.datastore.DatastoreFilter</filter-class>
</filter>
<filter>
<filter-name>FrontController</filter-name>
<filter-class>org.slim3.controller.FrontController</filter-class>
</filter>

<filter-mapping>
<filter-name>streamFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>HotReloadingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>DatastoreFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>FrontController</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>


参考

http://blog.honestyworks.jp/blog/archives/162