Skip to content
Snippets Groups Projects
Verified Commit 44c7aa5d authored by Timm Fitschen's avatar Timm Fitschen
Browse files

Consolidate ServerMessages

parent fab7a11a
Branches
Tags
3 merge requests!44Release 0.6,!43Merge f-GRPC-main to dev,!42DEPS: Update proto and fix breaking changes
Pipeline #15803 failed
Showing
with 99 additions and 97 deletions
caosdb-proto @ c01fd9ff
Subproject commit 547771c7df79f5b0d6c4d73a3d7e8c9c9e73cb71
Subproject commit c01fd9ff1471adafa707fdb36bdaa49749079fa6
......@@ -22,13 +22,12 @@
*/
package org.caosdb.server.database.backend.transaction;
import static org.caosdb.server.transaction.Transaction.ERROR_INTEGRITY_VIOLATION;
import org.caosdb.server.database.BackendTransaction;
import org.caosdb.server.database.backend.interfaces.DeleteEntityPropertiesImpl;
import org.caosdb.server.database.exceptions.IntegrityException;
import org.caosdb.server.entity.EntityInterface;
import org.caosdb.server.utils.EntityStatus;
import org.caosdb.server.utils.ServerMessages;
public class DeleteEntityProperties extends BackendTransaction {
......@@ -50,7 +49,7 @@ public class DeleteEntityProperties extends BackendTransaction {
try {
ret.execute(this.entity.getId());
} catch (final IntegrityException exc) {
this.entity.addError(ERROR_INTEGRITY_VIOLATION);
this.entity.addError(ServerMessages.ERROR_INTEGRITY_VIOLATION);
throw exc;
}
}
......
......@@ -24,13 +24,12 @@
*/
package org.caosdb.server.database.backend.transaction;
import static org.caosdb.server.transaction.Transaction.ERROR_INTEGRITY_VIOLATION;
import org.caosdb.server.database.BackendTransaction;
import org.caosdb.server.database.backend.interfaces.DeleteSparseEntityImpl;
import org.caosdb.server.database.exceptions.IntegrityException;
import org.caosdb.server.entity.EntityInterface;
import org.caosdb.server.utils.EntityStatus;
import org.caosdb.server.utils.ServerMessages;
public class DeleteSparseEntity extends BackendTransaction {
......@@ -54,7 +53,7 @@ public class DeleteSparseEntity extends BackendTransaction {
ret.execute(this.entity.getId());
}
} catch (final IntegrityException exc) {
this.entity.addError(ERROR_INTEGRITY_VIOLATION);
this.entity.addError(ServerMessages.ERROR_INTEGRITY_VIOLATION);
throw exc;
}
}
......
package org.caosdb.server.database.backend.transaction;
import static org.caosdb.server.transaction.Transaction.ERROR_INTEGRITY_VIOLATION;
import org.caosdb.server.database.BackendTransaction;
import org.caosdb.server.database.backend.interfaces.InsertEntityDatatypeImpl;
import org.caosdb.server.database.exceptions.IntegrityException;
import org.caosdb.server.database.proto.SparseEntity;
import org.caosdb.server.entity.EntityInterface;
import org.caosdb.server.utils.ServerMessages;
public class InsertEntityDatatype extends BackendTransaction {
......@@ -25,7 +24,7 @@ public class InsertEntityDatatype extends BackendTransaction {
try {
t.execute(e);
} catch (final IntegrityException exc) {
this.entity.addError(ERROR_INTEGRITY_VIOLATION);
this.entity.addError(ServerMessages.ERROR_INTEGRITY_VIOLATION);
throw exc;
}
......
......@@ -22,8 +22,6 @@
*/
package org.caosdb.server.database.backend.transaction;
import static org.caosdb.server.transaction.Transaction.ERROR_INTEGRITY_VIOLATION;
import org.caosdb.server.database.BackendTransaction;
import org.caosdb.server.database.backend.interfaces.InsertSparseEntityImpl;
import org.caosdb.server.database.exceptions.IntegrityException;
......@@ -31,6 +29,7 @@ import org.caosdb.server.database.exceptions.TransactionException;
import org.caosdb.server.database.proto.SparseEntity;
import org.caosdb.server.entity.EntityInterface;
import org.caosdb.server.entity.Version;
import org.caosdb.server.utils.ServerMessages;
import org.caosdb.server.utils.Undoable;
public class InsertSparseEntity extends BackendTransaction {
......@@ -52,7 +51,7 @@ public class InsertSparseEntity extends BackendTransaction {
try {
t.execute(e);
} catch (final IntegrityException exc) {
this.entity.addError(ERROR_INTEGRITY_VIOLATION);
this.entity.addError(ServerMessages.ERROR_INTEGRITY_VIOLATION);
throw exc;
}
......
......@@ -27,9 +27,6 @@ import org.caosdb.server.entity.Message;
@DatatypeDefinition(name = "Reference")
public class ReferenceDatatype extends AbstractDatatype {
public static final Message REFERENCE_ID_NOT_PARSABLE =
new Message(217, "The reference is not parsable. It must be an integer.");
@Override
public ReferenceValue parseValue(final Object value) throws Message {
return ReferenceValue.parseReference(value);
......
......@@ -662,18 +662,6 @@ public class Entity extends AbstractObservable implements EntityInterface {
return ret;
}
@Override
public final Message getMessage(final String type, final Integer code) {
for (final ToElementable m : this.messages) {
if (m instanceof Message
&& ((Message) m).getType().equalsIgnoreCase(type)
&& ((Message) m).getCode() == code) {
return (Message) m;
}
}
return null;
}
@Override
public final void addMessage(final ToElementable m) {
this.messages.add(m);
......@@ -691,7 +679,7 @@ public class Entity extends AbstractObservable implements EntityInterface {
@Override
public void addInfo(final String description) {
final Message m = new Message(MessageType.Info, 0, description);
final Message m = new Message(description);
addMessage(m);
}
......
......@@ -21,17 +21,18 @@
*/
package org.caosdb.server.entity;
import org.caosdb.api.entity.v1.MessageCode;
import org.caosdb.server.entity.xml.ToElementable;
import org.jdom2.Element;
public class Message extends Exception implements Comparable<Message>, ToElementable {
protected final String type;
private final Integer code;
private final MessageCode code;
private final String description;
private final String body;
private static final long serialVersionUID = -3005017964769041935L;
private static final long serialVersionUID = 1837110172902899264L;
public enum MessageType {
Warning,
......@@ -48,7 +49,7 @@ public class Message extends Exception implements Comparable<Message>, ToElement
public String toString() {
return this.type.toString()
+ " ("
+ (this.code != null ? Integer.toString(this.code) : "")
+ this.code.toString()
+ ") - "
+ (this.description != null ? this.description : "");
}
......@@ -74,33 +75,33 @@ public class Message extends Exception implements Comparable<Message>, ToElement
this(type, null, description, body);
}
public Message(final String type, final Integer code) {
public Message(final String type, final MessageCode code) {
this(type, code, null, null);
}
public Message(final Integer code, final String description) {
public Message(final MessageCode code, final String description) {
this(MessageType.Info, code, description);
}
public Message(final MessageType type, final Integer code, final String description) {
public Message(final MessageType type, final MessageCode code, final String description) {
this(type.toString(), code, description, null);
}
public Message(
final MessageType type, final Integer code, final String description, final String body) {
final MessageType type, final MessageCode code, final String description, final String body) {
this(type.toString(), code, description, body);
}
public Message(final String type, final Integer code, final String description) {
public Message(final String type, final MessageCode code, final String description) {
this(type, code, description, null);
}
public Message(final MessageType type, final String description) {
this(type.toString(), 0, description);
this(type.toString(), MessageCode.MESSAGE_CODE_UNKNOWN, description);
}
public Message(
final String type, final Integer code, final String description, final String body) {
final String type, final MessageCode code, final String description, final String body) {
this.code = code;
this.description = description;
this.body = body;
......@@ -108,7 +109,7 @@ public class Message extends Exception implements Comparable<Message>, ToElement
}
public Message(
final Integer code, final String description, final String body, final MessageType type) {
final MessageCode code, final String description, final String body, final MessageType type) {
this.code = code;
this.description = description;
this.body = body;
......@@ -122,14 +123,14 @@ public class Message extends Exception implements Comparable<Message>, ToElement
this.type = MessageType.Info.toString();
}
public Integer getCode() {
public MessageCode getCode() {
return this.code;
}
public Element toElement() {
final Element e = new Element(this.type);
if (this.code != null) {
e.setAttribute("code", Integer.toString(this.code));
e.setAttribute("code", this.code.toString());
}
if (this.description != null) {
e.setAttribute("description", this.description);
......@@ -153,8 +154,7 @@ public class Message extends Exception implements Comparable<Message>, ToElement
public final void print(final String indent) {
System.out.println(indent + "+---| " + this.type + " |------------------------ ");
System.out.println(
indent + "| Code: " + (this.code != null ? Integer.toString(this.code) : "null"));
System.out.println(indent + "| Code: " + this.code.toString());
System.out.println(indent + "| Description: " + this.description);
System.out.println(indent + "| Body: " + this.body);
System.out.println(indent + "+------------------------------------------------------ ");
......
......@@ -59,8 +59,6 @@ public interface TransactionEntity {
public abstract List<Message> getMessages(String type);
public abstract Message getMessage(String type, Integer code);
public abstract void addMessage(ToElementable m);
public abstract void addError(Message m);
......
......@@ -340,11 +340,6 @@ public class EntityWrapper implements EntityInterface {
return this.entity.getMessages(type);
}
@Override
public Message getMessage(final String type, final Integer code) {
return this.entity.getMessage(type, code);
}
@Override
public void addMessage(final ToElementable m) {
this.entity.addMessage(m);
......
......@@ -181,19 +181,7 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
}
public static MessageCode getMessageCode(final Message m) {
if (m.equalsCore(ServerMessages.ENTITY_DOES_NOT_EXIST)) {
return MessageCode.MESSAGE_CODE_ENTITY_DOES_NOT_EXIST;
} else if (m.equalsCore(ServerMessages.ENTITY_HAS_NO_PROPERTIES)) {
return MessageCode.MESSAGE_CODE_ENTITY_HAS_NO_PROPERTIES;
} else if (m.equalsCore(ServerMessages.INTEGER_OUT_OF_RANGE)) {
return MessageCode.MESSAGE_CODE_INTEGER_VALUE_OUT_OF_RANGE;
} else if (m.equalsCore(ServerMessages.ENTITY_HAS_BEEN_DELETED_SUCCESSFULLY)) {
return MessageCode.MESSAGE_CODE_ENTITY_HAS_BEEN_DELETED_SUCCESSFULLY;
} else if (m.getCode() == 0) {
return MessageCode.MESSAGE_CODE_UNSPECIFIED;
} else {
return MessageCode.MESSAGE_CODE_UNKNOWN;
}
return m.getCode();
}
public Version convert(final org.caosdb.server.entity.Version from) {
......
......@@ -127,8 +127,6 @@ public class CheckFileStorageConsistency extends FlagJob {
getContainer()
.addMessage(
new Message(
"Info",
0,
"Test took too long. The results will be written to './ConsistencyTest.xml'"));
} else {
// add info/warning/error
......
......@@ -31,7 +31,6 @@ import org.caosdb.server.datatype.ReferenceValue;
import org.caosdb.server.entity.Entity;
import org.caosdb.server.entity.EntityInterface;
import org.caosdb.server.entity.Message;
import org.caosdb.server.entity.Message.MessageType;
import org.caosdb.server.entity.Role;
import org.caosdb.server.entity.wrapper.Parent;
import org.caosdb.server.jobs.EntityJob;
......@@ -143,8 +142,6 @@ public class CheckRefidIsaParRefid extends EntityJob implements Observer {
getEntity()
.addInfo(
new Message(
MessageType.Info,
0,
"Could not resolve all parents of the entity with id "
+ child.toString()
+ ". Problematic parent: "
......
......@@ -26,6 +26,7 @@ package org.caosdb.server.jobs.core;
import java.util.ArrayList;
import java.util.List;
import org.caosdb.api.entity.v1.MessageCode;
import org.caosdb.server.database.backend.transaction.RetrieveFullEntityTransaction;
import org.caosdb.server.entity.Entity;
import org.caosdb.server.entity.EntityInterface;
......@@ -61,7 +62,7 @@ public class Inheritance extends EntityJob {
public static final Message ILLEGAL_INHERITANCE_MODE =
new Message(
MessageType.Warning,
0,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Unknown value for flag \"inheritance\". None of the parent's properties have been transfered to the child.");
@Override
......
......@@ -30,6 +30,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.caosdb.api.entity.v1.MessageCode;
import org.caosdb.server.CaosDBException;
import org.caosdb.server.CaosDBServer;
import org.caosdb.server.FileSystem;
......@@ -132,8 +133,6 @@ public class InsertFilesInDir extends FlagJob {
getContainer()
.addMessage(
new Message(
MessageType.Info,
0,
"Files count in "
+ dir.getName()
+ "/: "
......@@ -147,7 +146,12 @@ public class InsertFilesInDir extends FlagJob {
throw new TransactionException(e);
}
} else {
getContainer().addMessage(new Message(MessageType.Error, 0, "No such directory: " + dirStr));
getContainer()
.addMessage(
new Message(
MessageType.Error,
MessageCode.MESSAGE_CODE_UNKNOWN,
"No such directory: " + dirStr));
return;
}
}
......@@ -289,7 +293,7 @@ public class InsertFilesInDir extends FlagJob {
.addMessage(
new Message(
MessageType.Warning,
1,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Not explicitly included file: " + sub.getCanonicalPath()));
return false;
}
......@@ -297,7 +301,9 @@ public class InsertFilesInDir extends FlagJob {
getContainer()
.addMessage(
new Message(
MessageType.Warning, 2, "Explicitly excluded file: " + sub.getCanonicalPath()));
MessageType.Warning,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Explicitly excluded file: " + sub.getCanonicalPath()));
return false;
}
}
......@@ -305,14 +311,18 @@ public class InsertFilesInDir extends FlagJob {
getContainer()
.addMessage(
new Message(
MessageType.Warning, 3, "Hidden directory or file: " + sub.getCanonicalPath()));
MessageType.Warning,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Hidden directory or file: " + sub.getCanonicalPath()));
return false;
}
if (sub.isDirectory() && !sub.canExecute()) {
getContainer()
.addMessage(
new Message(
MessageType.Warning, 4, "Unaccessible directory: " + sub.getCanonicalPath()));
MessageType.Warning,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Unaccessible directory: " + sub.getCanonicalPath()));
return false;
}
if (!sub.canRead()) {
......@@ -320,7 +330,7 @@ public class InsertFilesInDir extends FlagJob {
.addMessage(
new Message(
MessageType.Warning,
5,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Unreadable directory or file: " + sub.getCanonicalPath()));
return false;
}
......@@ -329,7 +339,7 @@ public class InsertFilesInDir extends FlagJob {
.addMessage(
new Message(
MessageType.Warning,
6,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Directory or file is symbolic link: " + sub.getAbsolutePath()));
if (!this.forceSymLinks) {
return false;
......@@ -355,11 +365,15 @@ public class InsertFilesInDir extends FlagJob {
// overlaps the directory to be inserted.
if (!dir.isDirectory()) {
throw new Message(MessageType.Error, 0, "Dir is not a directory.");
throw new Message(
MessageType.Error, MessageCode.MESSAGE_CODE_UNKNOWN, "Dir is not a directory.");
}
if (!dir.canRead() || !dir.canExecute()) {
throw new Message(MessageType.Error, 0, "Cannot read or enter the desired directory.");
throw new Message(
MessageType.Error,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Cannot read or enter the desired directory.");
}
final File base = new File(FileSystem.getBasepath());
......@@ -375,7 +389,10 @@ public class InsertFilesInDir extends FlagJob {
|| isSubDir(tmp, dir)
|| isSubDir(dir, root)
|| isSubDir(root, dir)) {
throw new Message(MessageType.Error, 0, "Dir is not allowed: " + dir.toString());
throw new Message(
MessageType.Error,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Dir is not allowed: " + dir.toString());
}
for (final File f : getAllowedFolders()) {
......@@ -386,7 +403,7 @@ public class InsertFilesInDir extends FlagJob {
}
throw new Message(
MessageType.Error,
1,
MessageCode.MESSAGE_CODE_UNKNOWN,
"Dir is not allowed: "
+ dir.toString()
+ " Allowed directories: "
......
......@@ -25,7 +25,6 @@ package org.caosdb.server.jobs.core;
import org.caosdb.server.CaosDBServer;
import org.caosdb.server.ServerProperties;
import org.caosdb.server.entity.Message;
import org.caosdb.server.entity.Message.MessageType;
import org.caosdb.server.jobs.FlagJob;
import org.caosdb.server.jobs.JobAnnotation;
import org.caosdb.server.utils.ServerMessages;
......@@ -49,8 +48,7 @@ public class TestMail extends FlagJob {
if (Utils.isRFC822Compliant(value)) {
final Mail m = new Mail(NAME, EMAIL, null, value, "Test mail", "This is a test mail.");
m.send();
getContainer()
.addMessage(new Message(MessageType.Info, 0, "A mail has been send to " + value));
getContainer().addMessage(new Message("A mail has been send to " + value));
} else {
getContainer().addMessage(ServerMessages.EMAIL_NOT_WELL_FORMED);
}
......
......@@ -46,6 +46,7 @@ import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.commons.jcs.access.behavior.ICacheAccess;
import org.apache.shiro.subject.Subject;
import org.caosdb.api.entity.v1.MessageCode;
import org.caosdb.server.CaosDBServer;
import org.caosdb.server.ServerProperties;
import org.caosdb.server.caching.Cache;
......@@ -754,7 +755,7 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac
}
private void addWarning(final String w) {
this.messages.add(new Message(MessageType.Warning, 0, w));
this.messages.add(new Message(MessageType.Warning, MessageCode.MESSAGE_CODE_UNKNOWN, w));
}
private void cleanUp() {
......
......@@ -96,7 +96,17 @@ public class ScriptingResource extends AbstractCaosDBServerResource {
return null;
}
} catch (Message m) {
return error(m, Status.valueOf(m.getCode()));
if (m == ServerMessages.SERVER_SIDE_SCRIPT_DOES_NOT_EXIST) {
return error(m, Status.CLIENT_ERROR_NOT_FOUND);
} else if (m == ServerMessages.SERVER_SIDE_SCRIPT_MISSING_CALL) {
return error(m, Status.CLIENT_ERROR_BAD_REQUEST);
} else if (m == ServerMessages.SERVER_SIDE_SCRIPT_NOT_EXECUTABLE) {
return error(m, Status.CLIENT_ERROR_BAD_REQUEST);
} else if (m == ServerMessages.SERVER_SIDE_SCRIPT_TIMEOUT) {
return error(m, Status.CLIENT_ERROR_BAD_REQUEST);
}
return error(m, Status.SERVER_ERROR_INTERNAL);
} finally {
deleteTmpFiles();
}
......
......@@ -30,7 +30,6 @@ import org.caosdb.server.database.backend.transaction.DeletePassword;
import org.caosdb.server.database.backend.transaction.DeleteUser;
import org.caosdb.server.database.backend.transaction.RetrievePasswordValidator;
import org.caosdb.server.entity.Message;
import org.caosdb.server.entity.Message.MessageType;
import org.caosdb.server.utils.ServerMessages;
import org.jdom2.Element;
......@@ -64,7 +63,7 @@ public class DeleteUserTransaction extends AccessControlTransaction {
final Element ret = new Element("User");
ret.setAttribute("realm", this.realm);
ret.setAttribute("name", this.user);
ret.addContent(new Message(MessageType.Info, 0, "This user has been deleted.").toElement());
ret.addContent(new Message("This user has been deleted.").toElement());
return ret;
}
}
......@@ -28,6 +28,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.TimeZone;
import org.caosdb.api.entity.v1.MessageCode;
import org.caosdb.datetime.UTCDateTime;
import org.caosdb.server.database.DatabaseAccessManager;
import org.caosdb.server.database.access.Access;
......@@ -176,7 +177,13 @@ public class FileStorageConsistencyCheck extends Thread
sb.append('\n').append(t.toString());
}
e.addContent(new Message("Error", 0, "An exception was thrown.", sb.toString()).toElement());
e.addContent(
new Message(
"Error",
MessageCode.MESSAGE_CODE_UNKNOWN,
"An exception was thrown.",
sb.toString())
.toElement());
}
final List<Message> results2Messages = results2Messages(getResults(), this.location);
......@@ -196,24 +203,36 @@ public class FileStorageConsistencyCheck extends Thread
final ArrayList<Message> ret = new ArrayList<Message>();
if (results.isEmpty()) {
if (location.length() > 0) {
ret.add(new Message("Info", 0, "File system below " + location + " is consistent."));
ret.add(new Message("File system below " + location + " is consistent."));
} else {
ret.add(new Message("Info", 0, "File system is consistent."));
ret.add(new Message("File system is consistent."));
}
}
for (final Entry<String, Integer> r : results.entrySet()) {
switch (r.getValue()) {
case FileConsistencyCheck.FILE_DOES_NOT_EXIST:
ret.add(new Message("Error", 0, r.getKey() + ": File does not exist."));
ret.add(
new Message(
"Error",
MessageCode.MESSAGE_CODE_FILE_NOT_FOUND,
r.getKey() + ": File does not exist."));
break;
case FileConsistencyCheck.FILE_MODIFIED:
ret.add(new Message("Error", 0, r.getKey() + ": File was modified."));
ret.add(
new Message(
"Error", MessageCode.MESSAGE_CODE_UNKNOWN, r.getKey() + ": File was modified."));
break;
case FileConsistencyCheck.UNKNOWN_FILE:
ret.add(new Message("Warning", 0, r.getKey() + ": Unknown file."));
ret.add(
new Message(
"Warning", MessageCode.MESSAGE_CODE_UNKNOWN, r.getKey() + ": Unknown file."));
break;
case FileConsistencyCheck.NONE:
ret.add(new Message("Warning", 0, r.getKey() + ": Test result not available."));
ret.add(
new Message(
"Warning",
MessageCode.MESSAGE_CODE_UNKNOWN,
r.getKey() + ": Test result not available."));
break;
default:
break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment