Atom Feed HttpMessageConverter

This commit is contained in:
Arjen Poutsma
2010-03-05 14:32:36 +00:00
parent c91ff130d5
commit 5303ab68aa
10 changed files with 520 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter.feed;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedInput;
import com.sun.syndication.io.WireFeedOutput;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StringUtils;
/**
* Abstract base class for Atom and RSS Feed message converters, using java.net's
* <a href="https://rome.dev.java.net/">ROME</a> package.
*
* @author Arjen Poutsma
* @since 3.0.2
* @see AtomFeedHttpMessageConverter
* @see RssChannelHttpMessageConverter
*/
public abstract class AbstractWireFeedHttpMessageConverter<T extends WireFeed> extends AbstractHttpMessageConverter<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
protected AbstractWireFeedHttpMessageConverter(MediaType supportedMediaType) {
super(supportedMediaType);
}
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
WireFeedInput feedInput = new WireFeedInput();
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset;
if (contentType != null && contentType.getCharSet() != null) {
charset = contentType.getCharSet();
} else {
charset = DEFAULT_CHARSET;
}
try {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
return (T) feedInput.build(reader);
}
catch (FeedException ex) {
throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex);
}
}
@Override
protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
String wireFeedEncoding = wireFeed.getEncoding();
if (!StringUtils.hasLength(wireFeedEncoding)) {
wireFeedEncoding = DEFAULT_CHARSET.name();
}
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType != null) {
Charset wireFeedCharset = Charset.forName(wireFeedEncoding);
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), wireFeedCharset);
outputMessage.getHeaders().setContentType(contentType);
}
WireFeedOutput feedOutput = new WireFeedOutput();
try {
Writer writer = new OutputStreamWriter(outputMessage.getBody(), wireFeedEncoding);
feedOutput.output(wireFeed, writer);
}
catch (FeedException ex) {
throw new HttpMessageNotWritableException("Could not write WiredFeed: " + ex.getMessage(), ex);
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter.feed;
import com.sun.syndication.feed.atom.Feed;
import org.springframework.http.MediaType;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter} that can read and write Atom feeds.
* Specifically, this converter can handle {@link Feed} objects, from the <a href="https://rome.dev.java.net/">ROME</a>
* project.
*
* <p>By default, this converter reads and writes the media type ({@code application/atom+xml}). This can
* be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
*
* @author Arjen Poutsma
* @see Feed
* @since 3.0.2
*/
public class AtomFeedHttpMessageConverter extends AbstractWireFeedHttpMessageConverter<Feed> {
public AtomFeedHttpMessageConverter() {
super(new MediaType("application", "atom+xml"));
}
@Override
protected boolean supports(Class<?> clazz) {
return Feed.class.isAssignableFrom(clazz);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter.feed;
import com.sun.syndication.feed.rss.Channel;
import org.springframework.http.MediaType;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter} that can read and write RSS feeds.
* Specifically, this converter can handle {@link Channel} objects, from the <a href="https://rome.dev.java.net/">ROME</a>
* project.
*
* <p>By default, this converter reads and writes the media type ({@code application/rss+xml}). This can
* be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
*
* @author Arjen Poutsma
* @see Channel
* @since 3.0.2
*/
public class RssChannelHttpMessageConverter extends AbstractWireFeedHttpMessageConverter<Channel> {
public RssChannelHttpMessageConverter() {
super(new MediaType("application", "rss+xml"));
}
@Override
protected boolean supports(Class<?> clazz) {
return Channel.class.isAssignableFrom(clazz);
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
* Provides HttpMessageConverter implementations for handling Atom and RSS feeds.
*
*/
package org.springframework.http.converter.feed;