Skip to content

Commit

Permalink
Merge branch 'master' of github.com:caelum/brutal
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosalles committed Dec 9, 2013
2 parents 0efe08c + 7664960 commit 32f3c18
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 59 deletions.
41 changes: 41 additions & 0 deletions src/main/java/br/com/caelum/brutal/infra/rss/RSSType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package br.com.caelum.brutal.infra.rss;

import java.util.Locale;

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

public enum RSSType {
ONDE_TRABALHAR {
@Override
public DateTimeFormatter getDateFormat() {
return new DateTimeFormatterBuilder()
.appendDayOfWeekShortText().appendLiteral(", ")
.appendDayOfMonth(2).appendLiteral(" ")
.appendMonthOfYearShortText().appendLiteral(" ")
.appendYear(4,4).appendLiteral(" ")
.appendHourOfDay(2).appendLiteral(":")
.appendMinuteOfHour(2).appendLiteral(":")
.appendSecondOfMinute(2).appendLiteral(" ")
.appendTimeZoneOffset("0", false, 2, 2)
.toFormatter().withLocale(Locale.US);
}
},
INFO_Q {
@Override
public DateTimeFormatter getDateFormat() {
return new DateTimeFormatterBuilder()
.appendDayOfWeekShortText().appendLiteral(", ")
.appendDayOfMonth(2).appendLiteral(" ")
.appendMonthOfYearShortText().appendLiteral(" ")
.appendYear(4,4).appendLiteral(" ")
.appendHourOfDay(2).appendLiteral(":")
.appendMinuteOfHour(2).appendLiteral(":")
.appendSecondOfMinute(2).appendLiteral(" ")
.appendTimeZoneId()
.toFormatter().withLocale(Locale.US);
}
};

public abstract DateTimeFormatter getDateFormat();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package br.com.caelum.brutal.infra.rss.converter;

import javax.inject.Inject;

import org.apache.log4j.Logger;

import br.com.caelum.brutal.infra.rss.RSSType;
import br.com.caelum.brutal.infra.rss.read.FeedsMap;
import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.quartzjob.CronTask;

@Controller
public class InfoQRSSJob implements CronTask{
private static final String INFOQ_BASE_KEY = "infoq";
private static final Logger LOG = Logger.getLogger(InfoQRSSJob.class);
@Inject private Result result;
@Inject private FeedsMap feedsMap;


@Override
@Path("/jdnakfh3nfis39103f1")
public void execute() {
LOG.debug("executing " + getClass().getSimpleName());
feedsMap.putOrUpdate(INFOQ_BASE_KEY, RSSType.INFO_Q);
LOG.debug(feedsMap.get(INFOQ_BASE_KEY));
result.nothing();
}

@Override
public String frequency() {
return "0 0/15 * * * ?";//fire every hour
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.apache.log4j.Logger;

import br.com.caelum.brutal.infra.rss.RSSType;
import br.com.caelum.brutal.infra.rss.read.FeedsMap;
import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Path;
Expand All @@ -22,7 +23,7 @@ public class OndeTrabalharRSSJob implements CronTask{
@Path("/asjkfnaowo21jkhwe12341")
public void execute() {
LOG.debug("executing " + getClass().getSimpleName());
feedsMap.putOrUpdate(JOBS_BASE_KEY);
feedsMap.putOrUpdate(JOBS_BASE_KEY, RSSType.ONDE_TRABALHAR);
LOG.debug(feedsMap.get(JOBS_BASE_KEY));
result.nothing();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package br.com.caelum.brutal.infra.rss.converter;

import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
Expand All @@ -13,33 +11,28 @@
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class RSSDateTimeConverter implements Converter {

private DateTimeFormatter rssPattern;

private static final DateTimeFormatter RSS_PATTERN = new DateTimeFormatterBuilder()
.appendDayOfWeekShortText().appendLiteral(", ")
.appendDayOfMonth(2).appendLiteral(" ")
.appendMonthOfYearShortText().appendLiteral(" ")
.appendYear(4,4).appendLiteral(" ")
.appendHourOfDay(2).appendLiteral(":")
.appendMinuteOfHour(2).appendLiteral(":")
.appendSecondOfMinute(2).appendLiteral(" ")
.appendTimeZoneOffset("0", false, 2, 2)
.toFormatter().withLocale(Locale.US);

public RSSDateTimeConverter(DateTimeFormatter rssPattern) {
this.rssPattern = rssPattern;
}

@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return DateTime.class.isAssignableFrom(type);
return LocalDateTime.class.isAssignableFrom(type);
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
writer.setValue(RSS_PATTERN.print((DateTime) source));
writer.setValue(rssPattern.print((DateTime) source));
}

@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
return RSS_PATTERN.parseDateTime(reader.getValue());
return rssPattern.parseLocalDateTime(reader.getValue());
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package br.com.caelum.brutal.infra.rss.read;

import java.io.InputStream;

import javax.enterprise.context.ApplicationScoped;

import br.com.caelum.brutal.infra.rss.RSSType;
import br.com.caelum.brutal.infra.rss.converter.RSSDateTimeConverter;

import com.thoughtworks.xstream.XStream;

@ApplicationScoped
public class FeedConverter {
private XStream xStream;

public FeedConverter() {
public FeedConverter(RSSType rss) {
xStream = new XStream();
xStream.alias("rss", RSSFeed.class);
xStream.alias("channel", RSSChannel.class);
xStream.alias("image", RSSImage.class);
xStream.alias("item", RSSItem.class);
xStream.addImplicitCollection(RSSChannel.class, "items");
xStream.registerConverter(new RSSDateTimeConverter());
xStream.addImplicitCollection(RSSItem.class, "category", String.class);
xStream.processAnnotations(RSSItem.class);
xStream.registerConverter(new RSSDateTimeConverter(rss.getDateFormat()));
}

public RSSFeed convert(String content){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import java.io.IOException;
import java.io.InputStream;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand All @@ -14,13 +11,17 @@
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import br.com.caelum.brutal.infra.rss.RSSType;

@ApplicationScoped
public class FeedReader {

@Inject private FeedConverter converter;
private FeedConverter converter;
private static final Logger LOG = Logger.getLogger(FeedReader.class);

public FeedReader(RSSType rss) {
converter = new FeedConverter(rss);
}

public RSSFeed read(String uri, Integer numberOfItems) {
String rssXml = getXmlFrom(uri);
return limitItems(converter.convert(rssXml), numberOfItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import br.com.caelum.brutal.infra.rss.RSSType;
import br.com.caelum.vraptor.environment.Environment;

@ApplicationScoped
public class FeedsMap {

private Map<String, RSSFeed> hashMap = new HashMap<String, RSSFeed>();
@Inject private FeedReader feedReader;
@Inject private Environment env;

public void putOrUpdate(String feedBaseKey){
public void putOrUpdate(String feedBaseKey, RSSType type){
hashMap.remove(feedBaseKey);
String uri = env.get(feedBaseKey+".url");
Integer numberOfItems = valueOf(env.get(feedBaseKey+".items"));
FeedReader feedReader = new FeedReader(type);
RSSFeed feed = feedReader.read(uri, numberOfItems);
hashMap.put(feedBaseKey, feed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;

public class RSSChannel {
private String title;
Expand All @@ -14,7 +14,7 @@ public class RSSChannel {
private String docs;
private String ttl;
private RSSImage image;
private DateTime pubDate;
private LocalDateTime pubDate;
private String link;
private List<RSSItem> items;

Expand Down Expand Up @@ -54,7 +54,7 @@ public RSSImage getImage() {
return image;
}

public DateTime getPubDate() {
public LocalDateTime getPubDate() {
return pubDate;
}

Expand Down
49 changes: 44 additions & 5 deletions src/main/java/br/com/caelum/brutal/infra/rss/read/RSSItem.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
package br.com.caelum.brutal.infra.rss.read;

import org.joda.time.DateTime;
import java.util.List;

import org.joda.time.LocalDateTime;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

public class RSSItem {

private String title;
private DateTime pubDate;
private LocalDateTime pubDate;
private String guid;
private String link;
private String description;

private List<String> category;
@XStreamAlias("dc:creator")
private String creator;
@XStreamAlias("dc:date")
private String date;
@XStreamAlias("dc:identifier")
private String identifier;

public String getCreator() {
return creator;
}

public void setCreator(String creator) {
this.creator = creator;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public String getTitle() {
return title;
}

public DateTime getPubDate() {
public LocalDateTime getPubDate() {
return pubDate;
}

Expand All @@ -36,6 +73,8 @@ public String toString() {
+ guid + ", link=" + link + ", description=" + description
+ "]";
}



public List<String> getCategory() {
return category;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,12 @@ questions.rss.title = GUJ respostas
questions.rss.description = Últimas perguntas do GUJ respostas
news.rss.title = Notícias do GUJ
news.rss.description = Últimas notícias do GUJ
jobs.rss.url = http://www.ondetrabalhar.com/
jobs.rss.title = Ofertas de Empregos
jobs.rss.more = Mais empregos...
infoq.rss.url = http://www.infoq.com/br/news
infoq.rss.title = Notícias InfoQ
infoq.rss.more = Mais notícias...

#Input overflow error
error.input_overflow = Você precisa esperar mais {0} segundos antes de inserir um novo post.
Expand Down
7 changes: 4 additions & 3 deletions src/main/webapp/WEB-INF/tags/feed.tag
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<%@attribute name="rssFeed" type="br.com.caelum.brutal.infra.rss.read.RSSFeed" required="true" %>
<%@attribute name="rssUrl" type="java.lang.String" required="true" %>
<%@attribute name="rssType" type="java.lang.String" required="true" %>
<c:if test="${not empty rssFeed.channel.items}">
<div class="subheader">
<h2 class="title page-title">
<fmt:message key="jobs.rss.title"/>
<fmt:message key="${rssType}.rss.title"/>
</h2>
<a href="${rssUrl}" class="rss-link"><i class="icon-rss"></i></a>
</div>
Expand All @@ -16,7 +17,7 @@
<li class="post-item sidebar-item">
<div class="summary sidebar-summary">
<time class="when" datetime="${item.pubDate}">
<fmt:formatDate value="${item.pubDate.toGregorianCalendar().time}" pattern="dd/MM/yyyy"/>
<fmt:formatDate value="${item.pubDate.toDate()}" pattern="dd/MM/yyyy"/>
</time> -
<h3 class="title item-title sidebar-item-title">
<a href="${item.link}"><c:out escapeXml="${true}" value="${item.title}"/></a>
Expand All @@ -25,6 +26,6 @@
</li>
</c:forEach>
</ol>
<a class="more-items" href="http://ondetrabalhar.com/"><fmt:message key="jobs.rss.more" /></a>
<a class="more-items" href="<fmt:message key="${rssType}.rss.url" />"><fmt:message key="${rssType}.rss.more" /></a>

</c:if>
3 changes: 2 additions & 1 deletion src/main/webapp/WEB-INF/tags/sideBar.tag
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<tags:homeNewsList newses="${sidebarNews}" />
<div id="adSideBar" class="ad medium-ads"></div>
<tags:recentTagsUsage tagsUsage="${recentTags}"/>
<tags:feed rssUrl="${env.get('jobs.url')}" rssFeed="${jobs}"/>
<tags:feed rssUrl="${env.get('jobs.url')}" rssFeed="${jobs}" rssType="jobs"/>
<tags:feed rssUrl="${env.get('infoq.url')}" rssFeed="${infoq}" rssType="infoq"/>
<c:if test="${relatedQuestions != null }">
<tags:relatedQuestions questions="${relatedQuestions}"/>
</c:if>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/css/brutal.css
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ a.button.ask-a-question:hover {
margin-bottom: 0;
font-size: 1em;
display: inline;
line-height: 1.2em;
}

.more-items {
Expand Down
Loading

0 comments on commit 32f3c18

Please sign in to comment.