Hi my name is Jose, and this is my first post, I'd wish this code be 
useful for the project.

I'm waiting for your reply.


-----------------RunScript.java-start-------------------------
/**
 *  This class is an attempt to embed Rhino (JavaScript) on fproxy.
 *  It is distributed under the GNU Public Licence (GPL) version 2.  
See
 *  http://www.gnu.org/ for further details of the GPL.
 */
package freenet.client.http.javascript;
import org.mozilla.javascript.*;
import java.util.*;
import freenet.support.*;
import freenet.client.http.javascript.*;
import javax.servlet.http.*;
import java.io.*;

/**
 * RunScript
 * 
 * @author Jose Miguel Jim?nez Vilallonga (sh3n)
 * Thanks to hobx and r2q2.
 */
public class RunScript {
    String resultString;
    String mime;
        FileBucket data;
    public RunScript(FileBucket datasrc, HttpServletRequest req) 
        throws JavaScriptException 
    {   
                InputStreamReader inr = null;
                try {
                        this.data = new FileBucket();
                        InputStream in = datasrc.getInputStream();
                this.mime = "text/plain";
                        Context cx = Context.enter();
                        StringWriter swriter = new StringWriter();
                PrintWriter out = new PrintWriter(swriter);
                        Hashtable params = new Hashtable();
                        Enumeration pnames = req.getParameterNames();
                        while(pnames.hasMoreElements()) //Set 
parameters
                        {
                                Object pname = pnames.nextElement();
                                Object value = req.getParameter
((String)pname);
                                if (((String)value) != "key" && 
((String)value) != "force" && ((String)value) != "filename" && 
((String)value) != "htl" && ((String)value) != "mime" && ((String)
value) != "date" && ((String)value) != "content-type")  
                                params.put(pname, value);

                        }
                Scriptable scope = cx.initStandardObjects(null);
                        Scriptable jsArgs1 = Context.toObject
(this.mime, scope); 
                        scope.put("fproxy_mime", scope, jsArgs1); // 
Objects we can use in javascript
                        Scriptable jsArgs2 = Context.toObject(params, 
scope); 
                        scope.put("fproxy_params", scope, jsArgs2);
                        Scriptable jsArgs3 = Context.toObject(out, 
scope); 
                        scope.put("fproxy_out", scope, jsArgs3);
                inr = new InputStreamReader(in);
                        cx.setClassShutter((new FClassShutter())); // 
Disable access to java classes
                        cx.evaluateReader(scope, inr, "freenet", 1, 
null); //Run script
                        OutputStream dataOutput = 
this.data.getOutputStream();
                        String resultString = swriter.toString();
                        byte[] result = resultString.getBytes();
                        dataOutput.write(result); // Write new data.
                        cx.exit();
                }
        catch (WrappedException we) {
                System.out.println(we.getWrappedException().toString
());
                we.printStackTrace();
        }
        catch (EvaluatorException ee) {
                System.out.println("js: " + ee.getMessage());
        }
        catch (JavaScriptException jse) {
                System.out.println("js: " + jse.getMessage());
        }
        catch (IOException ioe) {
                System.out.println(ioe.toString());
        }
        finally {
                try {
                    inr.close();
                }
                catch (IOException ioe) {
                    System.out.println(ioe.toString());
                }
        }
    }
    public FileBucket getData() // Return data
            throws JavaScriptException
    {
                return this.data;       
    }
    public String getMIME() // Return content-type
    {
        return this.mime;
    }
}
-----------------RunScript.java-end-------------------------

-----------------FClassShutter.java-start-------------------------
package freenet.client.http.javascript;
import org.mozilla.javascript.*;
class FClassShutter implements ClassShutter
{
        void FClassShuter()
        {
        }
        public boolean visibleToScripts(String oclass) {
                return false;
        }
}
package freenet.client.http.javascript;
import org.mozilla.javascript.*;
class FClassShutter implements ClassShutter
{
        void FClassShuter()
        {
        }
        public boolean visibleToScripts(String oclass) {
                return false;
        }
}
-----------------FClassShutter.java-end-------------------------

-----------------readme.txt-start-------------------------
An attemp to embed Rhino (javascript) on fproxy.

Rhino's 1.5.4pre js.jar has to be in the classpath.

Todo
-------------------------------------------
Samething like jsp (Template engine).
Function to retrieve freenet documents, from javascript.
Function to get GMT time, from javascript.
Verify returned content-type.
...
-------------------------------------------
-----------------readme.txt-end-------------------------


__________________________________________________________________
 ?Quieres asistir a la ceremonia de entrega de los premios MTV en 
Barcelona?. Vodafone quiere regalarte un viaje para ti y tu 
acompa?ante con todo pagado: ida, cocktail-buffet de bienvenida, 
entrada VIP a la ceremonia, entrada a la fiesta, desplazamientos al 
hotel y... vuelta a casa. Apetecible ?Verdad?. Es muy f?cil, solicita 
la melod?a de uno de los artistas espa?oles nominados y entrar?s en el 
sorteo, b?scalas en http://www.vodafone.es 


_______________________________________________
devl mailing list
devl at freenetproject.org
http://hawk.freenetproject.org/cgi-bin/mailman/listinfo/devl

Reply via email to