Search in sources :

Example 1 with WireFeedOutput

use of com.rometools.rome.io.WireFeedOutput in project tutorials by eugenp.

the class JsonChannelHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Channel wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    WireFeedOutput feedOutput = new WireFeedOutput();
    try {
        String xmlStr = feedOutput.outputString(wireFeed, true);
        JSONObject xmlJSONObj = XML.toJSONObject(xmlStr);
        String jsonPrettyPrintString = xmlJSONObj.toString(4);
        outputMessage.getBody().write(jsonPrettyPrintString.getBytes());
    } catch (JSONException | FeedException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) FeedException(com.rometools.rome.io.FeedException) JSONException(org.json.JSONException)

Example 2 with WireFeedOutput

use of com.rometools.rome.io.WireFeedOutput in project spring-framework by spring-projects.

the class AbstractWireFeedHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    Charset charset = (StringUtils.hasLength(wireFeed.getEncoding()) ? Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET);
    MediaType contentType = outputMessage.getHeaders().getContentType();
    if (contentType != null) {
        contentType = new MediaType(contentType, charset);
        outputMessage.getHeaders().setContentType(contentType);
    }
    WireFeedOutput feedOutput = new WireFeedOutput();
    try {
        Writer writer = new OutputStreamWriter(outputMessage.getBody(), charset);
        feedOutput.output(wireFeed, writer);
    } catch (FeedException ex) {
        throw new HttpMessageNotWritableException("Could not write WireFeed: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) FeedException(com.rometools.rome.io.FeedException) Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 3 with WireFeedOutput

use of com.rometools.rome.io.WireFeedOutput in project opencast by opencast.

the class FeedServiceImpl method getFeed.

/*
   * Note: We're using Regex matching for the path here, instead of normal JAX-RS paths.  Previously this class was a servlet,
   * which was fine except that it had auth issues.  Removing the servlet fixed the auth issues, but then the paths (as written
   * in the RestQuery docs) don't work because  JAX-RS does not support having "/" characters as part of the variable's value.
   *
   * So, what we've done instead is match everything that comes in under the /feeds/ namespace, and then substring it out the way
   * the old servlet code did.  But without the servlet, or auth issues :)
   */
@GET
@Produces(MediaType.TEXT_XML)
@Path("{query: .*}")
// FIXME: These Opencast REST classes do not support this path style, and need to have that support added
@RestQuery(name = "getFeed", description = "Gets an Atom or RSS feed", pathParameters = { @RestParameter(description = "The feed type", name = "type", type = Type.STRING, isRequired = true), @RestParameter(description = "The feed version", name = "version", type = Type.STRING, isRequired = true), @RestParameter(description = "The feed query", name = "query", type = Type.STRING, isRequired = true) }, reponses = { @RestResponse(description = "Return the feed of the appropriate type", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "", responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR) }, returnDescription = "")
public Response getFeed(@Context HttpServletRequest request) {
    String contentType = null;
    logger.debug("Requesting RSS or Atom feed.");
    FeedInfo feedInfo = null;
    Organization organization = securityService.getOrganization();
    // Try to extract requested feed type and content
    try {
        feedInfo = extractFeedInfo(request);
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Set the content type
    if (feedInfo.getType().equals(Feed.Type.Atom))
        contentType = "application/atom+xml";
    else if (feedInfo.getType().equals(Feed.Type.RSS))
        contentType = "application/rss+xml";
    // Have a feed generator create the requested feed
    Feed feed = null;
    for (FeedGenerator generator : feeds) {
        if (generator.accept(feedInfo.getQuery())) {
            feed = generator.createFeed(feedInfo.getType(), feedInfo.getQuery(), feedInfo.getSize(), organization);
            if (feed == null) {
                return Response.serverError().build();
            }
            break;
        }
    }
    // Have we found a feed generator?
    if (feed == null) {
        logger.debug("RSS/Atom feed could not be generated");
        return Response.status(Status.NOT_FOUND).build();
    }
    // Set character encoding
    Variant v = new Variant(MediaType.valueOf(contentType), null, feed.getEncoding());
    String outputString = null;
    try {
        if (feedInfo.getType().equals(Feed.Type.RSS)) {
            logger.debug("Creating RSS feed output.");
            SyndFeedOutput output = new SyndFeedOutput();
            outputString = output.outputString(new RomeRssFeed(feed, feedInfo));
        } else {
            logger.debug("Creating Atom feed output.");
            WireFeedOutput output = new WireFeedOutput();
            outputString = output.outputString(new RomeAtomFeed(feed, feedInfo));
        }
    } catch (FeedException e) {
        return Response.serverError().build();
    }
    return Response.ok(outputString, v).build();
}
Also used : Variant(javax.ws.rs.core.Variant) Organization(org.opencastproject.security.api.Organization) FeedGenerator(org.opencastproject.feed.api.FeedGenerator) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) FeedException(com.rometools.rome.io.FeedException) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) FeedException(com.rometools.rome.io.FeedException) Feed(org.opencastproject.feed.api.Feed) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 4 with WireFeedOutput

use of com.rometools.rome.io.WireFeedOutput in project spring-framework by spring-projects.

the class AbstractFeedView method renderMergedOutputModel.

@Override
protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    T wireFeed = newFeed();
    buildFeedMetadata(model, wireFeed, request);
    buildFeedEntries(model, wireFeed, request, response);
    setResponseContentType(request, response);
    if (!StringUtils.hasText(wireFeed.getEncoding())) {
        wireFeed.setEncoding("UTF-8");
    }
    WireFeedOutput feedOutput = new WireFeedOutput();
    ServletOutputStream out = response.getOutputStream();
    feedOutput.output(wireFeed, new OutputStreamWriter(out, wireFeed.getEncoding()));
    out.flush();
}
Also used : ServletOutputStream(jakarta.servlet.ServletOutputStream) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

WireFeedOutput (com.rometools.rome.io.WireFeedOutput)4 FeedException (com.rometools.rome.io.FeedException)3 OutputStreamWriter (java.io.OutputStreamWriter)2 SyndFeedOutput (com.rometools.rome.io.SyndFeedOutput)1 ServletOutputStream (jakarta.servlet.ServletOutputStream)1 Writer (java.io.Writer)1 Charset (java.nio.charset.Charset)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Variant (javax.ws.rs.core.Variant)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Feed (org.opencastproject.feed.api.Feed)1 FeedGenerator (org.opencastproject.feed.api.FeedGenerator)1 Organization (org.opencastproject.security.api.Organization)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1 MediaType (org.springframework.http.MediaType)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1