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