Выборку метаданных вынес в отдельную функцию
This commit is contained in:
@ -11,20 +11,21 @@ issuer:
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: dbms
|
||||
name: org_ccalm_dbms
|
||||
datasource:
|
||||
url: jdbc:postgresql://91.201.214.156:5432/CCALM
|
||||
url: jdbc:postgresql://91.201.214.156:5432/CCALM?ApplicationName=org_ccalm_dbms&ssl=true&sslmode=require&connectTimeout=10000&socketTimeout=10000
|
||||
username: postgres
|
||||
password: PasSecrKey1
|
||||
password: 309A86FF65A78FB428F4E38DFE35F730
|
||||
driver-class-name: org.postgresql.Driver
|
||||
hikari:
|
||||
maximum-pool-size: 10
|
||||
minimum-idle: 5
|
||||
max-lifetime: 1700000
|
||||
idle-timeout: 600000
|
||||
max-lifetime: 600000
|
||||
idle-timeout: 300000
|
||||
connection-timeout: 30000
|
||||
connection-test-query: SELECT now()
|
||||
validation-timeout: 60000
|
||||
connection-test-query: SELECT 1
|
||||
validation-timeout: 5000
|
||||
validation-interval: 60000
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
@ -51,8 +52,8 @@ url:
|
||||
reset: http://127.0.0.1:8088/reset
|
||||
main: http://127.0.0.1:8088/
|
||||
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
zaxxer:
|
||||
hikari: DEBUG
|
||||
#logging:
|
||||
# level:
|
||||
# com:
|
||||
# zaxxer:
|
||||
# hikari: DEBUG
|
||||
|
||||
@ -19,14 +19,11 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
//import java.util.Iterator;
|
||||
import java.util.Date;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.Checksum;
|
||||
|
||||
@ -55,6 +52,7 @@ import javax.xml.xpath.XPathFactory;
|
||||
//import org.apache.commons.fileupload.FileItem;
|
||||
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import models.MetadataNameModel;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.MarkerManager;
|
||||
@ -68,6 +66,7 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -142,6 +141,132 @@ public class DBMSRecords implements ServletContextAware {
|
||||
return null;
|
||||
}
|
||||
|
||||
//Вместо fn="0"
|
||||
@RequestMapping(value = "/metadata",method = {RequestMethod.POST,RequestMethod.GET}) //,produces = "text/plain; charset=utf-8"
|
||||
@ResponseBody
|
||||
public ResponseEntity<String> getMetadata(
|
||||
@RequestBody(required = false) byte[] reqData,
|
||||
@CookieValue(value = "jwt_a", defaultValue = "") String jwt_a,
|
||||
@CookieValue(value = "lng", defaultValue = "1") String language_id
|
||||
) {
|
||||
final HttpHeaders httpHeaders= new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
String result=sendError(10000,"Request_not_processed",null);
|
||||
|
||||
if (jwt_a.equals("") || Tools.countOccurrences(jwt_a, '.') != 2) {
|
||||
result = sendError(10000, "You_are_not_logged_in",null);
|
||||
return new ResponseEntity<String>(result, httpHeaders, HttpStatus.OK);
|
||||
}
|
||||
//Проверяю подпись токена
|
||||
Jws<Claims> claims = null;
|
||||
PublicKey key_a = getPublicKey(); //SecretKey key_a = new SecretKeySpec(Base64.getDecoder().decode(env.getProperty("access.key")), "HmacSHA256");
|
||||
try {
|
||||
claims = Jwts.parserBuilder()
|
||||
.setSigningKey(key_a)
|
||||
.build()
|
||||
.parseClaimsJws(jwt_a);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<String>("JWT_token_verification_error", httpHeaders, HttpStatus.OK);
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.id = claims.getBody().get("user_id").toString();
|
||||
user.language_id = language_id;
|
||||
|
||||
result=sendError(10000,"Request not processed!",null);
|
||||
if(reqData==null)
|
||||
return new ResponseEntity<String>(result, httpHeaders, HttpStatus.OK);
|
||||
InputStream body = new ByteArrayInputStream(reqData);
|
||||
|
||||
Document doc = null;
|
||||
Element reqNode = null;
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
doc = dBuilder.parse(body);
|
||||
} catch (Exception ex) {
|
||||
logger.info(ex.getMessage());
|
||||
return new ResponseEntity<String>(sendError(10000,"Parsing request error!",null), httpHeaders, HttpStatus.OK);
|
||||
}
|
||||
|
||||
if (doc != null) {
|
||||
reqNode = doc.getDocumentElement();
|
||||
//fn = reqNode.getAttribute("fn"); //Номер функции
|
||||
}
|
||||
|
||||
//Get XML node from database and parse to DOM
|
||||
javax.xml.xpath.XPathFactory xPathfactory = javax.xml.xpath.XPathFactory.newInstance();
|
||||
XPath xpath = xPathfactory.newXPath();
|
||||
XPathExpression expr=null;
|
||||
|
||||
String name = "";
|
||||
//if (fn != null && fn.equals("0")) //Send metadata to client
|
||||
if (doc != null) {
|
||||
xPathfactory = XPathFactory.newInstance();
|
||||
xpath = xPathfactory.newXPath();
|
||||
try {
|
||||
expr = xpath.compile("//metadata/type/@n");
|
||||
name = "" + expr.evaluate(doc, XPathConstants.STRING);
|
||||
} catch (XPathExpressionException ex) {
|
||||
logger.info(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
String xml = "";
|
||||
|
||||
//Get XML node from database and parse to DOM
|
||||
doc = parseString(getTypeStrNode2(name));
|
||||
|
||||
if (doc != null) {
|
||||
doc.getDocumentElement().normalize(); //Del or concat text node
|
||||
|
||||
xml += "<metadata fn=\"0\">";
|
||||
|
||||
//Delete all child "sql-query" nodes.
|
||||
XPathExpression exp=null;
|
||||
NodeList nl=null;
|
||||
try {
|
||||
exp = xpath.compile("//sql-query");
|
||||
nl = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
|
||||
} catch (XPathExpressionException ex) {
|
||||
logger.info(ex.getMessage());
|
||||
}
|
||||
|
||||
for (int j = 0; j < nl.getLength(); j++) {
|
||||
nl.item(j).getParentNode().removeChild(nl.item(j));
|
||||
}
|
||||
// XML Node Serialisation
|
||||
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
|
||||
LSSerializer lsSerializer = domImplementation.createLSSerializer();
|
||||
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
|
||||
|
||||
//Set attributes to define access level (insert,update,selete,select).
|
||||
String allow;
|
||||
|
||||
allow = getAccess(user.id,"Select_" + name);
|
||||
doc.getDocumentElement().setAttribute("sel", allow);
|
||||
|
||||
allow = getAccess(user.id,"Insert_" + name);
|
||||
doc.getDocumentElement().setAttribute("ins", allow);
|
||||
|
||||
allow = getAccess(user.id,"Update_" + name);
|
||||
doc.getDocumentElement().setAttribute("upd", allow);
|
||||
|
||||
allow = getAccess(user.id,"Delete_" + name);
|
||||
doc.getDocumentElement().setAttribute("del", allow);
|
||||
|
||||
xml += lsSerializer.writeToString(doc.getDocumentElement());
|
||||
xml += "</metadata>";
|
||||
} else {
|
||||
xml += "<metadata fn=\"0\"></metadata>";
|
||||
}
|
||||
|
||||
result=trts2(xml,user);
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_XML);
|
||||
|
||||
return new ResponseEntity<String>(result, httpHeaders, HttpStatus.OK);
|
||||
}
|
||||
|
||||
//Документация по @RequestBody http://javastudy.ru/spring-mvc/json-xml/ application/xml
|
||||
//Обычно мантирую в: /api/dbms/v09/
|
||||
@RequestMapping(value = "/dbms",method = {RequestMethod.POST,RequestMethod.GET}) //,produces = "text/plain; charset=utf-8"
|
||||
@ -154,7 +279,6 @@ public class DBMSRecords implements ServletContextAware {
|
||||
) {
|
||||
final HttpHeaders httpHeaders= new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
String result=sendError(10000,"Request_not_processed",null);
|
||||
|
||||
if(user.id==null || user.id.equals("null")) { //
|
||||
@ -2266,8 +2390,32 @@ public class DBMSRecords implements ServletContextAware {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public String trt2(String key,User user)
|
||||
{
|
||||
String result="";
|
||||
JSONObject json = null;
|
||||
try {
|
||||
String sql="select translation from main._translations where identifier=:identifier and language_id=:language_id";
|
||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||
parameters.addValue("identifier", key);
|
||||
parameters.addValue("language_id", Integer.valueOf(user.language_id), Types.INTEGER);
|
||||
List<String> ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper());
|
||||
for (int i = 0; i < ret.size(); i++) {
|
||||
json = new JSONObject(ret.get(i));
|
||||
break;
|
||||
}
|
||||
}catch (DataAccessException ex){
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
logger.error(uuid,ex);
|
||||
}
|
||||
if(json!=null) {
|
||||
result=json.getString("translation");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//Translate text by patterns
|
||||
//Translate text by patterns (новая функция ниже)
|
||||
//TODO потом удалить
|
||||
public String trts(Connection conn,String text,User user) {
|
||||
int pos1 = 0;
|
||||
while (true) {
|
||||
@ -2282,6 +2430,20 @@ public class DBMSRecords implements ServletContextAware {
|
||||
}
|
||||
return text;
|
||||
}
|
||||
public String trts2(String text,User user) {
|
||||
int pos1 = 0;
|
||||
while (true) {
|
||||
pos1 = text.indexOf("trt('", pos1);
|
||||
if (pos1 == -1)
|
||||
break;
|
||||
int pos2 = text.indexOf("')", pos1);
|
||||
if (pos2 == -1)
|
||||
break;
|
||||
|
||||
text = text.substring(0, pos1) + trt2(text.substring(pos1 + 5, pos2),user) + text.substring(pos2 + 2);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
public static String afterFirst(String str, String ch)
|
||||
{
|
||||
@ -2293,7 +2455,8 @@ public class DBMSRecords implements ServletContextAware {
|
||||
return "";
|
||||
}
|
||||
|
||||
//Получить узел метаданных из базы данных
|
||||
//Получить узел метаданных из базы данных (новая функция ниже)
|
||||
//TODO потом удалить
|
||||
public String getTypeStrNode(Connection conn,String typeName)
|
||||
{
|
||||
String result="";
|
||||
@ -2324,6 +2487,47 @@ public class DBMSRecords implements ServletContextAware {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//Получить узел метаданных из базы данных (новая функция ниже)
|
||||
public String getTypeStrNode2(String typeName)
|
||||
{
|
||||
String result="";
|
||||
try {
|
||||
String sql="select xml from main._metadata where name=:name";
|
||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||
parameters.addValue("name", typeName);
|
||||
List<String> ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper());
|
||||
for (int i = 0; i < ret.size(); i++) {
|
||||
JSONObject json = new JSONObject(ret.get(i));
|
||||
result = json.getString("xml");
|
||||
}
|
||||
}catch (DataAccessException ex){
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
logger.error(uuid,ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getAccess(String user_id,String name)
|
||||
{
|
||||
String allow="1";
|
||||
try {
|
||||
String sql = "select main.get_Access(:user_id,:name) as acc;";
|
||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||
parameters.addValue("user_id", Integer.parseInt(user_id), Types.INTEGER);
|
||||
parameters.addValue("name", name);
|
||||
List<String> ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper());
|
||||
for (int i = 0; i < ret.size(); i++) {
|
||||
if ((new JSONObject(ret.get(i))).getBoolean("acc"))
|
||||
allow = "1";
|
||||
else
|
||||
allow = "0";
|
||||
}
|
||||
}catch (DataAccessException ex){
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
logger.error(uuid,ex);
|
||||
}
|
||||
return allow;
|
||||
}
|
||||
|
||||
//Пропарсить сткоку в DOM
|
||||
public Document parseString(String xml)
|
||||
|
||||
15
src/main/java/models/MetadataNameModel.java
Normal file
15
src/main/java/models/MetadataNameModel.java
Normal file
@ -0,0 +1,15 @@
|
||||
package models;
|
||||
|
||||
//import jakarta.persistence.Column;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class MetadataNameModel {
|
||||
@JsonProperty("metadata_name")
|
||||
private String metadata_name;
|
||||
public String getMetadataName() {
|
||||
return metadata_name;
|
||||
}
|
||||
public void setMetadataName(String metadata_name) {
|
||||
this.metadata_name = metadata_name;
|
||||
}
|
||||
}
|
||||
@ -25,7 +25,8 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<!--root level="info"-->
|
||||
<root level="warn">
|
||||
<appender-ref ref="FILE" />
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
|
||||
Reference in New Issue
Block a user