<commit_before>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: PortletResponseImpl.java 4496 2006-02-08 20:27:04Z wehrens $
 */
package org.gridsphere.portlet.impl;

import javax.portlet.PortletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The <CODE>PortletResponse</CODE> defines the base interface to assist a
 * portlet in creating and sending a response to the client.
 * The portlet container uses two specialized versions of this interface
 * when invoking a portlet, <CODE>ActionResponse</CODE> and
 * <CODE>RenderResponse</CODE>. The  portlet container creates these
 * objects and passes them as arguments to the portlet's <CODE>processAction</CODE>
 * and <CODE>render</CODE> methods.
 *
 * @see javax.portlet.ActionResponse
 * @see javax.portlet.RenderResponse
 */
public abstract class PortletResponseImpl extends HttpServletResponseWrapper implements PortletResponse {
    protected HttpServletRequest req = null;

    public PortletResponseImpl(HttpServletResponse res) {
        super(res);
    }

    /**
     * Constructs an instance of SportletResponse using an
     * <code>HttpServletResponse</code> as a proxy
     *
     * @param res the <code>HttpServletRequest</code>
     */
    public PortletResponseImpl(HttpServletRequest req, HttpServletResponse res) {
        super(res);
        this.req = req;
        Map map = (Map)req.getAttribute(SportletProperties.PORTAL_PROPERTIES);
        if (map == null) {
            req.setAttribute(SportletProperties.PORTAL_PROPERTIES, new HashMap());
        }
    }

    /**
     * Adds a String property to an existing key to be returned to the portal.
     * <p/>
     * This method allows response properties to have multiple values.
     * <p/>
     * Properties can be used by portlets to provide vendor specific
     * information to the portal.
     *
     * @param key   the key of the property to be returned to the portal
     * @param value the value of the property to be returned to the portal
     * @throws java.lang.IllegalArgumentException
     *          if key is <code>null</code>.
     */
    public void addProperty(String key, String value) {
        if (key == null) throw new IllegalArgumentException("key is NULL");
        Map properties = (Map)req.getAttribute(SportletProperties.PORTAL_PROPERTIES);
        List vals = (List) properties.get(key);
        if (vals == null) {
            vals = new ArrayList();
        }
        vals.add(value);
        properties.put(key, vals);
    }

    /**
     * Sets a String property to be returned to the portal.
     * <p/>
     * Properties can be used by portlets to provide vendor specific
     * information to the portal.
     * <p/>
     * This method resets all properties previously added with the same key.
     *
     * @param key   the key of the property to be returned to the portal
     * @param value the value of the property to be returned to the portal
     * @throws java.lang.IllegalArgumentException
     *          if key is <code>null</code>.
     */
    public void setProperty(String key, String value) {
        if (key == null) throw new IllegalArgumentException("key is NULL");
        Map properties = (Map)req.getAttribute(SportletProperties.PORTAL_PROPERTIES);
        List vals = new ArrayList();
        vals.add(value);<commit_msg>make sure properties are added only if the same name and value does not already exist, no duplicates<commit_after>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: PortletResponseImpl.java 4496 2006-02-08 20:27:04Z wehrens $
 */
package org.gridsphere.portlet.impl;

import javax.portlet.PortletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The <CODE>PortletResponse</CODE> defines the base interface to assist a
 * portlet in creating and sending a response to the client.
 * The portlet container uses two specialized versions of this interface
 * when invoking a portlet, <CODE>ActionResponse</CODE> and
 * <CODE>RenderResponse</CODE>. The  portlet container creates these
 * objects and passes them as arguments to the portlet's <CODE>processAction</CODE>
 * and <CODE>render</CODE> methods.
 *
 * @see javax.portlet.ActionResponse
 * @see javax.portlet.RenderResponse
 */
public abstract class PortletResponseImpl extends HttpServletResponseWrapper implements PortletResponse {
    protected HttpServletRequest req = null;

    public PortletResponseImpl(HttpServletResponse res) {
        super(res);
    }

    /**
     * Constructs an instance of SportletResponse using an
     * <code>HttpServletResponse</code> as a proxy
     *
     * @param res the <code>HttpServletRequest</code>
     */
    public PortletResponseImpl(HttpServletRequest req, HttpServletResponse res) {
        super(res);
        this.req = req;
        Map map = (Map)req.getAttribute(SportletProperties.PORTAL_PROPERTIES);
        if (map == null) {
            req.setAttribute(SportletProperties.PORTAL_PROPERTIES, new HashMap());
        }
    }

    /**
     * Adds a String property to an existing key to be returned to the portal.
     * <p/>
     * This method allows response properties to have multiple values.
     * <p/>
     * Properties can be used by portlets to provide vendor specific
     * information to the portal.
     *
     * @param key   the key of the property to be returned to the portal
     * @param value the value of the property to be returned to the portal
     * @throws java.lang.IllegalArgumentException
     *          if key is <code>null</code>.
     */
    public void addProperty(String key, String value) {
        if (key == null) throw new IllegalArgumentException("key is NULL");
        Map properties = (Map)req.getAttribute(SportletProperties.PORTAL_PROPERTIES);
        List vals = (List) properties.get(key);
        if (vals == null) vals = new ArrayList();
        if (!vals.contains(value)) vals.add(value);
        properties.put(key, vals);
    }

    /**
     * Sets a String property to be returned to the portal.
     * <p/>
     * Properties can be used by portlets to provide vendor specific
     * information to the portal.
     * <p/>
     * This method resets all properties previously added with the same key.
     *
     * @param key   the key of the property to be returned to the portal
     * @param value the value of the property to be returned to the portal
     * @throws java.lang.IllegalArgumentException
     *          if key is <code>null</code>.
     */
    public void setProperty(String key, String value) {
        if (key == null) throw new IllegalArgumentException("key is NULL");
        Map properties = (Map)req.getAttribute(SportletProperties.PORTAL_PROPERTIES);
        List vals = new ArrayList();
        vals.add(value);
{'fraction_non_alphanumeric': 0.09163179916317991, 'fraction_numerical': 0.00502092050209205, 'mean_word_length': 3.11889718552556, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, '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': '5041293', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1916, 'n_words': 672}