[jboss-cvs] JBossBlog SVN: r144 - in trunk: lib and 7 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Nov 29 09:20:31 EST 2007
Author: adamw
Date: 2007-11-29 09:20:31 -0500 (Thu, 29 Nov 2007)
New Revision: 144
Added:
trunk/lib/easymock.jar
trunk/lib/velocity-1.5.jar
trunk/lib/velocity-dep-1.5.jar
trunk/src/action/org/jboss/blog/servlet/
trunk/src/action/org/jboss/blog/servlet/FeedsServlet.java
trunk/src/action/org/jboss/blog/session/template/
trunk/src/action/org/jboss/blog/session/template/InvalidTemplateTypeException.java
trunk/src/action/org/jboss/blog/session/template/TemplateService.java
trunk/src/action/org/jboss/blog/session/template/VelocityTemplateService.java
trunk/src/model/org/jboss/blog/model/Template.java
trunk/src/model/org/jboss/blog/model/TemplateType.java
Removed:
trunk/src/model/org/jboss/blog/model/RemoteFeedType.java
Modified:
trunk/blog.iml
trunk/build.xml
trunk/resources/WEB-INF/web.xml
trunk/src/action/org/jboss/blog/tools/GeneralTools.java
trunk/src/model/org/jboss/blog/model/Feed.java
Log:
Modified: trunk/blog.iml
===================================================================
--- trunk/blog.iml 2007-11-28 22:00:09 UTC (rev 143)
+++ trunk/blog.iml 2007-11-29 14:20:31 UTC (rev 144)
@@ -89,19 +89,20 @@
</library>
</orderEntry>
<orderEntry type="module-library">
- <library name="shotoku-cache">
+ <library name="testng">
<CLASSES>
- <root url="jar://$MODULE_DIR$/lib/shotoku-cache.jar!/" />
+ <root url="jar://$MODULE_DIR$/lib/testng.jar!/" />
+ <root url="jar://$MODULE_DIR$/lib/easymock.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
- <library name="testng">
+ <library name="velocity">
<CLASSES>
- <root url="jar://$MODULE_DIR$/lib/testng.jar!/" />
- <root url="jar://$MODULE_DIR$/lib/easymock.jar!/" />
+ <root url="jar://$MODULE_DIR$/lib/velocity-1.5.jar!/" />
+ <root url="jar://$MODULE_DIR$/lib/velocity-dep-1.5.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Modified: trunk/build.xml
===================================================================
--- trunk/build.xml 2007-11-28 22:00:09 UTC (rev 143)
+++ trunk/build.xml 2007-11-29 14:20:31 UTC (rev 144)
@@ -160,6 +160,7 @@
<include name="lib/commons-digester.jar" />
<include name="lib/commons-beanutils.jar" />
<include name="lib/jsf-facelets.jar" />
+ <include name="lib/velocity*.jar" />
<include name="lib/jboss-seam-*.jar" />
<exclude name="lib/jboss-seam-gen.jar" />
</fileset>
Added: trunk/lib/easymock.jar
===================================================================
(Binary files differ)
Property changes on: trunk/lib/easymock.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: trunk/lib/velocity-1.5.jar
===================================================================
(Binary files differ)
Property changes on: trunk/lib/velocity-1.5.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: trunk/lib/velocity-dep-1.5.jar
===================================================================
(Binary files differ)
Property changes on: trunk/lib/velocity-dep-1.5.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Modified: trunk/resources/WEB-INF/web.xml
===================================================================
--- trunk/resources/WEB-INF/web.xml 2007-11-28 22:00:09 UTC (rev 143)
+++ trunk/resources/WEB-INF/web.xml 2007-11-29 14:20:31 UTC (rev 144)
@@ -62,6 +62,18 @@
<url-pattern>/seam/resource/*</url-pattern>
</servlet-mapping>
+ <!-- Feeds servlet -->
+
+ <servlet>
+ <servlet-name>Feeds Servlet</servlet-name>
+ <servlet-class>org.jboss.blog.servlet.FeedsServlet</servlet-class>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>Feeds Servlet</servlet-name>
+ <url-pattern>/feeds/*</url-pattern>
+ </servlet-mapping>
+
<!-- Facelets development mode (disable in production) -->
<context-param>
Added: trunk/src/action/org/jboss/blog/servlet/FeedsServlet.java
===================================================================
--- trunk/src/action/org/jboss/blog/servlet/FeedsServlet.java (rev 0)
+++ trunk/src/action/org/jboss/blog/servlet/FeedsServlet.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -0,0 +1,81 @@
+package org.jboss.blog.servlet;
+
+import org.jboss.blog.service.FeedNotFoundException;
+import org.jboss.blog.session.template.InvalidTemplateTypeException;
+import org.jboss.blog.session.template.TemplateService;
+import org.jboss.blog.tools.GeneralTools;
+import org.jboss.blog.tools.Pair;
+import org.jboss.seam.Component;
+import org.jboss.seam.log.Logging;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.UserTransaction;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public class FeedsServlet extends HttpServlet {
+ private void writeErrorMessage(String message, HttpServletResponse response) throws IOException {
+ response.setContentType("text/html");
+ response.getWriter().println(message);
+ response.getWriter().flush();
+ }
+
+ private void rollbackTx(UserTransaction tx, boolean txStarted) {
+ try {
+ if (txStarted) {
+ tx.rollback();
+ }
+ } catch (SystemException e1) {
+ Logging.getLog(FeedsServlet.class).error("Exception when rolling back the transaction", e1);
+ }
+ }
+
+ protected void service(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ String feedType = request.getParameter("type");
+ String feedName = request.getParameter("name");
+
+ UserTransaction tx = null;
+ boolean txStarted = false;
+ try {
+ tx = (UserTransaction) Component.getInstance("org.jboss.seam.transaction.transaction");
+ if (tx.getStatus() != Status.STATUS_ACTIVE) {
+ txStarted = true;
+ tx.begin();
+ }
+
+ TemplateService templateService = (TemplateService) Component.getInstance("templateService");
+ Pair<String, InputStream> data = templateService.getData(feedType, feedName);
+
+ response.setCharacterEncoding("UTF-8");
+ response.setContentType(data.getFirst());
+
+ GeneralTools.transfer(data.getSecond(), response.getWriter());
+ response.getWriter().flush();
+
+ if (txStarted) {
+ tx.commit();
+ }
+ } catch (InvalidTemplateTypeException e) {
+ rollbackTx(tx, txStarted);
+
+ writeErrorMessage("The given feed type does not exist.", response);
+ } catch (FeedNotFoundException e) {
+ rollbackTx(tx, txStarted);
+
+ writeErrorMessage("Request feed not found.", response);
+ } catch (Exception e) {
+ rollbackTx(tx, txStarted);
+
+ throw new ServletException(e);
+ }
+ }
+}
Added: trunk/src/action/org/jboss/blog/session/template/InvalidTemplateTypeException.java
===================================================================
--- trunk/src/action/org/jboss/blog/session/template/InvalidTemplateTypeException.java (rev 0)
+++ trunk/src/action/org/jboss/blog/session/template/InvalidTemplateTypeException.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -0,0 +1,21 @@
+package org.jboss.blog.session.template;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public class InvalidTemplateTypeException extends Exception {
+ public InvalidTemplateTypeException() {
+ }
+
+ public InvalidTemplateTypeException(String message) {
+ super(message);
+ }
+
+ public InvalidTemplateTypeException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InvalidTemplateTypeException(Throwable cause) {
+ super(cause);
+ }
+}
Added: trunk/src/action/org/jboss/blog/session/template/TemplateService.java
===================================================================
--- trunk/src/action/org/jboss/blog/session/template/TemplateService.java (rev 0)
+++ trunk/src/action/org/jboss/blog/session/template/TemplateService.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -0,0 +1,14 @@
+package org.jboss.blog.session.template;
+
+import org.jboss.blog.tools.Pair;
+import org.jboss.blog.service.FeedNotFoundException;
+
+import java.io.InputStream;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public interface TemplateService {
+ Pair<String, InputStream> getData(String feedType, String feedName)
+ throws FeedNotFoundException, InvalidTemplateTypeException;
+}
Added: trunk/src/action/org/jboss/blog/session/template/VelocityTemplateService.java
===================================================================
--- trunk/src/action/org/jboss/blog/session/template/VelocityTemplateService.java (rev 0)
+++ trunk/src/action/org/jboss/blog/session/template/VelocityTemplateService.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -0,0 +1,54 @@
+package org.jboss.blog.session.template;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.jboss.blog.model.Feed;
+import org.jboss.blog.model.Post;
+import org.jboss.blog.model.TemplateType;
+import org.jboss.blog.service.FeedNotFoundException;
+import org.jboss.blog.service.FeedsService;
+import org.jboss.blog.tools.Pair;
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.*;
+
+import java.io.InputStream;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+ at Name("templateService")
+ at Scope(ScopeType.STATELESS)
+ at AutoCreate
+public class VelocityTemplateService implements TemplateService {
+ @In
+ private FeedsService feedsService;
+
+ private VelocityEngine engine;
+
+ @Create
+ public void initVeloctiy() {
+ engine = new VelocityEngine();
+ }
+
+ public Pair<String, InputStream> getData(String feedType, String feedName)
+ throws FeedNotFoundException, InvalidTemplateTypeException {
+ TemplateType templateType;
+ try {
+ templateType = TemplateType.valueOf(feedType == null? "" : feedType.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ throw new InvalidTemplateTypeException(e);
+ }
+
+ FeedsService feedsService = (FeedsService) Component.getInstance("feedsService");
+ Feed feed = feedsService.getFeed(feedName);
+ List<Post> posts = feedsService.getPosts(feed, 0, feed.getMaxPostsInFeed());
+
+ VelocityContext context = new VelocityContext();
+ context.put("posts", posts);
+ context.put("feed", feed);
+
+ return null;
+ }
+}
Modified: trunk/src/action/org/jboss/blog/tools/GeneralTools.java
===================================================================
--- trunk/src/action/org/jboss/blog/tools/GeneralTools.java 2007-11-28 22:00:09 UTC (rev 143)
+++ trunk/src/action/org/jboss/blog/tools/GeneralTools.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -1,5 +1,7 @@
package org.jboss.blog.tools;
+import java.io.*;
+
/**
* @author <a href="mailto:adam at warski.org">Adam Warski</a>
*/
@@ -11,4 +13,43 @@
return o1.equals(o2);
}
}
+
+ private static final int TRANSFER_BUFFER_SIZE = 2048;
+
+ /**
+ * Transferes all bytes from the given input stream to the given output
+ * stream.
+ *
+ * @param is
+ * Input stream to read from.
+ * @param w
+ * Printwriter to write to.
+ * @throws java.io.IOException In case of an IO exception.
+ */
+ public static void transfer(InputStream is, PrintWriter w) throws IOException {
+ char[] buffer = new char[TRANSFER_BUFFER_SIZE];
+ int read;
+ InputStreamReader isr = new InputStreamReader(is);
+ while ((read = isr.read(buffer)) != -1) {
+ w.write(buffer, 0, read);
+ }
+ }
+
+ /**
+ * Transferes all bytes from the given input stream to the given output
+ * stream.
+ *
+ * @param is
+ * Input stream to read from.
+ * @param os
+ * Output stream to write to.
+ * @throws IOException In case of an IO exception.
+ */
+ public static void transfer(InputStream is, OutputStream os) throws IOException {
+ byte[] buffer = new byte[TRANSFER_BUFFER_SIZE];
+ int read;
+ while ((read = is.read(buffer)) != -1) {
+ os.write(buffer, 0, read);
+ }
+ }
}
Modified: trunk/src/model/org/jboss/blog/model/Feed.java
===================================================================
--- trunk/src/model/org/jboss/blog/model/Feed.java 2007-11-28 22:00:09 UTC (rev 143)
+++ trunk/src/model/org/jboss/blog/model/Feed.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -44,7 +44,7 @@
private String description;
@CollectionOfElements
- private Map<RemoteFeedType, String> templates;
+ private Map<TemplateType, String> templates;
@Column
private int maxPostsInFeed;
@@ -116,11 +116,11 @@
this.description = description;
}
- public Map<RemoteFeedType, String> getTemplates() {
+ public Map<TemplateType, String> getTemplates() {
return templates;
}
- public void setTemplates(Map<RemoteFeedType, String> templates) {
+ public void setTemplates(Map<TemplateType, String> templates) {
this.templates = templates;
}
Deleted: trunk/src/model/org/jboss/blog/model/RemoteFeedType.java
===================================================================
--- trunk/src/model/org/jboss/blog/model/RemoteFeedType.java 2007-11-28 22:00:09 UTC (rev 143)
+++ trunk/src/model/org/jboss/blog/model/RemoteFeedType.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -1,9 +0,0 @@
-package org.jboss.blog.model;
-
-/**
- * @author <a href="mailto:adam at warski.org">Adam Warski</a>
- */
-public enum RemoteFeedType {
- ATOM,
- RSS2
-}
Added: trunk/src/model/org/jboss/blog/model/Template.java
===================================================================
--- trunk/src/model/org/jboss/blog/model/Template.java (rev 0)
+++ trunk/src/model/org/jboss/blog/model/Template.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -0,0 +1,94 @@
+package org.jboss.blog.model;
+
+import org.hibernate.validator.Length;
+import org.hibernate.validator.NotEmpty;
+
+import javax.persistence.*;
+import java.util.Date;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+ at Entity
+ at Table(uniqueConstraints = @UniqueConstraint(columnNames = {"name", "templateType"}))
+public class Template {
+ @Id
+ @GeneratedValue
+ @Column(updatable = false)
+ private Integer id;
+
+ @Length(max = 128)
+ @NotEmpty
+ private String name;
+
+ @Lob
+ @NotEmpty
+ private String text;
+
+ @Enumerated
+ private TemplateType templateType;
+
+ @Temporal(value = TemporalType.TIMESTAMP)
+ private Date lastModified;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public TemplateType getFeedKind() {
+ return templateType;
+ }
+
+ public void setFeedKind(TemplateType templateType) {
+ this.templateType = templateType;
+ }
+
+ public Date getLastModified() {
+ return lastModified;
+ }
+
+ public void setLastModified(Date lastModified) {
+ this.lastModified = lastModified;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Template)) return false;
+
+ Template template = (Template) o;
+
+ if (templateType != template.templateType) return false;
+ if (id != null ? !id.equals(template.id) : template.id != null) return false;
+ if (name != null ? !name.equals(template.name) : template.name != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (name != null ? name.hashCode() : 0);
+ result = 31 * result + (templateType != null ? templateType.hashCode() : 0);
+ return result;
+ }
+}
Copied: trunk/src/model/org/jboss/blog/model/TemplateType.java (from rev 140, trunk/src/model/org/jboss/blog/model/RemoteFeedType.java)
===================================================================
--- trunk/src/model/org/jboss/blog/model/TemplateType.java (rev 0)
+++ trunk/src/model/org/jboss/blog/model/TemplateType.java 2007-11-29 14:20:31 UTC (rev 144)
@@ -0,0 +1,19 @@
+package org.jboss.blog.model;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public enum TemplateType {
+ ATOM("application/atom+xml"),
+ RSS2("application/xhtml+xml");
+
+ private final String contentType;
+
+ public String contentType() {
+ return contentType;
+ }
+
+ TemplateType(String contentType) {
+ this.contentType = contentType;
+ }
+}
More information about the jboss-cvs-commits
mailing list