Skip to content

Commit

Permalink
Revert "DDF-4049 Fixes DefaultErrorHandler so that it generates corre…
Browse files Browse the repository at this point in the history
…ct, safe response. (#3578)"

This reverts commit 2705ea2
  • Loading branch information
stustison committed Aug 18, 2018
1 parent 39c77e4 commit 9b526cb
Show file tree
Hide file tree
Showing 16 changed files with 1,077 additions and 199 deletions.
3 changes: 3 additions & 0 deletions platform/error/platform-error-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Embed-Dependency>
commons-io
</Embed-Dependency>
<Export-Package />
<Import-Package>
!com.sun.management,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
*/
package org.codice.ddf.platform.error.handler.impl;

import static org.boon.Boon.toJson;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.codice.ddf.platform.error.handler.ErrorHandler;
import org.eclipse.jetty.util.ByteArrayISO8859Writer;
import org.osgi.framework.Bundle;
Expand All @@ -32,21 +33,26 @@

public class DefaultErrorHandler implements ErrorHandler {

private static final String SERVER_ERROR_PLEASE_SEE_LOGS = "Server Error, please see logs.";
public static final String SERVER_ERROR_PLEASE_SEE_LOGS = "Server Error, please see logs.";

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultErrorHandler.class);

private static final int BUFFER_SIZE = 4096;

private String errorHtml = SERVER_ERROR_PLEASE_SEE_LOGS;

private Properties codeMessageProperties = new Properties();
private String indexHtml = SERVER_ERROR_PLEASE_SEE_LOGS;

public DefaultErrorHandler() {
try (InputStream inputStream = readResource("/codeMessages.properties")) {
codeMessageProperties.load(inputStream);
} catch (IOException e) {
LOGGER.error("Unable to load codeMessages properties", e);
private void initIndexHtml() {
if (SERVER_ERROR_PLEASE_SEE_LOGS.equals(indexHtml)) {
Bundle bundle = FrameworkUtil.getBundle(DefaultErrorHandler.class);
if (null != bundle) {
try {
indexHtml = IOUtils.toString(bundle.getEntry("/index.html").openStream());
} catch (Exception e) {
LOGGER.debug("Unable to read/parse index.html.", e);
}
} else {
LOGGER.debug("Unable to retrieve Bundle");
}
}
}

Expand All @@ -59,48 +65,29 @@ public void handleError(
String uri,
HttpServletRequest request,
HttpServletResponse response) {
initErrorHtml();
initIndexHtml();

Map<String, String> attributesMap = new HashMap<>();
attributesMap.put("code", String.valueOf(code));
attributesMap.put("message", message);
attributesMap.put("uri", uri);
String stack = ExceptionUtils.getFullStackTrace(throwable);

response.setStatus(code);
response.setContentType("text/html");
Map<String, String> jsonMap = new HashMap<>();
jsonMap.put("code", String.valueOf(code));
jsonMap.put("message", message);
jsonMap.put("type", type);
jsonMap.put("throwable", stack);
jsonMap.put("uri", uri);
String data = toJson(jsonMap);
String encodedBytes = Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));

String output =
errorHtml.replace(
"{{codeMessage}}", codeMessageProperties.getProperty(String.valueOf(code)));
String localIndexHtml = indexHtml.replace("WINDOW_DATA", "\"" + encodedBytes + "\"");

response.setStatus(code);
response.setContentType("text/html");
try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(BUFFER_SIZE)) {
for (Entry<String, String> row : attributesMap.entrySet()) {
output = output.replace("{{" + row.getKey() + "}}", row.getValue());
}
writer.write(output);
writer.write(localIndexHtml);
writer.flush();
writer.writeTo(response.getOutputStream());
} catch (IOException e) {
LOGGER.debug("Unable to write error html data to client.");
}
}

private void initErrorHtml() {
if (SERVER_ERROR_PLEASE_SEE_LOGS.equals(errorHtml)) {
try (InputStream input = readResource("/error.html")) {
errorHtml = IOUtils.toString(input, StandardCharsets.UTF_8);
} catch (IOException e) {
LOGGER.debug("Unable to read/parse error.html.", e);
}
}
}

private InputStream readResource(String resourcePath) throws IOException {
Bundle bundle = FrameworkUtil.getBundle(DefaultErrorHandler.class);
if (null != bundle) {
return bundle.getEntry(resourcePath).openStream();
} else {
throw new IOException("Unable to retrieve bundle");
}
}
}

This file was deleted.

125 changes: 0 additions & 125 deletions platform/error/platform-error-impl/src/main/resources/error.html

This file was deleted.

91 changes: 91 additions & 0 deletions ui/packages/platform-error-impl/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (c) Codice Foundation
*
* This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either
* version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details. A copy of the GNU Lesser General Public License is distributed along with this program and can be found at
* <http://www.gnu.org/licenses/lgpl.html>.
*
**/
/*global module,require*/

module.exports = function (grunt) {

require('load-grunt-tasks')(grunt, {requireResolution: true});

grunt.initConfig({

pkg: grunt.file.readJSON('package.json'),

clean: {
build: ['target/webapp']
},
cssmin: {
compress: {
files: {
"target/webapp/css/index.css": ["src/main/webapp/css/*.css"]
}
}
},
jshint: {
files: ['Gruntfile.js', 'src/main/webapp/js/**/*.js', 'src/main/webapp/main.js', 'src/main/webapp/properties.js'],
options: {
bitwise: true, // Prohibits the use of bitwise operators such as ^ (XOR), | (OR) and others.
forin: true, // Requires all for in loops to filter object's items.
latedef: true, // Prohibits the use of a variable before it was defined.
newcap: true, // Requires you to capitalize names of constructor functions.
noarg: true, // Prohibits the use of arguments.caller and arguments.callee. Both .caller and .callee make quite a few optimizations impossible so they were deprecated in future versions of JavaScript.
noempty: true, // Warns when you have an empty block in your code.
regexp: true, // Prohibits the use of unsafe . in regular expressions.
undef: true, // Prohibits the use of explicitly undeclared variables.
unused: true, // Warns when you define and never use your variables.
maxlen: 250, // Set the maximum length of a line to 250 characters. If triggered, the line should be wrapped.
eqeqeq: true, // Prohibits the use of == and != in favor of === and !==

// Relaxing Options
scripturl: true, // This option suppresses warnings about the use of script-targeted URLs—such as
es5: true, // Tells JSHint that your code uses ECMAScript 5 specific features such as getters and setters.

// options here to override JSHint defaults
globals: {
console: true,
module: true
}
}
},
watch: {
jsFiles: {
files: ['<%= jshint.files %>'],
tasks: ['jshint']
},
livereload : {
options : {livereload :true},
files : ['target/webapp/css/index.css'
// this one is more dangerous, tends to reload the page if one file changes
// probably too annoying to be useful, uncomment if you want to try it out
// '<%= jshint.files %>'
]
},
cssFiles : {
files :['src/main/webapp/css/*.css'],
tasks : ['cssmin']
},
},
express: {
options: {
port: 8282,
hostname: '*'
},
server: {
options: {
script: './server.js'
}
}
}
});

grunt.registerTask('build', ['clean', 'cssmin', 'jshint']);
grunt.registerTask('default', ['build','express:server','watch']);
};
Loading

0 comments on commit 9b526cb

Please sign in to comment.