<commit_before>/*
* @author <a href="mailto:<EMAIL>"><NAME></a>
* @version $Id: PortletServlet.java 5032 2006-08-17 18:15:06Z novotny $
*/
package org.gridsphere.provider.portlet.jsr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gridsphere.portlet.impl.*;
import org.gridsphere.portlet.service.spi.PortletServiceFactory;
import org.gridsphere.services.core.user.User;
import org.gridsphere.portletcontainer.PortletStatus;
import org.gridsphere.portletcontainer.impl.ApplicationPortletImpl;
import org.gridsphere.portletcontainer.impl.PortletWebApplicationImpl;
import org.gridsphere.portletcontainer.impl.descriptor.*;
import org.gridsphere.portletcontainer.impl.descriptor.types.TransportGuaranteeType;
import org.gridsphere.services.core.registry.PortletManagerService;
import org.gridsphere.services.core.registry.PortletRegistryService;
import org.gridsphere.services.core.security.auth.LoginService;

import javax.portlet.*;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.ResourceBundle;

public class PortletServlet extends HttpServlet
        implements Servlet, ServletConfig, 
        HttpSessionAttributeListener, HttpSessionListener, HttpSessionActivationListener {

    private transient Log log = LogFactory.getLog(PortletServlet.class);

    private transient PortletRegistryService registryService = null;

    private PortletWebApplicationImpl portletWebApp = null;

    private PortletContext portletContext = null;

    private Map portlets = null;
    private Map portletclasses = null;
    private Map portletApps = null;
    private Map portletConfigHash = null;

    private Map userKeys = new HashMap();
    private List securePortlets = new ArrayList();

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        log.debug("in init of PortletServlet");
        portlets = new Hashtable();
        portletclasses = new Hashtable();
        portletApps = new Hashtable();
        portletConfigHash = new Hashtable();
    }

    public void initJSRPortletWebapp() {
        registryService = (PortletRegistryService)PortletServiceFactory.createPortletService(PortletRegistryService.class, true);
        ServletContext ctx = this.getServletContext();

        portletWebApp = new PortletWebApplicationImpl(ctx, Thread.currentThread().getContextClassLoader());
        if (portletWebApp.getWebApplicationStatus().equals(PortletStatus.FAILURE)) return;

        Collection appPortlets = portletWebApp.getAllApplicationPortlets();
        Iterator it = appPortlets.iterator();
        while (it.hasNext()) {
            ApplicationPortletImpl appPortlet = (ApplicationPortletImpl) it.next();
            String portletClass = appPortlet.getApplicationPortletClassName();
            String portletName = appPortlet.getApplicationPortletName();
            try {
                // instantiate portlet classes
                Portlet portletInstance = (Portlet) Class.forName(portletClass).newInstance();

                portletApps.put(portletName, appPortlet);

                //portlets.put(portletClass, portletInstance);
                portlets.put(portletName, portletInstance);

                // mappings between names and classes
                portletclasses.put(portletClass, portletName);
                log.debug("Creating new portlet instance: " + portletClass);

                // put portlet web app in registry

            } catch (Exception e) {
                String msg = "Unable to create jsr portlet instance: " + portletClass;
                log.error(msg, e);
                appPortlet.setApplicationPortletStatus(PortletStatus.FAILURE);
                appPortlet.setApplicationPortletStatusMessage(msg);
                portletWebApp.setWebApplicationStatusMessage("FAILURE to instantiate one or more portlet instances");
                portletWebApp.setWebApplicationStatus(PortletStatus.FAILURE);
            } finally {
                registryService.addWebApplication(portletWebApp);
            }
        }

        UserAttribute[] userAttrs = portletWebApp.getUserAttributes();
        if (userAttrs != null) {
            String key = null;
            for (int i = 0; i < userAttrs.length; i++) {
                key = userAttrs[i].getName().getContent();
                userKeys.put(key, "");
            }
        }

        SecurityConstraint[] secConstraints = portletWebApp.getSecurityConstraints();
        if (secConstraints != null) {
            for (int i = 0; i < secConstraints.length; i++) {
                PortletCollection portlets = secConstraints[i].getPortletCollection();
                PortletName[] names = portlets.getPortletName();
                UserDataConstraint userConstraint = secConstraints[i].getUserDataConstraint();
                TransportGuaranteeType guaranteeType = userConstraint.getTransportGuarantee();
                if (guaranteeType.equals(TransportGuaranteeType.NONE)) {
                    names = null;
                }
                if (names != null) {
                    for (int j = 0; j < names.length; j++) {
                        securePortlets.add(names[j].getContent());
                    }
                }
            }
        }


        // create portlet context
        portletContext = new PortletContextImpl(ctx);

        // load in any authentication modules if found-- this is a GridSphere extension

        LoginService loginService = (LoginService)PortletServiceFactory.createPortletService(LoginService.class, true);
        InputStream is = getServletContext().getResourceAsStream("/WEB-INF/authmodules.xml");
        if (is != null) {
            String authModulePath = this.getServletContext().getRealPath("/WEB-INF/authmodules.xml");
            loginService.loadAuthModules(authModulePath, Thread.currentThread().getContextClassLoader());
            log.info("loading authentication modules from: " + authModulePath);
        } else {
            log.debug("no auth module descriptor found");
        }

    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // if no lifecycle method exists, redirect to error page!
        String method = (String) request.getAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
        if (method == null) {
            response.sendRedirect("/" + SportletProperties.getInstance().getProperty("gridsphere.deploy") +
                    "/" + SportletProperties.getInstance().getProperty("gridsphere.context") +
                    "?" + SportletProperties.LAYOUT_PAGE_PARAM + "=" + "ErrorLayout" + "&" + "errorPage=unauthorized.jsp");
            return;
        }

        if (method.equals(SportletProperties.INIT)) {
            initJSRPortletWebapp();
            if (portletWebApp.getWebApplicationStatus().equals(PortletStatus.FAILURE)) return;
            Set set = portlets.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
                String portletName = (String) it.next();
                ApplicationPortletImpl appPortlet = (ApplicationPortletImpl) portletApps.get(portletName);
                Portlet portlet = (Portlet) portlets.get(portletName);
                log.debug("in PortletServlet: service(): Initializing portlet " + portletName);
                PortletDefinition portletDef = portletWebApp.getPortletDefinition(portletName);
                PortletConfig portletConfig = new PortletConfigImpl(getServletConfig(), portletDef, Thread.currentThread().getContextClassLoader());
                try {
                    portlet.init(portletConfig);
                    portletConfigHash.put(portletName, portletConfig);
                } catch (Exception e) {
                    appPortlet.setApplicationPortletStatus(PortletStatus.FAILURE);
                    StringWriter sw = new StringWriter();
                    PrintWriter pout = new PrintWriter(sw);
                    e.printStackTrace(pout);
                    appPortlet.setApplicationPortletStatusMessage("Unable to initialize portlet " + portletName + "\n\n" + sw.getBuffer());
                    log.error("in PortletServlet: service(): Unable to INIT portlet " + portletName, e);
                    // PLT.5.5.2.1 Portlet that fails to initialize must not be placed in active service
                    it.remove();<commit_msg>put context properties in gridsphere.properties<commit_after>/*
* @author <a href="mailto:<EMAIL>"><NAME></a>
* @version $Id: PortletServlet.java 5032 2006-08-17 18:15:06Z novotny $
*/
package org.gridsphere.provider.portlet.jsr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gridsphere.portlet.impl.*;
import org.gridsphere.portlet.service.spi.PortletServiceFactory;
import org.gridsphere.services.core.user.User;
import org.gridsphere.portletcontainer.PortletStatus;
import org.gridsphere.portletcontainer.impl.ApplicationPortletImpl;
import org.gridsphere.portletcontainer.impl.PortletWebApplicationImpl;
import org.gridsphere.portletcontainer.impl.descriptor.*;
import org.gridsphere.portletcontainer.impl.descriptor.types.TransportGuaranteeType;
import org.gridsphere.services.core.registry.PortletManagerService;
import org.gridsphere.services.core.registry.PortletRegistryService;
import org.gridsphere.services.core.security.auth.LoginService;
import org.gridsphere.services.core.portal.PortalConfigService;

import javax.portlet.*;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.ResourceBundle;

public class PortletServlet extends HttpServlet
        implements Servlet, ServletConfig, 
        HttpSessionAttributeListener, HttpSessionListener, HttpSessionActivationListener {

    private transient Log log = LogFactory.getLog(PortletServlet.class);

    private transient PortletRegistryService registryService = null;
    private transient PortalConfigService configService = null;

    private PortletWebApplicationImpl portletWebApp = null;

    private PortletContext portletContext = null;

    private Map portlets = null;
    private Map portletclasses = null;
    private Map portletApps = null;
    private Map portletConfigHash = null;

    private Map userKeys = new HashMap();
    private List securePortlets = new ArrayList();

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        log.info("in init of PortletServlet");
        portlets = new Hashtable();
        portletclasses = new Hashtable();
        portletApps = new Hashtable();
        portletConfigHash = new Hashtable();
    }

    public void initJSRPortletWebapp() {
        registryService = (PortletRegistryService)PortletServiceFactory.createPortletService(PortletRegistryService.class, true);
        configService = (PortalConfigService)PortletServiceFactory.createPortletService(PortalConfigService.class, true);
        ServletContext ctx = this.getServletContext();

        portletWebApp = new PortletWebApplicationImpl(ctx, Thread.currentThread().getContextClassLoader());
        if (portletWebApp.getWebApplicationStatus().equals(PortletStatus.FAILURE)) return;

        Collection appPortlets = portletWebApp.getAllApplicationPortlets();
        Iterator it = appPortlets.iterator();
        while (it.hasNext()) {
            ApplicationPortletImpl appPortlet = (ApplicationPortletImpl) it.next();
            String portletClass = appPortlet.getApplicationPortletClassName();
            String portletName = appPortlet.getApplicationPortletName();
            try {
                // instantiate portlet classes
                Portlet portletInstance = (Portlet) Class.forName(portletClass).newInstance();

                portletApps.put(portletName, appPortlet);

                //portlets.put(portletClass, portletInstance);
                portlets.put(portletName, portletInstance);

                // mappings between names and classes
                portletclasses.put(portletClass, portletName);
                log.debug("Creating new portlet instance: " + portletClass);

                // put portlet web app in registry

            } catch (Exception e) {
                String msg = "Unable to create jsr portlet instance: " + portletClass;
                log.error(msg, e);
                appPortlet.setApplicationPortletStatus(PortletStatus.FAILURE);
                appPortlet.setApplicationPortletStatusMessage(msg);
                portletWebApp.setWebApplicationStatusMessage("FAILURE to instantiate one or more portlet instances");
                portletWebApp.setWebApplicationStatus(PortletStatus.FAILURE);
            } finally {
                registryService.addWebApplication(portletWebApp);
            }
        }

        UserAttribute[] userAttrs = portletWebApp.getUserAttributes();
        if (userAttrs != null) {
            String key = null;
            for (int i = 0; i < userAttrs.length; i++) {
                key = userAttrs[i].getName().getContent();
                userKeys.put(key, "");
            }
        }

        SecurityConstraint[] secConstraints = portletWebApp.getSecurityConstraints();
        if (secConstraints != null) {
            for (int i = 0; i < secConstraints.length; i++) {
                PortletCollection portlets = secConstraints[i].getPortletCollection();
                PortletName[] names = portlets.getPortletName();
                UserDataConstraint userConstraint = secConstraints[i].getUserDataConstraint();
                TransportGuaranteeType guaranteeType = userConstraint.getTransportGuarantee();
                if (guaranteeType.equals(TransportGuaranteeType.NONE)) {
                    names = null;
                }
                if (names != null) {
                    for (int j = 0; j < names.length; j++) {
                        securePortlets.add(names[j].getContent());
                    }
                }
            }
        }


        // create portlet context
        portletContext = new PortletContextImpl(ctx);

        // load in any authentication modules if found-- this is a GridSphere extension

        LoginService loginService = (LoginService)PortletServiceFactory.createPortletService(LoginService.class, true);
        InputStream is = getServletContext().getResourceAsStream("/WEB-INF/authmodules.xml");
        if (is != null) {
            String authModulePath = this.getServletContext().getRealPath("/WEB-INF/authmodules.xml");
            loginService.loadAuthModules(authModulePath, Thread.currentThread().getContextClassLoader());
            log.info("loading authentication modules from: " + authModulePath);
        } else {
            log.debug("no auth module descriptor found");
        }

    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // if no lifecycle method exists, redirect to error page!
        String method = (String) request.getAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
        if (method == null) {
            response.sendRedirect("/" + configService.getProperty("gridsphere.deploy") +
                    "/" + configService.getProperty("gridsphere.context") +
                    "?" + SportletProperties.LAYOUT_PAGE_PARAM + "=" + "ErrorLayout" + "&" + "errorPage=unauthorized.jsp");
            return;
        }

        if (method.equals(SportletProperties.INIT)) {
            initJSRPortletWebapp();
            if (portletWebApp.getWebApplicationStatus().equals(PortletStatus.FAILURE)) return;
            Set set = portlets.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
                String portletName = (String) it.next();
                ApplicationPortletImpl appPortlet = (ApplicationPortletImpl) portletApps.get(portletName);
                Portlet portlet = (Portlet) portlets.get(portletName);
                log.debug("in PortletServlet: service(): Initializing portlet " + portletName);
                PortletDefinition portletDef = portletWebApp.getPortletDefinition(portletName);
                PortletConfig portletConfig = new PortletConfigImpl(getServletConfig(), portletDef, Thread.currentThread().getContextClassLoader());
                try {
                    portlet.init(portletConfig);
                    portletConfigHash.put(portletName, portletConfig);
                } catch (Exception e) {
                    appPortlet.setApplicationPortletStatus(PortletStatus.FAILURE);
                    StringWriter sw = new StringWriter();
                    PrintWriter pout = new PrintWriter(sw);
                    e.printStackTrace(pout);
                    appPortlet.setApplicationPortletStatusMessage("Unable to initialize portlet " + portletName + "\n\n" + sw.getBuffer());
                    log.error("in PortletServlet: service(): Unable to INIT portlet " + portletName, e);
                    // PLT.5.5.2.1 Portlet that fails to initialize must not be placed in active service
                    it.remove();
{'fraction_non_alphanumeric': 0.09322848899700176, 'fraction_numerical': 0.0028285342535498107, 'mean_word_length': 2.7701002345915975, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, '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': '3630945', 'n_tokens_mistral': 4595, 'n_tokens_neox': 4380, 'n_words': 927}