diff --git a/src/main/java/de/dhbw/text2process/config/PropertiesWithJavaConfig.java b/src/main/java/de/dhbw/text2process/config/PropertiesWithJavaConfig.java index 2c897e48..723f0d9b 100644 --- a/src/main/java/de/dhbw/text2process/config/PropertiesWithJavaConfig.java +++ b/src/main/java/de/dhbw/text2process/config/PropertiesWithJavaConfig.java @@ -4,7 +4,7 @@ import org.springframework.context.annotation.PropertySource; @Configuration -@PropertySource("classpath:application.properties") +@PropertySource({"classpath:application.properties", "classpath:appParameter.properties"}) public class PropertiesWithJavaConfig { public static String stanfordHost = "https://woped.dhbw-karlsruhe.de"; diff --git a/src/main/java/de/dhbw/text2process/controller/T2PController.java b/src/main/java/de/dhbw/text2process/controller/T2PController.java index 606911f7..39d52a19 100644 --- a/src/main/java/de/dhbw/text2process/controller/T2PController.java +++ b/src/main/java/de/dhbw/text2process/controller/T2PController.java @@ -89,6 +89,7 @@ public File generateBPMNFileFromText( "The given parameter ist not a valid one. Please check the String and pass a correct" + " one. More details on the error is stored in the response json"); logger.error(e.getMessage()); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); bpmnResponse = new Response(true, e.getCause(), e.getMessage(), e.getStackTrace()); } catch (InvalidInputException e) { @@ -285,11 +286,21 @@ public String generatePetriNetFromText( @RequestBody String param, HttpServletRequest request, HttpServletResponse response) { Response pnmlResponse; + Response.ErrorCodeHolder responseCode = new Response.ErrorCodeHolder(); + responseCode.code = Response.ErrorCodes.NOEXCEPTION; logger.info("Trying to generate a PetriNet with the given String parameter"); try { + logger.info("Validating the String information, scanning for incompatible characters"); - t2PControllerHelper.checkInputValidity(param); + //MW, AL: NEU - Response mit detalierter Errorausgabe für Front-End + String validationResponse = t2PControllerHelper.checkInputValidity(param, responseCode); + if(!validationResponse.isEmpty()) { + response.setStatus(Response.getErrorCodeFromEnum(responseCode.code)); + //response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + return validationResponse; + } + logger.info("Starting generating PNML-String ..."); pnmlResponse = new Response(t2PControllerHelper.generatePetrinetFromText(param)); logger.info("Finished generating PNML-String"); @@ -322,4 +333,4 @@ public String generatePetriNetFromText( logger.info("Returning the pnmlString"); return pnmlResponse.getResponse(); } -} +} \ No newline at end of file diff --git a/src/main/java/de/dhbw/text2process/helper/appParameterHelper.java b/src/main/java/de/dhbw/text2process/helper/appParameterHelper.java new file mode 100644 index 00000000..dbf425f6 --- /dev/null +++ b/src/main/java/de/dhbw/text2process/helper/appParameterHelper.java @@ -0,0 +1,27 @@ +package de.dhbw.text2process.helper; + +import java.io.*; +import java.util.Properties; + +public class appParameterHelper { + + public static Properties GetConfigFile(){ + + try (InputStream input = new FileInputStream("src/main/resources/appParameter.properties")) { + + Properties prop = new Properties(); + + // load a properties file + prop.load(input); + + System.out.println(prop); + + return prop; + + } catch (IOException io) { + //io.printStackTrace(); + return null; + } + } + +} diff --git a/src/main/java/de/dhbw/text2process/helper/rest/Response.java b/src/main/java/de/dhbw/text2process/helper/rest/Response.java index d26a6e45..1971d25e 100644 --- a/src/main/java/de/dhbw/text2process/helper/rest/Response.java +++ b/src/main/java/de/dhbw/text2process/helper/rest/Response.java @@ -1,5 +1,6 @@ package de.dhbw.text2process.helper.rest; +import javax.servlet.http.HttpServletResponse; import java.util.Arrays; public class Response { @@ -10,6 +11,13 @@ public class Response { private String message; private StackTraceElement[] stackTrace; + public static final int NO_ERROR_CONTINUE = 100; + public static final int NO_ERROR = 200; + public static final int CONVERTION_ERROR = 450; + public static final int RPST_FAILURE = 451; + public static final int STRUCTURE_FAILURE = 452; + public static final int PARSING_ERROR = 453; + public Response(E response) { this.response = response; } @@ -23,6 +31,31 @@ public Response( this.stackTrace = stackTrace; } + public enum ErrorCodes{ + NOEXCEPTION, + INVALIDREQUEST, + INVALIDCHARACTER, + ALGORYTHMEXCEPTION, + SERVEREXCEPTION + } + + public static class ErrorCodeHolder{ + public ErrorCodes code; + } + + public static int getErrorCodeFromEnum(ErrorCodes code){ + switch(code){ + case NOEXCEPTION: + return NO_ERROR; + case INVALIDREQUEST: + return CONVERTION_ERROR; + case INVALIDCHARACTER: + return RPST_FAILURE; + default: + return NO_ERROR_CONTINUE; + } + } + public E getResponse() { return response; } diff --git a/src/main/java/de/dhbw/text2process/helper/rest/T2PControllerHelper.java b/src/main/java/de/dhbw/text2process/helper/rest/T2PControllerHelper.java index 00f6af59..629e04e4 100644 --- a/src/main/java/de/dhbw/text2process/helper/rest/T2PControllerHelper.java +++ b/src/main/java/de/dhbw/text2process/helper/rest/T2PControllerHelper.java @@ -18,13 +18,22 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.List; +import java.util.Properties; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import de.dhbw.text2process.helper.appParameterHelper; + + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + public class T2PControllerHelper { // Initialize log4j to log information into the console @@ -32,6 +41,8 @@ public class T2PControllerHelper { // Reject any Request larger than this public static final int MAX_INPUT_LENGTH = 15000; + public static final int MIN_INPUT_LENGTH = 10; + /** * @@ -196,24 +207,60 @@ private String readRequestBody(HttpServletRequest request) { * @param text * @throws InvalidInputException */ - public void checkInputValidity(String text) throws InvalidInputException { + public String checkInputValidity(String text) throws InvalidInputException { + Response.ErrorCodeHolder responseCode = new Response.ErrorCodeHolder(); + return checkInputValidity(text, responseCode); + } + + /** + * @param text + * @throws InvalidInputException + */ + public String checkInputValidity(String text, Response.ErrorCodeHolder code) throws InvalidInputException { + + Properties props = appParameterHelper.GetConfigFile(); + + Response pnmlResponse; + pnmlResponse = new Response(true, null, "", null); + + //if (text.length() > MAX_INPUT_LENGTH | text.equals("")) { + if (text.length() > Integer.parseInt(props.getProperty("request.maxTextLength")) | text.equals("")) { + pnmlResponse.setResponse("The input is too long."); - if (text.length() > MAX_INPUT_LENGTH | text.equals("")) { - throw new InvalidInputException("The input is too long."); + //ENUMparameter set for response status-code + code.code = Response.ErrorCodes.INVALIDREQUEST; + return pnmlResponse.getResponse(); + } + //if (text.length() < MIN_INPUT_LENGTH | text.equals("")) { + if (text.length() < Integer.parseInt(props.getProperty("request.minTextLength")) | text.equals("")) { + + pnmlResponse.setResponse("The input is too short."); + return pnmlResponse.getResponse(); } // Accept only characters common in a plain text in english language - Pattern p = Pattern.compile("[^a-z0-9,./?:!\\s\\t\\n£$%&*()_\\-`]", Pattern.CASE_INSENSITIVE); + //Pattern p = Pattern.compile("[^a-z0-9,./?:!\\s\\t\\n'£$%&*()_\\-`]", Pattern.CASE_INSENSITIVE); + Pattern p = Pattern.compile(props.getProperty("other.validation.regex"), Pattern.CASE_INSENSITIVE); + + //Pattern p = Pattern.compile("[^a-z0-9,./?:!\\s\\t'n£$%&*()_\\-`]", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(text); int count = 0; String message = ""; while (m.find()) { count = count + 1; - message += "position " + m.start() + ": " + text.charAt(m.start()) + "\n"; + String messageAdded = "[...]"; + for(int i = -5; i < 5; i++){ + if(m.start() + i >= 0 && m.start() + i <= text.length() - 1) + messageAdded += text.charAt(m.start() + i); + } + messageAdded +="[...]"; + message += "position " + m.start() + ": " + text.charAt(m.start()) + " in context: " + messageAdded + "\n"; } if (count > 0) { - throw new InvalidInputException( - "There are " + count + " invalid characters in the input:\n" + message); + code.code = Response.ErrorCodes.INVALIDREQUEST; + pnmlResponse.setResponse("There are " + count + " invalid characters in the input:\n" + message); + return pnmlResponse.getResponse(); } + return ""; } /** */ diff --git a/src/main/resources/appParameter.properties b/src/main/resources/appParameter.properties new file mode 100644 index 00000000..e4715b27 --- /dev/null +++ b/src/main/resources/appParameter.properties @@ -0,0 +1,9 @@ +# Request-Parameter-Configuration +request.minTextLength=10 +request.maxTextLength=15000 + +# Response-Parameter-Configuration +response.exception.textLength=30 + +# Other Project Settings +other.validation.regex=[^a-z0-9,./?:!\\s\\t\\n'£$%&*()_\\-`] \ No newline at end of file