jetspeedforwardservice.java

来自「jetspeed源代码」· Java 代码 · 共 665 行 · 第 1/2 页

JAVA
665
字号
    /**
     * Adds query parameters to the final URI.
     * Parameters are merged from the base forwards definition, with the
     * overlay parameters being overlaid over th base parameters
     *
     * @param duri The dynamic URI to have query parameters added to it
     * @param baseQueryParams The base query parameters from the forward definition
     * @param staticParams Map of static query parameters from PortletForward 
     *                     overriding the static Forwards query parameters
     * @param dynamicParams Map of dynamic query parameters overriding both
     *                     static PortletForward parameters and static Forwards query parameters     
     * @return DynamicURI The new URI including query parameters
     */
    private DynamicURI setQueryParams(DynamicURI duri, 
                                      Map baseQueryParams, 
                                      Map staticParams,
                                      Map dynamicParams)
    {
        if (baseQueryParams == null && staticParams == null && dynamicParams == null)
        {
            return duri;
        }

        Iterator it = null;

        // First add the base params
        if (baseQueryParams != null)
        {
            it = baseQueryParams.values().iterator();
            while (it.hasNext())
            {
                QueryParam qparam = (QueryParam)it.next();
                if (   (null == staticParams || !staticParams.containsKey(qparam.getName())) 
                    && (null == dynamicParams || !dynamicParams.containsKey(qparam.getName())))
                {
                    duri.addQueryData(qparam.getName(), qparam.getValue());
                }
            }            
        }

        // Then add the static params
        if (staticParams != null)
        {
            it = staticParams.values().iterator();
            while (it.hasNext())
            {
                QueryParam qparam = (QueryParam)it.next();
                if (null == dynamicParams || !dynamicParams.containsKey(qparam.getName()))
                {               
                    duri.addQueryData(qparam.getName(), qparam.getValue());
                }
            }            
        }
        
        // Then add the dynamic params
        if (dynamicParams != null)
        {
            it = dynamicParams.entrySet().iterator();
            while (it.hasNext())
            {
                Entry entry = (Entry)it.next();
                duri.addQueryData((String)entry.getKey(), entry.getValue());
            }            
        }

        return duri;
    }

    
    private void dumpMap(String mapName, Map map)
    {
        System.out.println("----------- MAP: " + mapName);
        Iterator it = map.values().iterator();
        while (it.hasNext())
        {
            QueryParam qparam = (QueryParam)it.next();
            System.out.println("name = " + qparam.getName() + ", value = " + qparam.getValue());
        }
    }

    /**
     *  For the given portlet and given action, forward to the target
     *  defined in the forward configuration for the portlet + action.
     *  All parameters are resolved statically (via the forward definition)     
     *
     * @param portlet The name of the portlet for which we are forwarding.
     * @param target A logical target name. Portlets can have 1 or more targets.
     * @return DynamicURI the full link to the referenced page
     */
    public DynamicURI forward(RunData rundata, String portlet, String target)
    {
        return forwardDynamic(rundata, portlet, target, null);
    }

    /**
     *  For the given portlet and given action, forward to the target
     *  defined in the forward configuration for the portlet + action.
     *  Parameters are resolved both statically and dynamically, with the 
     *  dynamic parameter overriding the static parameter definitions.     
     *
     * @param portlet The name of the portlet for which we are forwarding.
     * @param target A logical target name. Portlets can have 1 or more targets.
     * @param parameters The dynamic Validation Parameters used in creating validation forwards     
     * @return DynamicURI the full link to the referenced page
     */
    public DynamicURI forwardDynamic(RunData rundata, 
                                 String portlet, 
                                 String target,
                                 Map parameters)
    {
        try
        {
            Map staticParams = null;
            String forwardName = "";
            String key = makePortletForwardKey(portlet, target);
            PortletForward pf = (PortletForward)this.portletForwards.get(key);        
            if (null != pf)
            {
                staticParams = pf.getQueryParams();
                Forward forward = (Forward)this.forwards.get(pf.getForward());
                if (null != forward)
                {
                    forwardName = forward.getName();
                }
            }
            return forwardInternal(rundata, forwardName, staticParams, parameters);
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
        return new DynamicURI();

    }

    /**
     * Get a collection of all forwards in the system.
     *
     * @return Collection of all forward definitions
     */
    public Collection getForwards()
    {
        return this.forwards.values();
    }

    /**
     * Get a collection of all portlet forwards in the system.
     *
     * @return Collection of all portlet forward definitions
     */
    public Collection getPortletForwards()
    {
        return this.portletForwards.values();
    }

    /**
     * Lookup a single forward definition by forward name
     *
     * @param  forwardName The name of the Forward to find
     * @return Forward The found forward definition or null if not found
     */
    public Forward getForward(String forwardName)
    {
        return (Forward)this.forwards.get(forwardName);
    }

    /**
     * Lookup a single portlet forward definition by portlet name + target name
     *
     * @param  portlet The name of the portlet in the Portlet Forward to find
     * @param  target The name of the target in the Portlet Forward to find     
     * @return Forward The found portlet forward definition or null if not found
     */
    public PortletForward getPortletForward(String portlet, String target)
    {
        return (PortletForward)this.portletForwards.get(makePortletForwardKey(portlet, target));
    }

    /**
     * Load all forward configuration files from forwards directory.
     *
     * 
     */
    protected void loadForwards()
        throws InitializationException
    {
        // create the serializer output format
        this.format = new OutputFormat();
        this.format.setIndenting(true);
        this.format.setIndent(4);

        File map = new File(this.mapping);
        if (map.exists() && map.isFile() && map.canRead())
        {
            try
            {
                this.mapper = new Mapping();
                InputSource is = new InputSource(new FileReader(map));
                is.setSystemId(this.mapping);
                this.mapper.loadMapping(is);
            }
            catch (Exception e)
            {
                String msg = "ForwardService: Error in castor mapping creation";
                logger.error(msg, e);
                throw new InitializationException(msg, e);
            }
        }
        else
        {
            String msg = "ForwardService: Mapping not found or not a file or unreadable: " + this.mapping;
            logger.error(msg);
            throw new InitializationException(msg);
        }


        try
        {
        
            File directory = new File(this.directory); 
            File[] files = directory.listFiles();
            for (int ix=0; ix < files.length; ix++)
            {
                if (files[ix].isDirectory())
                {
                    continue;
                }

                loadForwardConfiguration(files[ix]);
            }

        }
        catch (Exception e)
        {
            String msg = "ForwardService: Fatal error loading Forward configurations";
            logger.error(msg, e);
            throw new InitializationException(msg, e);
        }

    }

    protected String makePortletForwardKey(String portlet, String target)
    {
        StringBuffer key = new StringBuffer(portlet);
        key.append(KEY_DELIMITER);
        key.append(target);
        return key.toString();
    }

    /**
     * Load and unmarshal a Forward Configuration from a file.
     *
     * @param file the absolute file path storing this fragment
     */
    protected void loadForwardConfiguration(File file)
    {
        try
        {
            DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dbfactory.newDocumentBuilder();

            Document doc = builder.parse(file);

            Unmarshaller unmarshaller = new Unmarshaller(this.mapper);
            ForwardsConfiguration configuration = 
                (ForwardsConfiguration) unmarshaller.unmarshal((Node) doc);

            Iterator it = configuration.getForwards().iterator();
            while (it.hasNext())
            {
                Forward forward = (Forward)it.next();
                if (this.forwards.containsKey(forward.getName()))
                {
                    logger.error("ForwardService: already contains Forward key: " + forward.getName());
                }
                else
                {
                    this.forwards.put(forward.getName(), forward);
                }
                
                resyncParamMap(forward.getQueryParams());

            }

            it = configuration.getPortletForwards().iterator();
            while (it.hasNext())
            {
                PortletForward pf = (PortletForward)it.next();
                String key = makePortletForwardKey(pf.getPortlet(), pf.getTarget());
                if (this.portletForwards.containsKey(key))
                {
                    logger.error("ForwardService: already contains portletForward key: " + key);
                }
                else
                {
                    this.portletForwards.put(key, pf);
                    resyncParamMap(pf.getQueryParams());
                }
            }


        }
        catch (Throwable t)
        {
            logger.error("ForwardService: Could not unmarshal: " + file, t);
        }

    }

    private void resyncParamMap(Map map)
    {
        // Castor doesn't set the keys properly for maps
        // get the base query params        
        ArrayList list = new ArrayList(map.size());
        Iterator it = map.values().iterator();
        while (it.hasNext())
        {
            QueryParam qp = (QueryParam)it.next();
            list.add(qp);
        }                    
        map.clear();
        it = list.iterator();
        while (it.hasNext())
        {
            QueryParam qp = (QueryParam)it.next();
            map.put(qp.getName(), qp);
        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?