<commit_before>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
package org.gridlab.gridsphere.portletcontainer.impl;

import org.gridlab.gridsphere.layout.event.PortletComponentEvent;
import org.gridlab.gridsphere.portlet.*;
import org.gridlab.gridsphere.portlet.impl.*;
import org.gridlab.gridsphere.portletcontainer.GridSphereEvent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.UnsupportedEncodingException;

/**
 * The <code>GridSphereEventImpl</code> is an implementation of the <code>GridSphereEvent</code> interface.
 * <p/>
 * A <code>GridSphereEvent</code> represents a general portlet container
 * event. The <code>GridSphereEvent</code> is passed into components that
 * need to access the <code>PortletRequest</code>
 * <code>PortletResponse</code> objects, such as the layout components.
 */
public class GridSphereEventImpl implements GridSphereEvent {

    protected transient PortletLog log = SportletLog.getInstance(GridSphereEventImpl.class);
    protected SportletRequest portletRequest;
    protected SportletResponse portletResponse;
    protected PortletContext portletContext;

    protected String portletComponentID = null;
    protected DefaultPortletAction action = null;
    protected PortletMessage message = null;

    protected Stack events = null;

    public GridSphereEventImpl(PortletContext ctx, HttpServletRequest req, HttpServletResponse res) {

        portletRequest = new SportletRequestImpl(req);
        portletResponse = new SportletResponse(res, portletRequest);
        portletContext = ctx;

        events = new Stack();

        // Set the context path and servlet path to be used when creating url's
        req.setAttribute(SportletProperties.CONTEXT_PATH, req.getContextPath()); // contextPath;
        req.setAttribute(SportletProperties.SERVLET_PATH, req.getServletPath());

        /*
        String[] portletNames = req.getParameterValues("portletName");
        if ( portletNames != null ) {
            System.err.println("have a TCK POrtlet!!");
            StringTokenizer tokenizer;
            if (portletNames.length > 1) System.err.println("Arggh multiple portlets!!");
            for (int i = 0; i < portletNames.length; i++) {
                tokenizer =  new StringTokenizer(portletNames[i], "/");
                String appName = tokenizer.nextToken();
                portletComponentID = tokenizer.nextToken();
            }


        } else {
         */
        portletComponentID = req.getParameter(SportletProperties.COMPONENT_ID);
        if (portletComponentID == null) {
            log.debug("Received a null component ID");
            portletComponentID = "";
        }

        try {
            portletComponentID = decodeUTF8(portletComponentID);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //}
        /*
        try {
           portletComponentID = new Integer(cidStr).intValue();
        } catch (NumberFormatException e) {
           portletComponentID = -1;
           log.debug("Received a non-number portlet component ID: " + cidStr);
        }
        */

        /* This is where a DefaultPortletAction gets put together if one exists */
        Enumeration e;
        String name, newname, value;
        String actionStr = portletRequest.getParameter(SportletProperties.DEFAULT_PORTLET_ACTION);
        try {
        if (actionStr != null) {
            actionStr = decodeUTF8(actionStr);
            log.debug("Received action=" + actionStr);

            action = new DefaultPortletAction(actionStr);
            String prefix = portletRequest.getParameter(SportletProperties.PREFIX);
            if (prefix != null) {
                e = portletRequest.getParameterNames();

                while (e.hasMoreElements()) {
                    name = decodeUTF8((String) e.nextElement());

                    if (name.startsWith(prefix)) {
                        newname = name.substring(prefix.length() + 1);
                        value = portletRequest.getParameter(name);
                        //newname = decodeUTF8(newname);
                        //value = decodeUTF8(newname);
                        action.addParameter(newname, value);
                    }
                }
            }
        } else {

            /// Check to see if action is of form action_name generated by submit button
            e = portletRequest.getParameterNames();
            while (e.hasMoreElements()) {
                name = (String) e.nextElement();
                if (name.startsWith(SportletProperties.DEFAULT_PORTLET_ACTION)) {
                    // check for parameter names and values

                    name = name.substring(SportletProperties.DEFAULT_PORTLET_ACTION.length() + 1);

                    StringTokenizer st = new StringTokenizer(name, "&");
                    newname = st.nextToken();
                    action = new DefaultPortletAction(newname);
                    log.debug("Received " + action);
                    String paramName = "";
                    String paramVal = "";
                    Map tmpParams = new HashMap();
                    String prefix = "";
                    while (st.hasMoreTokens()) {
                        // now check for "=" separating name and value
                        String namevalue = st.nextToken();
                        int hasvalue = namevalue.indexOf("=");
                        if (hasvalue > 0) {
                            paramName = namevalue.substring(0, hasvalue);
                            paramVal = namevalue.substring(hasvalue + 1);
                            if (paramName.equals(SportletProperties.PREFIX)) {
                                prefix = paramVal;
                            } else {
                                tmpParams.put(paramName, paramVal);
                            }
                        } else {
                            tmpParams.put(namevalue, paramVal);
                        }
                    }
                    // put unprefixed params in action
                    Iterator it = tmpParams.keySet().iterator();
                    while (it.hasNext()) {
                        String n = (String) it.next();
                        String v = (String) tmpParams.get(n);
                        if (!prefix.equals("")) {
                            n = n.substring(prefix.length() + 1);
                        }

                        action.addParameter(n, v);
                    }
                    tmpParams = null;
                }

            }
        }
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }

        /* This is where a DefaultPortletMessage gets put together if one exists */
        String messageStr = portletRequest.getParameter(SportletProperties.DEFAULT_PORTLET_MESSAGE);
        if (messageStr != null) {
            log.debug("Received message: " + messageStr);
            message = new DefaultPortletMessage(messageStr);
        }

    }

    private String decodeUTF8(String string) throws UnsupportedEncodingException {
        return new String(string.getBytes("iso-8859-1"), "UTF-8");
    }
    public PortletRequest getPortletRequest() {
        return portletRequest;
    }

    public PortletResponse getPortletResponse() {
        return portletResponse;
    }

    public PortletContext getPortletContext() {
        return portletContext;
    }

    public DefaultPortletAction getAction() {
        return action;
    }

    public boolean hasAction() {
        return (action != null) ? true : false;
    }
<commit_msg>GPF-121 the decodeUTF method didn't work<commit_after>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
package org.gridlab.gridsphere.portletcontainer.impl;

import org.gridlab.gridsphere.layout.event.PortletComponentEvent;
import org.gridlab.gridsphere.portlet.*;
import org.gridlab.gridsphere.portlet.impl.*;
import org.gridlab.gridsphere.portletcontainer.GridSphereEvent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * The <code>GridSphereEventImpl</code> is an implementation of the <code>GridSphereEvent</code> interface.
 * <p/>
 * A <code>GridSphereEvent</code> represents a general portlet container
 * event. The <code>GridSphereEvent</code> is passed into components that
 * need to access the <code>PortletRequest</code>
 * <code>PortletResponse</code> objects, such as the layout components.
 */
public class GridSphereEventImpl implements GridSphereEvent {

    protected transient PortletLog log = SportletLog.getInstance(GridSphereEventImpl.class);
    protected SportletRequest portletRequest;
    protected SportletResponse portletResponse;
    protected PortletContext portletContext;

    protected String portletComponentID = null;
    protected DefaultPortletAction action = null;
    protected PortletMessage message = null;

    protected Stack events = null;

    public GridSphereEventImpl(PortletContext ctx, HttpServletRequest req, HttpServletResponse res) {

        portletRequest = new SportletRequestImpl(req);
        portletResponse = new SportletResponse(res, portletRequest);
        portletContext = ctx;

        events = new Stack();

        // Set the context path and servlet path to be used when creating url's
        req.setAttribute(SportletProperties.CONTEXT_PATH, req.getContextPath()); // contextPath;
        req.setAttribute(SportletProperties.SERVLET_PATH, req.getServletPath());

        /*
        String[] portletNames = req.getParameterValues("portletName");
        if ( portletNames != null ) {
        System.err.println("have a TCK POrtlet!!");
        StringTokenizer tokenizer;
        if (portletNames.length > 1) System.err.println("Arggh multiple portlets!!");
        for (int i = 0; i < portletNames.length; i++) {
        tokenizer =  new StringTokenizer(portletNames[i], "/");
        String appName = tokenizer.nextToken();
        portletComponentID = tokenizer.nextToken();
        }


        } else {
        */
        portletComponentID = req.getParameter(SportletProperties.COMPONENT_ID);
        if (portletComponentID == null) {
            log.debug("Received a null component ID");
            portletComponentID = "";
        }


        log.debug("Received cid= " + portletComponentID);


        //}
        /*
        try {
        portletComponentID = new Integer(cidStr).intValue();
        } catch (NumberFormatException e) {
        portletComponentID = -1;
        log.debug("Received a non-number portlet component ID: " + cidStr);
        }
        */

        /* This is where a DefaultPortletAction gets put together if one exists */
        Enumeration e;
        String name, newname, value;
        String actionStr = portletRequest.getParameter(SportletProperties.DEFAULT_PORTLET_ACTION);
        //try {
        if (actionStr != null) {

            log.debug("Received action=" + actionStr);

            action = new DefaultPortletAction(actionStr);
            String prefix = portletRequest.getParameter(SportletProperties.PREFIX);
            if (prefix != null) {
                e = portletRequest.getParameterNames();

                while (e.hasMoreElements()) {
                    name = ((String) e.nextElement());

                    if (name.startsWith(prefix)) {
                        newname = name.substring(prefix.length() + 1);
                        value = portletRequest.getParameter(name);
                        //newname = decodeUTF8(newname);
                        //value = decodeUTF8(newname);
                        action.addParameter(newname, value);
                    }
                }
            }
        } else {

            /// Check to see if action is of form action_name generated by submit button
            e = portletRequest.getParameterNames();
            while (e.hasMoreElements()) {
                name = (String) e.nextElement();
                if (name.startsWith(SportletProperties.DEFAULT_PORTLET_ACTION)) {
                    // check for parameter names and values

                    name = name.substring(SportletProperties.DEFAULT_PORTLET_ACTION.length() + 1);

                    StringTokenizer st = new StringTokenizer(name, "&");
                    newname = st.nextToken();
                    action = new DefaultPortletAction(newname);
                    log.debug("Received " + action);
                    String paramName = "";
                    String paramVal = "";
                    Map tmpParams = new HashMap();
                    String prefix = "";
                    while (st.hasMoreTokens()) {
                        // now check for "=" separating name and value
                        String namevalue = st.nextToken();
                        int hasvalue = namevalue.indexOf("=");
                        if (hasvalue > 0) {
                            paramName = namevalue.substring(0, hasvalue);
                            paramVal = namevalue.substring(hasvalue + 1);
                            if (paramName.equals(SportletProperties.PREFIX)) {
                                prefix = paramVal;
                            } else {
                                tmpParams.put(paramName, paramVal);
                            }
                        } else {
                            tmpParams.put(namevalue, paramVal);
                        }
                    }
                    // put unprefixed params in action
                    Iterator it = tmpParams.keySet().iterator();
                    while (it.hasNext()) {
                        String n = (String) it.next();
                        String v = (String) tmpParams.get(n);
                        if (!prefix.equals("")) {
                            n = n.substring(prefix.length() + 1);
                        }

                        action.addParameter(n, v);
                    }
                    tmpParams = null;
                }

            }
        }

        /* This is where a DefaultPortletMessage gets put together if one exists */
        String messageStr = portletRequest.getParameter(SportletProperties.DEFAULT_PORTLET_MESSAGE);
        if (messageStr != null) {
            log.debug("Received message: " + messageStr);
            message = new DefaultPortletMessage(messageStr);
        }

    }

    public PortletRequest getPortletRequest() {
        return portletRequest;
    }

    public PortletResponse getPortletResponse() {
        return portletResponse;
    }

    public PortletContext getPortletContext() {
        return portletContext;
    }

    public DefaultPortletAction getAction() {
        return action;
    }

    public boolean hasAction() {
        return (action != null) ? true : false;
    }

{'fraction_non_alphanumeric': 0.09409422694094227, 'fraction_numerical': 0.00232249502322495, 'mean_word_length': 1.844658361645904, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3874629', 'n_tokens_mistral': 3812, 'n_tokens_neox': 3638, 'n_words': 863}