Permalink
Browse files

Integrazione lib java events

  • Loading branch information...
fpaparoni committed Jan 12, 2018
1 parent 218c508 commit 75ca26ba66fb0c6bd8fb7048eb2600451f3e0b17
@@ -24,6 +24,11 @@
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.256</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>de.l3s.boilerpipe</groupId>
<artifactId>boilerpipe</artifactId>
@@ -2,13 +2,14 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
public class ExampleScheduledRequestHandler implements RequestHandler<CloudWatchEvent, String>{
public class ExampleScheduledRequestHandler implements RequestHandler<ScheduledEvent, String>{
public String handleRequest(CloudWatchEvent event, Context context) {
public String handleRequest(ScheduledEvent event, Context context) {
String logString=String.format("Id %s source %s",event.getId(),event.getSource());
context.getLogger().log(logString);
return "Scheduled handler returns: " + logString;
}
}
@@ -10,10 +10,10 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
@@ -26,15 +26,13 @@
import de.l3s.boilerpipe.extractors.ArticleExtractor;
public class PrintServiceHandler implements RequestHandler<CloudWatchEvent, String>{
public class PrintServiceHandler implements RequestHandler<ScheduledEvent, String>{
public String handleRequest(CloudWatchEvent arg0, Context arg1) {
public String handleRequest(ScheduledEvent event, Context context) {
try {
URL feedUrl = new URL("http://www.repubblica.it/rss/homepage/rss2.0.xml");
SyndFeedInput input = new SyndFeedInput();
//Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
//HttpURLConnection conn = (HttpURLConnection)feedUrl.openConnection(proxy);
HttpURLConnection conn = (HttpURLConnection)feedUrl.openConnection();
InputStream in = conn.getInputStream();
SyndFeed feed = (SyndFeed) input.build(new InputStreamReader(in, Charset.forName("UTF-8")));
@@ -47,40 +45,46 @@ public String handleRequest(CloudWatchEvent arg0, Context arg1) {
Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC);
Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
Paragraph intestazione = new Paragraph("Giornale generato da feed RSS di Repubblica", chapterFont);
document.add(intestazione);
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
//Scorro gli articoli del feed rss
for(SyndEntry entry:feed.getEntries()) {
System.out.println(entry.getTitle()+" "+entry.getLink());
URL tempUrl=new URL(entry.getLink());
//HttpURLConnection conn2 = (HttpURLConnection)tempUrl.openConnection(proxy);
HttpURLConnection conn2 = (HttpURLConnection)tempUrl.openConnection();
InputStream in2 = conn2.getInputStream();
//Per ogni articolo ottengo il testo principale utilizzando Boilerpipe
String text=ArticleExtractor.INSTANCE.getText(new InputStreamReader(in2));
Chunk chunk = new Chunk(entry.getTitle(), chapterFont);
Chapter chapter = new Chapter(new Paragraph(chunk), 1);
chapter.setNumberDepth(0);
chapter.add(new Paragraph(text, paragraphFont));
document.add(chapter);
break;
//Aggiungo l'articolo al PDF
document.add(new Paragraph(entry.getTitle(), chapterFont));
document.add(new Paragraph(text, paragraphFont));
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
//document.add(chapter);
}
document.close();
//Collegamento ad S3
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withRegion("eu-west-3")
.build();
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(bais.available());
s3.putObject("giornali","prova.pdf",bais,meta);
//Salvataggio del pdf su S3
s3.putObject("giornali","giornale-"+System.currentTimeMillis()+".pdf",bais,meta);
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("ERROR: "+ex.getMessage());
}
return "YEAH";
return "Giornale consegnato";
}
}

0 comments on commit 75ca26b

Please sign in to comment.