diff options
author | talasila <talasila@research.att.com> | 2017-02-08 10:13:29 -0500 |
---|---|---|
committer | talasila <talasila@research.att.com> | 2017-02-08 10:15:00 -0500 |
commit | f8a8d5192b1e5013d9e2f699be54b072ef39d5f0 (patch) | |
tree | b7a65bb0a7d70a9dc24c064113868e5f3b8e6a09 /dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util | |
parent | 72a80fbff7120630576ccd5aa67c20818c2943c7 (diff) |
Initial OpenECOMP UI/DMaapBC commit
Change-Id: Ia492e1b88311b9bed4c31f593b28deaaad73b7e4
Signed-off-by: talasila <talasila@research.att.com>
Diffstat (limited to 'dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util')
3 files changed, 305 insertions, 0 deletions
diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/CustomLoggingFilter.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/CustomLoggingFilter.java new file mode 100644 index 0000000..760460f --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/CustomLoggingFilter.java @@ -0,0 +1,54 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ +package org.openecomp.fusionapp.util; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.filter.Filter; +import ch.qos.logback.core.spi.FilterReply; + +/** + * Custom Filter class bind with logback.xml + * configuration file to strip out certain log messages + * coming out of special packages or classes. + * + */ +public class CustomLoggingFilter extends Filter<ILoggingEvent> { + + /** + * Custom Filter is added to strip out the continuous U-EB logging messages + * But make sure we log the ERROR & WARNING Level messages. + */ + @Override + public FilterReply decide(ILoggingEvent event) { + try { + if ((event.getLevel() != Level.ERROR || event.getLevel() != Level.WARN) && + (event.getThreadName().equalsIgnoreCase("UEBConsumerThread")) && + (event.getLoggerName().contains("com.att.nsa") || event.getLoggerName().contains("org.apache.http")) + ) { + return FilterReply.DENY; + } else { + return FilterReply.NEUTRAL; + } + } catch(Exception e) { + return FilterReply.NEUTRAL; + } + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/LoggerDemo.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/LoggerDemo.java new file mode 100644 index 0000000..9bdff16 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/LoggerDemo.java @@ -0,0 +1,48 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ +package org.openecomp.fusionapp.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.LoggerContext; + +public class LoggerDemo { + + private static final Logger log = LoggerFactory.getLogger(LoggerDemo.class); + + public static void main(String[] args) { + new LoggerDemo().new Parent().hello(); + new LoggerDemo().new Child().hello(); + // Flush messages before shutting down async logger + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + loggerContext.stop(); + } + + public class Parent { + public void hello() { + log.info("Hello from " + getClass().getSimpleName()); + } + } + + public class Child extends Parent { + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/ValidateEncodingApp.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/ValidateEncodingApp.java new file mode 100644 index 0000000..cd327b2 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/ValidateEncodingApp.java @@ -0,0 +1,203 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ +package org.openecomp.fusionapp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; + +/** + * Reads bytes from file using a decoder that throws an exception if the bytes + * cannot be decoded as UTF-8. Can be used to validate a single file or a + * directory of files with full recursion. Includes a feature to create a file + * with a non-UTF-8 byte sequence for testing. + */ +public class ValidateEncodingApp { + + private final String charsetName; + private final CharsetDecoder decoder; + private int filesPassed = 0, filesFailed = 0; + + /** + * Obtains a decoder and configures it to blow up on problems. + * + * @param charsetName + * Name of character set to use; e.g., UTF-8 + */ + public ValidateEncodingApp(String charsetName) { + this.charsetName = charsetName; + Charset charset = Charset.forName(charsetName); + decoder = charset.newDecoder(); + decoder.onMalformedInput(CodingErrorAction.REPORT); + decoder.onUnmappableCharacter(CodingErrorAction.REPORT); + } + + /** + * @return The character set name supplied to the constructor + */ + public String getCharsetName() { + return charsetName; + } + + /** + * @return The number of files that could not be validated for whatever + * reason. + */ + public int getFilesFailed() { + return filesFailed; + } + + /** + * @return The number of files successfully validated. + */ + public int getFilesPassed() { + return filesPassed; + } + + /** + * Reads and decodes bytes from the specified file, which will find byte + * sequences that cannot be decoded using the current system. Traps all + * exceptions and reports to System.err. + * + * @param file + * @return True on success, false on IOException or decoding error + */ + private boolean validateFile(File file) { + boolean result = true; + int lineNr = 1; + BufferedReader br = null; + try { + br = new BufferedReader(new InputStreamReader(new FileInputStream(file), decoder)); + while (br.readLine() != null) + ++lineNr; + ++filesPassed; + } catch (CharacterCodingException ex) { + ++filesFailed; + System.err.println("Failed at line " + lineNr + ", file " + file.getPath() + ": " + ex.toString()); + result = false; + } catch (IOException ex) { + ++filesFailed; + System.err.println("Failed to read file " + file.getPath() + ": " + ex.toString()); + result = false; + } finally { + if (br != null) + try { + br.close(); + } catch (IOException ex) { + System.err.println("Failed to close file: " + ex.toString()); + } + } + return result; + } + + /** + * Validates all files in the specified directory. Optionally recurses into + * subdirectories. Prints message to stdout when it starts and again when it + * finishes traversing a directory. + * + * @param dir + * Directory to traverse. + * @param isRecurse + * If true, will call itself to process subdirectories. + * @throws Exception + */ + private void validateDirectory(File dir, boolean isRecurse) throws Exception { + if (!dir.exists() || !dir.isDirectory()) + throw new IllegalArgumentException("Not found or not a directory: " + dir.getPath()); + System.out.println("Entering directory " + dir.getPath()); + File[] contents = dir.listFiles(); + for (File f : contents) { + if (f.isFile()) + validateFile(f); + else if (f.isDirectory() && isRecurse) + validateDirectory(f, isRecurse); + } + System.out.println("Leaving directory " + dir.getPath()); + } + + /** + * Prints message(s) and exits + * + * @param msg + */ + private static void usage(String msg) { + if (msg != null) + System.err.println(msg); + System.err.println("Usage: ValidateEncodingApp -validate (file|dir)"); + System.err.println(" ValidateEncodingApp -create filename"); + System.exit(0); + } + + public static void main(String[] args) throws Exception { + + // TODO: extend to accept encoding system name + // and recurse option as arguments. + String charsetName = "UTF-8"; + boolean recurse = true; + + if (args.length != 2) + usage("Unexpected number of arguments"); + + String cmd = args[0]; + File file = new File(args[1]); + + if ("-create".equals(cmd)) { + if (file.exists()) + usage("Will not overwrite existing file: " + file.getPath()); + System.out.println("Creating file with ISO-8859-1 content: " + file.getPath()); + try { + // The word "dolt" has an o with a diagonal slash thru it. + // The slash-o is valid in ISO-8859-1 but not in UTF-8. + final byte[] invalid = "d\u00f8lt\n".getBytes("iso-8859-1"); + FileOutputStream fis = new FileOutputStream(file); + fis.write(invalid); + fis.close(); + } catch (IOException ex) { + System.err.println("Failed to write file: " + ex.toString()); + } + } else if ("-validate".equals(cmd)) { + if (!file.exists()) + usage("Not found: " + file.getPath()); + + ValidateEncodingApp validator = new ValidateEncodingApp(charsetName); + if (file.isFile()) { + System.out.println("Validating decoding of bytes as " + charsetName + " for file " + file.getPath()); + validator.validateFile(file); + } else { + System.out.println("Validating decoding of bytes as " + charsetName + " for files in/below directory " + + file.getPath()); + validator.validateDirectory(file, recurse); + } + System.out.println("Validation success count is " + validator.getFilesPassed()); + System.out.println("Validation failure count is " + validator.getFilesFailed()); + } else { + usage("Unexpected command: " + cmd); + } + + } + +} |