]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org/argeo/server/ServerAnswer.java
1 package org
.argeo
.server
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.StringWriter
;
6 import org
.apache
.commons
.io
.IOUtils
;
7 import org
.argeo
.ArgeoException
;
9 /** Answer to an execution of a remote service which performed changes. */
10 public class ServerAnswer
{
11 public final static String OK
= "OK";
12 public final static String ERROR
= "ERROR";
14 private String status
= OK
;
15 private String message
= "";
17 // TODO: add an exception field
19 /** Canonical constructor */
20 public ServerAnswer(String status
, String message
) {
23 throw new ArgeoException("Message cannot be null");
24 this.message
= message
;
27 /** Empty constructor */
28 public ServerAnswer() {
31 public String
getStatus() {
35 public void setStatus(String status
) {
36 if (status
== null || (!status
.equals(OK
) && !status
.equals(ERROR
)))
37 throw new ArgeoException("Bad status format: " + status
);
41 public String
getMessage() {
45 public void setMessage(String message
) {
46 this.message
= message
;
49 public Boolean
isOk() {
50 return status
.equals(OK
);
53 public Boolean
isError() {
54 return status
.equals(ERROR
);
57 public static ServerAnswer
error(String message
) {
58 return new ServerAnswer(ERROR
, message
);
61 public static ServerAnswer
error(Throwable e
) {
62 StringWriter writer
= new StringWriter();
64 e
.printStackTrace(new PrintWriter(writer
));
65 return new ServerAnswer(ERROR
, writer
.toString());
67 IOUtils
.closeQuietly(writer
);
71 public static ServerAnswer
ok(String message
) {
72 return new ServerAnswer(OK
, message
);
76 public String
toString() {
77 return "ServerAnswer{status:" + status
+ ", message:" + message
+ "}";