[opencms-dev] Container page to HTML

Christoph Fröhlich cfauto at folge2.de
Thu Dec 15 10:13:23 CET 2011


Hi Vincent.

we had a similar requirement. We need to render group containers that are not placed in the container page (to implement sidebar inheritance).

We ended up adding a new tag that extends the existing CmsJspTagContainer.

Since the render method of the CmsJspTagContainer is private we had to copy some code in our new tag.
It's an ugly solution and OpenCms-Updates become more difficult. But it works for us.


Regards 
c


---------------------------------------------------------------------------------------------------------------------------
package f3.tags;


import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.opencms.ade.configuration.CmsADEConfigData;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.flex.CmsFlexController;
import org.opencms.jsp.CmsJspTagContainer;
import org.opencms.jsp.CmsJspTagInclude;
import org.opencms.jsp.Messages;
import org.opencms.jsp.util.CmsJspStandardContextBean;
import org.opencms.loader.CmsLoaderException;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalStateException;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsUUID;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.containerpage.CmsContainerElementBean;
import org.opencms.xml.containerpage.CmsFormatterBean;
import org.opencms.xml.containerpage.CmsFormatterConfiguration;
import org.opencms.xml.containerpage.CmsGroupContainerBean;
import org.opencms.xml.containerpage.CmsXmlGroupContainer;
import org.opencms.xml.containerpage.CmsXmlGroupContainerFactory;

/**
 * @author jd
 * 
 * Overwriting doStartTag(). Our version reads the group container given and calls render().
 * 
 * The render-method is a copy of CmsJspTagContainer#renderContainerElement().
 * 
 * We just copied the stuff that we need to render group containers.  
 * 
 *
 */
public class F3GroupContainerTag extends CmsJspTagContainer {

	private String groupContainerPath;

	@Override
	public int doStartTag() throws JspException {

        ServletRequest req = pageContext.getRequest();

        CmsFlexController controller = CmsFlexController.getController(req);
        CmsObject cms = controller.getCmsObject();
        
        CmsJspStandardContextBean context = CmsJspStandardContextBean.getInstance(req);
        
        try {
			CmsResource groupContainerResource = cms.readResource(groupContainerPath);

            Locale locale = cms.getRequestContext().getLocale();
            
			render(cms, context, groupContainerResource, locale);
		} catch (Exception e) {
			e.printStackTrace();
		}

        
		return 0;
	}
	
	public void render(CmsObject cms, CmsJspStandardContextBean standardContext,
	        /*CmsContainerElementBean element,*/ CmsResource groupContainerResource,
	        Locale locale) throws CmsXmlException, CmsLoaderException, CmsException, IOException{
	
	 
		String containerType = "right";
		int containerWidth = 230;
		 
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
		
		// TODO determine if online
		boolean isOnline = false; 
		
		CmsUUID elementId = groupContainerResource.getStructureId();
		
		CmsResource formatter = cms.readResource("/system/modules/org.opencms.ade.containerpage/formatters/group-container.jsp");
		CmsUUID formatterId = formatter.getStructureId();
		
		CmsContainerElementBean element = new CmsContainerElementBean(elementId, formatterId, null, false);
		element.initResource(cms);
		       
		CmsADEConfigData adeConfig = OpenCms.getADEManager().lookupConfiguration(
            cms,
            cms.getRequestContext().getRootUri());
            
        CmsXmlGroupContainer xmlGroupContainer = CmsXmlGroupContainerFactory.unmarshal(
            cms,
            element.getResource(),
            req);
        CmsGroupContainerBean groupContainer = xmlGroupContainer.getGroupContainer(
            cms,
            cms.getRequestContext().getLocale());
        if (!groupContainer.getTypes().contains(containerType)) {
            //TODO: change message
            throw new CmsIllegalStateException(Messages.get().container(
                Messages.ERR_XSD_NO_TEMPLATE_FORMATTER_3,
                element.getResource().getRootPath(),
                OpenCms.getResourceManager().getResourceType(element.getResource()).getTypeName(),
                containerType));
        }
        // wrapping the elements with DIV containing initial element data. To be removed by the container-page editor

 
        printElementWrapperTagStart(isOnline, cms, element, true);
        for (CmsContainerElementBean subelement : groupContainer.getElements()) {
            try {
                subelement.initResource(cms);
                // writing elements to the session cache to improve performance of the container-page editor
//                getSessionCache(cms).setCacheContainerElement(subelement.editorHash(), subelement);
                CmsFormatterConfiguration subelementFormatters = adeConfig.getFormatters(
                    cms,
                    subelement.getResource());
                CmsFormatterBean subelementFormatter = subelementFormatters.getFormatter(
                    containerType,
                    containerWidth);
                if (subelementFormatter == null) {
                	// TODO Log correctly
                	System.out.println("TODO Log correctly: No formatter for "+subelement.getSitePath());
//                    if (LOG.isErrorEnabled()) {
//                        LOG.error(new CmsIllegalStateException(Messages.get().container(
//                            Messages.ERR_XSD_NO_TEMPLATE_FORMATTER_3,
//                            subelement.getSitePath(),
//                            OpenCms.getResourceManager().getResourceType(subelement.getResource()).getTypeName(),
//                            containerType)));
//                    }
                    // skip this element, it has no formatter for this container type defined
                    continue;
                }
                // execute the formatter JSP for the given element URI
                // wrapping the elements with DIV containing initial element data. To be removed by the container-page editor
                printElementWrapperTagStart(isOnline, cms, subelement, false);
                standardContext.setElement(subelement);
                try {
					CmsJspTagInclude.includeTagAction(
                        pageContext,
                        subelementFormatter.getJspRootPath(),
                        null,
                        locale,
                        false,
                        isOnline,
                        null,
                        CmsRequestUtil.getAtrributeMap(req),
                        req,
                        res);
                } catch (Exception e) {
                	// TODO Log error
        			e.printStackTrace();
//                    if (LOG.isErrorEnabled()) {
//                        LOG.error(Messages.get().getBundle().key(
//                            Messages.ERR_CONTAINER_PAGE_ELEMENT_RENDER_ERROR_2,
//                            subelement.getSitePath(),
//                            subelementFormatter), e);
//                    }
//                    printElementErrorTag(
//                        isOnline,
//                        subelement.getSitePath(),
//                        subelementFormatter.getJspRootPath(),
//                        e);
                }
                printElementWrapperTagEnd(isOnline, false);
            } catch (Exception e) {
            	// TODO Log error
    			e.printStackTrace();
//                if (LOG.isErrorEnabled()) {
//                    LOG.error(e);
//                }
            }
        }
        printElementWrapperTagEnd(isOnline, true);
	}

