<commit_before>/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.portlet.MimeResponse;
import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;

import com.vaadin.Application;
import com.vaadin.external.json.JSONException;
import com.vaadin.external.json.JSONObject;
import com.vaadin.terminal.DeploymentConfiguration;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.StreamVariable;
import com.vaadin.terminal.WrappedRequest;
import com.vaadin.terminal.WrappedResponse;
import com.vaadin.terminal.gwt.client.Connector;
import com.vaadin.ui.Root;

/**
 * TODO document me!
 * 
 * @author peholmst
 * 
 */
@SuppressWarnings("serial")
public class PortletCommunicationManager extends AbstractCommunicationManager {

    private transient ResourceResponse currentUidlResponse;

    public PortletCommunicationManager(Application application) {
        super(application);
    }

    public void handleFileUpload(WrappedRequest request,
            WrappedResponse response) throws IOException {
        String contentType = request.getContentType();
        String name = request.getParameter("name");
        String ownerId = request.getParameter("rec-owner");
        Connector owner = getConnector(getApplication(), ownerId);
        StreamVariable streamVariable = ownerToNameToStreamVariable.get(owner)
                .get(name);

        if (contentType.contains("boundary")) {
            doHandleSimpleMultipartFileUpload(request, response,
                    streamVariable, name, owner,
                    contentType.split("boundary=")[1]);
        } else {
            doHandleXhrFilePost(request, response, streamVariable, name, owner,
                    request.getContentLength());
        }

    }

    @Override
    protected void postPaint(Root root) {
        super.postPaint(root);

        Application application = root.getApplication();
        if (ownerToNameToStreamVariable != null) {
            Iterator<Connector> iterator = ownerToNameToStreamVariable.keySet()
                    .iterator();
            while (iterator.hasNext()) {
                Connector owner = iterator.next();
                if (application.getConnector(owner.getConnectorId()) == null) {
                    // Owner is no longer attached to the application
                    iterator.remove();
                }
            }
        }
    }

    @Override
    public void handleUidlRequest(WrappedRequest request,
            WrappedResponse response, Callback callback, Root root)
            throws IOException, InvalidUIDLSecurityKeyException {
        currentUidlResponse = (ResourceResponse) ((WrappedPortletResponse) response)
                .getPortletResponse();
        super.handleUidlRequest(request, response, callback, root);
        currentUidlResponse = null;
    }

    private Map<Connector, Map<String, StreamVariable>> ownerToNameToStreamVariable;

    @Override
    String getStreamVariableTargetUrl(Connector owner, String name,
            StreamVariable value) {
        if (ownerToNameToStreamVariable == null) {
            ownerToNameToStreamVariable = new HashMap<Connector, Map<String, StreamVariable>>();
        }
        Map<String, StreamVariable> nameToReceiver = ownerToNameToStreamVariable
                .get(owner);
        if (nameToReceiver == null) {
            nameToReceiver = new HashMap<String, StreamVariable>();
            ownerToNameToStreamVariable.put(owner, nameToReceiver);
        }
        nameToReceiver.put(name, value);
        ResourceURL resurl = currentUidlResponse.createResourceURL();
        resurl.setResourceID("UPLOAD");
        resurl.setParameter("name", name);
        resurl.setParameter("rec-owner", owner.getConnectorId());
        resurl.setProperty("name", name);
        resurl.setProperty("rec-owner", owner.getConnectorId());
        return resurl.toString();
    }

    @Override
    protected void cleanStreamVariable(Connector owner, String name) {
        Map<String, StreamVariable> map = ownerToNameToStreamVariable
                .get(owner);
        map.remove(name);
        if (map.isEmpty()) {
            ownerToNameToStreamVariable.remove(owner);
        }
    }

