diff --git a/build.gradle b/build.gradle index 2742a3896..2e472dd6a 100644 --- a/build.gradle +++ b/build.gradle @@ -153,6 +153,11 @@ subprojects { signing { if (project.hasProperty('signing.keyId') && project.hasProperty('signing.password') && project.hasProperty('signing.secretKeyRingFile')) { sign publishing.publications.mavenJava + } else if (project.hasProperty('signingKey') && project.hasProperty('signingPassword')) { + def signingKey = findProperty("signingKey") + def signingPassword = findProperty("signingPassword") + useInMemoryPgpKeys(signingKey, signingPassword) + sign publishing.publications.mavenJava } } diff --git a/crud/src/main/java/org/openmbee/mms/crud/controllers/BaseController.java b/crud/src/main/java/org/openmbee/mms/crud/controllers/BaseController.java index 8b6aa6ad2..e8171935d 100644 --- a/crud/src/main/java/org/openmbee/mms/crud/controllers/BaseController.java +++ b/crud/src/main/java/org/openmbee/mms/crud/controllers/BaseController.java @@ -5,8 +5,10 @@ import org.openmbee.mms.core.dao.ProjectDAO; import org.openmbee.mms.core.exceptions.BadRequestException; +import org.openmbee.mms.core.exceptions.ConflictException; import org.openmbee.mms.core.exceptions.DeletedException; import org.openmbee.mms.core.exceptions.ForbiddenException; +import org.openmbee.mms.core.exceptions.InternalErrorException; import org.openmbee.mms.core.exceptions.NotFoundException; import org.openmbee.mms.core.exceptions.NotModifiedException; import org.openmbee.mms.core.exceptions.UnauthorizedException; @@ -95,8 +97,12 @@ protected void handleSingleResponse(BaseResponse res) { throw new ForbiddenException(res); case 404: throw new NotFoundException(res); + case 409: + throw new ConflictException(res); case 410: throw new DeletedException(res); + case 500: + throw new InternalErrorException(res); default: break; } diff --git a/example/crud.postman_collection.json b/example/crud.postman_collection.json index a3f223e61..e51faf156 100644 --- a/example/crud.postman_collection.json +++ b/example/crud.postman_collection.json @@ -3477,6 +3477,92 @@ } }, "response": [] + }, + { + "name": "create branch with long id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"refs\": [\n\t\t{\n\t\t\t\"name\": \"long\",\n \"id\": \"a123456789012345678901234567890123456789012345678901234567890\"\n\t\t}\n\t]\n}" + }, + "url": { + "raw": "{{host}}/projects/aa/refs", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "aa", + "refs" + ] + } + }, + "response": [] + }, + { + "name": "post to long branch", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"elements\": [\n\t\t{\n\t\t\t\"name\": \"blah\",\n\t\t\t\"id\": \"blah\"\n\t\t}\n\t]\n}" + }, + "url": { + "raw": "{{host}}/projects/aa/refs/a123456789012345678901234567890123456789012345678901234567890/elements", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "aa", + "refs", + "a123456789012345678901234567890123456789012345678901234567890", + "elements" + ] + } + }, + "response": [] } ], "auth": { diff --git a/gradle.properties b/gradle.properties index efead23bc..b145c5e77 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version=4.0.2 +version=4.0.3 group=org.openmbee.mms springBootVersion=2.2.6.RELEASE diff --git a/rdb/src/main/java/org/openmbee/mms/rdb/config/DatabaseDefinitionService.java b/rdb/src/main/java/org/openmbee/mms/rdb/config/DatabaseDefinitionService.java index 84de0561e..1be5c9cc7 100644 --- a/rdb/src/main/java/org/openmbee/mms/rdb/config/DatabaseDefinitionService.java +++ b/rdb/src/main/java/org/openmbee/mms/rdb/config/DatabaseDefinitionService.java @@ -1,5 +1,9 @@ package org.openmbee.mms.rdb.config; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -158,11 +162,11 @@ public void copyTablesFromParent(String target, String parent, String parentComm return; } - final String targetNodeTable = String.format("nodes%s", target); + final String targetNodeTable = String.format("nodes%s", getNodeTableName(target)); StringBuilder parentNodeTable = new StringBuilder("nodes"); if (parent != null && !parent.equalsIgnoreCase("master")) { - parentNodeTable.append(String.format("%s", parent)); + parentNodeTable.append(String.format("%s", getNodeTableName(parent))); } JdbcTemplate jdbcTemplate = new JdbcTemplate( @@ -203,4 +207,29 @@ private Map getSchemaProperties() { return properties; } + + /** + * Returns the suffix that should be appended to 'nodes' table given refId + * Empty for 'master', lowercased refId if refId.length <= 50, SHA-1 hash of refId otherwise + * + * @param refId + * @return + */ + static public String getNodeTableName(String refId) { + String res = refId; + if (refId.equals(ContextObject.MASTER_BRANCH)) { + res = ""; + } else if (refId.length() <= 50) { + res = refId.toLowerCase(); + } else { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + byte[] encodedhash = digest.digest(refId.getBytes(StandardCharsets.UTF_8)); + res = new BigInteger(1, encodedhash).toString(16).toLowerCase(); + } catch (NoSuchAlgorithmException e) { + res = refId.toLowerCase().substring(0, 50); + } + } + return res; + } } diff --git a/rdb/src/main/java/org/openmbee/mms/rdb/config/SuffixedPhysicalNamingStrategy.java b/rdb/src/main/java/org/openmbee/mms/rdb/config/SuffixedPhysicalNamingStrategy.java index 7e3584bb1..4c6169f72 100644 --- a/rdb/src/main/java/org/openmbee/mms/rdb/config/SuffixedPhysicalNamingStrategy.java +++ b/rdb/src/main/java/org/openmbee/mms/rdb/config/SuffixedPhysicalNamingStrategy.java @@ -6,7 +6,6 @@ import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.openmbee.mms.core.config.ContextHolder; -import org.openmbee.mms.core.config.ContextObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,7 +32,7 @@ public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEn @Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) { String refId = ContextHolder.getContext().getBranchId(); - refId = refId.equals(ContextObject.MASTER_BRANCH) ? "" : refId.toLowerCase(); + refId = DatabaseDefinitionService.getNodeTableName(refId); return new Identifier(compoundKey(name.getText(), refId), name.isQuoted()); } diff --git a/rdb/src/main/java/org/openmbee/mms/rdb/repositories/BaseDAOImpl.java b/rdb/src/main/java/org/openmbee/mms/rdb/repositories/BaseDAOImpl.java index d2f878092..49a8c3218 100644 --- a/rdb/src/main/java/org/openmbee/mms/rdb/repositories/BaseDAOImpl.java +++ b/rdb/src/main/java/org/openmbee/mms/rdb/repositories/BaseDAOImpl.java @@ -1,7 +1,8 @@ package org.openmbee.mms.rdb.repositories; import org.openmbee.mms.core.config.ContextHolder; -import org.openmbee.mms.core.config.ContextObject; +import org.openmbee.mms.core.exceptions.BadRequestException; +import org.openmbee.mms.rdb.config.DatabaseDefinitionService; import org.openmbee.mms.rdb.datasources.CrudDataSources; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,10 +39,10 @@ public JdbcTemplate getConn() { public String getSuffix() { String refId = ContextHolder.getContext().getBranchId(); - if(BRANCH_ID_VALID_PATTERN.matcher(refId).matches()) { - return refId.equals(ContextObject.MASTER_BRANCH) ? "" : refId.toLowerCase(); + if (BRANCH_ID_VALID_PATTERN.matcher(refId).matches()) { + return DatabaseDefinitionService.getNodeTableName(refId); } else { - throw new IllegalArgumentException("Bad branch id, aborting current operation."); + throw new BadRequestException("Bad branch id, aborting current operation."); } } }