	public String getGroupContainerPath() {
		return groupContainerPath;
	}

	public void setGroupContainerPath(String groupContainerPath) {
		this.groupContainerPath = groupContainerPath;
	}
	
	
	/**
	 * original render method from OpenCms ContainerTag
	 * When updating OpenCMS we can diff this method with 
	 * the one provided by the update, in order to see if we have to
	 * adopt something new.
	 */
//	   private void renderContainerElement(
//		        CmsObject cms,
//		        CmsJspStandardContextBean standardContext,
//		        CmsContainerElementBean element,
//		        Locale locale) throws CmsException, CmsXmlException, CmsLoaderException, IOException {
//
//		        ServletRequest req = pageContext.getRequest();
//		        ServletResponse res = pageContext.getResponse();
//		        String containerType = getType();
//		        int containerWidth = getContainerWidth();
//		        boolean isOnline = cms.getRequestContext().getCurrentProject().isOnlineProject();
//		        element.initResource(cms);
//		        // writing elements to the session cache to improve performance of the container-page editor
//		        getSessionCache(cms).setCacheContainerElement(element.editorHash(), element);
//		        CmsADEConfigData adeConfig = OpenCms.getADEManager().lookupConfiguration(
//		            cms,
//		            cms.getRequestContext().getRootUri());
//		        if (element.isGroupContainer(cms)) {
//		            CmsXmlGroupContainer xmlGroupContainer = CmsXmlGroupContainerFactory.unmarshal(
//		                cms,
//		                element.getResource(),
//		                req);
//		            CmsGroupContainerBean groupContainer = xmlGroupContainer.getGroupContainer(
//		                cms,
//		                cms.getRequestContext().getLocale());
//		            if (!groupContainer.getTypes().contains(containerType)) {
//		                //TODO: change message
//		                throw new CmsIllegalStateException(Messages.get().container(
//		                    Messages.ERR_XSD_NO_TEMPLATE_FORMATTER_3,
//		                    element.getResource().getRootPath(),
//		                    OpenCms.getResourceManager().getResourceType(element.getResource()).getTypeName(),
//		                    containerType));
//		            }
//		            // wrapping the elements with DIV containing initial element data. To be removed by the container-page editor
//		            printElementWrapperTagStart(isOnline, cms, element, true);
//		            for (CmsContainerElementBean subelement : groupContainer.getElements()) {
//		                try {
//		                    subelement.initResource(cms);
//		                    // writing elements to the session cache to improve performance of the container-page editor
//		                    getSessionCache(cms).setCacheContainerElement(subelement.editorHash(), subelement);
//		                    CmsFormatterConfiguration subelementFormatters = adeConfig.getFormatters(
//		                        cms,
//		                        subelement.getResource());
//		                    CmsFormatterBean subelementFormatter = subelementFormatters.getFormatter(
//		                        containerType,
//		                        containerWidth);
//		                    if (subelementFormatter == null) {
//		                        if (LOG.isErrorEnabled()) {
//		                            LOG.error(new CmsIllegalStateException(Messages.get().container(
//		                                Messages.ERR_XSD_NO_TEMPLATE_FORMATTER_3,
//		                                subelement.getSitePath(),
//		                                OpenCms.getResourceManager().getResourceType(subelement.getResource()).getTypeName(),
//		                                containerType)));
//		                        }
//		                        // skip this element, it has no formatter for this container type defined
//		                        continue;
//		                    }
//		                    // execute the formatter JSP for the given element URI
//		                    // wrapping the elements with DIV containing initial element data. To be removed by the container-page editor
//		                    printElementWrapperTagStart(isOnline, cms, subelement, false);
//		                    standardContext.setElement(subelement);
//		                    try {
//		                        CmsJspTagInclude.includeTagAction(
//		                            pageContext,
//		                            subelementFormatter.getJspRootPath(),
//		                            null,
//		                            locale,
//		                            false,
//		                            isOnline,
//		                            null,
//		                            CmsRequestUtil.getAtrributeMap(req),
//		                            req,
//		                            res);
//		                    } catch (Exception e) {
//		                        if (LOG.isErrorEnabled()) {
//		                            LOG.error(Messages.get().getBundle().key(
//		                                Messages.ERR_CONTAINER_PAGE_ELEMENT_RENDER_ERROR_2,
//		                                subelement.getSitePath(),
//		                                subelementFormatter), e);
//		                        }
//		                        printElementErrorTag(
//		                            isOnline,
//		                            subelement.getSitePath(),
//		                            subelementFormatter.getJspRootPath(),
//		                            e);
//		                    }
//		                    printElementWrapperTagEnd(isOnline, false);
//		                } catch (Exception e) {
//		                    if (LOG.isErrorEnabled()) {
//		                        LOG.error(e);
//		                    }
//		                }
//		            }
//		            printElementWrapperTagEnd(isOnline, true);
//
//		        } else {
//
//		            String formatter = null;
//		            try {
//		                formatter = cms.getSitePath(cms.readResource(element.getFormatterId()));
//		            } catch (CmsException e) {
//		                // the formatter resource can not be found, try reading it form the configuration
//		                CmsFormatterConfiguration elementFormatters = adeConfig.getFormatters(cms, element.getResource());
//		                CmsFormatterBean elementFormatterBean = elementFormatters.getFormatter(containerType, containerWidth);
//		                if (elementFormatterBean == null) {
//		                    if (LOG.isErrorEnabled()) {
//		                        LOG.error(new CmsIllegalStateException(Messages.get().container(
//		                            Messages.ERR_XSD_NO_TEMPLATE_FORMATTER_3,
//		                            element.getSitePath(),
//		                            OpenCms.getResourceManager().getResourceType(element.getResource()).getTypeName(),
//		                            containerType)));
//		                    }
//		                    // skip this element, it has no formatter for this container type defined
//		                    return;
//		                }
//		                formatter = elementFormatterBean.getJspRootPath();
//		            }
//		            printElementWrapperTagStart(isOnline, cms, element, false);
//		            standardContext.setElement(element);
//		            try {
//		                // execute the formatter jsp for the given element uri
//		                CmsJspTagInclude.includeTagAction(
//		                    pageContext,
//		                    formatter,
//		                    null,
//		                    locale,
//		                    false,
//		                    isOnline,
//		                    null,
//		                    CmsRequestUtil.getAtrributeMap(req),
//		                    req,
//		                    res);
//		            } catch (Exception e) {
//		                if (LOG.isErrorEnabled()) {
//		                    LOG.error(Messages.get().getBundle().key(
//		                        Messages.ERR_CONTAINER_PAGE_ELEMENT_RENDER_ERROR_2,
//		                        element.getSitePath(),
//		                        formatter), e);
//		                }
//		                printElementErrorTag(isOnline, element.getSitePath(), formatter, e);
//		            }
//		            printElementWrapperTagEnd(isOnline, false);
//		        }
//		    }
 }
	







Am 15.12.2011 um 03:14 schrieb Daniel:

> How about sending a request to OC from that jsp ?
> 
> 2011/11/22 Vincent Lambert <v.lambert at eurelis.com>
> Hi all,
> 
> We need to transform programmatically container page (OpenCms 8) into HTML (to include then into other JSP).
> Does anyone know how to do that ?
> 
> Thanks in advance.
> 
> Regards,
> Vincent
> 
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev
> 
> 
> 
> 
> 
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20111215/79c79eb3/attachment.htm>


More information about the opencms-dev mailing list