Package org.olat.modules.webFeed

Source Code of org.olat.modules.webFeed.SyndFeedMediaResource

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) frentix GmbH<br>
* http://www.frentix.com<br>
* <p>
*/
package org.olat.modules.webFeed;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.olat.commons.servlets.RSSServlet;
import org.olat.core.gui.media.MediaResource;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;

/**
* The media resource for synd feeds. Used for dispatching.
*
* <P>
* Initial Date: Mar 12, 2009 <br>
*
* @author gwassmann
*/
public class SyndFeedMediaResource implements MediaResource {

  private SyndFeed feed;
  private String feedString;
  private static final String CONTENT_TYPE = "application/rss+xml";

  public SyndFeedMediaResource(SyndFeed feed) {
    this.feed = feed;

    feedString = null;
    try {
      SyndFeedOutput output = new SyndFeedOutput();
      feedString = output.outputString(feed);
    } catch (FeedException e) {
      // cannot convert feed to string or something
    }
  }

  /**
   * @see org.olat.core.gui.media.MediaResource#getContentType()
   */
  public String getContentType() {
    return CONTENT_TYPE;
  }

  /**
   * @see org.olat.core.gui.media.MediaResource#getInputStream()
   */
  public InputStream getInputStream() {
    ByteArrayInputStream inputStream = null;
    try {
      inputStream = new ByteArrayInputStream(feedString.getBytes(RSSServlet.DEFAULT_ENCODING));
    } catch (UnsupportedEncodingException e) {
      // log something
    }
    return inputStream;
  }

  /**
   * @see org.olat.core.gui.media.MediaResource#getLastModified()
   */
  public Long getLastModified() {
    Long lastModified = null;
    Date date = feed.getPublishedDate();
    if (date != null) {
      lastModified = Long.valueOf(date.getTime());
    }
    return lastModified;
  }

  /**
   * @see org.olat.core.gui.media.MediaResource#getSize()
   */
  public Long getSize() {
    return new Long(feedString.getBytes().length);
  }

  /**
   * @see org.olat.core.gui.media.MediaResource#prepare(javax.servlet.http.HttpServletResponse)
   */
  public void prepare(@SuppressWarnings("unused") HttpServletResponse hres) {
  // nothing to prepare
  }

  /**
   * @see org.olat.core.gui.media.MediaResource#release()
   */
  public void release() {
  // nothing to release
  }

}
TOP

Related Classes of org.olat.modules.webFeed.SyndFeedMediaResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.