Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/main/java/com/teragrep/cfe18/DatabaseExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Integration main data management for Teragrep
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.cfe18;

import jakarta.json.Json;
import jakarta.json.JsonObjectBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class DatabaseExceptionHandler {

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

MariaDBErrorResolver resolver;

public DatabaseExceptionHandler(MariaDBErrorResolver resolver) {
this.resolver = resolver;
}

@ExceptionHandler(SQLException.class)
public ResponseEntity<String> handleSQLException(SQLException ex) {
LOGGER.error(ex.getMessage(), ex);
JsonObjectBuilder errBuilder = Json.createObjectBuilder();
MariaDBError mariaDBError = resolver.resolve(ex.getErrorCode());
errBuilder.add("message", mariaDBError.message());
return new ResponseEntity<>(errBuilder.build().toString(), mariaDBError.status());

}

}
77 changes: 77 additions & 0 deletions src/main/java/com/teragrep/cfe18/MariaDBError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Integration main data management for Teragrep
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.cfe18;

import org.springframework.http.HttpStatus;

public enum MariaDBError {

MISSING(8000, HttpStatus.NOT_FOUND, "Record does not exist"),
INUSE(1451, HttpStatus.BAD_REQUEST, "Is in use"),
UNKNOWN(0, HttpStatus.INTERNAL_SERVER_ERROR, "Something went wrong"),;

private final int errorCode;
private final HttpStatus httpStatus;
private final String message;

private MariaDBError(int errorCode, HttpStatus httpStatus, String message) {
this.errorCode = errorCode;
this.httpStatus = httpStatus;
this.message = message;
}

public int errorCode() {
return errorCode;
}

public HttpStatus status() {
return httpStatus;
}

public String message() {
return message;
}
}
74 changes: 74 additions & 0 deletions src/main/java/com/teragrep/cfe18/MariaDBErrorResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Integration main data management for Teragrep
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.cfe18;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MariaDBErrorResolver {

private final Map<Integer, MariaDBError> registry;

// populate resolver on initialization
public MariaDBErrorResolver() {
this.registry = new HashMap<>();
for (MariaDBError error : MariaDBError.values()) {
this.registry.put(error.errorCode(), error);
}
}

public MariaDBErrorResolver(Map<Integer, MariaDBError> registry) {
this.registry = registry;
}

public MariaDBError resolve(int errorCode) {
return registry.get(errorCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.json.JSONObject;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -63,7 +62,6 @@
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

@RestController
Expand Down Expand Up @@ -110,26 +108,8 @@ public class CaptureDefinitionController {
)
})
public ResponseEntity<String> get(@PathVariable("id") Integer id, @RequestParam(required = false) Integer version) {
try {
CaptureDefinition captureDefinition = captureDefinitionMapper.get(id, version);
return new ResponseEntity<>(captureDefinition.toString(), HttpStatus.OK);
}
catch (RuntimeException ex) {
LOGGER.error(ex.getMessage());
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", id);
jsonErr.put("message", ex.getCause().toString());
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("45000")) {
jsonErr.put("message", "Record does not exist");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.NOT_FOUND);
}
}
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
CaptureDefinition captureDefinition = captureDefinitionMapper.get(id, version);
return new ResponseEntity<>(captureDefinition.toString(), HttpStatus.OK);
}

@RequestMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

@RestController
Expand Down Expand Up @@ -120,27 +119,19 @@ public String index(@AuthenticationPrincipal Jwt jwt) throws Exception {
})
public ResponseEntity<String> create(@RequestBody CaptureFile newCapture) {
LOGGER.info("About to insert <[{}]>", newCapture);
try {
CaptureFile c = captureFileMapper
.create(
newCapture.getTag(), newCapture.getRetentionTime(), newCapture.getCategory(),
newCapture.getApplication(), newCapture.getIndex(), newCapture.getSourceType(),
newCapture.getProtocol(), newCapture.getFlow(), newCapture.getTagPath(),
newCapture.getCapturePath(), newCapture.getFileProcessingTypeId()
);
LOGGER.debug("Values returned <[{}]>", c);
JSONObject jsonObjectFile = new JSONObject();
jsonObjectFile.put("id", c.getId());
jsonObjectFile.put("message", "New capture created");
return new ResponseEntity<>(jsonObjectFile.toString(), HttpStatus.CREATED);
}
catch (RuntimeException ex) {
LOGGER.error(ex.getMessage());
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", newCapture.getId());
jsonErr.put("message", ex.getCause().toString());
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}

CaptureFile c = captureFileMapper
.create(
newCapture.getTag(), newCapture.getRetentionTime(), newCapture.getCategory(),
newCapture.getApplication(), newCapture.getIndex(), newCapture.getSourceType(),
newCapture.getProtocol(), newCapture.getFlow(), newCapture.getTagPath(),
newCapture.getCapturePath(), newCapture.getFileProcessingTypeId()
);
LOGGER.debug("Values returned <[{}]>", c);
JSONObject jsonObjectFile = new JSONObject();
jsonObjectFile.put("id", c.getId());
jsonObjectFile.put("message", "New capture created");
return new ResponseEntity<>(jsonObjectFile.toString(), HttpStatus.CREATED);

}

Expand Down Expand Up @@ -173,26 +164,9 @@ public ResponseEntity<String> create(@RequestBody CaptureFile newCapture) {
)
})
public ResponseEntity<?> get(@PathVariable("id") int id, @RequestParam(required = false) Integer version) {
try {
CaptureFile c = captureFileMapper.get(id, version);
return new ResponseEntity<>(c, HttpStatus.OK);
}
catch (RuntimeException ex) {
LOGGER.error(ex.getMessage());
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", id);
jsonErr.put("message", ex.getCause().toString());
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("45000")) {
jsonErr.put("message", "Record does not exist");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.NOT_FOUND);
}
}
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
CaptureFile c = captureFileMapper.get(id, version);
return new ResponseEntity<>(c, HttpStatus.OK);

}

@RequestMapping(
Expand Down Expand Up @@ -250,32 +224,10 @@ public List<CaptureFile> getAll(@RequestParam(required = false) Integer version)
})
public ResponseEntity<String> delete(@PathVariable("id") int id) {
LOGGER.info("Deleting capture with id <[{}]>", id);
try {
captureFileMapper.delete(id);
JSONObject j = new JSONObject();
j.put("id", id);
j.put("message", "Capture deleted");
return new ResponseEntity<>(j.toString(), HttpStatus.OK);
}
catch (RuntimeException ex) {
LOGGER.error(ex.getMessage());
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", id);
jsonErr.put("message", ex.getCause().getMessage());
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("23000")) {
jsonErr.put("message", "Is in use");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
else if (state.equals("45000")) {
jsonErr.put("message", "Record does not exist");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.NOT_FOUND);
}
}
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
captureFileMapper.delete(id);
JSONObject j = new JSONObject();
j.put("id", id);
j.put("message", "Capture deleted");
return new ResponseEntity<>(j.toString(), HttpStatus.OK);
}
}
Loading