head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.08.11.09.08.07;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.19.19.36.29;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.19.19.28.53;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.07.19.01.03.53;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@package org.tigris.helm.publish;

/* ================================================================
 * Copyright (c) 2000 Collab.Net.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by Collab.Net <http://www.Collab.Net/>."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" or 
 * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without 
 * prior written permission of Collab.Net.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Collab.Net.
 */ 


import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import java.net.*;

import HTTPClient.*;

import org.apache.turbine.util.Log;
import org.apache.commons.util.exception.NestableException;

import org.apache.velocity.context.Context;

import org.tigris.helm.security.*;
import org.tigris.helm.tool.ContextAPI;

/**
    This is the main Noodle Servlet class. To implement
    within your own code, you simply need to copy/paste
    the stuff in the doGet() method.
    
    Note, this class now has a sibling over in org.tigris.helm.publish.
    It is also named Noodle.  It can't subclass from this class 
    because it subclasses a class that subclasses UnsecureServlet 
    but it does have dependencies on this and other Noodle classes.
    See <a href="http://projects.collab.net/project/eng/stack/projects/securenoodle/">SecureNoodle: Problem Statement, Requirements, Survey, Decisions and Testing</a>
    St.Ack 10/26/2000.
    
    @@author <a href="mailto:jon@@collab.net">Jon S. Stevens</a>
*/
public class Noodle extends BaseHelmServlet
{
    public static final String DEFAULT_PORT = "default.port";

    public static final String DEFAULT_HOST = "default.host";

    private static final String PROPS = "org.tigris.helm.publish.NoodleResources";

    public void start() throws ServletException
    {
        super.start();

        // get a reference to the properties file.
        ResourceBundle rb = null;
        String props = null;
        try
        {
            rb = ResourceBundle.getBundle(PROPS);
            NoodleResources.setBundle(rb);
        }
        catch (Exception e)
        {
            String message = "Noodle: Could not open properties file path: " +
                props + " " + e.getMessage();
            System.out.println (message);
            throw new ServletException (message);
        }
        // remove the default HTTPClient.CookieModule.class
        // so that it can be overridden by our own implementation
        HTTPConnection.removeDefaultModule(HTTPClient.CookieModule.class);
        try
        {
            HTTPConnection.addDefaultModule(
                Class.forName("org.tigris.helm.publish.NoodleCookieModule"),1);
        }
        catch (ClassNotFoundException cnfe)
        {
            // should never get here, but just in case
            throw new ServletException ("Noodle: " + cnfe.getMessage());
        }
                
        // setup some defaults
        String port;
        String hostname;
        try {
            port = rb.getString(DEFAULT_PORT);
            hostname = rb.getString(DEFAULT_HOST);
        } catch (MissingResourceException e) {
            port = myProperties.getProperty("http_port");
            hostname = myProperties.getProperty("hostname");
        }
        if (port == null) port = "80";
        if (hostname == null) hostname = "localhost";

        ProxyModule.setServerPort((new Integer(port)).intValue());
        ProxyModule.setServerName(hostname);
    }

    /**
     * This is the core of the application.
     */
    protected String processRequest(Context context)
        throws NestableException, RedirectException
    {
        String templateName = super.processRequest(context);
        try
        {
            // build up the NoodleData
            NoodleData noodleData =
                new NoodleData(ContextAPI.getRequest(context),
                               ContextAPI.getResponse(context));

            if (ContextAPI.getRequest(context)
                .getMethod().equalsIgnoreCase("POST"))
            {
                noodleData.setPostData(
                    postToByteArray(ContextAPI.getRequest(context)));
            }

            // builds up the incomming HTTP headers and then makes
            // a connection based on the URL. it is also responsible
            // for returning the data
            ProxyModule.proxyRequest(noodleData);

            context.put("page", new String(noodleData.getResponseData()));
        }
        catch (Throwable e)
        {
            context.put ("page", stackTrace(e));
        }
        return templateName;
    }

    /**
     * Noodle Servlet
     */
    public String getServletInfo()
    {
        return "Noodle Servlet";
    }

    /**
     * returns the output of printStackTrace as a String
     */
    public static final String stackTrace(Throwable e)
    {
        StringBuffer foo = new StringBuffer();
        try
        {
            // and show the Error Screen
            ByteArrayOutputStream ostr = new ByteArrayOutputStream();
            e.printStackTrace( new PrintWriter(ostr,true) );
            foo.append ("<pre>");
            foo.append (ostr.toString());
            foo.append ("</pre>");
        }
        catch (Exception f)
        {
            foo.append("Could not build up exception string");
        }
        return foo.toString();
    }

    /**
     * This method will convert all the parameters into a 
     * byte[]. It isn't the most efficient and should only 
     * be used in systems where you cannot get access to the 
     * req.getInputStream().
     */
    public static final byte[] postToByteArray(HttpServletRequest req)
        throws Exception
    {
        // special case file upload issues
        String ct = req.getContentType();
        if (ct.startsWith("multipart/form-data"))
        {
            byte[] out = ProxyModule.readFully(req.getInputStream());
            return out;
        }
        else
        {
            Vector nvars = new Vector();
            // loop over all the parameters
            for (Enumeration e = 
                req.getParameterNames(); e.hasMoreElements(); )
            {
                String name = (String) e.nextElement();
                String[] value = req.getParameterValues(name);
                if (value.length > 1)
                {
                    // deal with multiple's
                    for (int k=0;k<value.length ;k++ )
                    {
                        nvars.addElement(new NVPair(name, value[k]));
                    }
                }
                else
                {
                    nvars.addElement(new NVPair(name, value[0]));
                }
            }
            NVPair[] yep = new NVPair[nvars.size()];
            int cnt = 0;
            // create the NVPair
            for (Enumeration e = nvars.elements();e.hasMoreElements() ; )
            {
                yep[cnt] = (NVPair) e.nextElement();
                cnt++;
            }
            // take advantage of code already done
            return Codecs.nv2query(yep).getBytes();
        }
    }
}
@


1.3
log
@Removed Joist references.
@
text
@d60 1
a60 1
import org.apache.turbine.util.TurbineException;
d143 1
a143 1
        throws TurbineException, RedirectException
@


1.2
log
@Removed Joist references.
@
text
@d114 1
a114 1
                Class.forName("org.joist.publish.NoodleCookieModule"),1);
@


1.1
log
@Ported from Joist.
@
text
@d87 1
a87 1
    private static final String PROPS = "org.joist.publish.NoodleResources";
@

