views:

547

answers:

3

I am writting a Twitter web app by using Twitter4J on GAE/J.

I am saving Twitter and Request Token objects in session so that to be used after call back.

I have two servlets. IndexServlet sets session and HomeServlet get from session (hits on call back by twitter oAuth).

If I comment out session handling lines in both servlets then call backs works fine.

Please suggest any workaround. I am sharing my code here.

IndexServlet.java

 Twitter twitter = new Twitter();
               twitter.setOAuthConsumer("<masked>", "<masked>");
               RequestToken requestToken = null;
            try {
                    requestToken = twitter.getOAuthRequestToken();
                    log.info("OAuth token has been taken");
            } catch (TwitterException e) {
                    log.warning(e.toString());
            }

            HttpSession session = request.getSession();
            if (session.getAttribute("twitter")==null){
                    session.setAttribute("twitter", twitter);
                    out.println("-----------------------------> session is set");
            }

            if (session.getAttribute("token")==null){
                    session.setAttribute("token", requestToken);
                    out.println("-----------------------------> session is set");
            }
            String authUrl = requestToken.getAuthorizationURL();

HomeServlet.java

                       HttpSession session = request.getSession();

                    twitter = (Twitter)session.getAttribute("twitter");
                    r  = (RequestToken)session.getAttribute("token");

                    twitter.setOAuthAccessToken(r.getAccessToken());

                    twitter.updateStatus("Hello World!");

Exception

    javax.servlet.ServletException: java.lang.ArrayStoreException: [Ljava.lang.String;
 at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:239)
 at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
 at org.mortbay.jetty.Server.handle(Server.java:313)
 at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506)
 at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:830)
 at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
 at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381)
 at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:139)
 at com.google.apphosting.runtime.JavaRuntime.handleRequest(JavaRuntime.java:235)
 at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4950)
 at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4948)
 at com.google.net.rpc.impl.BlockingApplicationHandler.handleRequest(BlockingApplicationHandler.java:24)
 at com.google.net.rpc.impl.RpcUtil.runRpcInApplication(RpcUtil.java:359)
 at com.google.net.rpc.impl.Server$2.run(Server.java:823)
 at com.google.tracing.LocalTraceSpanRunnable.run(LocalTraceSpanRunnable.java:56)
A: 

recheck that you got

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"&gt;
...
    <sessions-enabled>true</sessions-enabled>
...
</appengine-web-app>

in your war/WEB-INF/lib/appengine-web.xml

Boris Daich
Yes, I forgot to mention, but I have enabled it in appengin xml file.
Tahir Akram
+1  A: 

If setting sessions to true in appengine doesn't work then get the token and tokenSecret from the requestToken

Twitter twitter = new Twitter();
twitter.setOAuthConsumer(consumerKey,consumerSecret);
RequestToken requestToken = twitter.getOAuthRequestToken();

String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();

HttpSession session = request.getSession();
session.setAttribute("token",token);
session.setAttribute("tokenSecret",tokenSecret);

In your HomeServlet retrieve the token and tokenSecret from the session:

Twitter twitter = new Twitter();
twitter.setOAuthConsumer(consumerKey,consumerSecret);
AccessToken accessToken =
   twitter.getOAuthAccessToken(token, tokenSecret);
twitter.setOAuthAccessToken(accessToken);

I also tried storing twitter and requestToken objects before but it didn't work out for me. But this code I just showed you did. I have a tutorial posted here btw: http://jeungun.wordpress.com/2009/09/03/quick-and-dirty-twitter4j-oauth-for-web-apps/

Jeune
A: 

HttpSession javadoc:

"Session information is scoped only to the current web application (ServletContext), so information stored in one context will not be directly visible in another."

http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpSession.html

I think that problem in using different Servlets

abuharsky