<commit_before>/*
 * Copyright 2003,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* 

 */

package org.apache.pluto.core.impl;

import org.apache.pluto.PortletContainer;
import org.apache.pluto.core.InternalPortletWindow;
import org.apache.pluto.core.PortletEntity;
import org.apache.pluto.descriptors.portlet.PortletDD;
import org.apache.pluto.descriptors.portlet.SupportsDD;
import org.apache.pluto.services.PortalCallbackProvider;
import org.apache.pluto.util.NamespaceMapper;
import org.apache.pluto.util.StringManager;
import org.apache.pluto.util.StringUtils;
import org.apache.pluto.util.impl.NamespaceMapperImpl;

import javax.portlet.PortletURL;
import javax.portlet.RenderResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

public class RenderResponseImpl extends PortletResponseImpl
    implements RenderResponse {
    
    private static final StringManager EXCEPTIONS =
            StringManager.getManager(RenderResponseImpl.class.getPackage().getName());

    /**
     * The current content type.
     * @todo This should be made dynamic
     */
    private String currentContentType = null;

    private NamespaceMapper mapper = new NamespaceMapperImpl();

    public RenderResponseImpl(PortletContainer container,
                              InternalPortletWindow internalPortletWindow,
                              javax.servlet.http.HttpServletRequest servletRequest,
                              javax.servlet.http.HttpServletResponse servletResponse) {
        super(container, internalPortletWindow, servletRequest,
              servletResponse);
    }

    public String getContentType() {
        // in servlet 2.4 we could simply use this:
        // return this._getHttpServletResponse().getContentType();
        return currentContentType;
    }

    public PortletURL createRenderURL() {
        PortletURL url = createURL(false);
        return url;
    }

    public PortletURL createActionURL() {
        PortletURL url = createURL(true);
        return url;
    }

    public String getNamespace() {
        return mapper.encode(getInternalPortletWindow().getId(), "");
    }

    public void setTitle(String title) {
        PortalCallbackProvider callback =
            container.getContainerServices().getPortalCallbackProvider();

        callback.setTitle(this.getHttpServletRequest(),
                          getInternalPortletWindow(),
                          title);
    }

    public void setContentType(String type) {
        String mimeType = StringUtils.stringCharacterEncoding(type);
        if (!isValidContentType(mimeType)) {
            throw new IllegalArgumentException(mimeType);
        }
        this._getHttpServletResponse().setContentType(mimeType);
        currentContentType = mimeType;
    }

    public String getCharacterEncoding() {
        return this._getHttpServletResponse().getCharacterEncoding();
    }

    /**
     * @todo is it appropriate to throw an illegal state if content type is not set.  how do we set automatically
     */ 
    public PrintWriter getWriter() throws IOException, IllegalStateException {
        if (currentContentType == null) {
            String msg = EXCEPTIONS.getString("error.contenttype.null");
            //throw new java.lang.IllegalStateException(msg);
        }

        return super.getWriter();
    }

    public java.util.Locale getLocale() {
        return this.getHttpServletRequest().getLocale();
    }

    //@todo port 1.0.1 setBufferSize fix to 1.1
    public void setBufferSize(int size) {
        throw new IllegalStateException(
            "portlet container does not support buffering");
    }

    public int getBufferSize() {
        //return this._getHttpServletResponse().getBufferSize();
        return 0;
    }

    public void flushBuffer() throws java.io.IOException {
        this._getHttpServletResponse().flushBuffer();
    }

    public void resetBuffer() {
        this._getHttpServletResponse().resetBuffer();
    }

    public boolean isCommitted() {
        return this._getHttpServletResponse().isCommitted();
    }

    public void reset() {
        this._getHttpServletResponse().reset();
    }

    /**
     * @todo is it appropriate to throw an illegal state if content type is not set.  how do we set automatically
     * @return
     * @throws java.io.IOException
     * @throws java.lang.IllegalStateException
     */
    public OutputStream getPortletOutputStream() throws java.io.IOException,
                                                        java.lang.IllegalStateException {
        if (currentContentType == null) {
            String msg = EXCEPTIONS.getString("error.contenttype.null");

            //throw new java.lang.IllegalStateException(msg);
        }
        return getOutputStream();
    }
    // --------------------------------------------------------------------------------------------

    // internal methods ---------------------------------------------------------------------------
    /**
     * @param isAction
     * @return
     * @todo make dynamic? as service?
     */
    private PortletURL createURL(boolean isAction) {
        return new PortletURLImpl(container,<commit_msg>PLUTO-150: Fixing namespace to be javascript usable.<commit_after>/*
 * Copyright 2003,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* 

 */

package org.apache.pluto.core.impl;

import org.apache.pluto.PortletContainer;
import org.apache.pluto.core.InternalPortletWindow;
import org.apache.pluto.core.PortletEntity;
import org.apache.pluto.descriptors.portlet.PortletDD;
import org.apache.pluto.descriptors.portlet.SupportsDD;
import org.apache.pluto.services.PortalCallbackProvider;
import org.apache.pluto.util.NamespaceMapper;
import org.apache.pluto.util.StringManager;
import org.apache.pluto.util.StringUtils;
import org.apache.pluto.util.impl.NamespaceMapperImpl;

import javax.portlet.PortletURL;
import javax.portlet.RenderResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

public class RenderResponseImpl extends PortletResponseImpl
    implements RenderResponse {
    
    private static final StringManager EXCEPTIONS =
            StringManager.getManager(RenderResponseImpl.class.getPackage().getName());

    /**
     * The current content type.
     * @todo This should be made dynamic
     */
    private String currentContentType = null;

    private NamespaceMapper mapper = new NamespaceMapperImpl();

    public RenderResponseImpl(PortletContainer container,
                              InternalPortletWindow internalPortletWindow,
                              javax.servlet.http.HttpServletRequest servletRequest,
                              javax.servlet.http.HttpServletResponse servletResponse) {
        super(container, internalPortletWindow, servletRequest,
              servletResponse);
    }

    public String getContentType() {
        // in servlet 2.4 we could simply use this:
        // return this._getHttpServletResponse().getContentType();
        return currentContentType;
    }

    public PortletURL createRenderURL() {
        return createURL(false);
    }

    public PortletURL createActionURL() {
        return createURL(true);
    }

    public String getNamespace() {
         String namespace =
            mapper.encode(getInternalPortletWindow().getId(), "");

         StringBuffer validNamespace = new StringBuffer();
         for (int i = 0; i < namespace.length(); i++) {
         	char ch = namespace.charAt(i);
         	if (Character.isJavaIdentifierPart(ch)) {
         		validNamespace.append(ch);
         	} else {
         		validNamespace.append('_');
         	}
         }

         return validNamespace.toString();
    }

    public void setTitle(String title) {
        PortalCallbackProvider callback =
            container.getContainerServices().getPortalCallbackProvider();

        callback.setTitle(this.getHttpServletRequest(),
                          getInternalPortletWindow(),
                          title);
    }

    public void setContentType(String type) {
        String mimeType = StringUtils.stringCharacterEncoding(type);
        if (!isValidContentType(mimeType)) {
            throw new IllegalArgumentException(mimeType);
        }
        this._getHttpServletResponse().setContentType(mimeType);
        currentContentType = mimeType;
    }

    public String getCharacterEncoding() {
        return this._getHttpServletResponse().getCharacterEncoding();
    }

    /**
     * @todo is it appropriate to throw an illegal state if content type is not set.  how do we set automatically
     */ 
    public PrintWriter getWriter() throws IOException, IllegalStateException {
        if (currentContentType == null) {
            String msg = EXCEPTIONS.getString("error.contenttype.null");
            //throw new java.lang.IllegalStateException(msg);
        }

        return super.getWriter();
    }

    public java.util.Locale getLocale() {
        return this.getHttpServletRequest().getLocale();
    }

    //@todo port 1.0.1 setBufferSize fix to 1.1
    public void setBufferSize(int size) {
        throw new IllegalStateException(
            "portlet container does not support buffering");
    }

    public int getBufferSize() {
        //return this._getHttpServletResponse().getBufferSize();
        return 0;
    }

    public void flushBuffer() throws java.io.IOException {
        this._getHttpServletResponse().flushBuffer();
    }

    public void resetBuffer() {
        this._getHttpServletResponse().resetBuffer();
    }

    public boolean isCommitted() {
        return this._getHttpServletResponse().isCommitted();
    }

    public void reset() {
        this._getHttpServletResponse().reset();
    }

    /**
     * @todo is it appropriate to throw an illegal state if content type is not set.  how do we set automatically
     * @return
     * @throws java.io.IOException
     * @throws java.lang.IllegalStateException
     */
    public OutputStream getPortletOutputStream() throws java.io.IOException,
                                                        java.lang.IllegalStateException {
        if (currentContentType == null) {
            String msg = EXCEPTIONS.getString("error.contenttype.null");
            //throw new java.lang.IllegalStateException(msg);
        }
        return getOutputStream();
    }
    // --------------------------------------------------------------------------------------------

    // internal methods ---------------------------------------------------------------------------
    /**
     * @param isAction
     * @return
     * @todo make dynamic? as service?
     */
    private PortletURL createURL(boolean isAction) {
        return new PortletURLImpl(container,
{'fraction_non_alphanumeric': 0.11307097096264249, 'fraction_numerical': 0.0036608702887095433, 'mean_word_length': 2.4323243860651056, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4233533', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2948, 'n_words': 779}