first commit
This commit is contained in:
46
util/src/main/java/de/muehlencord/shared/util/OSUtil.java
Normal file
46
util/src/main/java/de/muehlencord/shared/util/OSUtil.java
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* OSUtil.java
|
||||
*
|
||||
* Created on 10. Dezember 2007, 16:50
|
||||
*
|
||||
*/
|
||||
package de.muehlencord.shared.util;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public abstract class OSUtil {
|
||||
|
||||
/** OS unknown */
|
||||
public final static int OS_UNKNOWN = 0;
|
||||
/** OS Linux */
|
||||
public final static int OS_WINDOWS = 1;
|
||||
/** OS Linux */
|
||||
public final static int OS_LINUX = 2;
|
||||
/** OS MacOS X */
|
||||
public final static int OS_MACOS = 3;
|
||||
|
||||
/**
|
||||
* returns the operation system the current JVM is running on
|
||||
*
|
||||
* @return an OS_xxxx constants identifiying the operating system the current JVM is running on;
|
||||
*/
|
||||
public static int getOperationSystem() {
|
||||
String osName = System.getProperties().getProperty("os.name");
|
||||
if (osName != null) {
|
||||
osName = osName.toUpperCase();
|
||||
if (osName.contains("WINDOWS")) {
|
||||
return OS_WINDOWS;
|
||||
} else if (osName.contains("LINUX")) {
|
||||
return OS_LINUX;
|
||||
} else if (osName.contains("MAC")) {
|
||||
return OS_MACOS;
|
||||
} else {
|
||||
return OS_UNKNOWN;
|
||||
}
|
||||
} else {
|
||||
return OS_UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.util;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class StringEncodingException extends Exception {
|
||||
|
||||
/**
|
||||
* Creates a new instance of
|
||||
* <code>StringEncodingException</code> without detail message.
|
||||
*/
|
||||
public StringEncodingException() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>StringEncodingException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
*/
|
||||
public StringEncodingException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>StringEncodingException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
* @param th the causing exception
|
||||
*/
|
||||
public StringEncodingException(String msg, Throwable th) {
|
||||
super(msg, th);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* StringUtil.java
|
||||
*
|
||||
* Created on 4. Mai 2007, 12:36
|
||||
*/
|
||||
package de.muehlencord.shared.util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public abstract class StringUtil {
|
||||
|
||||
/** the logging object */
|
||||
private static final Logger logger = Logger.getLogger(StringUtil.class);
|
||||
|
||||
/**
|
||||
* returns the given string in ISO-8859-1 encoding
|
||||
*
|
||||
* @param input the input string in UTF-8
|
||||
* @return the converted string in
|
||||
*
|
||||
* @throws StringEncodingException if the string cannot be converted
|
||||
*/
|
||||
public static String getISOString(String input) throws StringEncodingException {
|
||||
try {
|
||||
byte[] b = input.getBytes("UTF-8");
|
||||
return new String(b, "ISO-8859-1");
|
||||
} catch (Exception ex) {
|
||||
logger.debug(getStackTraceString(ex));
|
||||
throw new StringEncodingException("Cannot convert string from UTF-8 to ISO-8859-1. Reason: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the stackstrace as one string
|
||||
*
|
||||
* @param ex the exception to return the string for
|
||||
* @return the stackstrace as one string
|
||||
*/
|
||||
public static String getStackTraceString(Throwable ex) {
|
||||
String logString = ex.toString() + "\n";
|
||||
StackTraceElement[] stack = ex.getStackTrace();
|
||||
for (int i = 0; i < stack.length; i++) {
|
||||
logString += (stack[i].toString()) + "\n";
|
||||
}
|
||||
return logString;
|
||||
}
|
||||
|
||||
/**
|
||||
* fills s with blanks if s < length
|
||||
*
|
||||
*
|
||||
*
|
||||
|
||||
*
|
||||
* @param length the needed length for this field
|
||||
* @param s the field to extend with blanks
|
||||
* @return s extended by trainling blanks. s.length == length
|
||||
*/
|
||||
public static String getBlankedString(int length, String s) {
|
||||
if (s == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
String returnValue = s;
|
||||
while (s.length() < length) {
|
||||
returnValue += " ";
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public static String getValueBetweenKeywords(String content, String keyWord1, String keyWord2) throws ParseException {
|
||||
int pos1 = content.indexOf(keyWord1);
|
||||
if (pos1 == -1) {
|
||||
throw new ParseException("Keyword1=" + keyWord1 + " not found in content string",0);
|
||||
}
|
||||
|
||||
int pos2 = content.indexOf(keyWord2, pos1);
|
||||
if (pos2 == -1) {
|
||||
throw new ParseException("Keyword2=" + keyWord2 + " not found in content string",0);
|
||||
}
|
||||
String returnValue = content.substring(pos1+keyWord1.length(), pos2);
|
||||
returnValue = returnValue.trim();
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package de.muehlencord.shared.util.file;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class FileHandlingException extends Exception implements Serializable {
|
||||
|
||||
public FileHandlingException (String hint) {
|
||||
super(hint);
|
||||
}
|
||||
|
||||
public FileHandlingException (String hint, Throwable th) {
|
||||
super (hint, th);
|
||||
}
|
||||
|
||||
}
|
||||
157
util/src/main/java/de/muehlencord/shared/util/file/FileUtil.java
Normal file
157
util/src/main/java/de/muehlencord/shared/util/file/FileUtil.java
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.util.file;
|
||||
|
||||
import de.muehlencord.shared.util.StringUtil;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.Format;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public abstract class FileUtil {
|
||||
|
||||
/** the logging object */
|
||||
private static final Logger logger = Logger.getLogger(FileUtil.class);
|
||||
|
||||
/**
|
||||
* returns a list of files found by the given regexp in the given folder
|
||||
*
|
||||
* @param directory the folder to search in as a string
|
||||
* @param regEx the regular expression to search with
|
||||
* @return a list of files
|
||||
*
|
||||
* @throws FileHandlingException if the given directory cannot be found
|
||||
*/
|
||||
public static List<File> getFilesFromDirecotry(final String directory, final String regEx) throws FileHandlingException {
|
||||
List<File> returnValue = new LinkedList<File>();
|
||||
File dir = new File(directory);
|
||||
if (!dir.exists()) {
|
||||
throw new FileHandlingException("Directory " + directory + " does not exist");
|
||||
}
|
||||
if (!dir.isDirectory()) {
|
||||
throw new FileHandlingException(directory + " is not a directory");
|
||||
}
|
||||
|
||||
FileFilter ff = new FileFilter() {
|
||||
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
boolean isFile = pathname.isFile();
|
||||
boolean match = pathname.toString().matches(regEx);
|
||||
return isFile && match;
|
||||
}
|
||||
};
|
||||
File[] files = dir.listFiles(ff);
|
||||
if (files != null) {
|
||||
returnValue.addAll(Arrays.asList(files));
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
} // method getFilesFromDirecotry
|
||||
|
||||
/**
|
||||
* returns a file handle to a file in the given folder using the given formater and the given number.
|
||||
*
|
||||
* @param outputFolder the folder to link the file to as strnig
|
||||
* @param ft a format object to use to format the filename of the given file
|
||||
* @param number a number used to call the given formater to name the file number
|
||||
* @return a file handle build from formater and number linked to given directory
|
||||
*/
|
||||
public static File getFileName(final String outputFolder, final Format ft, final int number) {
|
||||
String folderName = outputFolder;
|
||||
String fileName = ft.format(number);
|
||||
if (!outputFolder.endsWith(File.separator)) {
|
||||
folderName += File.separator;
|
||||
}
|
||||
fileName = folderName + fileName;
|
||||
return new File(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* copies the givn source file to the given destination filename
|
||||
*
|
||||
* @param source the file to copy from
|
||||
* @param destination the destination filename to copy the source file to
|
||||
* @return true, if the vile was copied, false, else
|
||||
*/
|
||||
public static boolean copyFileTo(File source, File destination) {
|
||||
FileInputStream fis = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fis = new FileInputStream(source);
|
||||
fos = new FileOutputStream(destination);
|
||||
|
||||
byte[] buf = new byte[4096];
|
||||
int loaded;
|
||||
while ((loaded = fis.read(buf)) > 0) {
|
||||
fos.write(buf, 0, loaded);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error(ex.getMessage());
|
||||
logger.debug(StringUtil.getStackTraceString(ex));
|
||||
}
|
||||
}
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error(ex.getMessage());
|
||||
logger.debug(StringUtil.getStackTraceString(ex));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* moves / renames the given file to the new file
|
||||
*
|
||||
* @param source the source file
|
||||
* @param destination the file to rename the source file to
|
||||
* @return true, if move / rename was successfull; false else
|
||||
*/
|
||||
public static boolean moveFileTo(File source, File destination) {
|
||||
boolean isRenamed;
|
||||
if (doesPathExists(destination.getAbsolutePath())) {
|
||||
destination.delete();
|
||||
}
|
||||
|
||||
isRenamed = source.renameTo(destination);
|
||||
if (!isRenamed) {
|
||||
copyFileTo(source, destination);
|
||||
source.delete();
|
||||
}
|
||||
return isRenamed;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true, if the givn path exists; false otherwise
|
||||
*
|
||||
* @param path the path to check
|
||||
* @return true, if the givn path exists; false otherwise
|
||||
*/
|
||||
public static boolean doesPathExists(String path) {
|
||||
File location = new File(path);
|
||||
return location.exists();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package de.muehlencord.shared.util;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class DefaultTest {
|
||||
|
||||
public String readContentFromFile(String name) throws IOException {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
byte[] buffer = new byte[4096];
|
||||
InputStream is = DefaultTest.class.getResourceAsStream(name);
|
||||
if (is == null) {
|
||||
throw new IOException("File " + name + " not found");
|
||||
}
|
||||
BufferedInputStream bis = new BufferedInputStream(is);
|
||||
|
||||
int bytesRead = 0;
|
||||
while ((bytesRead = bis.read(buffer)) != -1) {
|
||||
sb.append(new String(buffer, 0, bytesRead));
|
||||
}
|
||||
bis.close();
|
||||
is.close();
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
|
||||
package de.muehlencord.shared.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Basic StringUtilTests
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class StringUtilTest extends DefaultTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetValueBetweenKeywords() throws ParseException, IOException {
|
||||
String content = readContentFromFile("/test.txt");
|
||||
|
||||
String ipAddress = StringUtil.getValueBetweenKeywords(content, "The IP", "has just");
|
||||
assertEquals ("ipAddress", "222.184.230.118", ipAddress);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.util.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class FileUtilTest {
|
||||
|
||||
/**
|
||||
* tests the function getFilesFromDirectory
|
||||
* @throws FileHandlingException if the files could not be listed
|
||||
* @throws URISyntaxException if the testfile specification is wrong
|
||||
*/
|
||||
@Test
|
||||
public void testGetFilesFromDirecotry() throws FileHandlingException, URISyntaxException {
|
||||
System.out.println("testGetFilesFromDirectory");
|
||||
|
||||
|
||||
URL url = getClass().getResource("/testfile.txt");
|
||||
File testFile = new File(url.getFile());
|
||||
|
||||
List<File> fileList = FileUtil.getFilesFromDirecotry(testFile.getParent(), ".*.java");
|
||||
Iterator<File> it = fileList.iterator();
|
||||
while (it.hasNext()) {
|
||||
System.out.println(it.next());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
78
util/src/test/resources/test.txt
Normal file
78
util/src/test/resources/test.txt
Normal file
@ -0,0 +1,78 @@
|
||||
Hi,
|
||||
|
||||
The IP 222.184.230.118 has just been banned by Fail2Ban after
|
||||
4 attempts against ssh.
|
||||
|
||||
|
||||
Here are more information about 222.184.230.118:
|
||||
|
||||
% [whois.apnic.net node-4]
|
||||
% Whois data copyright terms http://www.apnic.net/db/dbcopyright.html
|
||||
|
||||
inetnum: 222.184.0.0 - 222.191.255.255
|
||||
netname: CHINANET-JS
|
||||
descr: CHINANET jiangsu province network
|
||||
descr: China Telecom
|
||||
descr: A12,Xin-Jie-Kou-Wai Street
|
||||
descr: Beijing 100088
|
||||
country: CN
|
||||
admin-c: CH93-AP
|
||||
tech-c: CJ186-AP
|
||||
mnt-by: APNIC-HM
|
||||
mnt-lower: MAINT-CHINANET-JS
|
||||
mnt-routes: MAINT-CHINANET-JS
|
||||
remarks: This object can only modify by APNIC hostmaster
|
||||
remarks: If you wish to modify this object details please
|
||||
remarks: send email to hostmaster@apnic.net with your
|
||||
remarks: organisation account name in the subject line.
|
||||
changed: hm-changed@apnic.net 20040223
|
||||
status: ALLOCATED PORTABLE
|
||||
source: APNIC
|
||||
|
||||
role: CHINANET JIANGSU
|
||||
address: 260 Zhongyang Road,Nanjing 210037
|
||||
country: CN
|
||||
phone: +86-25-86588231
|
||||
phone: +86-25-86588745
|
||||
fax-no: +86-25-86588104
|
||||
e-mail: ip@jsinfo.net
|
||||
remarks: send anti-spam reports to spam@jsinfo.net
|
||||
remarks: send abuse reports to abuse@jsinfo.net
|
||||
remarks: times in GMT+8
|
||||
admin-c: CH360-AP
|
||||
tech-c: CS306-AP
|
||||
tech-c: CN142-AP
|
||||
nic-hdl: CJ186-AP
|
||||
remarks: www.jsinfo.net
|
||||
notify: ip@jsinfo.net
|
||||
mnt-by: MAINT-CHINANET-JS
|
||||
changed: dns@jsinfo.net 20090831
|
||||
changed: ip@jsinfo.net 20090831
|
||||
changed: hm-changed@apnic.net 20090901
|
||||
source: APNIC
|
||||
changed: hm-changed@apnic.net 20111114
|
||||
|
||||
person: Chinanet Hostmaster
|
||||
nic-hdl: CH93-AP
|
||||
e-mail: anti-spam@ns.chinanet.cn.net
|
||||
address: No.31 ,jingrong street,beijing
|
||||
address: 100032
|
||||
phone: +86-10-58501724
|
||||
fax-no: +86-10-58501724
|
||||
country: CN
|
||||
changed: dingsy@cndata.com 20070416
|
||||
mnt-by: MAINT-CHINANET
|
||||
source: APNIC
|
||||
|
||||
|
||||
Lines containing IP:222.184.230.118 in /var/log/auth.log
|
||||
|
||||
Jan 6 23:35:47 localhost sshd[31328]: Invalid user cgi from 222.184.230.118
|
||||
Jan 6 23:35:49 localhost sshd[31330]: Invalid user richie from 222.184.230.118
|
||||
Jan 6 23:35:51 localhost sshd[31332]: Invalid user shirsh from 222.184.230.118
|
||||
Jan 6 23:36:00 localhost sshd[31340]: Invalid user system from 222.184.230.118
|
||||
|
||||
|
||||
Regards,
|
||||
|
||||
Fail2Ban
|
||||
1
util/src/test/resources/testfile.txt
Normal file
1
util/src/test/resources/testfile.txt
Normal file
@ -0,0 +1 @@
|
||||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||
Reference in New Issue
Block a user