Posts Tagged “slim3”


これでいいのかしら。。

01public static Key allocateId(AsyncDatastoreService ds, String kind)
02        throws NullPointerException {
03    if (ds == null) {
04        throw new NullPointerException("The ds parameter must not be null.");
05    }
06    if (kind == null) {
07        throw new NullPointerException(
08            "The kind parameter must not be null.");
09    }
10    String ns = NamespaceManager.get();
11    if (ns == null) {
12        ns = "";
13    }
14    String cacheKey = "!"+ns+":"+kind;
15    Iterator<Key> keys = keysCache.get(cacheKey);
16    if (keys != null && keys.hasNext()) {
17        return keys.next();
18    }
19    keys =
20        FutureUtil
21            .getQuietly(allocateIdsAsync(ds, kind, KEY_CACHE_SIZE))
22            .iterator();
23    keysCache.put(cacheKey, keys);
24    return keys.next();
25}

Comments [appengine][slim3] DatastoreUtil#allocateIdがNamespaceに対応するように修正 はコメントを受け付けていません


listプロパティにInMemoryInCriterionを適用するとエラーになるので修正。
これでいいのかしら。。

01public boolean accept(Object model) {
02     Object v = convertValueForDatastore(attributeMeta.getValue(model));
03     for (Object o : value) {
04         if (v instanceof Collection<?>) {
05             for (Object v2: (Collection<?>)v) {
06                 if (compareValue(v2, o) == 0) {
07                     return true;
08                 }
09             }
10         } else {
11             if (compareValue(v, o) == 0) {
12                 return true;
13             }
14         }
15     }
16     return false;
17 }

Comments [appengine][slim3]InMemoryInCriterionがlistプロパティでも動作するように修正 はコメントを受け付けていません


概要

appengineでJSON-RPCサーバーのようなものを作りたいときなど ServletRequest#getInputStream()で取得できるInputStreamを使いたい場合があります。ところがslim3のcontrollerでこれをやろうとするとInputStreamのIllegalStateExceptionが発生します。
Jettyや大概のサーブレットサーバーは getInputStreamとgetParameter(s)を同時には使えないようです。
JSON-RPCならslim3のcontrollerは使う必要ないじゃんといえばそれまでなのですができれば慣れているもので全てやってしまいたいというのも事実。そこで以下のようなworkaroundで回避します。

  1. StreamFilterを作って、そこでServletRequestをWrapperクラスに置き換える (一番最初にこのFilterが動くようにする)
  2. RequestWrapperでは getInputStreamをoverrideし、Streamを再利用可能にする

なお、このworkaroundは http://d.hatena.ne.jp/machi_pon/20090120/1232420325 にて紹介されているやり方とほぼ同じです。(ナイスポストありがとうございました!)

StreamFilter

01public class StreamFilter implements Filter {
02 
03    public void destroy() {
04        // TODO Auto-generated method stub
05 
06    }
07 
08    public void doFilter(ServletRequest request, ServletResponse response,
09            FilterChain chain) throws IOException, ServletException {
10         
11        HttpServletRequest req = (HttpServletRequest)request;
12        request = new BufferedServletRequestWrapper( req );
13        chain.doFilter(request, response);
14    }
15 
16    public void init(FilterConfig arg0) throws ServletException {
17        // TODO Auto-generated method stub
18 
19    }
20 
21}

BufferedServletRequestWrapper

01public class BufferedServletRequestWrapper extends HttpServletRequestWrapper {
02     
03    private byte[] buffer;
04 
05    public BufferedServletRequestWrapper(HttpServletRequest request) throws IOException {
06        super( request );
07 
08        InputStream is = request.getInputStream();
09        ByteArrayOutputStream baos = new ByteArrayOutputStream();
10        byte buff[] = new byte[ 1024 ];
11        int read;
12        while( ( read = is.read( buff ) ) > 0 ) {
13            baos.write( buff, 0, read );
14        }
15 
16        this.buffer = baos.toByteArray();
17    }
18 
19    @Override
20    public ServletInputStream getInputStream() throws IOException {
21        return new BufferedServletInputStream( this.buffer );
22    }
23 
24}

BufferedServletInputStream

01public class BufferedServletInputStream extends ServletInputStream {
02 
03    private ByteArrayInputStream inputStream;
04 
05    public BufferedServletInputStream(byte[] buffer) {
06        this.inputStream = new ByteArrayInputStream( buffer );
07    }
08 
09    @Override
10    public int available() throws IOException {
11        return inputStream.available();
12    }
13 
14    @Override
15    public int read() throws IOException {
16        return inputStream.read();
17    }
18 
19    @Override
20    public int read(byte[] b, int off, int len) throws IOException {
21        return inputStream.read( b, off, len );
22    }
23 
24}

参考

Comments slim3のcontrollerでServletInputStreamを使いたいとき はコメントを受け付けていません