summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/App.java
blob: 688ba81f0c50cbe62de02cbcd487f56e291651b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import gui.GuiManager;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

import config.Config;
import config.ConfigProxy;

public class App {

	// Logger
	private final static Logger LOGGER = Logger.getLogger(App.class);

	private static void setupLogger() {

		// path to the log file
		final String logFilePath = Config.getPath() + System.getProperty("file.separator") + "bwSuite.log";

		// check if we had an old log file
		final File logFile = new File(logFilePath);
		if (logFile.exists() && !logFile.isDirectory()) {
			// we have one, rename it to 'bwSuite.log.old'
			LOGGER.info("renaming old log file");
			try {
				File oldFile = new File(logFilePath + ".old");
				FileUtils.forceDelete(oldFile);
				logFile.renameTo(oldFile);
				FileUtils.forceDelete(logFile);
			} catch (Exception e) {
				LOGGER.error("Could not move '" + logFilePath + "' to '" + logFilePath + ".old'", e);
			}
		}

		// add file appender to global logger
		FileAppender fa = null;
		try {
			fa = new FileAppender(new PatternLayout("%d [%F:%M] %m%n"), logFilePath);
			// All classes should log to file, configure global file appender.
		} catch (IOException e) {
			e.printStackTrace();
			BasicConfigurator.configure();
			return;
		}

		final FileAppender ffa = fa;
		final Pattern re = Pattern.compile("authorization:(\\w|\\+|/|\\s)+", Pattern.CASE_INSENSITIVE
				| Pattern.MULTILINE);

		AppenderSkeleton ap = new AppenderSkeleton() {

			@Override
			public boolean requiresLayout() {
				return ffa.requiresLayout();
			}

			@Override
			public void close() {
				ffa.close();
			}

			@Override
			protected void append(LoggingEvent event) {
				String s = event.getRenderedMessage();
				if (s.contains("uthorization")) {
					Matcher m = re.matcher(s);
					if (!m.find()) {
						LOGGER.warn("Could not match pattern!");
					} else {
						s = m.replaceAll("Authorization: ***********");
						LOGGER.info("Patched log message");
					}
				}
				ffa.append(new LoggingEvent(event.getFQNOfLoggerClass(), event.getLogger(),
						event.getTimeStamp(), event.getLevel(), s, event.getThreadName(),
						event.getThrowableInformation(), event.getNDC(), event.getLocationInformation(),
						event.getProperties()));
			}
		};

		BasicConfigurator.configure(ap);
		LOGGER.info("Starting logging to: " + logFilePath);
	}

	public static void main(final String[] args) {

		// Pruefe und Erzeuge gegebenfalls Config
		try {
			Config.init();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

		setupLogger();

		// initialise the proxy settings
		try {
			ConfigProxy.init();
		} catch (IOException e) {
			LOGGER.error("IOException when trying to initialise the proxy, see trace: ", e);
		}

		// start the GUI
		GuiManager.initGui();

	}
}