Search code examples
javagoogle-app-enginejsfjava-ee-6

Java chat application on GAE, trouble with write messages to file


I have problem with my program in JSF on Google App Engine platform. I almost finished implement chat application in Java EE, when I read that class FileOutputStream isn't supported by GAE. By this class object I create file, to write inside it chat messages and by scirpt this file is loaded and refreshed on index.xhtml website.

I need help because I don't know which class can I replace FileOutputStream to finish this application. I found example in Python so I know that this is possible, but how to implement it in Java?

I will be grateful for any help.

Below I paste piece of class ChatBean with FileOutputSream operations:

@Stateful
@ApplicationScoped
@ManagedBean(name="Chat")
public class ChatBean {

    private List<String> users = new ArrayList<String>();
    private String newUser;
    FileOutputStream chatHtmlBufferWriter;

    public ChatBean () throws IOException {
        ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String chatHtmlPath = ctx.getRealPath("/") + "chat";
        try {
            this.chatHtmlBufferWriter = new FileOutputStream(chatHtmlPath);  
            this.chatHtmlBufferWriter.write("Start chatu ąęć. <br />".getBytes("UTF-8"));
        } catch (IOException ex) {
            this.chatHtmlBufferWriter.close();
            throw ex;
        }

        users.add("Admin");
    }

    @PreDestroy
    public void closeFileBuffor() throws Exception {
        this.chatHtmlBufferWriter.close();
    }

    public String addMessage(String msg) throws IOException {
        this.chatHtmlBufferWriter.write(msg.getBytes("UTF-8"));    
        FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        return "index"; 
    } 
...
}

And script inside index.xhtml file:

<script src="http://code.jquery.com/jquery-latest.js"></script>
            <script>
                var currPos = 0;
                var loadChat = function() {
                    $("#chatForm\\:chatbox").load('chat');
                    currPos = $(".chat")[0].scrollHeight;
                    $(".chat").scrollTop(currPos);
                }
                var scrollChat = function() {
                    $("#chatForm\\:chatbox").load('chat');
                    $(".chat").scrollTop(currPos);
                }
                var currPos;

                $(document).ready(function() {
                    $("#chatForm\\:chatbox").load('chat', function(){
                        loadChat();
                    });
                    var refreshId = setInterval(function() {
                        scrollChat();
                    }, 1000);
                    $.ajaxSetup({ cache: false });                    
                    $("#chatForm\\:chatbox").scroll(function() {
                        currPos = $(".chat").scrollTop();
                    });
                });
            </script>

Solution

  • Basically, you can't directly write to the File System (although you can read).

    You will need to use one of the existing GAE storage APIs, such as the blobstore which has a File like API. Other options are detailed on the Storing Data page.

    However, I'm not sure you're thinking about this correctly; you just want to create a GET method that returns the current messages and is called by your script. The messages will never be written to file at all. To begin with, you could just store the messages in memory. I suspect the tutorial you link to does the same.

    (UPDATE: I originally said FileOutputStream was in the whitelist, but I was looking at FilterOutputStream. Oops.)