Wiki

Clone wiki

SuperSimpleSemantics / example_filemanager_for_gwt

Below is an example of a filemanager suitable for a gwt project. See the filemanger example wiki for an explanation of why you might need this.

#!java


package com.darkflame.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;

import com.darkflame.client.interfaces.GenericFileManager;
import com.darkflame.client.interfaces.GenericFileManager.FileCallbackError;
import com.darkflame.client.interfaces.GenericFileManager.FileCallbackRunnable;
import com.darkflame.client.semantic.SSSNode;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.RequestBuilder.Method;
import com.google.gwt.http.client.Response;

/** Manages text files **/
public class SpiffyFileManager implements GenericFileManager{

    static Logger Log = Logger.getLogger("FileManager");

    /** If the game is running from a local source, php is naturally not used.
     * Because of this, files are accessed directly at this location,
     * rather then via "textfetcher.php **/
    public static String LocalFolderLocation="";

    /** The location and name of the textfetcher.php that returns requested text files **/
    public static final String textfetcher_url = GWT.getHostPageBaseURL() + "text%20fetcher.php"; //$NON-NLS-1$

    /** A cache of all known text files associated with their filename**/
    static HashMap<String,String> allCachedTextFiles = new HashMap<String,String>();

    /** gets a bit of text using a php file,  <br>
     * to help hide its location, or to get around sop restrictions. <br>
     * You can set if you want this to add to the cache or not. <br>
     * By default, everything is cached unless you use a POST request  <br>**/
    public void GetTextSecurely(final String fileurl,
            final FileCallbackRunnable callback, FileCallbackError onError, boolean forcePOST) {

        if (!forcePOST){
            GetTextSecurely(fileurl,callback,  onError,  forcePOST, true);
        } else {
            GetTextSecurely(fileurl,callback,  onError,  forcePOST, false);
        }

    }



    public void GetTextSecurely(final String fileurl,
            final FileCallbackRunnable callback, final FileCallbackError onError, boolean forcePOST,final boolean UseCache) {



        //if useCache is on, first we search for the filename in the cache
        if (UseCache){

            String contents = allCachedTextFiles.get(fileurl);

            if (contents!=null){
                Log.info("contents found in cache");
                callback.run(contents, Response.SC_OK);


            }


        }


        //if not a cache, we create a new RequestCallback for a real sever request response
        RequestCallback responseManagment = new RequestCallback(){
            @Override
            public void onResponseReceived(Request request, Response response) {    

                //add to cache if not a 404 
                if (response.getStatusCode()==Response.SC_OK && UseCache){
                    Log.info("found file, storing in cache");
                    allCachedTextFiles.put(fileurl,response.getText());
                }

                Log.info("got file,running callback ");
                callback.run(response.getText(), response.getStatusCode());

            }

            @Override
            public void onError(Request request, Throwable exception) {

                onError.run("",exception);


            }

        };


        if (LocalFolderLocation.length()<3) {
            RequestBuilder requestBuilder = new RequestBuilder(
                    RequestBuilder.POST, textfetcher_url);

            try {
                requestBuilder.sendRequest("FileURL=" + fileurl, responseManagment);
            } catch (RequestException e) {
                e.printStackTrace();
                System.out
                        .println("can not connect to file via php:" + fileurl);
            }
        } else {
            Method requestType = RequestBuilder.GET;
            if (forcePOST) {
                requestType = RequestBuilder.POST;
            }

            RequestBuilder requestBuilder = new RequestBuilder(requestType,
                    LocalFolderLocation + fileurl);

            try {
                requestBuilder.sendRequest("", responseManagment);
            } catch (RequestException e) {
                System.out.println("can not connect to file:" + fileurl); //$NON-NLS-1$
                e.printStackTrace();

                // special runnable commands on error if any
                if (onError != null) {

                    onError.run("could not event send request:"+e.getLocalizedMessage(),null);

                }

            }

        }

    }



}

Updated