Skip to content

Commit

Permalink
#47 - Error Feedback
Browse files Browse the repository at this point in the history
Feedback when error at T2P generating
- HTTP-Code (header) -> todo: UI-Team coordination
- body-information: error details
----------
Pairprogramming with Moritz Wolf & Alexander Lamers

Co-Authored-By: AlexDevelop94 <[email protected]>
  • Loading branch information
moritz200 and AlexDevelop94 committed Jun 26, 2023
1 parent fec6d5a commit 1f3a546
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/de/dhbw/text2process/controller/T2PController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<File>(true, e.getCause(), e.getMessage(), e.getStackTrace());
} catch (InvalidInputException e) {
Expand Down Expand Up @@ -285,11 +286,21 @@ public String generatePetriNetFromText(
@RequestBody String param, HttpServletRequest request, HttpServletResponse response) {

Response<String> 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<String>(t2PControllerHelper.generatePetrinetFromText(param));
logger.info("Finished generating PNML-String");
Expand Down Expand Up @@ -322,4 +333,4 @@ public String generatePetriNetFromText(
logger.info("Returning the pnmlString");
return pnmlResponse.getResponse();
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/de/dhbw/text2process/helper/appParameterHelper.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
33 changes: 33 additions & 0 deletions src/main/java/de/dhbw/text2process/helper/rest/Response.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.dhbw.text2process.helper.rest;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

public class Response<E> {
Expand All @@ -10,6 +11,13 @@ public class Response<E> {
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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,31 @@
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
Logger logger = LoggerFactory.getLogger(T2PControllerHelper.class);

// Reject any Request larger than this
public static final int MAX_INPUT_LENGTH = 15000;
public static final int MIN_INPUT_LENGTH = 10;


/**
*
Expand Down Expand Up @@ -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<String> pnmlResponse;
pnmlResponse = new Response<String>(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 "";
}

/** */
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/appParameter.properties
Original file line number Diff line number Diff line change
@@ -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'£$%&*()_\\-`]

0 comments on commit 1f3a546

Please sign in to comment.