Permalink
Browse files

Commit iniziale

  • Loading branch information...
fpaparoni committed Jan 2, 2018
1 parent 406ba58 commit 218c508cd54207acf111b28c71b5c3cbea0d27e5
80 pom.xml
@@ -0,0 +1,80 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javastaff</groupId>
<artifactId>aws.lambda</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AWSLambda</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.256</version>
</dependency>
<dependency>
<groupId>de.l3s.boilerpipe</groupId>
<artifactId>boilerpipe</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,9 @@
package com.javastaff.aws.lambda.hello;
import com.amazonaws.services.lambda.runtime.Context;
public class CustomHelloHandler {
public String myHandler(HelloRequest request, Context context) {
return String.format("Hello World custom %s.", request.getUsername());
}
}
@@ -0,0 +1,13 @@
package com.javastaff.aws.lambda.hello;
public class HelloRequest {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
@@ -0,0 +1,13 @@
package com.javastaff.aws.lambda.hello;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class HelloWorldRequestHandler implements RequestHandler<HelloRequest, String> {
public String handleRequest(HelloRequest input, Context context) {
context.getLogger().log("Stringa input: " + input.getUsername());
return "Hello World " + input.getUsername();
}
}
@@ -0,0 +1,17 @@
package com.javastaff.aws.lambda.hello;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context;
public class HelloWorldRequestStreamHandler implements RequestStreamHandler{
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
int letter;
outputStream.write("Hello World stream ".getBytes());
while((letter = inputStream.read()) != -1){
outputStream.write(Character.toUpperCase(letter));
}
}
}
@@ -0,0 +1,18 @@
package com.javastaff.aws.lambda.schedule;
public class CloudWatchEvent {
private String id;
private String source;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
}
@@ -0,0 +1,14 @@
package com.javastaff.aws.lambda.schedule;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class ExampleScheduledRequestHandler implements RequestHandler<CloudWatchEvent, String>{
public String handleRequest(CloudWatchEvent event, Context context) {
String logString=String.format("Id %s source %s",event.getId(),event.getSource());
context.getLogger().log(logString);
return "Scheduled handler returns: " + logString;
}
}
@@ -0,0 +1,86 @@
package com.javastaff.aws.lambda.schedule;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
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;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
public class PrintServiceHandler implements RequestHandler<CloudWatchEvent, String>{
public String handleRequest(CloudWatchEvent arg0, Context arg1) {
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")));
//PDF
ByteArrayOutputStream baos=new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC);
Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
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();
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;
}
document.close();
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);
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("ERROR: "+ex.getMessage());
}
return "YEAH";
}
}

0 comments on commit 218c508

Please sign in to comment.