001/*
002 * (C) Copyright 2008 JBoss and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Original file from org.jboss.seam.rss.ui.UIFeed.java in jboss-seam-rss
016 *     Anahide Tchertchian
017 */
018package org.nuxeo.ecm.platform.ui.web.component.seam;
019
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.Writer;
023
024import javax.faces.context.FacesContext;
025import javax.servlet.http.HttpServletResponse;
026
027import org.jboss.seam.contexts.Contexts;
028
029import yarfraw.core.datamodel.ChannelFeed;
030import yarfraw.core.datamodel.YarfrawException;
031import yarfraw.io.FeedWriter;
032
033/**
034 * Override for better resolution of feedFormat variable.
035 * <p>
036 * When this is a value expression, the original component does not decode the value correctly.
037 *
038 * @since 5.4.2
039 */
040public class UIFeed extends org.jboss.seam.rss.ui.UIFeed {
041
042    public static final String COMPONENT_TYPE = UIFeed.class.getName();
043
044    private static final String MIMETYPE = "text/xml";
045
046    @Override
047    public void encodeEnd(FacesContext facesContext) throws IOException {
048        ChannelFeed channelFeed = (ChannelFeed) Contexts.getEventContext().get(FEED_IMPL_KEY);
049        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
050        try {
051            // XXX: retrieve fee format with getter instead of using the static
052            // value set.
053            FeedWriter.writeChannel(getFeedFormat(), channelFeed, byteStream);
054        } catch (YarfrawException e) {
055            /**
056             * Was IOException, but 1.5 does not have this constructor
057             * http://java.sun.com/j2se/1.5.0/docs/api/java/io/IOException.html
058             */
059            throw new RuntimeException("Could not create feed", e);
060        }
061        Writer responseWriter = ((HttpServletResponse) facesContext.getExternalContext().getResponse()).getWriter();
062        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
063        response.setContentType(MIMETYPE);
064        response.setContentLength(byteStream.size());
065        responseWriter.write(byteStream.toString());
066        response.flushBuffer();
067        facesContext.responseComplete();
068    }
069
070}