init repo
This commit is contained in:
commit
2da318ef44
19
.gitignore
vendored
Normal file
19
.gitignore
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Intellij
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# Mac
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Maven
|
||||||
|
log/
|
||||||
|
target/
|
||||||
|
|
||||||
|
|
||||||
|
data.db
|
50
pom.xml
Normal file
50
pom.xml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>de.gurkengewuerz</groupId>
|
||||||
|
<artifactId>postfix-rest-send</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.simplejavamail</groupId>
|
||||||
|
<artifactId>simple-java-mail</artifactId>
|
||||||
|
<version>4.2.3</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jsoup</groupId>
|
||||||
|
<artifactId>jsoup</artifactId>
|
||||||
|
<version>1.10.3</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>1.10</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.nanohttpd</groupId>
|
||||||
|
<artifactId>nanohttpd</artifactId>
|
||||||
|
<version>2.2.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20170516</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
104
src/main/java/de/gurkengewuerz/postfix_rest_send/Main.java
Normal file
104
src/main/java/de/gurkengewuerz/postfix_rest_send/Main.java
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package de.gurkengewuerz.postfix_rest_send;
|
||||||
|
|
||||||
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by gurkengewuerz.de on 12.07.2017.
|
||||||
|
*/
|
||||||
|
public class Main extends NanoHTTPD {
|
||||||
|
public Main() throws IOException {
|
||||||
|
super(8081);
|
||||||
|
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://www.simplejavamail.org/#/debugging
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Mailer m = new Mailer(new ServerConfig("localhost", 25));
|
||||||
|
// m.setDebug(true);
|
||||||
|
//
|
||||||
|
// Email email = new Email();
|
||||||
|
// email.setFromAddress("admin@gurkengewuerz.de", "admin@gurkengewuerz.de");
|
||||||
|
// email.setReplyToAddress("support@gurkengewuerz.de", "support@gurkengewuerz.de");
|
||||||
|
// email.addRecipient("developer@the-town.net", "developer@the-town.net", Message.RecipientType.TO);
|
||||||
|
// email.setSubject("Email Test");
|
||||||
|
// email.setText("Dies ist ein Email Test von Java.\nHoffentlich bald mit Rest API");
|
||||||
|
// email.setTextHTML("<img src='cid:wink1'><b>We should meet up!</b><img src='cid:wink2'>");
|
||||||
|
//
|
||||||
|
// m.sendMail(email);
|
||||||
|
|
||||||
|
// HtmlToPlainText formatter = new HtmlToPlainText();
|
||||||
|
// formatter.getPlainText()
|
||||||
|
try {
|
||||||
|
System.out.println("Started");
|
||||||
|
new Main();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response serve(IHTTPSession session) {
|
||||||
|
Map<String, String> parms = session.getParms();
|
||||||
|
|
||||||
|
JSONObject json = new JSONObject("{'error':'not found'}");
|
||||||
|
Response.Status status = Response.Status.NOT_FOUND;
|
||||||
|
|
||||||
|
if (session.getUri().startsWith("/authorize")) {
|
||||||
|
if (session.getUri().startsWith("/authorize/add")) {
|
||||||
|
String username = parms.get("username");
|
||||||
|
String password = parms.get("password");
|
||||||
|
|
||||||
|
if (username != null && password != null) {
|
||||||
|
System.out.println(checkPassword("/var/www/html/tools/functions.inc.php", "md5", "EMailFam.Schuetrumpf@123", "460b7d128333a4be972d4c7bdb0ee142"));
|
||||||
|
|
||||||
|
|
||||||
|
status = Response.Status.OK;
|
||||||
|
json = new JSONObject("{'token':''}");
|
||||||
|
} else {
|
||||||
|
json = new JSONObject("{'error':'username/password is null'}");
|
||||||
|
}
|
||||||
|
} else if (session.getUri().startsWith("/authorize/revoke")) {
|
||||||
|
|
||||||
|
}
|
||||||
|
} else if (session.getUri().startsWith("/validate/address")) {
|
||||||
|
|
||||||
|
} else if (session.getUri().startsWith("/send")) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return newFixedLengthResponse(status, "application/json", json.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkPassword(String path, String encryption, String plain, String dbPW) {
|
||||||
|
String crypted = execPHP("-r '$CONF = array(); $CONF[\"encrypt\"] = \"" + encryption + "\"; include \"" + path + "\"; echo(pacrypt(\"" + plain + "\", \"" + dbPW + "\")).\"\\n\";'");
|
||||||
|
System.out.println(crypted);
|
||||||
|
return crypted.equals(dbPW);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String execPHP(String args) {
|
||||||
|
try {
|
||||||
|
System.out.println(args);
|
||||||
|
String line;
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
Process p = Runtime.getRuntime().exec("php " + args);
|
||||||
|
BufferedReader input =
|
||||||
|
new BufferedReader
|
||||||
|
(new InputStreamReader(p.getInputStream()));
|
||||||
|
while ((line = input.readLine()) != null) {
|
||||||
|
output.append(line);
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
input.close();
|
||||||
|
return output.toString();
|
||||||
|
} catch (Exception err) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,137 @@
|
|||||||
|
package de.gurkengewuerz.postfix_rest_send.utils;
|
||||||
|
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.helper.StringUtil;
|
||||||
|
import org.jsoup.helper.Validate;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.nodes.Node;
|
||||||
|
import org.jsoup.nodes.TextNode;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
|
import org.jsoup.select.NodeTraversor;
|
||||||
|
import org.jsoup.select.NodeVisitor;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTML to plain-text. This example program demonstrates the use of jsoup to convert HTML input to lightly-formatted
|
||||||
|
* plain-text. That is divergent from the general goal of jsoup's .text() methods, which is to get clean data from a
|
||||||
|
* scrape.
|
||||||
|
* <p>
|
||||||
|
* Note that this is a fairly simplistic formatter -- for real world use you'll want to embrace and extend.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* To invoke from the command line, assuming you've downloaded the jsoup jar to your current directory:</p>
|
||||||
|
* <p><code>java -cp jsoup.jar org.jsoup.examples.HtmlToPlainText url [selector]</code></p>
|
||||||
|
* where <i>url</i> is the URL to fetch, and <i>selector</i> is an optional CSS selector.
|
||||||
|
*
|
||||||
|
* @author Jonathan Hedley, jonathan@hedley.net
|
||||||
|
*/
|
||||||
|
public class HtmlToPlainText {
|
||||||
|
private static final String userAgent = "Mozilla/5.0 (jsoup)";
|
||||||
|
private static final int timeout = 5 * 1000;
|
||||||
|
|
||||||
|
public static void main(String... args) throws IOException {
|
||||||
|
Validate.isTrue(args.length == 1 || args.length == 2, "usage: java -cp jsoup.jar org.jsoup.examples.HtmlToPlainText url [selector]");
|
||||||
|
final String url = args[0];
|
||||||
|
final String selector = args.length == 2 ? args[1] : null;
|
||||||
|
|
||||||
|
// fetch the specified URL and parse to a HTML DOM
|
||||||
|
Document doc = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).get();
|
||||||
|
|
||||||
|
HtmlToPlainText formatter = new HtmlToPlainText();
|
||||||
|
|
||||||
|
if (selector != null) {
|
||||||
|
Elements elements = doc.select(selector); // get each element that matches the CSS selector
|
||||||
|
for (Element element : elements) {
|
||||||
|
String plainText = formatter.getPlainText(element); // format that element to plain text
|
||||||
|
System.out.println(plainText);
|
||||||
|
}
|
||||||
|
} else { // format the whole doc
|
||||||
|
String plainText = formatter.getPlainText(doc);
|
||||||
|
System.out.println(plainText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format an Element to plain-text
|
||||||
|
*
|
||||||
|
* @param element the root element to format
|
||||||
|
* @return formatted text
|
||||||
|
*/
|
||||||
|
public String getPlainText(Element element) {
|
||||||
|
FormattingVisitor formatter = new FormattingVisitor();
|
||||||
|
NodeTraversor traversor = new NodeTraversor(formatter);
|
||||||
|
traversor.traverse(element); // walk the DOM, and call .head() and .tail() for each node
|
||||||
|
|
||||||
|
return formatter.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlainText(String rawHtml) {
|
||||||
|
Document doc = Jsoup.parse(rawHtml);
|
||||||
|
return getPlainText(doc);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the formatting rules, implemented in a breadth-first DOM traverse
|
||||||
|
private class FormattingVisitor implements NodeVisitor {
|
||||||
|
private static final int maxWidth = 80;
|
||||||
|
private int width = 0;
|
||||||
|
private StringBuilder accum = new StringBuilder(); // holds the accumulated text
|
||||||
|
|
||||||
|
// hit when the node is first seen
|
||||||
|
public void head(Node node, int depth) {
|
||||||
|
String name = node.nodeName();
|
||||||
|
if (node instanceof TextNode)
|
||||||
|
append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
|
||||||
|
else if (name.equals("li"))
|
||||||
|
append("\n * ");
|
||||||
|
else if (name.equals("dt"))
|
||||||
|
append(" ");
|
||||||
|
else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr"))
|
||||||
|
append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// hit when all of the node's children (if any) have been visited
|
||||||
|
public void tail(Node node, int depth) {
|
||||||
|
String name = node.nodeName();
|
||||||
|
if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5"))
|
||||||
|
append("\n");
|
||||||
|
else if (name.equals("a"))
|
||||||
|
append(String.format(" <%s>", node.absUrl("href")));
|
||||||
|
}
|
||||||
|
|
||||||
|
// appends text to the string builder with a simple word wrap method
|
||||||
|
private void append(String text) {
|
||||||
|
if (text.startsWith("\n"))
|
||||||
|
width = 0; // reset counter if starts with a newline. only from formats above, not in natural text
|
||||||
|
if (text.equals(" ") &&
|
||||||
|
(accum.length() == 0 || StringUtil.in(accum.substring(accum.length() - 1), " ", "\n")))
|
||||||
|
return; // don't accumulate long runs of empty spaces
|
||||||
|
|
||||||
|
if (text.length() + width > maxWidth) { // won't fit, needs to wrap
|
||||||
|
String words[] = text.split("\\s+");
|
||||||
|
for (int i = 0; i < words.length; i++) {
|
||||||
|
String word = words[i];
|
||||||
|
boolean last = i == words.length - 1;
|
||||||
|
if (!last) // insert a space if not the last word
|
||||||
|
word = word + " ";
|
||||||
|
if (word.length() + width > maxWidth) { // wrap and reset counter
|
||||||
|
accum.append("\n").append(word);
|
||||||
|
width = word.length();
|
||||||
|
} else {
|
||||||
|
accum.append(word);
|
||||||
|
width += word.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else { // fits as is, without need to wrap text
|
||||||
|
accum.append(text);
|
||||||
|
width += text.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return accum.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user