    @Override
    protected BootstrapHandler createBootstrapHandler() {<commit_msg>Allow creation of stream variables in RenderRequests aswell (#8785)<commit_after>/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.portlet.MimeResponse;
import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;

import com.vaadin.Application;
import com.vaadin.external.json.JSONException;
import com.vaadin.external.json.JSONObject;
import com.vaadin.terminal.DeploymentConfiguration;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.StreamVariable;
import com.vaadin.terminal.WrappedRequest;
import com.vaadin.terminal.WrappedResponse;
import com.vaadin.terminal.gwt.client.Connector;
import com.vaadin.ui.Root;

/**
 * TODO document me!
 * 
 * @author peholmst
 * 
 */
@SuppressWarnings("serial")
public class PortletCommunicationManager extends AbstractCommunicationManager {

    private transient MimeResponse currentMimeResponse;

    public PortletCommunicationManager(Application application) {
        super(application);
    }

    public void handleFileUpload(WrappedRequest request,
            WrappedResponse response) throws IOException {
        String contentType = request.getContentType();
        String name = request.getParameter("name");
        String ownerId = request.getParameter("rec-owner");
        Connector owner = getConnector(getApplication(), ownerId);
        StreamVariable streamVariable = ownerToNameToStreamVariable.get(owner)
                .get(name);

        if (contentType.contains("boundary")) {
            doHandleSimpleMultipartFileUpload(request, response,
                    streamVariable, name, owner,
                    contentType.split("boundary=")[1]);
        } else {
            doHandleXhrFilePost(request, response, streamVariable, name, owner,
                    request.getContentLength());
        }

    }

    @Override
    protected void postPaint(Root root) {
        super.postPaint(root);

        Application application = root.getApplication();
        if (ownerToNameToStreamVariable != null) {
            Iterator<Connector> iterator = ownerToNameToStreamVariable.keySet()
                    .iterator();
            while (iterator.hasNext()) {
                Connector owner = iterator.next();
                if (application.getConnector(owner.getConnectorId()) == null) {
                    // Owner is no longer attached to the application
                    iterator.remove();
                }
            }
        }
    }

    @Override
    protected boolean handleApplicationRequest(WrappedRequest request,
            WrappedResponse response) throws IOException {
        currentMimeResponse = (RenderResponse) ((WrappedPortletResponse) response)
                .getPortletResponse();
        try {
            return super.handleApplicationRequest(request, response);
        } finally {
            currentMimeResponse = null;
        }
    }

    @Override
    public void handleUidlRequest(WrappedRequest request,
            WrappedResponse response, Callback callback, Root root)
            throws IOException, InvalidUIDLSecurityKeyException {
        currentMimeResponse = (ResourceResponse) ((WrappedPortletResponse) response)
                .getPortletResponse();
        super.handleUidlRequest(request, response, callback, root);
        currentMimeResponse = null;
    }

    private Map<Connector, Map<String, StreamVariable>> ownerToNameToStreamVariable;

    @Override
    String getStreamVariableTargetUrl(Connector owner, String name,
            StreamVariable value) {
        if (ownerToNameToStreamVariable == null) {
            ownerToNameToStreamVariable = new HashMap<Connector, Map<String, StreamVariable>>();
        }
        Map<String, StreamVariable> nameToReceiver = ownerToNameToStreamVariable
                .get(owner);
        if (nameToReceiver == null) {
            nameToReceiver = new HashMap<String, StreamVariable>();
            ownerToNameToStreamVariable.put(owner, nameToReceiver);
        }
        nameToReceiver.put(name, value);
        ResourceURL resurl = createResourceURL();
        resurl.setResourceID("UPLOAD");
        resurl.setParameter("name", name);
        resurl.setParameter("rec-owner", owner.getConnectorId());
        resurl.setProperty("name", name);
        resurl.setProperty("rec-owner", owner.getConnectorId());
        return resurl.toString();
    }

    private ResourceURL createResourceURL() {
        if (currentMimeResponse == null) {
            throw new RuntimeException(
                    "No reponse object available. Cannot create a resource URL");
        }
        return currentMimeResponse.createResourceURL();
    }

    @Override
    protected void cleanStreamVariable(Connector owner, String name) {
        Map<String, StreamVariable> map = ownerToNameToStreamVariable
                .get(owner);
        map.remove(name);
        if (map.isEmpty()) {
            ownerToNameToStreamVariable.remove(owner);
        }
    }

    @Override
    protected BootstrapHandler createBootstrapHandler() {
{'fraction_non_alphanumeric': 0.09115334859189969, 'fraction_numerical': 0.0007960991143397353, 'mean_word_length': 2.953579858379229, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '3140682', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2518, 'n_words': 526}